Commit 0e28ba41 authored by Jeremy Bokobza's avatar Jeremy Bokobza

Added endpoint to get general information about the state of a particular wallet

parent 848c1a11
...@@ -89,16 +89,20 @@ POST /wallet/send-transaction - Attempts to send a transaction ...@@ -89,16 +89,20 @@ POST /wallet/send-transaction - Attempts to send a transaction
# Details # Details
## GET /wallet/general-info - Displays general information on the wallet ## GET /wallet/general-info - Displays general information on the wallet
### Query parameters
`walletName` (required) - the name of the wallet.
### Responses ### Responses
``` ```
{ {
"walletFilePath": "path to the wallet file", "walletFilePath":"path to the wallet file",
"encryptedSeed": "6PYKWP34en1wELfcJDgXaFRPugjgkDdEk2p2Pzytm1158dxgNyLAUXwpKL", "network":"main", //"testnet", "stratismain", "stratistest"
"chainCode": "q/Fn7+RSIVM0p0Nj6rIuNkybF+0WKeSZPMQS2QCbDzY=", "creationTime":"2017-03-21",
"network": "main", // main/testnet "isDecrypted":true,
"creationTime": "2017-03-21", "lastBlockHeight":123234,
"isDecrypted": true, "chainTip": 173721,
"uniqueId": "sadwpiqjdpijwqdpijwqidjoi" // can only get if decrypted, if not it's empty string "connectedNodes": 5
} }
``` ```
## GET /wallet/sensitive - Displays sensitive information on the wallet ## GET /wallet/sensitive - Displays sensitive information on the wallet
......
...@@ -10,6 +10,7 @@ using Breeze.Wallet.Errors; ...@@ -10,6 +10,7 @@ using Breeze.Wallet.Errors;
using Breeze.Wallet.Helpers; using Breeze.Wallet.Helpers;
using Breeze.Wallet.Models; using Breeze.Wallet.Models;
using NBitcoin; using NBitcoin;
using Stratis.Bitcoin.Connection;
namespace Breeze.Api.Tests namespace Breeze.Api.Tests
{ {
...@@ -22,7 +23,7 @@ namespace Breeze.Api.Tests ...@@ -22,7 +23,7 @@ namespace Breeze.Api.Tests
var mockWalletCreate = new Mock<IWalletManager>(); var mockWalletCreate = new Mock<IWalletManager>();
mockWalletCreate.Setup(wallet => wallet.CreateWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null)).Returns(mnemonic); mockWalletCreate.Setup(wallet => wallet.CreateWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null)).Returns(mnemonic);
var controller = new WalletController(mockWalletCreate.Object, new Mock<ITracker>().Object); var controller = new WalletController(mockWalletCreate.Object, new Mock<ITracker>().Object, new Mock<ConnectionManager>().Object, new Mock<Network>().Object, new Mock<ConcurrentChain>().Object);
// Act // Act
var result = controller.Create(new WalletCreationRequest var result = controller.Create(new WalletCreationRequest
...@@ -52,7 +53,7 @@ namespace Breeze.Api.Tests ...@@ -52,7 +53,7 @@ namespace Breeze.Api.Tests
var mockWalletWrapper = new Mock<IWalletManager>(); var mockWalletWrapper = new Mock<IWalletManager>();
mockWalletWrapper.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<DateTime>())).Returns(wallet); mockWalletWrapper.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<DateTime>())).Returns(wallet);
var controller = new WalletController(mockWalletWrapper.Object, new Mock<ITracker>().Object); var controller = new WalletController(mockWalletWrapper.Object, new Mock<ITracker>().Object, new Mock<ConnectionManager>().Object, new Mock<Network>().Object, new Mock<ConcurrentChain>().Object);
// Act // Act
var result = controller.Recover(new WalletRecoveryRequest var result = controller.Recover(new WalletRecoveryRequest
...@@ -81,7 +82,7 @@ namespace Breeze.Api.Tests ...@@ -81,7 +82,7 @@ namespace Breeze.Api.Tests
var mockWalletWrapper = new Mock<IWalletManager>(); var mockWalletWrapper = new Mock<IWalletManager>();
mockWalletWrapper.Setup(w => w.LoadWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(wallet); mockWalletWrapper.Setup(w => w.LoadWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(wallet);
var controller = new WalletController(mockWalletWrapper.Object, new Mock<ITracker>().Object); var controller = new WalletController(mockWalletWrapper.Object, new Mock<ITracker>().Object, new Mock<ConnectionManager>().Object, new Mock<Network>().Object, new Mock<ConcurrentChain>().Object);
// Act // Act
var result = controller.Load(new WalletLoadRequest var result = controller.Load(new WalletLoadRequest
...@@ -103,7 +104,7 @@ namespace Breeze.Api.Tests ...@@ -103,7 +104,7 @@ namespace Breeze.Api.Tests
var mockWalletWrapper = new Mock<IWalletManager>(); var mockWalletWrapper = new Mock<IWalletManager>();
mockWalletWrapper.Setup(wallet => wallet.LoadWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws<FileNotFoundException>(); mockWalletWrapper.Setup(wallet => wallet.LoadWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws<FileNotFoundException>();
var controller = new WalletController(mockWalletWrapper.Object, new Mock<ITracker>().Object); var controller = new WalletController(mockWalletWrapper.Object, new Mock<ITracker>().Object, new Mock<ConnectionManager>().Object, new Mock<Network>().Object, new Mock<ConcurrentChain>().Object);
// Act // Act
var result = controller.Load(new WalletLoadRequest var result = controller.Load(new WalletLoadRequest
...@@ -119,6 +120,5 @@ namespace Breeze.Api.Tests ...@@ -119,6 +120,5 @@ namespace Breeze.Api.Tests
Assert.NotNull(viewResult); Assert.NotNull(viewResult);
Assert.Equal(404, viewResult.StatusCode); Assert.Equal(404, viewResult.StatusCode);
} }
} }
} }
...@@ -8,6 +8,7 @@ using Breeze.Wallet.Errors; ...@@ -8,6 +8,7 @@ using Breeze.Wallet.Errors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Breeze.Wallet.Models; using Breeze.Wallet.Models;
using NBitcoin; using NBitcoin;
using Stratis.Bitcoin.Connection;
namespace Breeze.Wallet.Controllers namespace Breeze.Wallet.Controllers
{ {
...@@ -21,10 +22,22 @@ namespace Breeze.Wallet.Controllers ...@@ -21,10 +22,22 @@ namespace Breeze.Wallet.Controllers
private readonly ITracker tracker; private readonly ITracker tracker;
public WalletController(IWalletManager walletManager, ITracker tracker) private readonly CoinType coinType;
private readonly Network network;
private readonly ConnectionManager connectionManager;
private readonly ConcurrentChain chain;
public WalletController(IWalletManager walletManager, ITracker tracker, ConnectionManager connectionManager, Network network, ConcurrentChain chain)
{ {
this.walletManager = walletManager; this.walletManager = walletManager;
this.tracker = tracker; this.tracker = tracker;
this.connectionManager = connectionManager;
this.network = network;
this.coinType = (CoinType)network.Consensus.CoinType;
this.chain = chain;
} }
/// <summary> /// <summary>
...@@ -143,11 +156,11 @@ namespace Breeze.Wallet.Controllers ...@@ -143,11 +156,11 @@ namespace Breeze.Wallet.Controllers
/// <summary> /// <summary>
/// Get some general info about a wallet. /// Get some general info about a wallet.
/// </summary> /// </summary>
/// <param name="model">The name of the wallet.</param> /// <param name="request">The name of the wallet.</param>
/// <returns></returns> /// <returns></returns>
[Route("general-info")] [Route("general-info")]
[HttpGet] [HttpGet]
public IActionResult GetGeneralInfo([FromQuery] WalletName model) public IActionResult GetGeneralInfo([FromQuery] WalletName request)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -158,7 +171,19 @@ namespace Breeze.Wallet.Controllers ...@@ -158,7 +171,19 @@ namespace Breeze.Wallet.Controllers
try try
{ {
return this.Json(this.walletManager.GetGeneralInfo(model.Name)); Wallet wallet = this.walletManager.GetWallet(request.Name);
var model = new WalletGeneralInfoModel
{
Network = wallet.Network,
WalletFilePath = wallet.WalletFilePath,
CreationTime = wallet.CreationTime,
LastBlockSyncedHeight = wallet.AccountsRoot.Single(a => a.CoinType == this.coinType).LastBlockSyncedHeight,
ConnectedNodes = this.connectionManager.ConnectedNodes.Count(),
ChainTip = this.chain.Tip.Height,
IsDecrypted = true
};
return this.Json(model);
} }
catch (Exception e) catch (Exception e)
......
...@@ -113,7 +113,12 @@ namespace Breeze.Wallet ...@@ -113,7 +113,12 @@ namespace Breeze.Wallet
/// <returns></returns> /// <returns></returns>
IEnumerable<HdAddress> GetHistoryByCoinType(Wallet wallet, CoinType coinType); IEnumerable<HdAddress> GetHistoryByCoinType(Wallet wallet, CoinType coinType);
WalletGeneralInfoModel GetGeneralInfo(string walletName); /// <summary>
/// Gets some general information about a wallet.
/// </summary>
/// <param name="walletName">The name of the wallet.</param>
/// <returns></returns>
Wallet GetWallet(string walletName);
/// <summary> /// <summary>
/// Gets a list of accounts filtered by coin type. /// Gets a list of accounts filtered by coin type.
......
using System; using System;
using System.Collections.Generic; using Breeze.Wallet.JsonConverters;
using System.Linq; using NBitcoin;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Breeze.Wallet.Models namespace Breeze.Wallet.Models
...@@ -11,22 +10,36 @@ namespace Breeze.Wallet.Models ...@@ -11,22 +10,36 @@ namespace Breeze.Wallet.Models
[JsonProperty(PropertyName = "walletFilePath")] [JsonProperty(PropertyName = "walletFilePath")]
public string WalletFilePath { get; set; } public string WalletFilePath { get; set; }
[JsonProperty(PropertyName = "encryptedSeed")]
public string EncryptedSeed { get; set; }
[JsonProperty(PropertyName = "chainCode")]
public string ChainCode { get; set; }
[JsonProperty(PropertyName = "network")] [JsonProperty(PropertyName = "network")]
public string Network { get; set; } [JsonConverter(typeof(NetworkConverter))]
public Network Network { get; set; }
/// <summary>
/// The time this wallet was created.
/// </summary>
[JsonProperty(PropertyName = "creationTime")] [JsonProperty(PropertyName = "creationTime")]
public string CreationTime { get; set; } [JsonConverter(typeof(DateTimeOffsetConverter))]
public DateTimeOffset CreationTime { get; set; }
[JsonProperty(PropertyName = "isDecrypted")] [JsonProperty(PropertyName = "isDecrypted")]
public bool IsDecrypted { get; set; } public bool IsDecrypted { get; set; }
[JsonProperty(PropertyName = "uniqueId")] /// <summary>
public string UniqueId { get; set; } /// The height of the last block that was synced.
/// </summary>
[JsonProperty(PropertyName = "lastBlockSyncedHeight")]
public int? LastBlockSyncedHeight { get; set; }
/// <summary>
/// The total number of blocks.
/// </summary>
[JsonProperty(PropertyName = "chainTip")]
public int? ChainTip { get; set; }
/// <summary>
/// The total number of nodes that we're connected to.
/// </summary>
[JsonProperty(PropertyName = "connectedNodes")]
public int ConnectedNodes { get; set; }
} }
} }
...@@ -314,9 +314,11 @@ namespace Breeze.Wallet ...@@ -314,9 +314,11 @@ namespace Breeze.Wallet
return addressesCreated; return addressesCreated;
} }
public WalletGeneralInfoModel GetGeneralInfo(string name) /// <inheritdoc />
public Wallet GetWallet(string walletName)
{ {
throw new System.NotImplementedException(); Wallet wallet = this.GetWalletByName(walletName);
return wallet;
} }
/// <inheritdoc /> /// <inheritdoc />
......
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