Skip to content

Instantly share code, notes, and snippets.

@adyliu
Created August 3, 2018 07:33
Show Gist options
  • Save adyliu/6c5ff4d41aa0177da55f4b8b1703f54a to your computer and use it in GitHub Desktop.
Save adyliu/6c5ff4d41aa0177da55f4b8b1703f54a to your computer and use it in GitHub Desktop.
ethereum address checker
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));
});
}
}
@adyliu
Copy link
Author

adyliu commented Aug 3, 2018

Ethereum EIP-55 Mixed-case checksum address encoding

dependency:

@mpeteriii
Copy link

igen,de ez meg csak proposal azóta rájöttem

@mpeteriii
Copy link

tényleg nincs semmilyen checksumming by default

@mmo2112
Copy link

mmo2112 commented Jul 3, 2020

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