Commit faaec180 authored by Pieterjan Vanhoof's avatar Pieterjan Vanhoof Committed by GitHub

Get address endpoint (#39)

parents 1dc3c9a7 8e626ee6
...@@ -182,21 +182,20 @@ Works as expected. ...@@ -182,21 +182,20 @@ Works as expected.
"account one" "account one"
``` ```
## POST /wallet/address - Adds an address to an account ## GET /wallet/address - Gets an unused address
### Parameters
``` This endpoint will get the last address containing no transaction or will create a new address.
{ ### Query parameters
"walletName": "myFirstWallet", `walletName` (required) - the name of the wallet in which this address is contained.
"accountName": "account one",
"coinType": 0 `coinType` (required) - the type of coin for which to get the address, e.g 0 for bitcoin, 105 for stratis.
}
``` `accountName` (required) - the name of the account in which this address is contained.
### Responses ### Responses
``` ```
"1HDypWxXWZC5KXK259EHMnrWaa2youy7Mj" "1HDypWxXWZC5KXK259EHMnrWaa2youy7Mj"
``` ```
## GET /wallet/receive/[account1/account2] - Displays unused receive addresses of the specified wallet account ## GET /wallet/receive/[account1/account2] - Displays unused receive addresses of the specified wallet account
### Responses ### Responses
``` ```
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"variables": [], "variables": [],
"info": { "info": {
"name": "Wallet", "name": "Wallet",
"_postman_id": "5eec0912-fcf0-50f5-05a2-0835fa13c670", "_postman_id": "57013f2c-02dc-df32-41e9-6e4aaa14ad5e",
"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"
}, },
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
], ],
"body": { "body": {
"mode": "raw", "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}" "raw": "{ \n\t\"password\": \"123456\",\n\t\"network\": \"Main\",\n\t\"folderPath\": \"Wallets\",\n\t\"name\": \"myRecoveredWallet\",\n\t\"mnemonic\": \"elbow scale error joke labor page beyond curve indicate exit brass laundry\",\n\t\"creationDate\": \"2016-02-25 16:20:33\"\n}"
}, },
"description": "" "description": ""
}, },
...@@ -248,10 +248,10 @@ ...@@ -248,10 +248,10 @@
"response": [] "response": []
}, },
{ {
"name": "Create new address for wallet", "name": "Get unused address in wallet",
"request": { "request": {
"url": "http://localhost:5000/api/v1/wallet/address", "url": "http://localhost:5000/api/v1/wallet/address?walletName=wallet1&accountName=account one&coinType=0",
"method": "POST", "method": "GET",
"header": [ "header": [
{ {
"key": "Content-Type", "key": "Content-Type",
...@@ -261,7 +261,7 @@ ...@@ -261,7 +261,7 @@
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\n\t\"walletName\": \"myFirstWallet\",\n\t\"accountName\": \"account one\",\n\t\"coinType\": 0\n}" "raw": ""
}, },
"description": "" "description": ""
}, },
......
...@@ -376,12 +376,12 @@ namespace Breeze.Wallet.Controllers ...@@ -376,12 +376,12 @@ namespace Breeze.Wallet.Controllers
} }
/// <summary> /// <summary>
/// Creates a new address for a wallet. /// Gets an unused address.
/// </summary> /// </summary>
/// <returns>An address in Base58 format.</returns> /// <returns>The last created and unused address or creates a new address (in Base58 format).</returns>
[Route("address")] [Route("address")]
[HttpPost] [HttpGet]
public IActionResult CreateNewAddress([FromBody]CreateAddressModel request) public IActionResult GetUnusedAddress([FromQuery]GetUnusedAddressModel request)
{ {
// checks the request is valid // checks the request is valid
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
...@@ -392,7 +392,7 @@ namespace Breeze.Wallet.Controllers ...@@ -392,7 +392,7 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var result = this.walletManager.CreateNewAddress(request.WalletName, request.CoinType, request.AccountName); var result = this.walletManager.GetUnusedAddress(request.WalletName, request.CoinType, request.AccountName);
return this.Json(result); return this.Json(result);
} }
catch (Exception e) catch (Exception e)
......
...@@ -64,13 +64,13 @@ namespace Breeze.Wallet ...@@ -64,13 +64,13 @@ namespace Breeze.Wallet
string CreateNewAccount(string walletName, CoinType coinType, string accountName, string password); string CreateNewAccount(string walletName, CoinType coinType, string accountName, string password);
/// <summary> /// <summary>
/// Creates the new address. /// Gets an address that contains no transaction.
/// </summary> /// </summary>
/// <param name="walletName">The name of the wallet in which this address will be created.</param> /// <param name="walletName">The name of the wallet in which this address is contained.</param>
/// <param name="coinType">The type of coin for which to create an account.</param> /// <param name="coinType">The type of coin for which to get the address.</param>
/// <param name="accountName">The name of the account in which this address will be created.</param> /// <param name="accountName">The name of the account in which this address is contained.</param>
/// <returns>The new address, in Base58 format.</returns> /// <returns>An unused address or a newly created address, in Base58 format.</returns>
string CreateNewAddress(string walletName, CoinType coinType, string accountName); string GetUnusedAddress(string walletName, CoinType coinType, string accountName);
WalletGeneralInfoModel GetGeneralInfo(string walletName); WalletGeneralInfoModel GetGeneralInfo(string walletName);
......
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Breeze.Wallet.Models
{
public class CreateAddressModel
{
/// <summary>
/// The name of the wallet in which to create the address.
/// </summary>
[Required]
public string WalletName { get; set; }
/// <summary>
/// The type of coin this account contains.
/// </summary>
[Required]
public CoinType CoinType { get; set; }
/// <summary>
/// The name of the account in which to create the address.
/// </summary>
[Required]
public string AccountName { get; set; }
}
}
...@@ -94,4 +94,24 @@ namespace Breeze.Wallet.Models ...@@ -94,4 +94,24 @@ namespace Breeze.Wallet.Models
public string Hex { get; set; } public string Hex { get; set; }
} }
public class GetUnusedAddressModel
{
/// <summary>
/// The name of the wallet from which to get the address.
/// </summary>
[Required]
public string WalletName { get; set; }
/// <summary>
/// The type of coin this address is for.
/// </summary>
[Required]
public CoinType CoinType { get; set; }
/// <summary>
/// The name of the account for which to get the address.
/// </summary>
[Required]
public string AccountName { get; set; }
}
} }
...@@ -150,7 +150,7 @@ namespace Breeze.Wallet ...@@ -150,7 +150,7 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public string CreateNewAddress(string walletName, CoinType coinType, string accountName) public string GetUnusedAddress(string walletName, CoinType coinType, string accountName)
{ {
Wallet wallet = this.Wallets.SingleOrDefault(w => w.Name == walletName); Wallet wallet = this.Wallets.SingleOrDefault(w => w.Name == walletName);
if (wallet == null) if (wallet == null)
...@@ -175,7 +175,7 @@ namespace Breeze.Wallet ...@@ -175,7 +175,7 @@ namespace Breeze.Wallet
var lastAddress = account.ExternalAddresses.SingleOrDefault(a => a.Index == lastAddressIndex); var lastAddress = account.ExternalAddresses.SingleOrDefault(a => a.Index == lastAddressIndex);
if (lastAddress != null && !lastAddress.Transactions.Any()) if (lastAddress != null && !lastAddress.Transactions.Any())
{ {
throw new Exception($"Cannot create new address in account '{accountName}' if the previous address '{lastAddress.Address}' has not been used."); return lastAddress.Address;
} }
newAddressIndex = lastAddressIndex + 1; newAddressIndex = lastAddressIndex + 1;
......
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