From 769dcf3c008c3686e2348ddabade65766d25d286 Mon Sep 17 00:00:00 2001 From: AB Date: Fri, 16 Sep 2022 15:12:22 +0200 Subject: [PATCH] Adding MixedCaseChecksum feature implementing CEP57 standard --- .../key/checksum/MixedCaseChecksum.java | 58 +++++++++++++------ .../syntifi/crypto/key/hash/Keccak256.java | 13 +++++ .../key/checksum/MixedCaseChecksumTest.java | 30 +++++++--- .../syntifi/crypto/key/hash/Blake2bTest.java | 16 +++++ .../crypto/key/hash/Keccak256Test.java | 14 +++++ 5 files changed, 104 insertions(+), 27 deletions(-) create mode 100644 crypto-key-common/src/main/java/com/syntifi/crypto/key/hash/Keccak256.java create mode 100644 crypto-key-common/src/test/java/com/syntifi/crypto/key/hash/Blake2bTest.java create mode 100644 crypto-key-common/src/test/java/com/syntifi/crypto/key/hash/Keccak256Test.java diff --git a/crypto-key-common/src/main/java/com/syntifi/crypto/key/checksum/MixedCaseChecksum.java b/crypto-key-common/src/main/java/com/syntifi/crypto/key/checksum/MixedCaseChecksum.java index 1500b84..2097ec7 100644 --- a/crypto-key-common/src/main/java/com/syntifi/crypto/key/checksum/MixedCaseChecksum.java +++ b/crypto-key-common/src/main/java/com/syntifi/crypto/key/checksum/MixedCaseChecksum.java @@ -1,11 +1,11 @@ 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 @@ -13,26 +13,48 @@ */ 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<