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)},
* {@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:<p>
*
* <pre>new Address(MainNetParams.get(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));</pre>
*/
/** @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)} */

View File

@@ -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))

View File

@@ -4273,7 +4273,7 @@ public class Wallet extends BaseTaggableObject
keys.addAll(getActiveKeyChain().getLeafKeys());
List<Address> 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));

View File

@@ -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);

View File

@@ -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);

View File

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

View File

@@ -79,7 +79,7 @@ public class ScriptTest {
byte[] pubkeyBytes = HEX.decode(pubkeyProg);
Script pubkey = new Script(pubkeyBytes);
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());
}

View File

@@ -486,7 +486,7 @@ public class WalletTest extends TestWithWallet {
List<ScriptChunk> scriptSigChunks = t2.getInput(0).getScriptSig().getChunks();
// check 'from address' -- in a unit test this is fine
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());
// We have NOT proven that the signature is correct!
@@ -2147,7 +2147,7 @@ public class WalletTest extends TestWithWallet {
Coin v = CENT;
// 3100 outputs to a random address.
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);
wallet.completeTx(req);