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