From 8fa8a195c1fbf168999c31801bf8923bc386f067 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 12 Apr 2013 15:29:33 +0200 Subject: [PATCH] Transactions: Make some methods public and add a couple of accessor methods. --- .../com/google/bitcoin/core/TransactionInput.java | 11 ++++++++++- .../com/google/bitcoin/core/TransactionOutPoint.java | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/TransactionInput.java b/core/src/main/java/com/google/bitcoin/core/TransactionInput.java index 57fb443c..434aa59c 100644 --- a/core/src/main/java/com/google/bitcoin/core/TransactionInput.java +++ b/core/src/main/java/com/google/bitcoin/core/TransactionInput.java @@ -337,7 +337,7 @@ public class TransactionInput extends ChildMessage implements Serializable { * * @return true if the disconnection took place, false if it was not connected. */ - boolean disconnect() { + public boolean disconnect() { if (outpoint.fromTx == null) return false; TransactionOutput output = outpoint.fromTx.getOutput((int) outpoint.getIndex()); if (output.getSpentBy() == this) { @@ -375,4 +375,13 @@ public class TransactionInput extends ChildMessage implements Serializable { int myIndex = parentTransaction.getInputs().indexOf(this); sig.correctlySpends(parentTransaction, myIndex, pubKey, true); } + + /** + * Returns the connected output, assuming the input was connected with + * {@link TransactionInput#connect(TransactionOutput)} or variants at some point. If it wasn't connected, then + * this method returns null. + */ + public TransactionOutput getConnectedOutput() { + return getOutpoint().getConnectedOutput(); + } } diff --git a/core/src/main/java/com/google/bitcoin/core/TransactionOutPoint.java b/core/src/main/java/com/google/bitcoin/core/TransactionOutPoint.java index 7c13525a..0e6b0516 100644 --- a/core/src/main/java/com/google/bitcoin/core/TransactionOutPoint.java +++ b/core/src/main/java/com/google/bitcoin/core/TransactionOutPoint.java @@ -112,7 +112,7 @@ public class TransactionOutPoint extends ChildMessage implements Serializable { * sides in memory, and they have been linked together, this returns a pointer to the connected output, or null * if there is no such connection. */ - TransactionOutput getConnectedOutput() { + public TransactionOutput getConnectedOutput() { if (fromTx == null) return null; return fromTx.getOutputs().get((int) index); } @@ -139,7 +139,9 @@ public class TransactionOutPoint extends ChildMessage implements Serializable { * @return an ECKey or null if the connected key cannot be found in the wallet. */ public ECKey getConnectedKey(Wallet wallet) throws ScriptException { - Script connectedScript = getConnectedOutput().getScriptPubKey(); + TransactionOutput connectedOutput = getConnectedOutput(); + checkState(connectedOutput != null, "Input is not connected in wallet so cannot retrieve connected key"); + Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return wallet.findKeyFromPubHash(addressBytes);