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

Rename Safe to Wallet (#4)

parent 5c919e29
...@@ -13,15 +13,15 @@ namespace Breeze.Api.Tests ...@@ -13,15 +13,15 @@ namespace Breeze.Api.Tests
public class ControllersTests public class ControllersTests
{ {
[Fact] [Fact]
public void CreateSafeSuccessfullyReturnsMnemonic() public void CreateWalletSuccessfullyReturnsMnemonic()
{ {
var mockSafeCreate = new Mock<ISafeWrapper>(); var mockWalletCreate = new Mock<IWalletWrapper>();
mockSafeCreate.Setup(safe => safe.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns("mnemonic"); mockWalletCreate.Setup(wallet => wallet.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns("mnemonic");
var controller = new SafeController(mockSafeCreate.Object); var controller = new WalletController(mockWalletCreate.Object);
// Act // Act
var result = controller.Create(new SafeCreationModel var result = controller.Create(new WalletCreationModel
{ {
Name = "myName", Name = "myName",
FolderPath = "", FolderPath = "",
...@@ -30,29 +30,29 @@ namespace Breeze.Api.Tests ...@@ -30,29 +30,29 @@ namespace Breeze.Api.Tests
}); });
// Assert // Assert
mockSafeCreate.VerifyAll(); mockWalletCreate.VerifyAll();
var viewResult = Assert.IsType<JsonResult>(result); var viewResult = Assert.IsType<JsonResult>(result);
Assert.Equal("mnemonic", viewResult.Value); Assert.Equal("mnemonic", viewResult.Value);
Assert.NotNull(result); Assert.NotNull(result);
} }
[Fact] [Fact]
public void LoadSafeSuccessfullyReturnsSafeModel() public void LoadWalletSuccessfullyReturnsWalletModel()
{ {
SafeModel safeModel = new SafeModel WalletModel walletModel = new WalletModel
{ {
FileName = "myWallet", FileName = "myWallet",
Network = "MainNet", Network = "MainNet",
Addresses = new List<string> { "address1", "address2", "address3", "address4", "address5" } Addresses = new List<string> { "address1", "address2", "address3", "address4", "address5" }
}; };
var mockSafeWrapper = new Mock<ISafeWrapper>(); var mockWalletWrapper = new Mock<IWalletWrapper>();
mockSafeWrapper.Setup(safe => safe.Recover(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(safeModel); mockWalletWrapper.Setup(wallet => wallet.Recover(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(walletModel);
var controller = new SafeController(mockSafeWrapper.Object); var controller = new WalletController(mockWalletWrapper.Object);
// Act // Act
var result = controller.Recover(new SafeRecoveryModel var result = controller.Recover(new WalletRecoveryModel
{ {
Name = "myName", Name = "myName",
FolderPath = "", FolderPath = "",
...@@ -62,32 +62,32 @@ namespace Breeze.Api.Tests ...@@ -62,32 +62,32 @@ namespace Breeze.Api.Tests
}); });
// Assert // Assert
mockSafeWrapper.VerifyAll(); mockWalletWrapper.VerifyAll();
var viewResult = Assert.IsType<JsonResult>(result); var viewResult = Assert.IsType<JsonResult>(result);
Assert.NotNull(viewResult.Value); Assert.NotNull(viewResult.Value);
Assert.IsType<SafeModel>(viewResult.Value); Assert.IsType<WalletModel>(viewResult.Value);
var model = viewResult.Value as SafeModel; var model = viewResult.Value as WalletModel;
Assert.Equal("myWallet", model.FileName); Assert.Equal("myWallet", model.FileName);
} }
[Fact] [Fact]
public void RecoverSafeSuccessfullyReturnsSafeModel() public void RecoverWalletSuccessfullyReturnsWalletModel()
{ {
SafeModel safeModel = new SafeModel WalletModel walletModel = new WalletModel
{ {
FileName = "myWallet", FileName = "myWallet",
Network = "MainNet", Network = "MainNet",
Addresses = new List<string> { "address1", "address2", "address3", "address4", "address5" } Addresses = new List<string> { "address1", "address2", "address3", "address4", "address5" }
}; };
var mockSafeWrapper = new Mock<ISafeWrapper>(); var mockWalletWrapper = new Mock<IWalletWrapper>();
mockSafeWrapper.Setup(safe => safe.Load(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(safeModel); mockWalletWrapper.Setup(wallet => wallet.Load(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(walletModel);
var controller = new SafeController(mockSafeWrapper.Object); var controller = new WalletController(mockWalletWrapper.Object);
// Act // Act
var result = controller.Load(new SafeLoadModel var result = controller.Load(new WalletLoadModel
{ {
Name = "myName", Name = "myName",
FolderPath = "", FolderPath = "",
...@@ -95,25 +95,25 @@ namespace Breeze.Api.Tests ...@@ -95,25 +95,25 @@ namespace Breeze.Api.Tests
}); });
// Assert // Assert
mockSafeWrapper.VerifyAll(); mockWalletWrapper.VerifyAll();
var viewResult = Assert.IsType<JsonResult>(result); var viewResult = Assert.IsType<JsonResult>(result);
Assert.NotNull(viewResult.Value); Assert.NotNull(viewResult.Value);
Assert.IsType<SafeModel>(viewResult.Value); Assert.IsType<WalletModel>(viewResult.Value);
var model = viewResult.Value as SafeModel; var model = viewResult.Value as WalletModel;
Assert.Equal("myWallet", model.FileName); Assert.Equal("myWallet", model.FileName);
} }
[Fact] [Fact]
public void FileNotFoundExceptionandReturns404() public void FileNotFoundExceptionandReturns404()
{ {
var mockSafeWrapper = new Mock<ISafeWrapper>(); var mockWalletWrapper = new Mock<IWalletWrapper>();
mockSafeWrapper.Setup(safe => safe.Load(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws<FileNotFoundException>(); mockWalletWrapper.Setup(wallet => wallet.Load(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws<FileNotFoundException>();
var controller = new SafeController(mockSafeWrapper.Object); var controller = new WalletController(mockWalletWrapper.Object);
// Act // Act
var result = controller.Load(new SafeLoadModel var result = controller.Load(new WalletLoadModel
{ {
Name = "myName", Name = "myName",
FolderPath = "", FolderPath = "",
...@@ -121,7 +121,7 @@ namespace Breeze.Api.Tests ...@@ -121,7 +121,7 @@ namespace Breeze.Api.Tests
}); });
// Assert // Assert
mockSafeWrapper.VerifyAll(); mockWalletWrapper.VerifyAll();
var viewResult = Assert.IsType<ObjectResult>(result); var viewResult = Assert.IsType<ObjectResult>(result);
Assert.NotNull(viewResult); Assert.NotNull(viewResult);
Assert.Equal(404, viewResult.StatusCode); Assert.Equal(404, viewResult.StatusCode);
......
{ {
"variables": [], "variables": [],
"info": { "info": {
"name": "Safe", "name": "Wallet",
"_postman_id": "11f915f1-7aac-bfeb-000c-b66348f4636f", "_postman_id": "11f915f1-7aac-bfeb-000c-b66348f4636f",
"description": "Requests relating to operations on the safe", "description": "Requests relating to operations on the wallet",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
}, },
"item": [ "item": [
{ {
"name": "Create safe - success", "name": "Create wallet - success",
"request": { "request": {
"url": "http://localhost:5000/api/safe/", "url": "http://localhost:5000/api/wallet/",
"method": "POST", "method": "POST",
"header": [ "header": [
{ {
...@@ -28,9 +28,9 @@ ...@@ -28,9 +28,9 @@
"response": [] "response": []
}, },
{ {
"name": "Create safe - validation errors", "name": "Create wallet - validation errors",
"request": { "request": {
"url": "http://localhost:5000/api/safe/", "url": "http://localhost:5000/api/wallet/",
"method": "POST", "method": "POST",
"header": [ "header": [
{ {
...@@ -48,9 +48,9 @@ ...@@ -48,9 +48,9 @@
"response": [] "response": []
}, },
{ {
"name": "Load safe", "name": "Load wallet",
"request": { "request": {
"url": "http://localhost:5000/api/safe/?password=123456&folderPath=MyWallets&name=myFirstWallet", "url": "http://localhost:5000/api/wallet/?password=123456&folderPath=MyWallets&name=myFirstWallet",
"method": "GET", "method": "GET",
"header": [ "header": [
{ {
...@@ -68,9 +68,9 @@ ...@@ -68,9 +68,9 @@
"response": [] "response": []
}, },
{ {
"name": "Recover safe", "name": "Recover wallet",
"request": { "request": {
"url": "http://localhost:5000/api/safe/recover", "url": "http://localhost:5000/api/wallet/recover",
"method": "POST", "method": "POST",
"header": [ "header": [
{ {
......
...@@ -12,22 +12,22 @@ using Stratis.Bitcoin; ...@@ -12,22 +12,22 @@ using Stratis.Bitcoin;
namespace Breeze.Wallet.Controllers namespace Breeze.Wallet.Controllers
{ {
[Route("api/[controller]")] [Route("api/[controller]")]
public class SafeController : Controller public class WalletController : Controller
{ {
private readonly ISafeWrapper safeWrapper; private readonly IWalletWrapper walletWrapper;
public SafeController(ISafeWrapper safeWrapper) public WalletController(IWalletWrapper walletWrapper)
{ {
this.safeWrapper = safeWrapper; this.walletWrapper = walletWrapper;
} }
/// <summary> /// <summary>
/// Creates a new safe on the local machine. /// Creates a new wallet on the local machine.
/// </summary> /// </summary>
/// <param name="safeCreation">The object containing the parameters used to create the wallet.</param> /// <param name="walletCreation">The object containing the parameters used to create the wallet.</param>
/// <returns>A JSON object containing the mnemonic created for the new wallet.</returns> /// <returns>A JSON object containing the mnemonic created for the new wallet.</returns>
[HttpPost] [HttpPost]
public IActionResult Create([FromBody]SafeCreationModel safeCreation) public IActionResult Create([FromBody]WalletCreationModel walletCreation)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -38,7 +38,7 @@ namespace Breeze.Wallet.Controllers ...@@ -38,7 +38,7 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var mnemonic = this.safeWrapper.Create(safeCreation.Password, safeCreation.FolderPath, safeCreation.Name, safeCreation.Network); var mnemonic = this.walletWrapper.Create(walletCreation.Password, walletCreation.FolderPath, walletCreation.Name, walletCreation.Network);
return this.Json(mnemonic); return this.Json(mnemonic);
} }
catch (NotSupportedException e) catch (NotSupportedException e)
...@@ -50,7 +50,7 @@ namespace Breeze.Wallet.Controllers ...@@ -50,7 +50,7 @@ namespace Breeze.Wallet.Controllers
} }
} }
public IActionResult Load(SafeLoadModel safeLoad) public IActionResult Load(WalletLoadModel walletLoad)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -61,8 +61,8 @@ namespace Breeze.Wallet.Controllers ...@@ -61,8 +61,8 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var safe = this.safeWrapper.Load(safeLoad.Password, safeLoad.FolderPath, safeLoad.Name); var wallet = this.walletWrapper.Load(walletLoad.Password, walletLoad.FolderPath, walletLoad.Name);
return this.Json(safe); return this.Json(wallet);
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
...@@ -88,7 +88,7 @@ namespace Breeze.Wallet.Controllers ...@@ -88,7 +88,7 @@ namespace Breeze.Wallet.Controllers
[Route("recover")] [Route("recover")]
[HttpPost] [HttpPost]
public IActionResult Recover([FromBody]SafeRecoveryModel safeRecovery) public IActionResult Recover([FromBody]WalletRecoveryModel walletRecovery)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -99,8 +99,8 @@ namespace Breeze.Wallet.Controllers ...@@ -99,8 +99,8 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var safe = this.safeWrapper.Recover(safeRecovery.Password, safeRecovery.FolderPath, safeRecovery.Name, safeRecovery.Network, safeRecovery.Mnemonic); var wallet = this.walletWrapper.Recover(walletRecovery.Password, walletRecovery.FolderPath, walletRecovery.Name, walletRecovery.Network, walletRecovery.Mnemonic);
return this.Json(safe); return this.Json(wallet);
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
......
...@@ -7,21 +7,21 @@ namespace Breeze.Wallet.Models ...@@ -7,21 +7,21 @@ namespace Breeze.Wallet.Models
/// <summary> /// <summary>
/// Object used to create a new wallet /// Object used to create a new wallet
/// </summary> /// </summary>
public class SafeCreationModel public class WalletCreationModel
{ {
[Required(ErrorMessage = "A password is required.")] [Required(ErrorMessage = "A password is required.")]
public string Password { get; set; } public string Password { get; set; }
public string Network { get; set; } public string Network { get; set; }
[Required(ErrorMessage = "The folder path where the safe will be created is required.")] [Required(ErrorMessage = "The folder path where the wallet will be created is required.")]
public string FolderPath { get; set; } public string FolderPath { get; set; }
[Required(ErrorMessage = "The name of the safe to create is missing.")] [Required(ErrorMessage = "The name of the wallet to create is missing.")]
public string Name { get; set; } public string Name { get; set; }
} }
public class SafeLoadModel public class WalletLoadModel
{ {
[Required(ErrorMessage = "A password is required.")] [Required(ErrorMessage = "A password is required.")]
public string Password { get; set; } public string Password { get; set; }
...@@ -29,11 +29,11 @@ namespace Breeze.Wallet.Models ...@@ -29,11 +29,11 @@ namespace Breeze.Wallet.Models
[Required(ErrorMessage = "The folder path is required.")] [Required(ErrorMessage = "The folder path is required.")]
public string FolderPath { get; set; } public string FolderPath { get; set; }
[Required(ErrorMessage = "The name of the safe is missing.")] [Required(ErrorMessage = "The name of the wallet is missing.")]
public string Name { get; set; } public string Name { get; set; }
} }
public class SafeRecoveryModel public class WalletRecoveryModel
{ {
[Required(ErrorMessage = "A mnemonic is required.")] [Required(ErrorMessage = "A mnemonic is required.")]
public string Mnemonic { get; set; } public string Mnemonic { get; set; }
...@@ -44,7 +44,7 @@ namespace Breeze.Wallet.Models ...@@ -44,7 +44,7 @@ namespace Breeze.Wallet.Models
[Required(ErrorMessage = "The folder path is required.")] [Required(ErrorMessage = "The folder path is required.")]
public string FolderPath { get; set; } public string FolderPath { get; set; }
[Required(ErrorMessage = "The name of the safe is missing.")] [Required(ErrorMessage = "The name of the wallet is missing.")]
public string Name { get; set; } public string Name { get; set; }
public string Network { get; set; } public string Network { get; set; }
......
...@@ -23,9 +23,9 @@ namespace Breeze.Wallet ...@@ -23,9 +23,9 @@ namespace Breeze.Wallet
.AddFeature<WalletFeature>() .AddFeature<WalletFeature>()
.FeatureServices(services => .FeatureServices(services =>
{ {
services.AddTransient<ISafeWrapper, SafeWrapper>(); services.AddTransient<IWalletWrapper, WalletWrapper>();
services.AddTransient<ITrackerWrapper, TrackerWrapper>(); services.AddTransient<ITrackerWrapper, TrackerWrapper>();
services.AddSingleton<SafeController>(); services.AddSingleton<WalletController>();
}); });
}); });
......
...@@ -5,7 +5,7 @@ using System.Threading.Tasks; ...@@ -5,7 +5,7 @@ using System.Threading.Tasks;
namespace Breeze.Wallet namespace Breeze.Wallet
{ {
public class SafeModel public class WalletModel
{ {
public string Network { get; set; } public string Network { get; set; }
......
...@@ -4,12 +4,12 @@ namespace Breeze.Wallet.Wrappers ...@@ -4,12 +4,12 @@ namespace Breeze.Wallet.Wrappers
/// <summary> /// <summary>
/// An interface enabling wallet operations. /// An interface enabling wallet operations.
/// </summary> /// </summary>
public interface ISafeWrapper public interface IWalletWrapper
{ {
string Create(string password, string folderPath, string name, string network); string Create(string password, string folderPath, string name, string network);
SafeModel Load(string password, string folderPath, string name); WalletModel Load(string password, string folderPath, string name);
SafeModel Recover(string password, string folderPath, string name, string network, string mnemonic); WalletModel Recover(string password, string folderPath, string name, string network, string mnemonic);
} }
} }
...@@ -6,64 +6,64 @@ using NBitcoin; ...@@ -6,64 +6,64 @@ using NBitcoin;
namespace Breeze.Wallet.Wrappers namespace Breeze.Wallet.Wrappers
{ {
/// <summary> /// <summary>
/// An implementation of the <see cref="ISafeWrapper"/> interface. /// An implementation of the <see cref="IWalletWrapper"/> interface.
/// </summary> /// </summary>
public class SafeWrapper : ISafeWrapper public class WalletWrapper : IWalletWrapper
{ {
/// <summary> /// <summary>
/// Creates a safe on the local device. /// Creates a wallet on the local device.
/// </summary> /// </summary>
/// <param name="password">The user's password.</param> /// <param name="password">The user's password.</param>
/// <param name="folderPath">The folder where the safe will be saved.</param> /// <param name="folderPath">The folder where the wallet will be saved.</param>
/// <param name="name">The name of the safe.</param> /// <param name="name">The name of the wallet.</param>
/// <param name="network">The network for which to create a safe.</param> /// <param name="network">The network for which to create a wallet.</param>
/// <returns>A mnemonic allowing recovery of the safe.</returns> /// <returns>A mnemonic allowing recovery of the wallet.</returns>
public string Create(string password, string folderPath, string name, string network) public string Create(string password, string folderPath, string name, string network)
{ {
Mnemonic mnemonic; Mnemonic mnemonic;
Safe safe = Safe.Create(out mnemonic, password, Path.Combine(folderPath, $"{name}.json"), this.GetNetwork(network)); Safe wallet = Safe.Create(out mnemonic, password, Path.Combine(folderPath, $"{name}.json"), this.GetNetwork(network));
return mnemonic.ToString(); return mnemonic.ToString();
} }
/// <summary> /// <summary>
/// Loads a safe from the local device. /// Loads a wallet from the local device.
/// </summary> /// </summary>
/// <param name="password">The user's password.</param> /// <param name="password">The user's password.</param>
/// <param name="folderPath">The folder where the safe will be loaded.</param> /// <param name="folderPath">The folder where the wallet will be loaded.</param>
/// <param name="name">The name of the safe.</param> /// <param name="name">The name of the wallet.</param>
/// <returns>The safe loaded from the local device</returns> /// <returns>The wallet loaded from the local device</returns>
public SafeModel Load(string password, string folderPath, string name) public WalletModel Load(string password, string folderPath, string name)
{ {
Safe safe = Safe.Load(password, Path.Combine(folderPath, $"{name}.json")); Safe wallet = Safe.Load(password, Path.Combine(folderPath, $"{name}.json"));
//TODO review here which data should be returned //TODO review here which data should be returned
return new SafeModel return new WalletModel
{ {
Network = safe.Network.Name, Network = wallet.Network.Name,
Addresses = safe.GetFirstNAddresses(10).Select(a => a.ToWif()), Addresses = wallet.GetFirstNAddresses(10).Select(a => a.ToWif()),
FileName = safe.WalletFilePath FileName = wallet.WalletFilePath
}; };
} }
/// <summary> /// <summary>
/// Recovers a safe from the local device. /// Recovers a wallet from the local device.
/// </summary> /// </summary>
/// <param name="password">The user's password.</param> /// <param name="password">The user's password.</param>
/// <param name="folderPath">The folder where the safe will be loaded.</param> /// <param name="folderPath">The folder where the wallet will be loaded.</param>
/// <param name="name">The name of the safe.</param> /// <param name="name">The name of the wallet.</param>
/// <param name="network">The network in which to creae this wallet</param> /// <param name="network">The network in which to creae this wallet</param>
/// <param name="mnemonic">The user's mnemonic for the safe.</param> /// <param name="mnemonic">The user's mnemonic for the wallet.</param>
/// <returns></returns> /// <returns></returns>
public SafeModel Recover(string password, string folderPath, string name, string network, string mnemonic) public WalletModel Recover(string password, string folderPath, string name, string network, string mnemonic)
{ {
Safe safe = Safe.Recover(new Mnemonic(mnemonic), password, Path.Combine(folderPath, $"{name}.json"), this.GetNetwork(network)); Safe wallet = Safe.Recover(new Mnemonic(mnemonic), password, Path.Combine(folderPath, $"{name}.json"), this.GetNetwork(network));
//TODO review here which data should be returned //TODO review here which data should be returned
return new SafeModel return new WalletModel
{ {
Network = safe.Network.Name, Network = wallet.Network.Name,
Addresses = safe.GetFirstNAddresses(10).Select(a => a.ToWif()), Addresses = wallet.GetFirstNAddresses(10).Select(a => a.ToWif()),
FileName = safe.WalletFilePath FileName = wallet.WalletFilePath
}; };
} }
......
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