/** * Convert a human readable address string to an address object * * @param address - the human readable address * @return an address object with type and network ad encoded in the readable string * @throws ValidationException - for malformed address strings */ public static Address fromSatoshiStyle(String address) throws ValidationException { try { Network network = Network.PRODUCTION; Address.Type type = Type.COMMON; byte[] raw = ByteUtils.fromBase58(address); if ((raw[0] & 0xff) == 0x0) { network = Network.PRODUCTION; type = Address.Type.COMMON; } if ((raw[0] & 0xff) == 5) { network = Network.PRODUCTION; type = Address.Type.P2SH; } if ((raw[0] & 0xff) == 0x6f) { network = Network.TEST; type = Address.Type.COMMON; } if ((raw[0] & 0xff) == 196) { network = Network.TEST; type = Address.Type.P2SH; } byte[] check = Hash.hash(raw, 0, raw.length - 4); for (int i = 0; i < 4; ++i) { if (check[i] != raw[raw.length - 4 + i]) { throw new ValidationException("Address checksum mismatch"); } } byte[] keyDigest = new byte[raw.length - 5]; System.arraycopy(raw, 1, keyDigest, 0, raw.length - 5); return new Address(network, type, keyDigest); } catch (Exception e) { throw new ValidationException(e); } }
/** * Convert a human readable address string to an address object * * @param address - the human readable address * @param addressFlag - a flag encoded in the first byte of string representation * @return * @throws ValidationException */ @Deprecated public static byte[] fromSatoshiStyle(String address, int addressFlag) throws ValidationException { try { byte[] raw = ByteUtils.fromBase58(address); if (raw[0] != (byte) (addressFlag & 0xff)) { throw new ValidationException("invalid address for this chain"); } byte[] check = Hash.hash(raw, 0, raw.length - 4); for (int i = 0; i < 4; ++i) { if (check[i] != raw[raw.length - 4 + i]) { throw new ValidationException("Address checksum mismatch"); } } byte[] keyDigest = new byte[raw.length - 5]; System.arraycopy(raw, 1, keyDigest, 0, raw.length - 5); return keyDigest; } catch (Exception e) { throw new ValidationException(e); } }