diff --git a/src/com/google/bitcoin/core/Transaction.java b/src/com/google/bitcoin/core/Transaction.java index d3b570cc..2394439d 100644 --- a/src/com/google/bitcoin/core/Transaction.java +++ b/src/com/google/bitcoin/core/Transaction.java @@ -98,9 +98,11 @@ public class Transaction extends Message implements Serializable { } /** - * Returns the sum of the outputs that are sending coins to a key in our wallet. + * Calculates the sum of the outputs that are sending coins to a key in the wallet. + * @return sum in nanocoins */ public BigInteger getValueSentToMe(Wallet wallet) { + // This is tested in WalletTest. BigInteger v = BigInteger.ZERO; for (TransactionOutput o : outputs) { if (o.isMine(wallet)) { @@ -110,6 +112,28 @@ public class Transaction extends Message implements Serializable { return v; } + /** + * Calculates the sum of the inputs that are spending coins with keys in the wallet. This requires the + * transactions sending coins to those keys to be in the wallet. This method will not attempt to download the + * blocks containing the input transactions if the key is in the wallet but the transactions are not. + * + * @return sum in nanocoins. + */ + public BigInteger getValueSentFromMe(Wallet wallet) throws ScriptException { + // This is tested in WalletTest. + BigInteger v = BigInteger.ZERO; + for (TransactionInput input : inputs) { + boolean connected = input.outpoint.connect(wallet.unspent) || + input.outpoint.connect(wallet.fullySpent); + if (connected) { + // This input is taking value from an transaction in our wallet. To discover the value, + // we must find the connected transaction. + v = v.add(input.outpoint.getConnectedOutput().getValue()); + } + } + return v; + } + /** * These constants are a part of a scriptSig signature on the inputs. They define the details of how a * transaction can be redeemed, specifically, they control how the hash of the transaction is calculated. @@ -223,7 +247,7 @@ public class Transaction extends Message implements Serializable { /** * Once a transaction has some inputs and outputs added, the signatures in the inputs can be calculated. The * signature is over the transaction itself, to prove the redeemer actually created that transaction, - * so we have to do this step last. + * so we have to do this step last.
*
* This method is similar to SignatureHash in script.cpp
*
diff --git a/src/com/google/bitcoin/core/TransactionOutPoint.java b/src/com/google/bitcoin/core/TransactionOutPoint.java
index e2beacf1..6ae16d39 100644
--- a/src/com/google/bitcoin/core/TransactionOutPoint.java
+++ b/src/com/google/bitcoin/core/TransactionOutPoint.java
@@ -19,15 +19,16 @@ package com.google.bitcoin.core;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
+import java.util.Arrays;
+import java.util.List;
/**
- * This message is effectively a reference or pointer to a transaction output.
+ * This message is a reference or pointer to an output of a different transaction.
*/
public class TransactionOutPoint extends Message implements Serializable {
- // ID of the transaction to which we refer.
-
+ /** Hash of the transaction to which we refer. */
byte[] hash;
- // Which output of that transaction we are talking about.
+ /** Which output of that transaction we are talking about. */
long index;
// This is not part of bitcoin serialization.
@@ -63,6 +64,21 @@ public class TransactionOutPoint extends Message implements Serializable {
Utils.uint32ToByteStreamLE(index, stream);
}
+ /**
+ * Scans the list for the transaction this outpoint refers to, and sets up the internal reference used by
+ * getConnectedOutput().
+ * @return true if connection took place, false if the referenced transaction was not in the list.
+ */
+ boolean connect(List