/** * @param addr length should be 20 * @return short string represent 1f21c... */ public static String getAddressShortString(byte[] addr) { if (!isValidAddress(addr)) { throw new Error("not an address"); } String addrShort = Hex.toHexString(addr, 0, 3); return addrShort + "..."; }
/** * Decodes a hex string to address bytes and checks validity * * @param hex - a hex string of the address, e.g., 6c386a4b26f73c802f34673f7248bb118f97424a * @return - decode and validated address byte[] */ public static byte[] addressStringToBytes(String hex) { final byte[] addr; try { addr = Hex.decode(hex); } catch (DecoderException addressIsNotValid) { return null; } if (isValidAddress(addr)) { return addr; } return null; }
@Test public void isValidAddress() { assert HexUtil.isValidAddress(HexUtil.addressStringToBytes(VALID_ADDR)); }