Created
August 3, 2018 07:33
-
-
Save adyliu/6c5ff4d41aa0177da55f4b8b1703f54a to your computer and use it in GitHub Desktop.
ethereum address checker
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
package demo; | |
import java.math.BigInteger; | |
import java.util.Arrays; | |
import org.web3j.crypto.Hash; | |
import org.web3j.utils.Numeric; | |
public class AddressChecker { | |
public static String checkedAddress(final String address) { | |
final String cleanAddress = Numeric.cleanHexPrefix(address).toLowerCase(); | |
// | |
StringBuilder o = new StringBuilder(); | |
String keccak = Hash.sha3String(cleanAddress); | |
char[] checkChars = keccak.substring(2).toCharArray(); | |
char[] cs = cleanAddress.toLowerCase().toCharArray(); | |
for (int i = 0; i < cs.length; i++) { | |
char c = cs[i]; | |
c = (Character.digit(checkChars[i], 16) & 0xFF) > 7 ? Character.toUpperCase(c) : Character.toLowerCase(c); | |
o.append(c); | |
} | |
return Numeric.prependHexPrefix(o.toString()); | |
} | |
public static boolean isCheckedAddress(final String address) { | |
return Numeric.prependHexPrefix(address).equals(checkedAddress(address)); | |
} | |
public static void main(String[] args) { | |
Arrays.asList("00Be17C6aD2738fb20B80f290C8fa1F4F8aB5902", // valid address | |
"0x00Be17C6aD2738fb20B80f290C8fa1F4F8aB5902", // valid address | |
"00be17C6aD2738fb20B80f290C8fa1F4F8aB5902", // invalid address | |
"0x00be17C6aD2738fb20B80f290C8fa1F4F8aB5902"// invalid address | |
).forEach(addr -> { | |
System.out.printf("%s => %s === %s\n", addr, checkedAddress(addr), isCheckedAddress(addr)); | |
}); | |
} | |
} |
igen,de ez meg csak proposal azóta rájöttem
tényleg nincs semmilyen checksumming by default
I think a combination between try to convert it to big integer and your code is better.
For input 0xXxxXxxxXXXXxxxXxxXxXXxxxxxxxxxxXxXXxxxXx it still passed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ethereum EIP-55 Mixed-case checksum address encoding
dependency: