Address: Migrate use of the other pubKeyHash constructor to fromPubKeyHash(). Deprecate the constructor.

This commit is contained in:
Andreas Schildbach
2018-02-27 11:41:13 +01:00
parent cc2e8ba935
commit 44474be186
8 changed files with 31 additions and 22 deletions

View File

@@ -52,7 +52,7 @@ public class Address extends VersionedChecksummedBytes {
/** /**
* Private constructor. Use {@link #fromBase58(NetworkParameters, String)}, * 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)}. * {@link #fromKey(NetworkParameters, ECKey)}.
* *
* @param params * @param params
@@ -71,12 +71,26 @@ public class Address extends VersionedChecksummedBytes {
this.params = params; 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 * 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). * 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) { 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. */ /** Returns an Address that represents the given P2SH script hash. */
@@ -109,15 +123,10 @@ public class Address extends VersionedChecksummedBytes {
return new Address(params, base58); return new Address(params, base58);
} }
/** /** @deprecated use {@link #fromPubKeyHash(NetworkParameters, byte[])} */
* Construct an address from parameters and the hash160 form. Example:<p> @Deprecated
*
* <pre>new Address(MainNetParams.get(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));</pre>
*/
public Address(NetworkParameters params, byte[] hash160) { public Address(NetworkParameters params, byte[] hash160) {
super(params.getAddressHeader(), hash160); this(params, params.getAddressHeader(), hash160);
checkArgument(hash160.length == 20, "Addresses are 160-bit hashes, so you must provide 20 bytes");
this.params = params;
} }
/** @deprecated Use {@link #fromBase58(NetworkParameters, String)} */ /** @deprecated Use {@link #fromBase58(NetworkParameters, String)} */

View File

@@ -294,7 +294,7 @@ public class Script {
*/ */
public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException { public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
if (ScriptPattern.isPayToPubKeyHash(this)) if (ScriptPattern.isPayToPubKeyHash(this))
return new Address(params, ScriptPattern.extractHashFromPayToPubKeyHash(this)); return Address.fromPubKeyHash(params, ScriptPattern.extractHashFromPayToPubKeyHash(this));
else if (ScriptPattern.isPayToScriptHash(this)) else if (ScriptPattern.isPayToScriptHash(this))
return Address.fromP2SHScript(params, this); return Address.fromP2SHScript(params, this);
else if (forcePayToPubKey && ScriptPattern.isPayToPubKey(this)) else if (forcePayToPubKey && ScriptPattern.isPayToPubKey(this))

View File

@@ -4273,7 +4273,7 @@ public class Wallet extends BaseTaggableObject
keys.addAll(getActiveKeyChain().getLeafKeys()); keys.addAll(getActiveKeyChain().getLeafKeys());
List<Address> addresses = new ArrayList<>(); List<Address> addresses = new ArrayList<>();
for (ECKey key : keys) { for (ECKey key : keys) {
Address address = new Address(params, key.getPubKeyHash()); Address address = Address.fromKey(params, key);
addresses.add(address); addresses.add(address);
} }
candidates.addAll(utxoProvider.getOpenTransactionOutputs(addresses)); candidates.addAll(utxoProvider.getOpenTransactionOutputs(addresses));

View File

@@ -260,7 +260,7 @@ public abstract class AbstractFullPrunedBlockChainTest {
// Create bitcoin spend of 1 BTC. // Create bitcoin spend of 1 BTC.
ECKey toKey = new ECKey(); ECKey toKey = new ECKey();
Coin amount = Coin.valueOf(100000000); Coin amount = Coin.valueOf(100000000);
Address address = new Address(PARAMS, toKey.getPubKeyHash()); Address address = Address.fromKey(PARAMS, toKey);
Coin totalAmount = Coin.ZERO; Coin totalAmount = Coin.ZERO;
Transaction t = new Transaction(PARAMS); 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). // Create another spend of 1/2 the value of BTC we have available using the wallet (store coin selector).
ECKey toKey2 = new ECKey(); ECKey toKey2 = new ECKey();
Coin amount2 = amount.divide(2); Coin amount2 = amount.divide(2);
Address address2 = new Address(PARAMS, toKey2.getPubKeyHash()); Address address2 = Address.fromKey(PARAMS, toKey2);
SendRequest req = SendRequest.to(address2, amount2); SendRequest req = SendRequest.to(address2, amount2);
wallet.completeTx(req); wallet.completeTx(req);
wallet.commitTx(req.tx); wallet.commitTx(req.tx);

View File

@@ -58,11 +58,11 @@ public class AddressTest {
@Test @Test
public void stringification() throws Exception { public void stringification() throws Exception {
// Test a testnet address. // Test a testnet address.
Address a = new Address(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); Address a = Address.fromPubKeyHash(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString()); assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
assertFalse(a.isP2SHAddress()); assertFalse(a.isP2SHAddress());
Address b = new Address(MAINNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a")); Address b = Address.fromPubKeyHash(MAINNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString()); assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
assertFalse(b.isP2SHAddress()); assertFalse(b.isP2SHAddress());
} }
@@ -190,7 +190,7 @@ public class AddressTest {
@Test @Test
public void cloning() throws Exception { 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(); Address b = a.clone();
assertEquals(a, b); assertEquals(a, b);

View File

@@ -71,7 +71,7 @@ public class PaymentSessionTest {
tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes())); tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes()));
ArrayList<Transaction> txns = new ArrayList<>(); ArrayList<Transaction> txns = new ArrayList<>();
txns.add(tx); txns.add(tx);
Address refundAddr = new Address(PARAMS, serverKey.getPubKeyHash()); Address refundAddr = Address.fromKey(PARAMS, serverKey);
paymentSession.sendPayment(txns, refundAddr, paymentMemo); paymentSession.sendPayment(txns, refundAddr, paymentMemo);
assertEquals(1, paymentSession.getPaymentLog().size()); assertEquals(1, paymentSession.getPaymentLog().size());
assertEquals(simplePaymentUrl, paymentSession.getPaymentLog().get(0).getUrl().toString()); assertEquals(simplePaymentUrl, paymentSession.getPaymentLog().get(0).getUrl().toString());
@@ -142,7 +142,7 @@ public class PaymentSessionTest {
tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes())); tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes()));
ArrayList<Transaction> txns = new ArrayList<>(); ArrayList<Transaction> txns = new ArrayList<>();
txns.add(tx); txns.add(tx);
Address refundAddr = new Address(PARAMS, serverKey.getPubKeyHash()); Address refundAddr = Address.fromKey(PARAMS, serverKey);
paymentSession.sendPayment(txns, refundAddr, paymentMemo); paymentSession.sendPayment(txns, refundAddr, paymentMemo);
assertEquals(1, paymentSession.getPaymentLog().size()); assertEquals(1, paymentSession.getPaymentLog().size());
} }

View File

@@ -79,7 +79,7 @@ public class ScriptTest {
byte[] pubkeyBytes = HEX.decode(pubkeyProg); byte[] pubkeyBytes = HEX.decode(pubkeyProg);
Script pubkey = new Script(pubkeyBytes); Script pubkey = new Script(pubkeyBytes);
assertEquals("DUP HASH160 PUSHDATA(20)[33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString()); assertEquals("DUP HASH160 PUSHDATA(20)[33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString());
Address toAddr = new Address(PARAMS, ScriptPattern.extractHashFromPayToPubKeyHash(pubkey)); Address toAddr = Address.fromPubKeyHash(PARAMS, ScriptPattern.extractHashFromPayToPubKeyHash(pubkey));
assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr.toString()); assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr.toString());
} }

View File

@@ -486,7 +486,7 @@ public class WalletTest extends TestWithWallet {
List<ScriptChunk> scriptSigChunks = t2.getInput(0).getScriptSig().getChunks(); List<ScriptChunk> scriptSigChunks = t2.getInput(0).getScriptSig().getChunks();
// check 'from address' -- in a unit test this is fine // check 'from address' -- in a unit test this is fine
assertEquals(2, scriptSigChunks.size()); assertEquals(2, scriptSigChunks.size());
assertEquals(myAddress, new Address(PARAMS, Utils.sha256hash160(scriptSigChunks.get(1).data))); assertEquals(myAddress, Address.fromPubKeyHash(PARAMS, Utils.sha256hash160(scriptSigChunks.get(1).data)));
assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType()); assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType());
// We have NOT proven that the signature is correct! // We have NOT proven that the signature is correct!
@@ -2147,7 +2147,7 @@ public class WalletTest extends TestWithWallet {
Coin v = CENT; Coin v = CENT;
// 3100 outputs to a random address. // 3100 outputs to a random address.
for (int i = 0; i < 3100; i++) { for (int i = 0; i < 3100; i++) {
tx.addOutput(v, new Address(PARAMS, bits)); tx.addOutput(v, Address.fromPubKeyHash(PARAMS, bits));
} }
SendRequest req = SendRequest.forTx(tx); SendRequest req = SendRequest.forTx(tx);
wallet.completeTx(req); wallet.completeTx(req);