From 4971d3fc254323318b050d83fb5d23fcdf6cfc5a Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sun, 4 Mar 2018 17:08:47 +0100 Subject: [PATCH] LegacyAddress: Deprecate fromP2SHScript() static constructor. --- .../src/main/java/org/bitcoinj/core/LegacyAddress.java | 10 +++------- core/src/main/java/org/bitcoinj/script/Script.java | 2 +- .../main/java/org/bitcoinj/wallet/KeyChainGroup.java | 3 ++- core/src/main/java/org/bitcoinj/wallet/Wallet.java | 3 ++- .../test/java/org/bitcoinj/core/LegacyAddressTest.java | 7 +++++-- core/src/test/java/org/bitcoinj/script/ScriptTest.java | 3 ++- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/LegacyAddress.java b/core/src/main/java/org/bitcoinj/core/LegacyAddress.java index c10f7078d..06edc4426 100644 --- a/core/src/main/java/org/bitcoinj/core/LegacyAddress.java +++ b/core/src/main/java/org/bitcoinj/core/LegacyAddress.java @@ -119,14 +119,10 @@ public class LegacyAddress extends Address { } /** - * Constructs a {@link LegacyAddress} that represents the script hash extracted from the given scriptPubKey. - * - * @param params - * network this address is valid for - * @param scriptPubKey - * scriptPubKey - * @return constructed address + * @deprecated use {@link #fromScriptHash(NetworkParameters, byte[])} in combination with + * {@link ScriptPattern#extractHashFromPayToScriptHash(Script)} */ + @Deprecated public static LegacyAddress fromP2SHScript(NetworkParameters params, Script scriptPubKey) { checkArgument(ScriptPattern.isPayToScriptHash(scriptPubKey), "Not a P2SH script"); return fromScriptHash(params, ScriptPattern.extractHashFromPayToScriptHash(scriptPubKey)); diff --git a/core/src/main/java/org/bitcoinj/script/Script.java b/core/src/main/java/org/bitcoinj/script/Script.java index b2f7e745e..daf23ab33 100644 --- a/core/src/main/java/org/bitcoinj/script/Script.java +++ b/core/src/main/java/org/bitcoinj/script/Script.java @@ -293,7 +293,7 @@ public class Script { if (ScriptPattern.isPayToPubKeyHash(this)) return LegacyAddress.fromPubKeyHash(params, ScriptPattern.extractHashFromPayToPubKeyHash(this)); else if (ScriptPattern.isPayToScriptHash(this)) - return LegacyAddress.fromP2SHScript(params, this); + return LegacyAddress.fromScriptHash(params, ScriptPattern.extractHashFromPayToScriptHash(this)); else if (forcePayToPubKey && ScriptPattern.isPayToPubKey(this)) return LegacyAddress.fromKey(params, ECKey.fromPublicOnly(ScriptPattern.extractKeyFromPayToPubKey(this))); else if (ScriptPattern.isPayToWitnessHash(this)) diff --git a/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java b/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java index d08716c05..17a10c64a 100644 --- a/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java +++ b/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java @@ -246,7 +246,8 @@ public class KeyChainGroup implements KeyBag { if (chain.isMarried()) { Script outputScript = chain.freshOutputScript(purpose); checkState(ScriptPattern.isPayToScriptHash(outputScript)); // Only handle P2SH for now - LegacyAddress freshAddress = LegacyAddress.fromP2SHScript(params, outputScript); + LegacyAddress freshAddress = LegacyAddress.fromScriptHash(params, + ScriptPattern.extractHashFromPayToScriptHash(outputScript)); maybeLookaheadScripts(); currentAddresses.put(purpose, freshAddress); return freshAddress; diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index e93779953..c58815893 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -1115,7 +1115,8 @@ public class Wallet extends BaseTaggableObject byte[] pubkeyHash = ScriptPattern.extractHashFromPayToPubKeyHash(script); keyChainGroup.markPubKeyHashAsUsed(pubkeyHash); } else if (ScriptPattern.isPayToScriptHash(script)) { - LegacyAddress a = LegacyAddress.fromP2SHScript(tx.getParams(), script); + LegacyAddress a = LegacyAddress.fromScriptHash(tx.getParams(), + ScriptPattern.extractHashFromPayToScriptHash(script)); keyChainGroup.markP2SHAddressAsUsed(a); } } catch (ScriptException e) { diff --git a/core/src/test/java/org/bitcoinj/core/LegacyAddressTest.java b/core/src/test/java/org/bitcoinj/core/LegacyAddressTest.java index daa8831f9..178b68908 100644 --- a/core/src/test/java/org/bitcoinj/core/LegacyAddressTest.java +++ b/core/src/test/java/org/bitcoinj/core/LegacyAddressTest.java @@ -22,6 +22,7 @@ import org.bitcoinj.params.Networks; import org.bitcoinj.params.TestNet3Params; import org.bitcoinj.script.Script; import org.bitcoinj.script.ScriptBuilder; +import org.bitcoinj.script.ScriptPattern; import org.bitcoinj.script.Script.ScriptType; import org.junit.Test; @@ -168,7 +169,8 @@ public class LegacyAddressTest { assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString()); LegacyAddress b = LegacyAddress.fromScriptHash(TESTNET, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722")); assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString()); - LegacyAddress c = LegacyAddress.fromP2SHScript(MAINNET, ScriptBuilder.createP2SHOutputScript(hex)); + LegacyAddress c = LegacyAddress.fromScriptHash(MAINNET, + ScriptPattern.extractHashFromPayToScriptHash(ScriptBuilder.createP2SHOutputScript(hex))); assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString()); } @@ -184,7 +186,8 @@ public class LegacyAddressTest { List keys = Arrays.asList(key1, key2, key3); Script p2shScript = ScriptBuilder.createP2SHOutputScript(2, keys); - LegacyAddress address = LegacyAddress.fromP2SHScript(MAINNET, p2shScript); + LegacyAddress address = LegacyAddress.fromScriptHash(MAINNET, + ScriptPattern.extractHashFromPayToScriptHash(p2shScript)); assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString()); } diff --git a/core/src/test/java/org/bitcoinj/script/ScriptTest.java b/core/src/test/java/org/bitcoinj/script/ScriptTest.java index 3666469ba..c330305bf 100644 --- a/core/src/test/java/org/bitcoinj/script/ScriptTest.java +++ b/core/src/test/java/org/bitcoinj/script/ScriptTest.java @@ -445,7 +445,8 @@ public class ScriptTest { assertEquals(toAddress, ScriptBuilder.createOutputScript(toAddress).getToAddress(TESTNET, true)); // pay to script hash Script p2shScript = ScriptBuilder.createP2SHOutputScript(new byte[20]); - LegacyAddress scriptAddress = LegacyAddress.fromP2SHScript(TESTNET, p2shScript); + LegacyAddress scriptAddress = LegacyAddress.fromScriptHash(TESTNET, + ScriptPattern.extractHashFromPayToScriptHash(p2shScript)); assertEquals(scriptAddress, p2shScript.getToAddress(TESTNET, true)); }