Commit 72c7a83c authored by Jeremy Bokobza's avatar Jeremy Bokobza

Added Safe recovery

parent cd4e3f8f
...@@ -8,3 +8,4 @@ ...@@ -8,3 +8,4 @@
/Breeze.Api/src/Breeze.Api/bin /Breeze.Api/src/Breeze.Api/bin
/Breeze.Api/src/Breeze.Api/obj /Breeze.Api/src/Breeze.Api/obj
*.user *.user
/Breeze.Api/src/Breeze.Api/Wallets
using Microsoft.AspNetCore.Mvc; using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Xunit; using Xunit;
using Moq; using Moq;
using Breeze.Api.Controllers; using Breeze.Api.Controllers;
...@@ -10,7 +12,7 @@ namespace Breeze.Api.Tests ...@@ -10,7 +12,7 @@ namespace Breeze.Api.Tests
public class ControllersTests public class ControllersTests
{ {
[Fact] [Fact]
public void CreateSafesuccessfullyReturnsMnemonic() public void CreateSafeSuccessfullyReturnsMnemonic()
{ {
var mockSafeCreate = new Mock<ISafeWrapper>(); var mockSafeCreate = new Mock<ISafeWrapper>();
mockSafeCreate.Setup(safe => safe.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns("mnemonic"); mockSafeCreate.Setup(safe => safe.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns("mnemonic");
...@@ -27,9 +29,102 @@ namespace Breeze.Api.Tests ...@@ -27,9 +29,102 @@ namespace Breeze.Api.Tests
}); });
// Assert // Assert
mockSafeCreate.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]
public void LoadSafeSuccessfullyReturnsSafeModel()
{
SafeModel safeModel = new SafeModel
{
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 controller = new SafeController(mockSafeWrapper.Object);
// Act
var result = controller.Recover(new SafeRecoveryModel
{
Name = "myName",
FolderPath = "",
Password = "",
Network = "",
Mnemonic = "mnemonic"
});
// Assert
mockSafeWrapper.VerifyAll();
var viewResult = Assert.IsType<JsonResult>(result);
Assert.NotNull(viewResult.Value);
Assert.IsType<SafeModel>(viewResult.Value);
var model = viewResult.Value as SafeModel;
Assert.Equal("myWallet", model.FileName);
}
[Fact]
public void RecoverSafeSuccessfullyReturnsSafeModel()
{
SafeModel safeModel = new SafeModel
{
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 controller = new SafeController(mockSafeWrapper.Object);
// Act
var result = controller.Load(new SafeLoadModel
{
Name = "myName",
FolderPath = "",
Password = ""
});
// Assert
mockSafeWrapper.VerifyAll();
var viewResult = Assert.IsType<JsonResult>(result);
Assert.NotNull(viewResult.Value);
Assert.IsType<SafeModel>(viewResult.Value);
var model = viewResult.Value as SafeModel;
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 controller = new SafeController(mockSafeWrapper.Object);
// Act
var result = controller.Load(new SafeLoadModel
{
Name = "myName",
FolderPath = "",
Password = ""
});
// Assert
mockSafeWrapper.VerifyAll();
var viewResult = Assert.IsType<ObjectResult>(result);
Assert.NotNull(viewResult);
Assert.Equal(404, viewResult.StatusCode);
}
} }
} }
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
}, },
"item": [ "item": [
{ {
"name": "Create wallet - success", "name": "Create safe - success",
"request": { "request": {
"url": "http://localhost:5000/api/safe/", "url": "http://localhost:5000/api/safe/",
"method": "POST", "method": "POST",
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
"response": [] "response": []
}, },
{ {
"name": "Create wallet - validation errors", "name": "Create safe - validation errors",
"request": { "request": {
"url": "http://localhost:5000/api/safe/", "url": "http://localhost:5000/api/safe/",
"method": "POST", "method": "POST",
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
"response": [] "response": []
}, },
{ {
"name": "Load wallet", "name": "Load safe",
"request": { "request": {
"url": "http://localhost:5000/api/safe/?password=123456&folderPath=MyWallets&name=myFirstWallet", "url": "http://localhost:5000/api/safe/?password=123456&folderPath=MyWallets&name=myFirstWallet",
"method": "GET", "method": "GET",
...@@ -66,6 +66,26 @@ ...@@ -66,6 +66,26 @@
"description": "" "description": ""
}, },
"response": [] "response": []
},
{
"name": "Recover safe",
"request": {
"url": "http://localhost:5000/api/safe/recover",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{ \n\t\"password\": \"123456\",\n\t\"network\": \"Main\",\n\t\"folderPath\": \"Wallets\",\n\t\"name\": \"myFirstWalletRecovered\",\n\t\"mnemonic\": \"elbow scale error joke labor page beyond curve indicate exit brass laundry\"\n\t\n}"
},
"description": ""
},
"response": []
} }
] ]
} }
\ No newline at end of file
...@@ -84,5 +84,43 @@ namespace Breeze.Api.Controllers ...@@ -84,5 +84,43 @@ namespace Breeze.Api.Controllers
return this.StatusCode((int)HttpStatusCode.BadRequest, e.Message); return this.StatusCode((int)HttpStatusCode.BadRequest, e.Message);
} }
} }
[Route("recover")]
[HttpPost]
public IActionResult Recover([FromBody]SafeRecoveryModel safeRecovery)
{
// checks the request is valid
if (!this.ModelState.IsValid)
{
var errors = this.ModelState.Values.SelectMany(e => e.Errors.Select(m => m.ErrorMessage));
return this.BadRequest(string.Join(Environment.NewLine, errors));
}
try
{
var safe = this.safeWrapper.Recover(safeRecovery.Password, safeRecovery.FolderPath, safeRecovery.Name, safeRecovery.Network, safeRecovery.Mnemonic);
return this.Json(safe);
}
catch (FileNotFoundException e)
{
Console.WriteLine(e);
// indicates that this wallet does not exist
return this.StatusCode((int)HttpStatusCode.NotFound, "Wallet not found.");
}
catch (SecurityException e)
{
Console.WriteLine(e);
// indicates that the password is wrong
return this.StatusCode((int)HttpStatusCode.Forbidden, "Wrong password, please try again.");
}
catch (Exception e)
{
Console.WriteLine(e);
return this.StatusCode((int)HttpStatusCode.BadRequest, e.Message);
}
}
} }
} }
...@@ -14,10 +14,10 @@ namespace Breeze.Api.Models ...@@ -14,10 +14,10 @@ namespace Breeze.Api.Models
public string Network { get; set; } public string Network { get; set; }
[Required(ErrorMessage = "The folder path where the wallet will be created is required.")] [Required(ErrorMessage = "The folder path where the safe will be created is required.")]
public string FolderPath { get; set; } public string FolderPath { get; set; }
[Required(ErrorMessage = "The name of the wallet to create is missing.")] [Required(ErrorMessage = "The name of the safe to create is missing.")]
public string Name { get; set; } public string Name { get; set; }
} }
...@@ -29,10 +29,27 @@ namespace Breeze.Api.Models ...@@ -29,10 +29,27 @@ namespace Breeze.Api.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 wallet is missing.")] [Required(ErrorMessage = "The name of the safe is missing.")]
public string Name { get; set; } public string Name { get; set; }
} }
public class SafeRecoveryModel
{
[Required(ErrorMessage = "A mnemonic is required.")]
public string Mnemonic { get; set; }
[Required(ErrorMessage = "A password is required.")]
public string Password { get; set; }
[Required(ErrorMessage = "The folder path is required.")]
public string FolderPath { get; set; }
[Required(ErrorMessage = "The name of the safe is missing.")]
public string Name { get; set; }
public string Network { get; set; }
}
public class SafeModel public class SafeModel
{ {
public string Network { get; set; } public string Network { get; set; }
......
...@@ -10,5 +10,7 @@ namespace Breeze.Api.Wrappers ...@@ -10,5 +10,7 @@ namespace Breeze.Api.Wrappers
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); SafeModel Load(string password, string folderPath, string name);
SafeModel Recover(string password, string folderPath, string name, string network, string mnemonic);
} }
} }
...@@ -21,21 +21,8 @@ namespace Breeze.Api.Wrappers ...@@ -21,21 +21,8 @@ namespace Breeze.Api.Wrappers
/// <returns>A mnemonic allowing recovery of the safe.</returns> /// <returns>A mnemonic allowing recovery of the safe.</returns>
public string Create(string password, string folderPath, string name, string network) public string Create(string password, string folderPath, string name, string network)
{ {
// any network different than MainNet will default to TestNet
Network net;
switch (network.ToLowerInvariant())
{
case "main":
case "mainnet":
net = Network.Main;
break;
default:
net = Network.TestNet;
break;
}
Mnemonic mnemonic; Mnemonic mnemonic;
Safe safe = Safe.Create(out mnemonic, password, Path.Combine(folderPath, $"{name}.json"), net); Safe safe = Safe.Create(out mnemonic, password, Path.Combine(folderPath, $"{name}.json"), this.GetNetwork(network));
return mnemonic.ToString(); return mnemonic.ToString();
} }
...@@ -58,5 +45,40 @@ namespace Breeze.Api.Wrappers ...@@ -58,5 +45,40 @@ namespace Breeze.Api.Wrappers
FileName = safe.WalletFilePath FileName = safe.WalletFilePath
}; };
} }
/// <summary>
/// Recovers a safe 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="network">The network in which to creae this wallet</param>
/// <param name="mnemonic">The user's mnemonic for the safe.</param>
/// <returns></returns>
public SafeModel 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));
//TODO review here which data should be returned
return new SafeModel
{
Network = safe.Network.Name,
Addresses = safe.GetFirstNAddresses(10).Select(a => a.ToWif()),
FileName = safe.WalletFilePath
};
}
private Network GetNetwork(string network)
{
// any network different than MainNet will default to TestNet
switch (network.ToLowerInvariant())
{
case "main":
case "mainnet":
return Network.Main;
default:
return Network.TestNet;
}
}
} }
} }
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