-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alexander Tarasenko <[email protected]>
- Loading branch information
1 parent
cd3e512
commit 35d266e
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.kethereum.erc1191 | ||
|
||
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 | ||
*/ | ||
|
||
fun Address.withERC1191Checksum(chainId: ChainId) = "${chainId.value}${hex}".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.hasValidERC1191ChecksumAssumingValidAddress(chainId: ChainId) = withERC1191Checksum(chainId).hex == hex | ||
|
||
fun Address.hasValidERC1191Checksum(chainId: ChainId) = isValid() && hasValidERC1191ChecksumAssumingValidAddress(chainId) | ||
fun Address.hasValidERC1191ChecksumOrNoChecksum(chainId: ChainId) = isValid() && | ||
(hasValidERC1191ChecksumAssumingValidAddress(chainId) || | ||
cleanHex.toLowerCase() == cleanHex || | ||
cleanHex.toUpperCase() == cleanHex) | ||
|
35 changes: 35 additions & 0 deletions
35
erc1191/src/test/kotlin/org/kethereum/erc1191/TheERC1191.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.kethereum.erc1191 | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.kethereum.model.Address | ||
import org.kethereum.model.ChainId | ||
|
||
class TheERC1191 { | ||
|
||
val rskMainnet = ChainId(30) | ||
val rskTestnet = ChainId(31) | ||
|
||
@Test | ||
fun returnsTrueForValidERC1191(){ | ||
assertThat(Address("0x27b1FdB04752BBc536007A920D24ACB045561c26") | ||
.hasValidERC1191Checksum(rskMainnet)).isTrue() | ||
assertThat(Address("0x27B1FdB04752BbC536007a920D24acB045561C26") | ||
.hasValidERC1191Checksum(rskTestnet)).isTrue() | ||
} | ||
|
||
@Test | ||
fun returnsFalseForInvalidERC1191(){ | ||
assertThat(Address("0x27b1fdb04752bbc536007a920d24acb045561c26") | ||
.hasValidERC1191Checksum(rskTestnet)).isFalse() | ||
assertThat(Address("0x27b1fdb04752bbc536007a920d24acb045561c26") | ||
.hasValidERC1191Checksum(rskMainnet)).isFalse() | ||
assertThat(Address("0x27b1FdB04752BBc536007A920D24ACB045561c26") | ||
.hasValidERC1191Checksum(rskTestnet)).isFalse() | ||
assertThat(Address("0x27B1FdB04752BbC536007a920D24acB045561C26") | ||
.hasValidERC1191Checksum(rskMainnet)).isFalse() | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,4 @@ include(":types") | |
include(":uri_common") | ||
include(":userdoc") | ||
include(":wallet") | ||
include(":erc1191") |