Commit acbb206a authored by Jeremy Bokobza's avatar Jeremy Bokobza Committed by GitHub

Merge pull request #61 from bokobza/master

Get general info from the wallet and the node
parents c0070c3b 95494856
......@@ -89,16 +89,20 @@ POST /wallet/send-transaction - Attempts to send a transaction
# Details
## GET /wallet/general-info - Displays general information on the wallet
### Query parameters
`walletName` (required) - the name of the wallet.
### Responses
```
{
"walletFilePath": "path to the wallet file",
"encryptedSeed": "6PYKWP34en1wELfcJDgXaFRPugjgkDdEk2p2Pzytm1158dxgNyLAUXwpKL",
"chainCode": "q/Fn7+RSIVM0p0Nj6rIuNkybF+0WKeSZPMQS2QCbDzY=",
"network": "main", // main/testnet
"creationTime": "2017-03-21",
"isDecrypted": true,
"uniqueId": "sadwpiqjdpijwqdpijwqidjoi" // can only get if decrypted, if not it's empty string
"walletFilePath":"path to the wallet file",
"network":"main", //"testnet", "stratismain", "stratistest"
"creationTime":"2017-03-21",
"isDecrypted":true,
"lastBlockHeight":123234,
"chainTip": 173721,
"connectedNodes": 5
}
```
## GET /wallet/sensitive - Displays sensitive information on the wallet
......
......@@ -10,6 +10,7 @@ using Breeze.Wallet.Errors;
using Breeze.Wallet.Helpers;
using Breeze.Wallet.Models;
using NBitcoin;
using Stratis.Bitcoin.Connection;
namespace Breeze.Api.Tests
{
......@@ -22,7 +23,7 @@ namespace Breeze.Api.Tests
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);
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
var result = controller.Create(new WalletCreationRequest
......@@ -52,7 +53,7 @@ namespace Breeze.Api.Tests
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);
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
var result = controller.Recover(new WalletRecoveryRequest
......@@ -81,7 +82,7 @@ namespace Breeze.Api.Tests
var mockWalletWrapper = new Mock<IWalletManager>();
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
var result = controller.Load(new WalletLoadRequest
......@@ -102,8 +103,8 @@ namespace Breeze.Api.Tests
{
var mockWalletWrapper = new Mock<IWalletManager>();
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
var result = controller.Load(new WalletLoadRequest
......@@ -118,7 +119,6 @@ namespace Breeze.Api.Tests
var viewResult = Assert.IsType<ErrorResult>(result);
Assert.NotNull(viewResult);
Assert.Equal(404, viewResult.StatusCode);
}
}
}
}
......@@ -8,6 +8,7 @@ using Breeze.Wallet.Errors;
using Microsoft.AspNetCore.Mvc;
using Breeze.Wallet.Models;
using NBitcoin;
using Stratis.Bitcoin.Connection;
namespace Breeze.Wallet.Controllers
{
......@@ -21,10 +22,22 @@ namespace Breeze.Wallet.Controllers
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.tracker = tracker;
this.connectionManager = connectionManager;
this.network = network;
this.coinType = (CoinType)network.Consensus.CoinType;
this.chain = chain;
}
/// <summary>
......@@ -143,11 +156,11 @@ namespace Breeze.Wallet.Controllers
/// <summary>
/// Get some general info about a wallet.
/// </summary>
/// <param name="model">The name of the wallet.</param>
/// <param name="request">The name of the wallet.</param>
/// <returns></returns>
[Route("general-info")]
[HttpGet]
public IActionResult GetGeneralInfo([FromQuery] WalletName model)
public IActionResult GetGeneralInfo([FromQuery] WalletName request)
{
// checks the request is valid
if (!this.ModelState.IsValid)
......@@ -158,7 +171,19 @@ namespace Breeze.Wallet.Controllers
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)
......
......@@ -113,7 +113,12 @@ namespace Breeze.Wallet
/// <returns></returns>
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>
/// Gets a list of accounts filtered by coin type.
......
......@@ -18,13 +18,13 @@ namespace Breeze.Wallet.JsonConverters
/// <inheritdoc />
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return DateTimeOffset.FromUnixTimeMilliseconds(long.Parse((string)reader.Value));
return DateTimeOffset.FromUnixTimeSeconds(long.Parse((string)reader.Value));
}
/// <inheritdoc />
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((DateTimeOffset)value).ToUnixTimeMilliseconds().ToString());
writer.WriteValue(((DateTimeOffset)value).ToUnixTimeSeconds().ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Breeze.Wallet.JsonConverters;
using NBitcoin;
using Newtonsoft.Json;
namespace Breeze.Wallet.Models
{
public class WalletGeneralInfoModel
{
[JsonProperty(PropertyName = "walletFilePath")]
public string WalletFilePath { get; set; }
public class WalletGeneralInfoModel
{
[JsonProperty(PropertyName = "walletFilePath")]
public string WalletFilePath { get; set; }
[JsonProperty(PropertyName = "network")]
[JsonConverter(typeof(NetworkConverter))]
public Network Network { get; set; }
[JsonProperty(PropertyName = "encryptedSeed")]
public string EncryptedSeed { get; set; }
/// <summary>
/// The time this wallet was created.
/// </summary>
[JsonProperty(PropertyName = "creationTime")]
[JsonConverter(typeof(DateTimeOffsetConverter))]
public DateTimeOffset CreationTime { get; set; }
[JsonProperty(PropertyName = "chainCode")]
public string ChainCode { get; set; }
[JsonProperty(PropertyName = "isDecrypted")]
public bool IsDecrypted { get; set; }
[JsonProperty(PropertyName = "network")]
public string Network { get; set; }
/// <summary>
/// The height of the last block that was synced.
/// </summary>
[JsonProperty(PropertyName = "lastBlockSyncedHeight")]
public int? LastBlockSyncedHeight { get; set; }
[JsonProperty(PropertyName = "creationTime")]
public string CreationTime { get; set; }
/// <summary>
/// The total number of blocks.
/// </summary>
[JsonProperty(PropertyName = "chainTip")]
public int? ChainTip { get; set; }
[JsonProperty(PropertyName = "isDecrypted")]
public bool IsDecrypted { get; set; }
[JsonProperty(PropertyName = "uniqueId")]
public string UniqueId { get; set; }
}
/// <summary>
/// The total number of nodes that we're connected to.
/// </summary>
[JsonProperty(PropertyName = "connectedNodes")]
public int ConnectedNodes { get; set; }
}
}
......@@ -314,11 +314,13 @@ namespace Breeze.Wallet
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 />
public IEnumerable<HdAccount> GetAccountsByCoinType(string walletName, CoinType coinType)
{
......@@ -511,7 +513,7 @@ namespace Breeze.Wallet
Amount = amount,
BlockHeight = blockHeight,
Id = transactionHash,
CreationTime = DateTimeOffset.FromUnixTimeMilliseconds(blockTime ?? time),
CreationTime = DateTimeOffset.FromUnixTimeSeconds(blockTime ?? time),
Index = index
};
trans.Add(newTransaction);
......@@ -556,7 +558,7 @@ namespace Breeze.Wallet
// update the block time
if (blockTime != null)
{
foundTransaction.CreationTime = DateTimeOffset.FromUnixTimeMilliseconds(blockTime.Value);
foundTransaction.CreationTime = DateTimeOffset.FromUnixTimeSeconds(blockTime.Value);
}
}
......
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