TransactionInput: Add getIndex() helper.

This commit is contained in:
Andreas Schildbach
2019-02-04 19:37:30 +01:00
parent 72f78041ac
commit 415686e001
2 changed files with 13 additions and 3 deletions

View File

@@ -147,6 +147,17 @@ public class TransactionInput extends ChildMessage {
this.value = null;
}
/**
* Gets the index of this input in the parent transaction, or throws if this input is free standing. Iterates
* over the parents list to discover this.
*/
public int getIndex() {
final int myIndex = getParentTransaction().getInputs().indexOf(this);
if (myIndex < 0)
throw new IllegalStateException("Input linked to wrong parent transaction?");
return myIndex;
}
@Override
protected void parse() throws ProtocolException {
outpoint = new TransactionOutPoint(params, payload, cursor, this, serializer);
@@ -464,7 +475,7 @@ public class TransactionInput extends ChildMessage {
}
Script pubKey = output.getScriptPubKey();
int myIndex = getParentTransaction().getInputs().indexOf(this);
getScriptSig().correctlySpends(getParentTransaction(), myIndex, pubKey);
getScriptSig().correctlySpends(getParentTransaction(), getIndex(), pubKey);
}
/**

View File

@@ -295,9 +295,8 @@ public class WalletProtobufSerializer {
final TransactionInput spentBy = output.getSpentBy();
if (spentBy != null) {
Sha256Hash spendingHash = spentBy.getParentTransaction().getHash();
int spentByTransactionIndex = spentBy.getParentTransaction().getInputs().indexOf(spentBy);
outputBuilder.setSpentByTransactionHash(hashToByteString(spendingHash))
.setSpentByTransactionIndex(spentByTransactionIndex);
.setSpentByTransactionIndex(spentBy.getIndex());
}
txBuilder.addTransactionOutput(outputBuilder);
}