From 234384c87e48dc8884cc47040e1218d90789ea5f Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Mon, 26 Feb 2018 11:14:13 +0100 Subject: [PATCH] Script.getPubKey(): Remove ability to extract the pubKey from inputs spending from P2PKH outputs. This was only used in unit tests to determine the from address. One of the tests now tests this manually so that we don't loose test coverage. --- core/src/main/java/org/bitcoinj/script/Script.java | 10 ++-------- core/src/test/java/org/bitcoinj/script/ScriptTest.java | 7 +++---- core/src/test/java/org/bitcoinj/wallet/WalletTest.java | 5 ++++- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/script/Script.java b/core/src/main/java/org/bitcoinj/script/Script.java index 2be5769f..2d8e936a 100644 --- a/core/src/main/java/org/bitcoinj/script/Script.java +++ b/core/src/main/java/org/bitcoinj/script/Script.java @@ -258,9 +258,7 @@ public class Script { } /** - * Returns the public key in this script. If a script contains two constants and nothing else, it is assumed to - * be a scriptSig (input) for a pay-to-address output and the second constant is returned (the first is the - * signature). If a script contains a constant and an OP_CHECKSIG opcode, the constant is returned as it is + * Returns the public key in this script. If a script contains a constant and an OP_CHECKSIG opcode, the constant is returned as it is * assumed to be a direct pay-to-key scriptPubKey (output) and the first constant is the public key. * * @throws ScriptException if the script is none of the named forms. @@ -272,11 +270,7 @@ public class Script { final ScriptChunk chunk0 = chunks.get(0); final byte[] chunk0data = chunk0.data; final ScriptChunk chunk1 = chunks.get(1); - final byte[] chunk1data = chunk1.data; - if (chunk0data != null && chunk0data.length > 2 && chunk1data != null && chunk1data.length > 2) { - // If we have two large constants assume the input to a pay-to-address output. - return chunk1data; - } else if (chunk1.equalsOpCode(OP_CHECKSIG) && chunk0data != null && chunk0data.length > 2) { + if (chunk1.equalsOpCode(OP_CHECKSIG) && chunk0data != null && chunk0data.length > 2) { // A large constant followed by an OP_CHECKSIG is the key. return chunk0data; } else { diff --git a/core/src/test/java/org/bitcoinj/script/ScriptTest.java b/core/src/test/java/org/bitcoinj/script/ScriptTest.java index 6beb1438..e2d99427 100644 --- a/core/src/test/java/org/bitcoinj/script/ScriptTest.java +++ b/core/src/test/java/org/bitcoinj/script/ScriptTest.java @@ -68,10 +68,9 @@ public class ScriptTest { public void testScriptSig() throws Exception { byte[] sigProgBytes = HEX.decode(sigProg); Script script = new Script(sigProgBytes); - // Test we can extract the from address. - byte[] hash160 = Utils.sha256hash160(script.getPubKey()); - Address a = new Address(PARAMS, hash160); - assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", a.toString()); + assertEquals( + "PUSHDATA(71)[304402202b4da291cc39faf8433911988f9f49fc5c995812ca2f94db61468839c228c3e90220628bff3ff32ec95825092fa051cba28558a981fcf59ce184b14f2e215e69106701] PUSHDATA(65)[0414b38f4be3bb9fa0f4f32b74af07152b2f2f630bc02122a491137b6c523e46f18a0d5034418966f93dfc37cc3739ef7b2007213a302b7fba161557f4ad644a1c]", + script.toString()); } @Test diff --git a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java index 62c57c5e..8b40ba8c 100644 --- a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java +++ b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java @@ -39,6 +39,7 @@ import org.bitcoinj.core.TransactionConfidence.ConfidenceType; import org.bitcoinj.crypto.*; import org.bitcoinj.script.Script; import org.bitcoinj.script.ScriptBuilder; +import org.bitcoinj.script.ScriptChunk; import org.bitcoinj.signers.StatelessTransactionSigner; import org.bitcoinj.signers.TransactionSigner; import org.bitcoinj.store.BlockStoreException; @@ -481,8 +482,10 @@ public class WalletTest extends TestWithWallet { // Do some basic sanity checks. assertEquals(1, t2.getInputs().size()); + List scriptSigChunks = t2.getInput(0).getScriptSig().getChunks(); // check 'from address' -- in a unit test this is fine - assertEquals(myAddress, new Address(PARAMS, Utils.sha256hash160(t2.getInput(0).getScriptSig().getPubKey()))); + assertEquals(2, scriptSigChunks.size()); + assertEquals(myAddress, new Address(PARAMS, Utils.sha256hash160(scriptSigChunks.get(1).data))); assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType()); // We have NOT proven that the signature is correct!