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;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using Breeze.Wallet.Errors;
using Microsoft.AspNetCore.Mvc;
......@@ -27,11 +28,11 @@ namespace Breeze.Wallet.Controllers
/// <summary>
/// Creates a new wallet on the local machine.
/// </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>
[Route("create")]
[HttpPost]
public IActionResult Create([FromBody]WalletCreationRequest walletCreation)
public IActionResult Create([FromBody]WalletCreationRequest request)
{
// checks the request is valid
if (!this.ModelState.IsValid)
......@@ -42,7 +43,10 @@ namespace Breeze.Wallet.Controllers
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);
}
catch (NotSupportedException e)
......@@ -55,11 +59,11 @@ namespace Breeze.Wallet.Controllers
/// <summary>
/// Loads a wallet previously created by the user.
/// </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>
[Route("load")]
[HttpPost]
public IActionResult Load([FromBody]WalletLoadRequest walletLoad)
public IActionResult Load([FromBody]WalletLoadRequest request)
{
// checks the request is valid
if (!this.ModelState.IsValid)
......@@ -70,7 +74,10 @@ namespace Breeze.Wallet.Controllers
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);
}
......@@ -92,11 +99,11 @@ namespace Breeze.Wallet.Controllers
/// <summary>
/// Recovers a wallet.
/// </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>
[Route("recover")]
[HttpPost]
public IActionResult Recover([FromBody]WalletRecoveryRequest walletRecovery)
public IActionResult Recover([FromBody]WalletRecoveryRequest request)
{
// checks the request is valid
if (!this.ModelState.IsValid)
......@@ -107,7 +114,10 @@ namespace Breeze.Wallet.Controllers
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);
}
......@@ -266,5 +276,41 @@ namespace Breeze.Wallet.Controllers
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
public string Network { get; set; }
[Required(ErrorMessage = "The folder path where the wallet will be created is required.")]
public string FolderPath { get; set; }
[Required(ErrorMessage = "The name of the wallet to create is missing.")]
......@@ -26,7 +25,6 @@ namespace Breeze.Wallet.Models
[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 wallet is missing.")]
......@@ -41,7 +39,6 @@ namespace Breeze.Wallet.Models
[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 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