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 1 commit
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
19 changes: 14 additions & 5 deletions erc55/src/main/kotlin/org/kethereum/erc55/ERC55.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ package org.kethereum.erc55

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

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

fun Address.withERC55Checksum() = cleanHex.toLowerCase(Locale.ROOT).toByteArray().keccak().toNoPrefixHexString().let { hexHash ->
private fun Address.cleanHexWithPrefix(chainId: ChainId?) =
if (chainId != null) {
"${chainId.value}0x${cleanHex}"
} else {
cleanHex
}

fun Address.withERC55Checksum(chainId: ChainId? = null) = cleanHexWithPrefix(chainId).toLowerCase(Locale.ROOT).toByteArray().keccak().toNoPrefixHexString().let { hexHash ->
Address(cleanHex.mapIndexed { index, hexChar ->
when {
hexChar in '0'..'9' -> hexChar
Expand All @@ -19,10 +28,10 @@ fun Address.withERC55Checksum() = cleanHex.toLowerCase(Locale.ROOT).toByteArray(
}.joinToString(""))
}

private fun Address.hasValidERC55ChecksumAssumingValidAddress() = withERC55Checksum().hex == hex
private fun Address.hasValidERC55ChecksumAssumingValidAddress(chainId: ChainId? = null) = withERC55Checksum(chainId).hex == hex

fun Address.hasValidERC55Checksum() = isValid() && hasValidERC55ChecksumAssumingValidAddress()
fun Address.hasValidERC55ChecksumOrNoChecksum() = isValid() &&
(hasValidERC55ChecksumAssumingValidAddress() ||
fun Address.hasValidERC55Checksum(chainId: ChainId? = null) = isValid() && hasValidERC55ChecksumAssumingValidAddress(chainId)
fun Address.hasValidERC55ChecksumOrNoChecksum(chainId: ChainId? = null) = isValid() &&
(hasValidERC55ChecksumAssumingValidAddress(chainId) ||
cleanHex.toLowerCase() == cleanHex ||
cleanHex.toUpperCase() == cleanHex)
26 changes: 26 additions & 0 deletions erc55/src/test/kotlin/org/kethereum/erc55/TheERC55.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package org.kethereum.erc55
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.kethereum.model.Address
import org.kethereum.model.ChainId

class TheERC55 {

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

@Test
fun returnsTrueForValidChecksum() {
assertThat(Address("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
Expand Down Expand Up @@ -54,5 +58,27 @@ class TheERC55 {

}

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

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


}