diff --git a/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs b/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs index aee99dcd4..a717f38e1 100644 --- a/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs @@ -1,36 +1,79 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Rendering; using NBitcoin; namespace BTCPayServer.Models.StoreViewModels { public class DerivationSchemeViewModel { + private string _derivationScheme; + public DerivationSchemeViewModel() { } + public string DerivationScheme { - get; set; + get => _derivationScheme; + set + { + _derivationScheme = value; + var split = _derivationScheme.Split("-"); + ExtPubKey = split[0]; + if (split.Length <= 1) return; + var suffix = ""; + if (split.Length > 1) + { + suffix = "-" + split[1]; + } + + var type = AddressTypes.SingleOrDefault(addressType => addressType.Suffix == suffix); + AddressType = type?.Type; + } } - public List<(string KeyPath, string Address)> AddressSamples - { - get; set; - } = new List<(string KeyPath, string Address)>(); + + public List<(string KeyPath, string Address)> AddressSamples { get; set; } = + new List<(string KeyPath, string Address)>(); public string CryptoCode { get; set; } - [Display(Name = "Hint address")] - public string HintAddress { get; set; } + [Display(Name = "Hint address")] public string HintAddress { get; set; } public bool Confirmation { get; set; } public bool Enabled { get; set; } = true; public string ServerUrl { get; set; } public string StatusMessage { get; internal set; } public KeyPath RootKeyPath { get; set; } + + [Display(Name = "xpub / ypub key")] + public string ExtPubKey { get; set; } + public string AddressType { get; set; } + + + public IEnumerable AddressTypes = new List() + { + new AddressType() + { + Suffix = "", + Type = "P2WPKH / Multi-sig P2WSH" + }, + new AddressType() + { + Suffix = "-[p2sh]", + Type = "P2SH-P2WPKH / Multi-sig P2SH-P2WSH" + }, + new AddressType() + { + Suffix = "-[legacy]", + Type = "P2PKH / Multi-sig P2SH" + } + }; + } + + public class AddressType + { + public string Type { get; set; } + public string Suffix { get; set; } } } diff --git a/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml b/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml index fa06cea84..2941b5422 100644 --- a/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml +++ b/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml @@ -4,7 +4,7 @@ ViewData.SetActivePageAndTitle(StoreNavPages.Index, $"{Model.CryptoCode} Derivation scheme"); } - +

@ViewData["Title"]

@@ -15,70 +15,97 @@
- @if(!Model.Confirmation) + @if (!Model.Confirmation) {
Derivation Scheme
The DerivationScheme represents the destination of the funds received by your invoice. It is generated by your wallet software. Please, verify that you are generating the right addresses by clicking on 'Check ExtPubKey'
- -
+ + + +
+
+ + +
+
+ + +
+
+ +

+

+
+
-
+
- +
} @@ -88,25 +115,25 @@
Confirm the addresses (@Model.CryptoCode)
Please check that your @Model.CryptoCode wallet is generating the same addresses as below.
- - - + + +
- - - - + + + + - @foreach(var sample in Model.AddressSamples) - { - - - - - } + @foreach (var sample in Model.AddressSamples) + { + + + + + }
Key pathAddress
Key pathAddress
@sample.KeyPath@sample.Address
@sample.KeyPath@sample.Address
@@ -117,7 +144,7 @@
- +
@@ -129,7 +156,7 @@ @section Scripts { @await Html.PartialAsync("_ValidationScriptsPartial") diff --git a/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js b/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js index 7379e4f3c..9ecd3aa34 100644 --- a/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js +++ b/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js @@ -1,14 +1,67 @@ $(function () { + + + var classicDerivationInputElement = $("#DerivationScheme"); + var smartDerivationInputElement = $("#ExtPubKey"); + var smartDerivationTypeElement = $("#AddressType"); + var smartDerivationInputContainer = $("#smart-derivation-input"); + var classDerivationInputContainer = $(".classic-derivation-input"); + var derivationInputMethodElement = $("#derivation-input-method"); + + + function toggleClassicDerivationInput() { + classDerivationInputContainer.toggle(); + smartDerivationInputContainer.toggle(); + } + + function setClassicDerivationInput() { + var newVal = smartDerivationInputElement.val(); + var typeSuffix = smartDerivationTypeElement.find(":selected").data("suffix"); + newVal += typeSuffix; + classicDerivationInputElement.val(newVal); + } + + classicDerivationInputElement.on("input", function () { + var newValue = $(this).val(); + var split = newValue.split("-"); + smartDerivationInputElement.val(split[0]); + var suffix = ""; + var typeValue = ""; + if (split.length > 1) { + suffix = "-" + split[1]; + typeValue = smartDerivationTypeElement.find("[data-suffix='" + suffix + "']").attr("value"); + } + + if (!typeValue) { + smartDerivationTypeElement.find("[data-suffix]").each(function (i, x) { + x = $(x); + if (!x.data("data-suffix")) { + typeValue = x.val(); + } + }); + } + smartDerivationTypeElement.val(typeValue); + }); + + smartDerivationInputElement.on("input", setClassicDerivationInput); + smartDerivationTypeElement.on("input", setClassicDerivationInput); + + derivationInputMethodElement.on("click", function (event) { + event.preventDefault(); + toggleClassicDerivationInput(); + }); + var ledgerDetected = false; var bridge = new ledgerwebsocket.LedgerWebSocketBridge(srvModel); var cryptoSelector = $("#CryptoCurrency"); + function GetSelectedCryptoCode() { return cryptoSelector.val(); } function WriteAlert(type, message) { - + } function Write(prefix, type, message) { @@ -29,7 +82,9 @@ $("#DerivationScheme").val(result.extPubKey); $("#DerivationSchemeFormat").val("BTCPay"); }) - .catch(function (reason) { Write('check', 'error', reason); }); + .catch(function (reason) { + Write('check', 'error', reason); + }); return false; }); @@ -38,7 +93,9 @@ return false; var cryptoCode = GetSelectedCryptoCode(); bridge.sendCommand("getxpub", "cryptoCode=" + cryptoCode) - .catch(function (reason) { Write('check', 'error', reason); }) + .catch(function (reason) { + Write('check', 'error', reason); + }) .then(function (result) { if (!result) return;