Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

adyliu commented Aug 3, 2018

Ethereum EIP-55 Mixed-case checksum address encoding

dependency:

@mpeteriii

This comment has been minimized.

Copy link

mpeteriii commented Aug 25, 2018

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

@mpeteriii

This comment has been minimized.

Copy link

mpeteriii commented Aug 25, 2018

tényleg nincs semmilyen checksumming by default

@mmo2112

This comment has been minimized.

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
You can’t perform that action at this time.