Commit 6ab3dbb1 authored by Jeremy Bokobza's avatar Jeremy Bokobza

For spending transactions, added a record of the payments made out to other scripts

parent 636cc76a
...@@ -155,10 +155,7 @@ namespace Breeze.Wallet ...@@ -155,10 +155,7 @@ namespace Breeze.Wallet
throw new Exception($"No account with name {accountName} could be found."); throw new Exception($"No account with name {accountName} could be found.");
} }
return account; return account;
} }
} }
/// <summary> /// <summary>
...@@ -417,6 +414,12 @@ namespace Breeze.Wallet ...@@ -417,6 +414,12 @@ namespace Breeze.Wallet
[JsonConverter(typeof(MoneyJsonConverter))] [JsonConverter(typeof(MoneyJsonConverter))]
public Money Amount { get; set; } public Money Amount { get; set; }
/// <summary>
/// A list of payments made out in this transaction.
/// </summary>
[JsonProperty(PropertyName = "payments", NullValueHandling = NullValueHandling.Ignore)]
public ICollection<PaymentDetails> Payments { get; set; }
/// <summary> /// <summary>
/// The index of this scriptPubKey in the transaction it is contained. /// The index of this scriptPubKey in the transaction it is contained.
/// </summary> /// </summary>
...@@ -444,4 +447,30 @@ namespace Breeze.Wallet ...@@ -444,4 +447,30 @@ namespace Breeze.Wallet
return this.BlockHeight != null; return this.BlockHeight != null;
} }
} }
/// <summary>
/// An object representing a payment.
/// </summary>
public class PaymentDetails
{
/// <summary>
/// The script pub key of the destination address.
/// </summary>
[JsonProperty(PropertyName = "destinationScriptPubKey")]
[JsonConverter(typeof(ScriptJsonConverter))]
public Script DestinationScriptPubKey { get; set; }
/// <summary>
/// The Base58 representation of the destination address.
/// </summary>
[JsonProperty(PropertyName = "destinationAddress")]
public string DestinationAddress { get; set; }
/// <summary>
/// The transaction amount.
/// </summary>
[JsonProperty(PropertyName = "amount")]
[JsonConverter(typeof(MoneyJsonConverter))]
public Money Amount { get; set; }
}
} }
\ No newline at end of file
...@@ -475,8 +475,12 @@ namespace Breeze.Wallet ...@@ -475,8 +475,12 @@ namespace Breeze.Wallet
TransactionData tTx = this.keysLookup.Values.SelectMany(v => v.Transactions).Single(trackedTx => trackedTx.Id == input.PrevOut.Hash && trackedTx.Index == input.PrevOut.N); TransactionData tTx = this.keysLookup.Values.SelectMany(v => v.Transactions).Single(trackedTx => trackedTx.Id == input.PrevOut.Hash && trackedTx.Index == input.PrevOut.N);
// find the script this input references // find the script this input references
var keyToSpend = this.keysLookup.Single(v => v.Value.Transactions.Contains(tTx)).Key; var keyToSpend = this.keysLookup.Single(v => v.Value.Transactions.Contains(tTx)).Key;
AddTransactionToWallet(transaction.GetHash(), transaction.Time, null, -tTx.Amount, keyToSpend, blockHeight, blockTime, tTx.Id, tTx.Index);
// get the details of the outputs paid out
IEnumerable<TxOut> paidoutto = transaction.Outputs.Where(o => !this.keysLookup.Keys.Contains(o.ScriptPubKey));
AddTransactionToWallet(transaction.GetHash(), transaction.Time, null, -tTx.Amount, keyToSpend, blockHeight, blockTime, tTx.Id, tTx.Index, paidoutto);
} }
} }
...@@ -492,7 +496,7 @@ namespace Breeze.Wallet ...@@ -492,7 +496,7 @@ namespace Breeze.Wallet
/// <param name="blockTime">The block time.</param> /// <param name="blockTime">The block time.</param>
/// <param name="spendingTransactionId">The id of the transaction containing the output being spent, if this is a spending transaction.</param> /// <param name="spendingTransactionId">The id of the transaction containing the output being spent, if this is a spending transaction.</param>
/// <param name="spendingTransactionIndex">The index of the output in the transaction being referenced, if this is a spending transaction.</param> /// <param name="spendingTransactionIndex">The index of the output in the transaction being referenced, if this is a spending transaction.</param>
private void AddTransactionToWallet(uint256 transactionHash, uint time, int? index, Money amount, Script script, int? blockHeight = null, uint? blockTime = null, uint256 spendingTransactionId = null, int? spendingTransactionIndex = null) private void AddTransactionToWallet(uint256 transactionHash, uint time, int? index, Money amount, Script script, int? blockHeight = null, uint? blockTime = null, uint256 spendingTransactionId = null, int? spendingTransactionIndex = null, IEnumerable<TxOut> paidToOutputs = null)
{ {
// get the collection of transactions to add to. // get the collection of transactions to add to.
this.keysLookup.TryGetValue(script, out HdAddress address); this.keysLookup.TryGetValue(script, out HdAddress address);
...@@ -502,14 +506,32 @@ namespace Breeze.Wallet ...@@ -502,14 +506,32 @@ namespace Breeze.Wallet
// if it's the first time we see this transaction // if it's the first time we see this transaction
if (trans.All(t => t.Id != transactionHash)) if (trans.All(t => t.Id != transactionHash))
{ {
trans.Add(new TransactionData var newTransaction = new TransactionData
{ {
Amount = amount, Amount = amount,
BlockHeight = blockHeight, BlockHeight = blockHeight,
Id = transactionHash, Id = transactionHash,
CreationTime = DateTimeOffset.FromUnixTimeMilliseconds(blockTime ?? time), CreationTime = DateTimeOffset.FromUnixTimeMilliseconds(blockTime ?? time),
Index = index Index = index
}); };
trans.Add(newTransaction);
// if this is a spending transaction, keep a record of the payments made out to other scripts.
if (paidToOutputs != null && paidToOutputs.Any())
{
List<PaymentDetails> payments = new List<PaymentDetails>();
foreach (var paidToOutput in paidToOutputs)
{
payments.Add(new PaymentDetails
{
DestinationScriptPubKey = paidToOutput.ScriptPubKey,
DestinationAddress = paidToOutput.ScriptPubKey.GetDestinationAddress(this.network).ToString(),
Amount = paidToOutput.Value
});
}
newTransaction.Payments = payments;
}
// if this is a spending transaction, mark the spent transaction as such // if this is a spending transaction, mark the spent transaction as such
if (spendingTransactionId != null) if (spendingTransactionId != null)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment