All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bccapi.bitlib.model.Address Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.bccapi.bitlib.model;

import com.bccapi.bitlib.crypto.PublicKey;
import com.bccapi.bitlib.util.Base58;
import com.bccapi.bitlib.util.BitUtils;
import com.bccapi.bitlib.util.HashUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Address implements Serializable {

   private static final long serialVersionUID = 1L;

   private byte[] _bytes;
   private String _address;

   public static Collection
fromStrings(Collection addresses, NetworkParameters network) { List
list = new LinkedList
(); for (String address : addresses) { Address a = Address.fromString(address, network); if (a == null) { return null; } list.add(a); } return list; } public static Address[] fromStrings(String[] addressStrings) { Address[] addresses = new Address[addressStrings.length]; for (int i = 0; i < addressStrings.length; i++) { addresses[i] = Address.fromString(addressStrings[i]); } return addresses; } public static String[] toStrings(Address[] addresses) { String[] addressStrings = new String[addresses.length]; for (int i = 0; i < addressStrings.length; i++) { addressStrings[i] = addresses[i].toString(); } return addressStrings; } public static Address fromString(String address, NetworkParameters network) { Address addr = Address.fromString(address); if (addr == null) { return null; } int version = addr.getVersion(); if (version != network.getStandardAddressHeader() && version != network.getMultisigAddressHeader()) { return null; } return addr; } public static Address fromString(String address) { if (address.length() == 0) { return null; } byte[] bytes = Base58.decodeChecked(address); if (bytes == null || bytes.length != 21) { return null; } return new Address(bytes); } public static Address fromMultisigBytes(byte[] bytes, NetworkParameters network) { if (bytes.length != 20) { return null; } byte[] all = new byte[21]; all[0] = (byte) (network.getMultisigAddressHeader() & 0xFF); System.arraycopy(bytes, 0, all, 1, 20); return new Address(all); } public static Address fromStandardBytes(byte[] bytes, NetworkParameters network) { if (bytes.length != 20) { return null; } byte[] all = new byte[21]; all[0] = (byte) (network.getStandardAddressHeader() & 0xFF); System.arraycopy(bytes, 0, all, 1, 20); return new Address(all); } public static Address fromStandardPublicKey(PublicKey key, NetworkParameters network) { byte[] hashedPublicKey = HashUtils.addressHash(key.getPublicKeyBytes()); byte[] addressBytes = new byte[1 + 20]; addressBytes[0] = (byte) (network.getStandardAddressHeader() & 0xFF); System.arraycopy(hashedPublicKey, 0, addressBytes, 1, 20); return new Address(addressBytes); } /** * Construct a Bitcoin address from an array of bytes containing both the * address version and address bytes, but without the checksum (1 + 20 = 21 * bytes). * * @param bytes * containing the full address representation 1 + 20 bytes. */ public Address(byte[] bytes) { _bytes = bytes; _address = null; } /** * Construct a Bitcoin address from an array of bytes and the string * representation of the address. The byte array contains both the address * version and address bytes, but without the checksum (1 + 20 = 21 bytes). * * Note: No attempt is made to verify that the byte array and string * representation match. * * @param bytes * containing the full address representation 1 + 20 bytes. * @param stringAddress * the string representation of a Bitcoin address */ public Address(byte[] bytes, String stringAddress) { _bytes = bytes; _address = stringAddress; } /** * Validate that an address is a valid address on the specified network * * @param network bitcoin network this address is on * * @return is valid */ public boolean isValidAddress(NetworkParameters network) { byte version = getVersion(); if (getAllAddressBytes().length != 21) { return false; } return ((byte) (network.getStandardAddressHeader() & 0xFF)) == version || ((byte) (network.getMultisigAddressHeader() & 0xFF)) == version; } public boolean isMultisig(NetworkParameters network) { return getVersion() == (byte) (network.getMultisigAddressHeader() & 0xFF); } public byte getVersion() { return _bytes[0]; } /** * Get the address as an array of bytes. The array contains the one byte * address type and the 20 address bytes, totaling 21 bytes. * * @return The address as an array of 21 bytes. */ public byte[] getAllAddressBytes() { return _bytes; } public byte[] getTypeSpecificBytes() { byte[] result = new byte[20]; System.arraycopy(_bytes, 1, result, 0, 20); return result; } @Override public String toString() { if (_address == null) { byte[] addressBytes = new byte[1 + 20 + 4]; addressBytes[0] = _bytes[0]; System.arraycopy(_bytes, 0, addressBytes, 0, 21); byte[] checkSum = HashUtils.doubleSha256(addressBytes, 0, 21); System.arraycopy(checkSum, 0, addressBytes, 21, 4); _address = Base58.encode(addressBytes); } return _address; } @Override public int hashCode() { return ((_bytes[16] & 0xFF) << 0) | ((_bytes[17] & 0xFF) << 8) | ((_bytes[18] & 0xFF) << 16) | ((_bytes[19] & 0xFF) << 24); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof Address)) { return false; } return BitUtils.areEqual(_bytes, ((Address) obj)._bytes); } public static Address getNullAddress(NetworkParameters network) { byte[] bytes = new byte[21]; bytes[0] = (byte) (network.getStandardAddressHeader() & 0xFF); return new Address(bytes); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy