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

Added get history

parent 0a7b7a87
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
......@@ -176,11 +177,11 @@ namespace Breeze.Wallet.Controllers
/// <summary>
/// Retrieves the history of a wallet.
/// </summary>
/// <param name="model">The name of the wallet.</param>
/// <param name="request">The request parameters.</param>
/// <returns></returns>
[Route("history")]
[HttpGet]
public IActionResult GetHistory([FromQuery] WalletName model)
public IActionResult GetHistory([FromQuery] WalletHistoryRequest request)
{
// checks the request is valid
if (!this.ModelState.IsValid)
......@@ -191,8 +192,25 @@ namespace Breeze.Wallet.Controllers
try
{
return this.Json(this.walletManager.GetHistory(model.Name));
WalletHistoryModel model = new WalletHistoryModel {Transactions = new List<TransactionItem>()};
var accounts = this.walletManager.GetAccountsByCoinType(request.WalletName, request.CoinType).ToList();
foreach (var address in accounts.SelectMany(a => a.ExternalAddresses).Concat(accounts.SelectMany(a => a.InternalAddresses)))
{
foreach (var transaction in address.Transactions)
{
model.Transactions.Add(new TransactionItem
{
Amount = transaction.Amount,
Confirmed = transaction.Confirmed,
Timestamp = transaction.CreationTime,
TransactionId = transaction.Id,
Address = address.Address
});
}
}
return this.Json(model.Transactions.OrderByDescending(t => t.Timestamp));
}
catch (Exception e)
{
......
using System;
using System.Collections.Generic;
using Breeze.Wallet.Models;
using NBitcoin;
......@@ -75,7 +76,13 @@ namespace Breeze.Wallet
WalletBalanceModel GetBalance(string walletName);
WalletHistoryModel GetHistory(string walletName);
/// <summary>
/// Gets a list of accounts filtered by coin type.
/// </summary>
/// <param name="walletName">The name of the wallet to look into.</param>
/// <param name="coinType">The type of coin to filter by.</param>
/// <returns></returns>
IEnumerable<HdAccount> GetAccountsByCoinType(string walletName, CoinType coinType);
WalletBuildTransactionModel BuildTransaction(string password, string address, Money amount, string feeType, bool allowUnconfirmed);
......
......@@ -47,7 +47,16 @@ namespace Breeze.Wallet.Models
public string Network { get; set; }
}
public class WalletName
public class WalletHistoryRequest
{
[Required(ErrorMessage = "The name of the wallet is missing.")]
public string WalletName { get; set; }
[Required(ErrorMessage = "The type of coin for which history is requested is missing.")]
public CoinType CoinType { get; set; }
}
public class WalletName
{
[Required(ErrorMessage = "The name of the wallet is missing.")]
public string Name { get; set; }
......
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Breeze.Wallet.JsonConverters;
using NBitcoin;
using NBitcoin.JsonConverters;
using Newtonsoft.Json;
namespace Breeze.Wallet.Models
......@@ -12,8 +15,15 @@ namespace Breeze.Wallet.Models
public class TransactionItem
{
[JsonProperty(PropertyName = "txId")]
public string TransactionId { get; set; }
/// <summary>
/// The Base58 representation of this address.
/// </summary>
[JsonProperty(PropertyName = "address")]
public string Address { get; set; }
[JsonProperty(PropertyName = "txId")]
[JsonConverter(typeof(UInt256JsonConverter))]
public uint256 TransactionId { get; set; }
[JsonProperty(PropertyName = "amount")]
public Money Amount { get; set; }
......@@ -22,6 +32,7 @@ namespace Breeze.Wallet.Models
public bool Confirmed { get; set; }
[JsonProperty(PropertyName = "timestamp")]
public string Timestamp { get; set; }
}
[JsonConverter(typeof(DateTimeOffsetConverter))]
public DateTimeOffset Timestamp { get; set; }
}
}
......@@ -223,7 +223,7 @@ namespace Breeze.Wallet
/// <summary>
/// The index of this scriptPubKey in the transaction it is contained.
/// </summary>
[JsonProperty(PropertyName = "index")]
[JsonProperty(PropertyName = "index", NullValueHandling = NullValueHandling.Ignore)]
public int? Index { get; set; }
/// <summary>
......
......@@ -211,9 +211,12 @@ namespace Breeze.Wallet
throw new System.NotImplementedException();
}
public WalletHistoryModel GetHistory(string walletName)
/// <inheritdoc />
public IEnumerable<HdAccount> GetAccountsByCoinType(string walletName, CoinType coinType)
{
throw new System.NotImplementedException();
return this.Wallets.
SelectMany(w => w.AccountsRoot.Where(a => a.CoinType == coinType)).
SelectMany(a => a.Accounts);
}
public WalletBuildTransactionModel BuildTransaction(string password, string address, Money amount, string feeType, bool allowUnconfirmed)
......@@ -250,7 +253,7 @@ namespace Breeze.Wallet
public void ProcessTransaction(CoinType coinType, Transaction transaction, int? blockHeight = null, uint? blockTime = null)
{
Console.WriteLine($"transaction notification: tx hash {transaction.GetHash()}, coin type: {coinType}");
foreach (var k in this.PubKeys)
{
// check if the outputs contain one of our addresses
......@@ -267,7 +270,7 @@ namespace Breeze.Wallet
// compare the index of the output in its original transaction and the index references in the input
if (input.PrevOut.N == tTx.Index)
{
{
AddTransactionToWallet(coinType, transaction.GetHash(), transaction.Time, null, -tTx.Amount, k, blockHeight, blockTime);
}
}
......
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