Esempio n. 1
0
 /**
  * Convert to a human readable (BASE58) address with network and type prefixes.
  *
  * @param address
  * @return human readable representation of the address
  * @throws ValidationException
  */
 public static String toSatoshiStyle(Address address) throws ValidationException {
   byte[] keyDigest = address.toByteArray();
   int addressFlag;
   if (address.getNetwork() == Network.PRODUCTION) {
     if (address.getType() == Address.Type.COMMON) {
       addressFlag = 0x0;
     } else if (address.getType() == Address.Type.P2SH) {
       addressFlag = 0x5;
     } else {
       throw new ValidationException("unknown address type");
     }
   } else if (address.getNetwork() == Network.TEST) {
     if (address.getType() == Address.Type.COMMON) {
       addressFlag = 0x6f;
     } else if (address.getType() == Address.Type.P2SH) {
       addressFlag = 196;
     } else {
       throw new ValidationException("unknown address type");
     }
   } else {
     throw new ValidationException("unknown network");
   }
   byte[] addressBytes = new byte[1 + keyDigest.length + 4];
   addressBytes[0] = (byte) (addressFlag & 0xff);
   System.arraycopy(keyDigest, 0, addressBytes, 1, keyDigest.length);
   byte[] check = Hash.hash(addressBytes, 0, keyDigest.length + 1);
   System.arraycopy(check, 0, addressBytes, keyDigest.length + 1, 4);
   return ByteUtils.toBase58(addressBytes);
 }
Esempio n. 2
0
 /**
  * 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);
   }
 }
Esempio n. 3
0
 /**
  * Convert to a human readable address string from a digest
  *
  * @param keyDigest - the digest
  * @param addressFlag - a flag encoded in the first byte of string representation
  * @return
  * @throws ValidationException
  */
 @Deprecated
 public static String toSatoshiStyle(byte[] keyDigest, int addressFlag) {
   byte[] addressBytes = new byte[1 + keyDigest.length + 4];
   addressBytes[0] = (byte) (addressFlag & 0xff);
   System.arraycopy(keyDigest, 0, addressBytes, 1, keyDigest.length);
   byte[] check = Hash.hash(addressBytes, 0, keyDigest.length + 1);
   System.arraycopy(check, 0, addressBytes, keyDigest.length + 1, 4);
   return ByteUtils.toBase58(addressBytes);
 }
Esempio n. 4
0
 /**
  * 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);
   }
 }