diff --git a/core/src/main/java/org/bitcoinj/core/Address.java b/core/src/main/java/org/bitcoinj/core/Address.java index 51de9000..71e229c2 100644 --- a/core/src/main/java/org/bitcoinj/core/Address.java +++ b/core/src/main/java/org/bitcoinj/core/Address.java @@ -52,7 +52,7 @@ public class Address extends VersionedChecksummedBytes { /** * Private constructor. Use {@link #fromBase58(NetworkParameters, String)}, - * {@link #Address(NetworkParameters, byte[])}, {@link #fromP2SHHash(NetworkParameters, byte[])} or + * {@link #fromPubKeyHash(NetworkParameters, byte[])}, {@link #fromP2SHHash(NetworkParameters, byte[])} or * {@link #fromKey(NetworkParameters, ECKey)}. * * @param params @@ -71,12 +71,26 @@ public class Address extends VersionedChecksummedBytes { this.params = params; } + /** + * Construct an {@link Address} that represents the given pubkey hash. The resulting address will be a P2PKH type of + * address. + * + * @param params + * the network this address is valid for + * @param hash160 + * 20-byte pubkey hash + * @return constructed address + */ + public static Address fromPubKeyHash(NetworkParameters params, byte[] hash160) { + return new Address(params, params.getAddressHeader(), hash160); + } + /** * Returns an {@link Address} that represents the public part of the given {@link ECKey}. Note that an address is * derived from a hash of the public key and is not the public key itself (which is too large to be convenient). */ public static Address fromKey(NetworkParameters params, ECKey key) { - return new Address(params, key.getPubKeyHash()); + return fromPubKeyHash(params, key.getPubKeyHash()); } /** Returns an Address that represents the given P2SH script hash. */ @@ -109,15 +123,10 @@ public class Address extends VersionedChecksummedBytes { return new Address(params, base58); } - /** - * Construct an address from parameters and the hash160 form. Example:
- * - *
new Address(MainNetParams.get(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));- */ + /** @deprecated use {@link #fromPubKeyHash(NetworkParameters, byte[])} */ + @Deprecated public Address(NetworkParameters params, byte[] hash160) { - super(params.getAddressHeader(), hash160); - checkArgument(hash160.length == 20, "Addresses are 160-bit hashes, so you must provide 20 bytes"); - this.params = params; + this(params, params.getAddressHeader(), hash160); } /** @deprecated Use {@link #fromBase58(NetworkParameters, String)} */ diff --git a/core/src/main/java/org/bitcoinj/script/Script.java b/core/src/main/java/org/bitcoinj/script/Script.java index ecdf9a02..d08e3160 100644 --- a/core/src/main/java/org/bitcoinj/script/Script.java +++ b/core/src/main/java/org/bitcoinj/script/Script.java @@ -294,7 +294,7 @@ public class Script { */ public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException { if (ScriptPattern.isPayToPubKeyHash(this)) - return new Address(params, ScriptPattern.extractHashFromPayToPubKeyHash(this)); + return Address.fromPubKeyHash(params, ScriptPattern.extractHashFromPayToPubKeyHash(this)); else if (ScriptPattern.isPayToScriptHash(this)) return Address.fromP2SHScript(params, this); else if (forcePayToPubKey && ScriptPattern.isPayToPubKey(this)) diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index 37c4e809..e142ae3a 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -4273,7 +4273,7 @@ public class Wallet extends BaseTaggableObject keys.addAll(getActiveKeyChain().getLeafKeys()); List addresses = new ArrayList<>(); for (ECKey key : keys) { - Address address = new Address(params, key.getPubKeyHash()); + Address address = Address.fromKey(params, key); addresses.add(address); } candidates.addAll(utxoProvider.getOpenTransactionOutputs(addresses)); diff --git a/core/src/test/java/org/bitcoinj/core/AbstractFullPrunedBlockChainTest.java b/core/src/test/java/org/bitcoinj/core/AbstractFullPrunedBlockChainTest.java index 74e7d39a..b5dbbaa1 100644 --- a/core/src/test/java/org/bitcoinj/core/AbstractFullPrunedBlockChainTest.java +++ b/core/src/test/java/org/bitcoinj/core/AbstractFullPrunedBlockChainTest.java @@ -260,7 +260,7 @@ public abstract class AbstractFullPrunedBlockChainTest { // Create bitcoin spend of 1 BTC. ECKey toKey = new ECKey(); Coin amount = Coin.valueOf(100000000); - Address address = new Address(PARAMS, toKey.getPubKeyHash()); + Address address = Address.fromKey(PARAMS, toKey); Coin totalAmount = Coin.ZERO; Transaction t = new Transaction(PARAMS); @@ -327,7 +327,7 @@ public abstract class AbstractFullPrunedBlockChainTest { // Create another spend of 1/2 the value of BTC we have available using the wallet (store coin selector). ECKey toKey2 = new ECKey(); Coin amount2 = amount.divide(2); - Address address2 = new Address(PARAMS, toKey2.getPubKeyHash()); + Address address2 = Address.fromKey(PARAMS, toKey2); SendRequest req = SendRequest.to(address2, amount2); wallet.completeTx(req); wallet.commitTx(req.tx); diff --git a/core/src/test/java/org/bitcoinj/core/AddressTest.java b/core/src/test/java/org/bitcoinj/core/AddressTest.java index f3bc8bbc..4a186e0c 100644 --- a/core/src/test/java/org/bitcoinj/core/AddressTest.java +++ b/core/src/test/java/org/bitcoinj/core/AddressTest.java @@ -58,11 +58,11 @@ public class AddressTest { @Test public void stringification() throws Exception { // Test a testnet address. - Address a = new Address(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); + Address a = Address.fromPubKeyHash(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString()); assertFalse(a.isP2SHAddress()); - Address b = new Address(MAINNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a")); + Address b = Address.fromPubKeyHash(MAINNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a")); assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString()); assertFalse(b.isP2SHAddress()); } @@ -190,7 +190,7 @@ public class AddressTest { @Test public void cloning() throws Exception { - Address a = new Address(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); + Address a = Address.fromPubKeyHash(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); Address b = a.clone(); assertEquals(a, b); diff --git a/core/src/test/java/org/bitcoinj/protocols/payments/PaymentSessionTest.java b/core/src/test/java/org/bitcoinj/protocols/payments/PaymentSessionTest.java index dd6da6fd..37e15198 100644 --- a/core/src/test/java/org/bitcoinj/protocols/payments/PaymentSessionTest.java +++ b/core/src/test/java/org/bitcoinj/protocols/payments/PaymentSessionTest.java @@ -71,7 +71,7 @@ public class PaymentSessionTest { tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes())); ArrayList