Remove some code duplication

This commit is contained in:
nicolas.dorier
2021-03-11 21:29:00 +09:00
parent 4bad7d7c52
commit 1f7992e5da

View File

@@ -477,9 +477,7 @@ namespace BTCPayServer.Controllers
}) })
.ToArray(); .ToArray();
var balance = _walletProvider.GetWallet(network).GetBalance(paymentMethod.AccountDerivation); var balance = _walletProvider.GetWallet(network).GetBalance(paymentMethod.AccountDerivation);
model.NBXSeedAvailable = await CanUseHotWallet() && !string.IsNullOrEmpty(await ExplorerClientProvider.GetExplorerClient(network) model.NBXSeedAvailable = await GetSeed(walletId, network) != null;
.GetMetadataAsync<string>(GetDerivationSchemeSettings(walletId).AccountDerivation,
WellknownMetadataKeys.MasterHDKey));
model.CurrentBalance = await balance; model.CurrentBalance = await balance;
await Task.WhenAll(recommendedFees); await Task.WhenAll(recommendedFees);
@@ -512,6 +510,15 @@ namespace BTCPayServer.Controllers
return View(model); return View(model);
} }
private async Task<string> GetSeed(WalletId walletId, BTCPayNetwork network)
{
return await CanUseHotWallet() &&
GetDerivationSchemeSettings(walletId) is DerivationSchemeSettings s &&
s.IsHotWallet &&
ExplorerClientProvider.GetExplorerClient(network) is ExplorerClient client &&
await client.GetMetadataAsync<string>(s.AccountDerivation, WellknownMetadataKeys.MasterHDKey) is string seed &&
!string.IsNullOrEmpty(seed) ? seed : null;
}
[HttpPost] [HttpPost]
[Route("{walletId}/send")] [Route("{walletId}/send")]
@@ -528,9 +535,7 @@ namespace BTCPayServer.Controllers
if (network == null || network.ReadonlyWallet) if (network == null || network.ReadonlyWallet)
return NotFound(); return NotFound();
vm.SupportRBF = network.SupportRBF; vm.SupportRBF = network.SupportRBF;
vm.NBXSeedAvailable = await CanUseHotWallet() && !string.IsNullOrEmpty(await ExplorerClientProvider.GetExplorerClient(network) vm.NBXSeedAvailable = await GetSeed(walletId, network) != null;
.GetMetadataAsync<string>(GetDerivationSchemeSettings(walletId).AccountDerivation,
WellknownMetadataKeys.MasterHDKey, cancellation));
if (!string.IsNullOrEmpty(bip21)) if (!string.IsNullOrEmpty(bip21))
{ {
LoadFromBIP21(vm, bip21, network); LoadFromBIP21(vm, bip21, network);
@@ -1150,18 +1155,7 @@ namespace BTCPayServer.Controllers
} }
else if (command == "view-seed" && await CanUseHotWallet()) else if (command == "view-seed" && await CanUseHotWallet())
{ {
var seed = await ExplorerClientProvider.GetExplorerClient(walletId.CryptoCode) if (await GetSeed(walletId, derivationScheme.Network) is string seed)
.GetMetadataAsync<string>(derivationScheme.AccountDerivation,
WellknownMetadataKeys.Mnemonic, cancellationToken);
if (string.IsNullOrEmpty(seed))
{
TempData.SetStatusMessageModel(new StatusMessageModel()
{
Severity = StatusMessageModel.StatusSeverity.Error,
Message = "The seed was not found"
});
}
else
{ {
var recoveryVm = new RecoverySeedBackupViewModel() var recoveryVm = new RecoverySeedBackupViewModel()
{ {
@@ -1173,6 +1167,14 @@ namespace BTCPayServer.Controllers
}; };
return this.RedirectToRecoverySeedBackup(recoveryVm); return this.RedirectToRecoverySeedBackup(recoveryVm);
} }
else
{
TempData.SetStatusMessageModel(new StatusMessageModel()
{
Severity = StatusMessageModel.StatusSeverity.Error,
Message = "The seed was not found"
});
}
return RedirectToAction(nameof(WalletSettings)); return RedirectToAction(nameof(WalletSettings));
} }