From bc8828c8585cfd3ce581d6fbbc8a37d86776fbba Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sun, 4 Mar 2018 16:48:27 +0100 Subject: [PATCH] TransactionOutput: Deprecate getAddressFromP2PKHScript() and getAddressFromP2SH() methods. --- .../org/bitcoinj/core/TransactionOutput.java | 34 ++++--------------- .../org/bitcoinj/script/ScriptPattern.java | 7 +++- .../bitcoinj/core/TransactionOutputTest.java | 8 +++-- .../java/org/bitcoinj/wallet/WalletTest.java | 3 +- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/TransactionOutput.java b/core/src/main/java/org/bitcoinj/core/TransactionOutput.java index bcbfba4e..18ff3029 100644 --- a/core/src/main/java/org/bitcoinj/core/TransactionOutput.java +++ b/core/src/main/java/org/bitcoinj/core/TransactionOutput.java @@ -119,40 +119,20 @@ public class TransactionOutput extends ChildMessage { return scriptPubKey; } - /** - *

If the output script pays to an address as in - * P2PKH, return the address of the receiver, i.e., a base58 encoded hash of the public key in the script.

- * - * @param networkParameters needed to specify an address - * @return null, if the output script is not the form OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG, - * i.e., not P2PKH - * @return an address made out of the public key hash - */ @Nullable - public LegacyAddress getAddressFromP2PKHScript(NetworkParameters networkParameters) throws ScriptException{ + @Deprecated + public LegacyAddress getAddressFromP2PKHScript(NetworkParameters params) throws ScriptException { if (ScriptPattern.isPayToPubKeyHash(getScriptPubKey())) - return (LegacyAddress) getScriptPubKey().getToAddress(networkParameters); - + return LegacyAddress.fromPubKeyHash(params, + ScriptPattern.extractHashFromPayToPubKeyHash(getScriptPubKey())); return null; } - /** - *

If the output script pays to a redeem script, return the address of the redeem script as described by, - * i.e., a base58 encoding of [one-byte version][20-byte hash][4-byte checksum], where the 20-byte hash refers to - * the redeem script.

- * - *

P2SH is described by BIP 16 and - * documented in the Bitcoin Developer Guide.

- * - * @param networkParameters needed to specify an address - * @return null if the output script does not pay to a script hash - * @return an address that belongs to the redeem script - */ @Nullable - public LegacyAddress getAddressFromP2SH(NetworkParameters networkParameters) throws ScriptException{ + @Deprecated + public LegacyAddress getAddressFromP2SH(NetworkParameters params) throws ScriptException { if (ScriptPattern.isPayToScriptHash(getScriptPubKey())) - return (LegacyAddress) getScriptPubKey().getToAddress(networkParameters); - + return LegacyAddress.fromP2SHHash(params, ScriptPattern.extractHashFromPayToScriptHash(getScriptPubKey())); return null; } diff --git a/core/src/main/java/org/bitcoinj/script/ScriptPattern.java b/core/src/main/java/org/bitcoinj/script/ScriptPattern.java index 13667b11..9b4e94ae 100644 --- a/core/src/main/java/org/bitcoinj/script/ScriptPattern.java +++ b/core/src/main/java/org/bitcoinj/script/ScriptPattern.java @@ -65,9 +65,14 @@ public class ScriptPattern { } /** - *

Whether or not this is a scriptPubKey representing a pay-to-script-hash output. In such outputs, the logic that + *

+ * Whether or not this is a scriptPubKey representing a pay-to-script-hash output. In such outputs, the logic that * controls reclamation is not actually in the output at all. Instead there's just a hash, and it's up to the * spending input to provide a program matching that hash. + *

+ *

+ * P2SH is described by BIP16. + *

*/ public static boolean isPayToScriptHash(Script script) { List chunks = script.chunks; diff --git a/core/src/test/java/org/bitcoinj/core/TransactionOutputTest.java b/core/src/test/java/org/bitcoinj/core/TransactionOutputTest.java index 054fab27..0cc14273 100644 --- a/core/src/test/java/org/bitcoinj/core/TransactionOutputTest.java +++ b/core/src/test/java/org/bitcoinj/core/TransactionOutputTest.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList; import org.bitcoinj.params.MainNetParams; import org.bitcoinj.script.Script; import org.bitcoinj.script.ScriptBuilder; +import org.bitcoinj.script.ScriptPattern; import org.bitcoinj.testing.TestWithWallet; import org.bitcoinj.wallet.SendRequest; import org.hamcrest.CoreMatchers; @@ -71,15 +72,16 @@ public class TransactionOutputTest extends TestWithWallet { Script script = ScriptBuilder.createOutputScript(P2SHAddress); Transaction tx = new Transaction(MAINNET); tx.addOutput(Coin.COIN, script); - assertEquals(P2SHAddressString, tx.getOutput(0).getAddressFromP2SH(MAINNET).toString()); + assertEquals(P2SHAddressString, tx.getOutput(0).getScriptPubKey().getToAddress(MAINNET).toString()); } @Test public void getAddressTests() throws Exception { Transaction tx = new Transaction(MAINNET); tx.addOutput(Coin.CENT, ScriptBuilder.createOpReturnScript("hello world!".getBytes())); - assertNull(tx.getOutput(0).getAddressFromP2SH(UNITTEST)); - assertNull(tx.getOutput(0).getAddressFromP2PKHScript(UNITTEST)); + assertTrue(ScriptPattern.isOpReturn(tx.getOutput(0).getScriptPubKey())); + assertFalse(ScriptPattern.isPayToPubKey(tx.getOutput(0).getScriptPubKey())); + assertFalse(ScriptPattern.isPayToPubKeyHash(tx.getOutput(0).getScriptPubKey())); } @Test diff --git a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java index a2747655..8b605165 100644 --- a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java +++ b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java @@ -19,6 +19,7 @@ package org.bitcoinj.wallet; import org.bitcoinj.core.listeners.TransactionConfidenceEventListener; import org.bitcoinj.core.AbstractBlockChain; +import org.bitcoinj.core.Address; import org.bitcoinj.core.LegacyAddress; import org.bitcoinj.core.Block; import org.bitcoinj.core.BlockChain; @@ -3035,7 +3036,7 @@ public class WalletTest extends TestWithWallet { wallet.setKeyRotationTime(goodKey.getCreationTimeSeconds()); List txns = wallet.doMaintenance(null, false).get(); assertEquals(1, txns.size()); - LegacyAddress output = txns.get(0).getOutput(0).getAddressFromP2PKHScript(UNITTEST); + Address output = txns.get(0).getOutput(0).getScriptPubKey().getToAddress(UNITTEST); ECKey usedKey = wallet.findKeyFromPubHash(output.getHash()); assertEquals(goodKey.getCreationTimeSeconds(), usedKey.getCreationTimeSeconds()); assertEquals(goodKey.getCreationTimeSeconds(), wallet.freshReceiveKey().getCreationTimeSeconds());