From 47eb087d1b8b77b4a7ba37a730458786090f5b1d Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 18 Dec 2019 22:28:03 +0900 Subject: [PATCH] Rename CanGenerateWallet to CanUseHotWallet, small refactoring on generatewallet --- BTCPayServer.Tests/SeleniumTests.cs | 2 +- .../Controllers/StoresController.BTCLike.cs | 42 +++++++++---------- BTCPayServer/Controllers/StoresController.cs | 6 +++ .../HostedServices/CssThemeManager.cs | 3 -- .../DerivationSchemeViewModel.cs | 2 +- BTCPayServer/Services/PoliciesSettings.cs | 4 +- BTCPayServer/Views/Server/Policies.cshtml | 6 +-- .../Views/Stores/AddDerivationScheme.cshtml | 2 +- ...vationSchemes_HardwareWalletDialogs.cshtml | 2 +- 9 files changed, 35 insertions(+), 34 deletions(-) diff --git a/BTCPayServer.Tests/SeleniumTests.cs b/BTCPayServer.Tests/SeleniumTests.cs index 79e7a50c7..dd8bd6fea 100644 --- a/BTCPayServer.Tests/SeleniumTests.cs +++ b/BTCPayServer.Tests/SeleniumTests.cs @@ -415,7 +415,7 @@ namespace BTCPayServer.Tests var mnemonic = s.GenerateWallet("BTC", "", true, false); var invoiceId = s.CreateInvoice(storeId.storeId); - var invoice = await s.Server.PayTester.InvoiceRepository.GetInvoice( invoiceId); + var invoice = await s.Server.PayTester.InvoiceRepository.GetInvoice(invoiceId); var address = invoice.EntityToDTO().Addresses["BTC"]; var result = await s.Server.ExplorerNode.GetAddressInfoAsync(BitcoinAddress.Create(address, Network.RegTest)); diff --git a/BTCPayServer/Controllers/StoresController.BTCLike.cs b/BTCPayServer/Controllers/StoresController.BTCLike.cs index 4809d296f..ff9f2aba9 100644 --- a/BTCPayServer/Controllers/StoresController.BTCLike.cs +++ b/BTCPayServer/Controllers/StoresController.BTCLike.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.AspNetCore.Authorization; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -27,7 +28,7 @@ namespace BTCPayServer.Controllers { [HttpGet] [Route("{storeId}/derivations/{cryptoCode}")] - public IActionResult AddDerivationScheme(string storeId, string cryptoCode) + public async Task AddDerivationScheme(string storeId, string cryptoCode) { var store = HttpContext.GetStoreData(); if (store == null) @@ -42,7 +43,14 @@ namespace BTCPayServer.Controllers vm.CryptoCode = cryptoCode; vm.RootKeyPath = network.GetRootKeyPath(); vm.Network = network; - SetExistingValues(store, vm); + var derivation = GetExistingDerivationStrategy(vm.CryptoCode, store); + if (derivation != null) + { + vm.DerivationScheme = derivation.AccountDerivation.ToString(); + vm.Config = derivation.ToJson(); + } + vm.Enabled = !store.GetStoreBlob().IsExcluded(new PaymentMethodId(vm.CryptoCode, PaymentTypes.BTCLike)); + vm.CanUseHotWallet = await CanUseHotWallet(); return View(vm); } @@ -121,20 +129,6 @@ namespace BTCPayServer.Controllers return new EmptyResult(); } - - - private void SetExistingValues(StoreData store, DerivationSchemeViewModel vm) - { - var derivation = GetExistingDerivationStrategy(vm.CryptoCode, store); - if (derivation != null) - { - vm.DerivationScheme = derivation.AccountDerivation.ToString(); - vm.Config = derivation.ToJson(); - } - vm.Enabled = !store.GetStoreBlob().IsExcluded(new PaymentMethodId(vm.CryptoCode, PaymentTypes.BTCLike)); - vm.CanUseGenerateWallet = CanUseGenerateWallet(); - } - private DerivationSchemeSettings GetExistingDerivationStrategy(string cryptoCode, StoreData store) { var id = new PaymentMethodId(cryptoCode, PaymentTypes.BTCLike); @@ -326,7 +320,7 @@ namespace BTCPayServer.Controllers public async Task GenerateNBXWallet(string storeId, string cryptoCode, GenerateWalletRequest request) { - if (!CanUseGenerateWallet()) + if (!await CanUseHotWallet()) { return NotFound(); } @@ -363,6 +357,15 @@ namespace BTCPayServer.Controllers return result; } + private async Task CanUseHotWallet() + { + var isAdmin = (await _authorizationService.AuthorizeAsync(User, BTCPayServer.Security.Policies.CanModifyServerSettings.Key)).Succeeded; + if (isAdmin) + return true; + var policies = await _settingsRepository.GetSettingAsync(); + return policies?.AllowHotWalletForAll is true; + } + private async Task ReadAllText(IFormFile file) { using (var stream = new StreamReader(file.OpenReadStream())) @@ -391,10 +394,5 @@ namespace BTCPayServer.Controllers ModelState.Remove(nameof(vm.Config)); // Remove the cached value return View(nameof(AddDerivationScheme),vm); } - - private bool CanUseGenerateWallet() - { - return (_BTCPayEnv.IsDevelopping || User.IsInRole(Roles.ServerAdmin) || _CssThemeManager.AllowGenerateWalletForAll); - } } } diff --git a/BTCPayServer/Controllers/StoresController.cs b/BTCPayServer/Controllers/StoresController.cs index f8497f252..5f92d26d7 100644 --- a/BTCPayServer/Controllers/StoresController.cs +++ b/BTCPayServer/Controllers/StoresController.cs @@ -63,6 +63,8 @@ namespace BTCPayServer.Controllers ChangellyClientProvider changellyClientProvider, IWebHostEnvironment env, IHttpClientFactory httpClientFactory, PaymentMethodHandlerDictionary paymentMethodHandlerDictionary, + SettingsRepository settingsRepository, + IAuthorizationService authorizationService, CssThemeManager cssThemeManager) { _RateFactory = rateFactory; @@ -76,6 +78,8 @@ namespace BTCPayServer.Controllers _Env = env; _httpClientFactory = httpClientFactory; _paymentMethodHandlerDictionary = paymentMethodHandlerDictionary; + _settingsRepository = settingsRepository; + _authorizationService = authorizationService; _CssThemeManager = cssThemeManager; _NetworkProvider = networkProvider; _ExplorerProvider = explorerProvider; @@ -100,6 +104,8 @@ namespace BTCPayServer.Controllers IWebHostEnvironment _Env; private IHttpClientFactory _httpClientFactory; private readonly PaymentMethodHandlerDictionary _paymentMethodHandlerDictionary; + private readonly SettingsRepository _settingsRepository; + private readonly IAuthorizationService _authorizationService; private readonly CssThemeManager _CssThemeManager; [TempData] diff --git a/BTCPayServer/HostedServices/CssThemeManager.cs b/BTCPayServer/HostedServices/CssThemeManager.cs index c615a898d..ddba26c96 100644 --- a/BTCPayServer/HostedServices/CssThemeManager.cs +++ b/BTCPayServer/HostedServices/CssThemeManager.cs @@ -88,11 +88,8 @@ namespace BTCPayServer.HostedServices RootAppId = data.RootAppId; DomainToAppMapping = data.DomainToAppMapping; AllowLightningInternalNodeForAll = data.AllowLightningInternalNodeForAll; - AllowGenerateWalletForAll = data.AllowGenerateWalletForAll; } - public bool AllowGenerateWalletForAll { get; set; } - public bool AllowLightningInternalNodeForAll { get; set; } } diff --git a/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs b/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs index 74b833966..b447d44d8 100644 --- a/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs @@ -41,7 +41,7 @@ namespace BTCPayServer.Models.StoreViewModels public string DerivationSchemeFormat { get; set; } public string AccountKey { get; set; } public BTCPayNetwork Network { get; set; } - public bool CanUseGenerateWallet { get; set; } + public bool CanUseHotWallet { get; set; } public RootedKeyPath GetAccountKeypath() { diff --git a/BTCPayServer/Services/PoliciesSettings.cs b/BTCPayServer/Services/PoliciesSettings.cs index 72e66f7e9..9dddeca6e 100644 --- a/BTCPayServer/Services/PoliciesSettings.cs +++ b/BTCPayServer/Services/PoliciesSettings.cs @@ -23,8 +23,8 @@ namespace BTCPayServer.Services public bool DiscourageSearchEngines { get; set; } [Display(Name = "Allow non-admins to use the internal lightning node in their stores")] public bool AllowLightningInternalNodeForAll { get; set; } - [Display(Name = "Allow non-admins to use the NBXplorer wallet generator in their stores")] - public bool AllowGenerateWalletForAll { get; set; } + [Display(Name = "Allow non-admins to create hot wallets for their stores")] + public bool AllowHotWalletForAll { get; set; } [Display(Name = "Display app on website root")] public string RootAppId { get; set; } diff --git a/BTCPayServer/Views/Server/Policies.cshtml b/BTCPayServer/Views/Server/Policies.cshtml index 4103083a6..972d6db01 100644 --- a/BTCPayServer/Views/Server/Policies.cshtml +++ b/BTCPayServer/Views/Server/Policies.cshtml @@ -33,9 +33,9 @@
- - - + + +
diff --git a/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml b/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml index f2c3cba43..4b1d2a90d 100644 --- a/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml +++ b/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml @@ -85,7 +85,7 @@ { } - @if (Model.CanUseGenerateWallet) + @if (Model.CanUseHotWallet) { } diff --git a/BTCPayServer/Views/Stores/AddDerivationSchemes_HardwareWalletDialogs.cshtml b/BTCPayServer/Views/Stores/AddDerivationSchemes_HardwareWalletDialogs.cshtml index 47b2c21da..ac2f2fd17 100644 --- a/BTCPayServer/Views/Stores/AddDerivationSchemes_HardwareWalletDialogs.cshtml +++ b/BTCPayServer/Views/Stores/AddDerivationSchemes_HardwareWalletDialogs.cshtml @@ -1,6 +1,6 @@ @using NBXplorer.Models @model DerivationSchemeViewModel -@if (Model.CanUseGenerateWallet) +@if (Model.CanUseHotWallet) { }