Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support EIP-1191 #85

Merged
merged 5 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions eip1191/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
"implementation"(project(":model"))
"implementation"(project(":keccak_shortcut"))
"implementation"("com.github.komputing:khex:${Versions.khex}")
"implementation"(project(":erc55"))
}

38 changes: 38 additions & 0 deletions eip1191/src/main/kotlin/org.kethereum.eip1191/EIP1191.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.kethereum.eip1191

import org.kethereum.erc55.isValid
import org.kethereum.keccakshortcut.keccak
import org.kethereum.model.Address
import org.kethereum.model.ChainId
import org.komputing.khex.extensions.toNoPrefixHexString
import java.util.*

/*
EIP-1191 Checksum as in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1191.md
*/

private fun Address.cleanHexWithPrefix(chainId: ChainId?) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this function supposed to do? I think If you add a test for it you will see that this makes no sense at all ..

if (chainId != null) {
"${chainId.value}0x${cleanHex}"
} else {
cleanHex
}

fun Address.withEIP1191Checksum(chainId: ChainId? = null) = cleanHexWithPrefix(chainId).toLowerCase(Locale.ROOT).toByteArray().keccak().toNoPrefixHexString().let { hexHash ->
Address(cleanHex.mapIndexed { index, hexChar ->
when {
hexChar in '0'..'9' -> hexChar
hexHash[index] in '0'..'7' -> hexChar.toLowerCase()
else -> hexChar.toUpperCase()
}
}.joinToString(""))
}

private fun Address.hasValidEIP1191ChecksumAssumingValidAddress(chainId: ChainId? = null) = withEIP1191Checksum(chainId).hex == hex

fun Address.hasValidEIP1191Checksum(chainId: ChainId? = null) = isValid() && hasValidEIP1191ChecksumAssumingValidAddress(chainId)
fun Address.hasValidEIP1191ChecksumOrNoChecksum(chainId: ChainId? = null) = isValid() &&
(hasValidEIP1191ChecksumAssumingValidAddress(chainId) ||
cleanHex.toLowerCase() == cleanHex ||
cleanHex.toUpperCase() == cleanHex)

37 changes: 37 additions & 0 deletions eip1191/src/test/kotlin/org/kethereum/eip1191/TheEIP1191.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.kethereum.eip1191

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.kethereum.model.Address
import org.kethereum.model.ChainId

class TheEIP1191 {

val rskMainnet = ChainId(30)
val rskTestnet = ChainId(31)

@Test
fun returnsTrueForValidEIP1191(){
assertThat(Address("0x27b1fdb04752bbc536007a920d24acb045561c26")
.hasValidEIP1191Checksum(null)).isTrue()
assertThat(Address("0x27b1FdB04752BBc536007A920D24ACB045561c26")
.hasValidEIP1191Checksum(rskMainnet)).isTrue()
assertThat(Address("0x27B1FdB04752BbC536007a920D24acB045561C26")
.hasValidEIP1191Checksum(rskTestnet)).isTrue()
}

@Test
fun returnsFalseForInvalidEIP1191(){
assertThat(Address("0x27b1fdb04752bbc536007a920d24acb045561c26")
.hasValidEIP1191Checksum(rskTestnet)).isFalse()
assertThat(Address("0x27b1fdb04752bbc536007a920d24acb045561c26")
.hasValidEIP1191Checksum(rskMainnet)).isFalse()
assertThat(Address("0x27b1FdB04752BBc536007A920D24ACB045561c26")
.hasValidEIP1191Checksum(rskTestnet)).isFalse()
assertThat(Address("0x27B1FdB04752BbC536007a920D24acB045561C26")
.hasValidEIP1191Checksum(rskMainnet)).isFalse()
}



}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ include(":types")
include(":uri_common")
include(":userdoc")
include(":wallet")
include(":eip1191")