Commit 94932a80 authored by Jeremy Bokobza's avatar Jeremy Bokobza

Added endpoint to list the wallets saved under the default folder /Breeze.

Removed requirement for the client to provide a wallet folder path, as the default one will be used.
parent d727e6f6
...@@ -3,6 +3,7 @@ using System.Collections.Generic; ...@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Runtime.InteropServices;
using System.Security; using System.Security;
using Breeze.Wallet.Errors; using Breeze.Wallet.Errors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
...@@ -27,11 +28,11 @@ namespace Breeze.Wallet.Controllers ...@@ -27,11 +28,11 @@ namespace Breeze.Wallet.Controllers
/// <summary> /// <summary>
/// Creates a new wallet on the local machine. /// Creates a new wallet on the local machine.
/// </summary> /// </summary>
/// <param name="walletCreation">The object containing the parameters used to create the wallet.</param> /// <param name="request">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>
[Route("create")] [Route("create")]
[HttpPost] [HttpPost]
public IActionResult Create([FromBody]WalletCreationRequest walletCreation) public IActionResult Create([FromBody]WalletCreationRequest request)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -42,7 +43,10 @@ namespace Breeze.Wallet.Controllers ...@@ -42,7 +43,10 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var mnemonic = this.walletWrapper.Create(walletCreation.Password, walletCreation.FolderPath, walletCreation.Name, walletCreation.Network); // get the folder path
string folderPath = string.IsNullOrEmpty(request.FolderPath) ? this.GetDefaultFolderPath() : request.FolderPath;
var mnemonic = this.walletWrapper.Create(request.Password, folderPath, request.Name, request.Network);
return this.Json(mnemonic); return this.Json(mnemonic);
} }
catch (NotSupportedException e) catch (NotSupportedException e)
...@@ -55,11 +59,11 @@ namespace Breeze.Wallet.Controllers ...@@ -55,11 +59,11 @@ namespace Breeze.Wallet.Controllers
/// <summary> /// <summary>
/// Loads a wallet previously created by the user. /// Loads a wallet previously created by the user.
/// </summary> /// </summary>
/// <param name="walletLoad">The name of the wallet to load.</param> /// <param name="request">The name of the wallet to load.</param>
/// <returns></returns> /// <returns></returns>
[Route("load")] [Route("load")]
[HttpPost] [HttpPost]
public IActionResult Load([FromBody]WalletLoadRequest walletLoad) public IActionResult Load([FromBody]WalletLoadRequest request)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -70,7 +74,10 @@ namespace Breeze.Wallet.Controllers ...@@ -70,7 +74,10 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var wallet = this.walletWrapper.Load(walletLoad.Password, walletLoad.FolderPath, walletLoad.Name); // get the folder path
string folderPath = string.IsNullOrEmpty(request.FolderPath) ? this.GetDefaultFolderPath() : request.FolderPath;
var wallet = this.walletWrapper.Load(request.Password, folderPath, request.Name);
return this.Json(wallet); return this.Json(wallet);
} }
...@@ -92,11 +99,11 @@ namespace Breeze.Wallet.Controllers ...@@ -92,11 +99,11 @@ namespace Breeze.Wallet.Controllers
/// <summary> /// <summary>
/// Recovers a wallet. /// Recovers a wallet.
/// </summary> /// </summary>
/// <param name="walletRecovery">The object containing the parameters used to recover a wallet.</param> /// <param name="request">The object containing the parameters used to recover a wallet.</param>
/// <returns></returns> /// <returns></returns>
[Route("recover")] [Route("recover")]
[HttpPost] [HttpPost]
public IActionResult Recover([FromBody]WalletRecoveryRequest walletRecovery) public IActionResult Recover([FromBody]WalletRecoveryRequest request)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -107,7 +114,10 @@ namespace Breeze.Wallet.Controllers ...@@ -107,7 +114,10 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var wallet = this.walletWrapper.Recover(walletRecovery.Password, walletRecovery.FolderPath, walletRecovery.Name, walletRecovery.Network, walletRecovery.Mnemonic); // get the folder path
string folderPath = string.IsNullOrEmpty(request.FolderPath) ? this.GetDefaultFolderPath() : request.FolderPath;
var wallet = this.walletWrapper.Recover(request.Password, folderPath, request.Name, request.Network, request.Mnemonic);
return this.Json(wallet); return this.Json(wallet);
} }
...@@ -266,5 +276,41 @@ namespace Breeze.Wallet.Controllers ...@@ -266,5 +276,41 @@ namespace Breeze.Wallet.Controllers
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()); return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
} }
} }
/// <summary>
/// Lists all the wallet files found under the default folder.
/// </summary>
/// <returns>A list of the wallets files found.</returns>
[Route("files")]
[HttpGet]
public IActionResult ListWalletsFiles()
{
try
{
var defaultWalletsPath = this.GetDefaultFolderPath();
WalletFileModel model = new WalletFileModel
{
WalletsPath = defaultWalletsPath,
WalletsFiles = Directory.EnumerateFiles(defaultWalletsPath, "*.json", SearchOption.TopDirectoryOnly).Select(p => Path.GetFileName(p))
};
return this.Json(model);
}
catch (Exception e)
{
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
}
}
private string GetDefaultFolderPath()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return $@"{Environment.GetEnvironmentVariable("AppData")}\Breeze";
}
return $"{Environment.GetEnvironmentVariable("HOME")}/.breeze";
}
} }
} }
...@@ -14,7 +14,6 @@ namespace Breeze.Wallet.Models ...@@ -14,7 +14,6 @@ namespace Breeze.Wallet.Models
public string Network { get; set; } public string Network { get; set; }
[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 wallet to create is missing.")] [Required(ErrorMessage = "The name of the wallet to create is missing.")]
...@@ -26,7 +25,6 @@ namespace Breeze.Wallet.Models ...@@ -26,7 +25,6 @@ namespace Breeze.Wallet.Models
[Required(ErrorMessage = "A password is required.")] [Required(ErrorMessage = "A password is required.")]
public string Password { get; set; } public string Password { get; set; }
[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 wallet is missing.")]
...@@ -41,7 +39,6 @@ namespace Breeze.Wallet.Models ...@@ -41,7 +39,6 @@ namespace Breeze.Wallet.Models
[Required(ErrorMessage = "A password is required.")] [Required(ErrorMessage = "A password is required.")]
public string Password { get; set; } public string Password { get; set; }
[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 wallet is missing.")]
......
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace Breeze.Wallet.Models
{
public class WalletFileModel
{
[JsonProperty(PropertyName = "walletsPath")]
public string WalletsPath { get; set; }
[JsonProperty(PropertyName = "walletsFiles")]
public IEnumerable<string> WalletsFiles { get; set; }
}
}
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