Commit 5ae9c823 authored by Pieterjan Vanhoof's avatar Pieterjan Vanhoof Committed by GitHub

Added utility endpoint for UI to list wallets files (#30)

Added utility endpoint for UI to list wallets files
parents 497b0dca 4be0a960
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"variables": [], "variables": [],
"info": { "info": {
"name": "Wallet", "name": "Wallet",
"_postman_id": "c9001498-0a8d-e095-7b3e-ba9f2b0df7e7", "_postman_id": "88e42ac6-4e01-8ec4-c109-12a07b8722de",
"description": "Requests relating to operations on the wallet", "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"
}, },
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
{ {
"name": "Create wallet - success", "name": "Create wallet - success",
"request": { "request": {
"url": "http://localhost:5000/api/v1/wallet/", "url": "http://localhost:5000/api/v1/wallet/create",
"method": "POST", "method": "POST",
"header": [ "header": [
{ {
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
{ {
"name": "Create wallet - validation errors", "name": "Create wallet - validation errors",
"request": { "request": {
"url": "http://localhost:5000/api/v1/wallet/", "url": "http://localhost:5000/api/v1/wallet/create",
"method": "POST", "method": "POST",
"header": [ "header": [
{ {
...@@ -99,7 +99,10 @@ ...@@ -99,7 +99,10 @@
"description": "" "description": ""
} }
], ],
"body": {}, "body": {
"mode": "raw",
"raw": ""
},
"description": "" "description": ""
}, },
"response": [] "response": []
...@@ -116,7 +119,10 @@ ...@@ -116,7 +119,10 @@
"description": "" "description": ""
} }
], ],
"body": {}, "body": {
"mode": "raw",
"raw": ""
},
"description": "" "description": ""
}, },
"response": [] "response": []
...@@ -133,7 +139,10 @@ ...@@ -133,7 +139,10 @@
"description": "" "description": ""
} }
], ],
"body": {}, "body": {
"mode": "raw",
"raw": ""
},
"description": "" "description": ""
}, },
"response": [] "response": []
...@@ -177,6 +186,23 @@ ...@@ -177,6 +186,23 @@
"description": "" "description": ""
}, },
"response": [] "response": []
},
{
"name": "Get wallets files",
"request": {
"url": "http://localhost:5000/api/v1/wallet/files",
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
}
],
"body": {},
"description": "Gets all the wallets files stored in the default folder"
},
"response": []
} }
] ]
} }
\ No newline at end of file
using System; using System;
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 +27,11 @@ namespace Breeze.Wallet.Controllers ...@@ -27,11 +27,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 +42,10 @@ namespace Breeze.Wallet.Controllers ...@@ -42,7 +42,10 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var mnemonic = this.walletWrapper.Create(walletCreation.Password, walletCreation.FolderPath, walletCreation.Name, walletCreation.Network); // get the wallet folder
DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath);
var mnemonic = this.walletWrapper.Create(request.Password, walletFolder.FullName, request.Name, request.Network);
return this.Json(mnemonic); return this.Json(mnemonic);
} }
catch (NotSupportedException e) catch (NotSupportedException e)
...@@ -55,11 +58,11 @@ namespace Breeze.Wallet.Controllers ...@@ -55,11 +58,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 +73,10 @@ namespace Breeze.Wallet.Controllers ...@@ -70,7 +73,10 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var wallet = this.walletWrapper.Load(walletLoad.Password, walletLoad.FolderPath, walletLoad.Name); // get the wallet folder
DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath);
var wallet = this.walletWrapper.Load(request.Password, walletFolder.FullName, request.Name);
return this.Json(wallet); return this.Json(wallet);
} }
...@@ -92,11 +98,11 @@ namespace Breeze.Wallet.Controllers ...@@ -92,11 +98,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,9 +113,11 @@ namespace Breeze.Wallet.Controllers ...@@ -107,9 +113,11 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var wallet = this.walletWrapper.Recover(walletRecovery.Password, walletRecovery.FolderPath, walletRecovery.Name, walletRecovery.Network, walletRecovery.Mnemonic); // get the wallet folder
return this.Json(wallet); DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath);
var wallet = this.walletWrapper.Recover(request.Password, walletFolder.FullName, request.Name, request.Network, request.Mnemonic);
return this.Json(wallet);
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
{ {
...@@ -266,5 +274,59 @@ namespace Breeze.Wallet.Controllers ...@@ -266,5 +274,59 @@ 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
{
DirectoryInfo walletsFolder = GetWalletFolder();
WalletFileModel model = new WalletFileModel
{
WalletsPath = walletsFolder.FullName,
WalletsFiles = Directory.EnumerateFiles(walletsFolder.FullName, "*.json", SearchOption.TopDirectoryOnly).Select(p => Path.GetFileName(p))
};
return this.Json(model);
}
catch (Exception e)
{
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
}
}
/// <summary>
/// Gets a folder.
/// </summary>
/// <returns>The path folder of the folder.</returns>
/// <remarks>The folder is created if it doesn't exist.</remarks>
private static DirectoryInfo GetWalletFolder(string folderPath = null)
{
if (string.IsNullOrEmpty(folderPath))
{
folderPath = GetDefaultWalletFolderPath();
}
return Directory.CreateDirectory(folderPath);
}
/// <summary>
/// Gets the path of the default folder in which the wallets will be stored.
/// </summary>
/// <returns>The folder path for Windows, Linux or OSX systems.</returns>
private static string GetDefaultWalletFolderPath()
{
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