Commit cea9240b authored by Jeremy Bokobza's avatar Jeremy Bokobza

Fixed catching transactions in change addresses.

parent 56c9a9cf
......@@ -2,7 +2,7 @@
"variables": [],
"info": {
"name": "Wallet",
"_postman_id": "57013f2c-02dc-df32-41e9-6e4aaa14ad5e",
"_postman_id": "b5720ab4-24a5-6957-0ea6-766a9cbaf488",
"description": "Requests relating to operations on the wallet",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
......@@ -21,7 +21,7 @@
],
"body": {
"mode": "raw",
"raw": "{ \n\t\"password\": \"123456\",\n\t\"network\": \"Main\",\n\t\"folderPath\": \"Wallets\",\n\t\"name\": \"myFirstWallet\"\n}"
"raw": "{ \n\t\"password\": \"123456\",\n\t\"network\": \"testnet\",\n\t\"name\": \"testwallet\"\n}"
},
"description": ""
},
......@@ -161,7 +161,7 @@
],
"body": {
"mode": "raw",
"raw": "{\r\n \"password\": \"password\",\r\n \"address\": \"1FYp9uguYCz7DgSF9jTWDeZF8kdRKQTXPg\",\r\n \"amount\": \"0.12\",\r\n \"feeType\": \"low\",\r\n \"allowUnconfirmed\": \"true\"\r\n}"
"raw": "{\r\n\t\"walletName\": \"testwallet\",\r\n\t\"accountName\": \"account 0\",\r\n\t\"coinType\": 1,\r\n \"password\": \"password\",\r\n \"destinationAddress\": \"1FYp9uguYCz7DgSF9jTWDeZF8kdRKQTXPg\",\r\n \"amount\": \"0.12\",\r\n \"feeType\": \"low\",\r\n \"allowUnconfirmed\": \"true\"\r\n}"
},
"description": ""
},
......
......@@ -84,6 +84,20 @@ namespace Breeze.Wallet
}
return result;
}
/// <summary>
/// Gets all the pub keys conatined in this wallet.
/// </summary>
/// <param name="coinType">Type of the coin.</param>
/// <returns></returns>
public IEnumerable<Script> GetAllPubKeysByCoinType(CoinType coinType)
{
var accounts = this.GetAccountsByCoinType(coinType).ToList();
foreach (var address in accounts.SelectMany(a => a.ExternalAddresses).Concat(accounts.SelectMany(a => a.InternalAddresses)))
{
yield return address.ScriptPubKey;
}
}
}
/// <summary>
......@@ -142,6 +156,9 @@ namespace Breeze.Wallet
}
return account;
}
}
/// <summary>
......
......@@ -496,14 +496,10 @@ namespace Breeze.Wallet
private void AddTransactionToWallet(CoinType coinType, uint256 transactionHash, uint time, int? index, Money amount, Script script, int? blockHeight = null, uint? blockTime = null, uint256 spendingTransactionId = null, int? spendingTransactionIndex = null)
{
// selects all the transactions we already have in the wallet
var txs = this.Wallets.
SelectMany(w => w.AccountsRoot.Where(a => a.CoinType == coinType)).
SelectMany(a => a.Accounts).
SelectMany(a => a.ExternalAddresses).
SelectMany(t => t.Transactions);
var txs = this.Wallets.SelectMany(w => w.GetAllTransactionsByCoinType(coinType));
// add this transaction if it is not in the list
if (txs.All(t => t.Id != transactionHash))
if (txs.All(t => t.Id != transactionHash || t.Index != index))
{
foreach (var wallet in this.Wallets)
{
......@@ -511,7 +507,12 @@ namespace Breeze.Wallet
{
foreach (var account in accountRoot.Accounts)
{
foreach (var address in account.ExternalAddresses.Where(a => a.ScriptPubKey == script))
var receivingAddress = account.ExternalAddresses.SingleOrDefault(a => a.ScriptPubKey == script);
var changeAddress = account.InternalAddresses.SingleOrDefault(a => a.ScriptPubKey == script);
bool isChange = receivingAddress == null && changeAddress != null;
var address = receivingAddress ?? changeAddress;
if (address != null)
{
address.Transactions = address.Transactions.Concat(new[]
{
......@@ -527,8 +528,7 @@ namespace Breeze.Wallet
});
// notify a transaction has been found
this.TransactionFound?.Invoke(this, new TransactionFoundEventArgs(wallet, accountRoot.CoinType, account, address, false));
}
this.TransactionFound?.Invoke(this, new TransactionFoundEventArgs(wallet, accountRoot.CoinType, account, address, isChange));
// if this is a spending transaction, mark the spent transaction as such
if (spendingTransactionId != null)
......@@ -542,6 +542,7 @@ namespace Breeze.Wallet
}
}
}
}
this.TrackedTransactions.Add(new TransactionDetails
{
......@@ -725,13 +726,12 @@ namespace Breeze.Wallet
/// <returns></returns>
private HashSet<Script> LoadKeys(CoinType coinType)
{
return new HashSet<Script>(this.Wallets.
SelectMany(w => w.AccountsRoot.Where(a => a.CoinType == coinType)).
SelectMany(a => a.Accounts).
SelectMany(a => a.ExternalAddresses).
Select(s => s.ScriptPubKey));
// uncomment the following for testing on a random address
//Select(t => (new BitcoinPubKeyAddress(t.Address, Network.Main)).ScriptPubKey));
var keys = new HashSet<Script>();
foreach (Wallet wallet in this.Wallets)
{
keys.UnionWith(wallet.GetAllPubKeysByCoinType(coinType));
}
return keys;
}
/// <summary>
......@@ -741,18 +741,20 @@ namespace Breeze.Wallet
/// <returns></returns>
private HashSet<TransactionDetails> LoadTransactions(CoinType coinType)
{
return new HashSet<TransactionDetails>(this.Wallets.
SelectMany(w => w.AccountsRoot.Where(a => a.CoinType == coinType)).
SelectMany(a => a.Accounts).
SelectMany(a => a.ExternalAddresses).
SelectMany(t => t.Transactions).
Select(t => new TransactionDetails
var keys = new HashSet<TransactionDetails>();
foreach (Wallet wallet in this.Wallets)
{
keys.UnionWith(wallet.GetAllTransactionsByCoinType(coinType)
.Select(t =>
new TransactionDetails
{
Hash = t.Id,
Index = t.Index,
Amount = t.Amount
}));
}
return keys;
}
/// <summary>
/// Gets a wallet given its name.
......
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