View seed option if available (#1518)

This commit is contained in:
Andrew Camilleri
2020-04-28 17:55:53 +02:00
committed by GitHub
parent ff99ab1239
commit e75b4ec6bf
3 changed files with 141 additions and 76 deletions

View File

@@ -26,6 +26,7 @@ using Newtonsoft.Json.Linq;
using BTCPayServer.Logging;
using Microsoft.Extensions.Logging;
using BTCPayServer.Client;
using NBXplorer;
namespace BTCPayServer.Controllers
{
@@ -53,6 +54,9 @@ namespace BTCPayServer.Controllers
{
vm.DerivationScheme = derivation.AccountDerivation.ToString();
vm.Config = derivation.ToJson();
vm.NBXSeedAvailable = (await CanUseHotWallet() ).HotWallet && !string.IsNullOrEmpty(await _ExplorerProvider.GetExplorerClient(network)
.GetMetadataAsync<string>(derivation.AccountDerivation,
WellknownMetadataKeys.Mnemonic));
}
vm.Enabled = !store.GetStoreBlob().IsExcluded(new PaymentMethodId(vm.CryptoCode, PaymentTypes.BTCLike));
var hotWallet = await CanUseHotWallet();
@@ -60,6 +64,50 @@ namespace BTCPayServer.Controllers
vm.CanUseRPCImport = hotWallet.RPCImport;
return View(vm);
}
[HttpPost]
[Route("{storeId}/derivations/{cryptoCode}/seed")]
[ApiExplorerSettings(IgnoreApi = true)]
public async Task<IActionResult> ViewSeed(string storeId, string cryptoCode)
{
var store = HttpContext.GetStoreData();
if (store == null)
return NotFound();
var network = cryptoCode == null ? null : _ExplorerProvider.GetNetwork(cryptoCode);
if (network == null)
{
return NotFound();
}
var derivation = GetExistingDerivationStrategy(cryptoCode, store);
if (derivation == null)
{
return NotFound();
}
var canExtract = (await CanUseHotWallet()).HotWallet;
var seed = await _ExplorerProvider.GetExplorerClient(network)
.GetMetadataAsync<string>(derivation.AccountDerivation,
WellknownMetadataKeys.Mnemonic);
if (string.IsNullOrEmpty(seed))
{
TempData.SetStatusMessageModel(new StatusMessageModel()
{
Severity = StatusMessageModel.StatusSeverity.Error,
Message = "The seed was not found"
});
}
else
{
TempData.SetStatusMessageModel(new StatusMessageModel()
{
Severity = StatusMessageModel.StatusSeverity.Success,
Html = $"Please store your seed securely! <br/><code class=\"alert-link\">{seed}</code>"
});
}
return RedirectToAction(nameof(UpdateStore), new { storeId });
}
class GetXPubs
{
@@ -163,6 +211,7 @@ namespace BTCPayServer.Controllers
return NotFound();
}
vm.NBXSeedAvailable = false;
vm.Network = network;
vm.RootKeyPath = network.GetRootKeyPath();
DerivationSchemeSettings strategy = null;

View File

@@ -24,7 +24,7 @@ namespace BTCPayServer.Models.StoreViewModels
{
get; set;
} = new List<(string KeyPath, string Address, RootedKeyPath RootedKeyPath)>();
public bool NBXSeedAvailable { get; set; }
public string CryptoCode { get; set; }
public string KeyPath { get; set; }
public string RootFingerprint { get; set; }

View File

@@ -55,14 +55,15 @@
</div>
</template>
}
@if (!Model.Confirmation)
{
<form method="post" asp-action="AddDerivationScheme"
asp-route-cryptoCode="@Model.CryptoCode"
asp-route-storeId="@this.Context.GetRouteValue("storeId")">
<input id="Config" asp-for="Config" type="hidden"/>
@if (!Model.Confirmation)
{
<input id="CryptoCurrency" asp-for="CryptoCode" type="hidden"/>
<input id="DerivationSchemeFormat" asp-for="DerivationSchemeFormat" type="hidden"/>
<input id="AccountKey" asp-for="AccountKey" type="hidden"/>
@@ -155,9 +156,21 @@
</div>
</div>
<button name="command" type="submit" class="btn btn-primary" value="save" id="Continue">Continue</button>
</form>
@if (Model.NBXSeedAvailable)
{
<form asp-action="ViewSeed" asp-route-cryptoCode="@Model.CryptoCode" asp-route-storeId="@Context.GetRouteValue("storeId")">
<button type="submit" class="btn btn-link">View seed</button>
</form>
}
}
else
{
<form method="post" asp-action="AddDerivationScheme"
asp-route-cryptoCode="@Model.CryptoCode"
asp-route-storeId="@this.Context.GetRouteValue("storeId")">
<input id="Config" asp-for="Config" type="hidden"/>
<div class="form-group">
<h5>Confirm the addresses (@Model.CryptoCode)</h5>
<span>Please check that your @Model.CryptoCode wallet is generating the same addresses as below.</span>
@@ -185,7 +198,9 @@
<td>@sample.Address</td>
@if (Model.Source == "Vault")
{
<td><a class="showaddress" href="#" onclick='showAddress(@Safe.Json(sample.RootedKeyPath.ToString()), @Safe.Json(sample.Address)); return false;'>Show on device</a></td>
<td>
<a class="showaddress" href="#" onclick='showAddress(@Safe.Json(sample.RootedKeyPath.ToString()), @Safe.Json(sample.Address)); return false;'>Show on device</a>
</td>
}
</tr>
}
@@ -203,10 +218,11 @@
<span asp-validation-for="HintAddress" class="text-danger"></span>
</div>
<button name="command" type="submit" class="btn btn-primary" value="save" id="Confirm">Confirm</button>
}
</form>
}
</div>
</div>
@section Scripts {
@await Html.PartialAsync("_ValidationScriptsPartial")
<script src="~/js/ledgerwebsocket.js" type="text/javascript" defer="defer" asp-append-version="true"></script>