-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding MixedCaseChecksum feature implementing CEP57 standard
- Loading branch information
Showing
5 changed files
with
104 additions
and
27 deletions.
There are no files selected for viewing
58 changes: 40 additions & 18 deletions
58
crypto-key-common/src/main/java/com/syntifi/crypto/key/checksum/MixedCaseChecksum.java
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 |
---|---|---|
@@ -1,38 +1,60 @@ | ||
package com.syntifi.crypto.key.checksum; | ||
|
||
import org.bouncycastle.jcajce.provider.digest.Keccak; | ||
import org.bouncycastle.util.encoders.Hex; | ||
import com.syntifi.crypto.key.encdec.Hex; | ||
import com.syntifi.crypto.key.hash.Blake2b; | ||
import com.syntifi.crypto.key.hash.Keccak256; | ||
|
||
/** | ||
* Implementation of the EIP-55: Mixed-case checksum address encoding | ||
* Documentation available at: https://eips.ethereum.org/EIPS/eip-55 | ||
* Mixed checksumEncoding | ||
* | ||
* @author Alexandre Carvalho | ||
* @author Andre Bertolace | ||
* @since 0.5.0 | ||
*/ | ||
public class MixedCaseChecksum { | ||
|
||
public static String kekkac256(String value) { | ||
Keccak.DigestKeccak kekkac256 = new Keccak.Digest256(); | ||
byte[] hash = kekkac256.digest(value.getBytes()); | ||
return Hex.toHexString(hash); | ||
} | ||
|
||
public static String checksumEncode(String address) { | ||
Boolean withPrefix = address.startsWith("0x"); | ||
String value = address.toLowerCase().replace("0x", ""); | ||
char[] hash = kekkac256(value).toCharArray(); | ||
/** | ||
* Implementation of the EIP-55: Mixed-case checksum address encoding | ||
* Documentation available at: https://eips.ethereum.org/EIPS/eip-55 | ||
* | ||
* @param value Hex string | ||
* @return | ||
*/ | ||
public static String checksumEncodeEIP55(String value) { | ||
char[] hash = Hex.encode(Keccak256.digest(value.toLowerCase().getBytes())).toCharArray(); | ||
char[] chars = value.toCharArray(); | ||
char[] encoded = new char[chars.length]; | ||
for (int i = 0; i < chars.length; i++) { | ||
encoded[i] = Character.digit(hash[i], 16) > 7 | ||
? Character.toUpperCase(chars[i]) | ||
: chars[i]; | ||
: Character.toLowerCase(chars[i]); | ||
} | ||
return withPrefix | ||
? "0x" + String.valueOf(encoded) | ||
: String.valueOf(encoded); | ||
return String.valueOf(encoded); | ||
} | ||
|
||
/** | ||
* Implementation of the CEP-57 | ||
* Documentation available at: https://github.com/casper-network/ceps/blob/master/text/0057-checksummed-addresses.md | ||
* | ||
* @param value Hex string | ||
* @return | ||
*/ | ||
public static String checksumEncodeCEP57(String value) { | ||
byte[] hash = Blake2b.digest(Hex.decode(value), 32); | ||
char[] chars = value.toCharArray(); | ||
char[] encoded = new char[chars.length]; | ||
int whichByte = 0; | ||
int whichBit = 0; | ||
for (int i = 0; i < chars.length; i++) { | ||
boolean bitSet = (Byte.toUnsignedInt(hash[whichByte]) & (1<<whichBit)) != 0; | ||
if (Character.isAlphabetic(chars[i])) { | ||
encoded[i] = bitSet ? Character.toUpperCase(chars[i]) : Character.toLowerCase(chars[i]); | ||
whichByte = (whichByte + whichBit/7) % hash.length; | ||
whichBit = (whichBit + 1) % 8; | ||
} else { | ||
encoded[i] = chars[i]; | ||
} | ||
} | ||
return String.valueOf(encoded); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
crypto-key-common/src/main/java/com/syntifi/crypto/key/hash/Keccak256.java
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,13 @@ | ||
package com.syntifi.crypto.key.hash; | ||
|
||
import org.bouncycastle.jcajce.provider.digest.Keccak; | ||
import org.bouncycastle.util.encoders.Hex; | ||
|
||
public class Keccak256 { | ||
|
||
public static byte[] digest(byte[] value) { | ||
Keccak.DigestKeccak kekkac256 = new Keccak.Digest256(); | ||
byte[] hash = kekkac256.digest(value); | ||
return hash; | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
crypto-key-common/src/test/java/com/syntifi/crypto/key/hash/Blake2bTest.java
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,16 @@ | ||
package com.syntifi.crypto.key.hash; | ||
|
||
import com.syntifi.crypto.key.encdec.Hex; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
public class Blake2bTest { | ||
|
||
@Test | ||
void test() { | ||
Assertions.assertEquals("44682ea86b704fb3c65cd16f84a76b621e04bbdb3746280f25cf062220e471b4", | ||
Hex.encode(Blake2b.digest("أبو يوسف يعقوب بن إسحاق الصبّاح الكندي".getBytes(StandardCharsets.UTF_8), 32))); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
crypto-key-common/src/test/java/com/syntifi/crypto/key/hash/Keccak256Test.java
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,14 @@ | ||
package com.syntifi.crypto.key.hash; | ||
|
||
import com.syntifi.crypto.key.encdec.Hex; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class Keccak256Test { | ||
|
||
@Test | ||
void kekkac256ShouldMatch() { | ||
Assertions.assertEquals("a9059cbb2ab09eb219583f4a59a5d0623ade346d962bcd4e46b11da047c9049b", | ||
Hex.encode(Keccak256.digest("transfer(address,uint256)".getBytes()))); | ||
} | ||
} |