2021-12-31 08:59:02 +01:00
|
|
|
using System;
|
2021-07-27 16:53:44 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2022-02-24 09:00:44 +01:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2021-07-27 16:53:44 +02:00
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
using BTCPayServer.Payments;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using NBXplorer.Models;
|
|
|
|
|
2022-01-14 05:05:23 +01:00
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
2021-07-27 16:53:44 +02:00
|
|
|
{
|
2022-01-07 04:17:59 +01:00
|
|
|
public partial class GreenfieldStoreOnChainPaymentMethodsController
|
2021-07-27 16:53:44 +02:00
|
|
|
{
|
|
|
|
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/generate")]
|
|
|
|
public async Task<IActionResult> GenerateOnChainWallet(string storeId, string cryptoCode,
|
|
|
|
GenerateWalletRequest request)
|
|
|
|
{
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-12-23 05:32:08 +01:00
|
|
|
AssertCryptoCodeWallet(cryptoCode, out var network, out var wallet);
|
2021-07-27 16:53:44 +02:00
|
|
|
|
|
|
|
if (!_walletProvider.IsAvailable(network))
|
|
|
|
{
|
2022-01-10 03:59:13 +01:00
|
|
|
return this.CreateAPIError(503, "not-available",
|
2021-07-27 16:53:44 +02:00
|
|
|
$"{cryptoCode} services are not currently available");
|
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-07-27 16:53:44 +02:00
|
|
|
var method = GetExistingBtcLikePaymentMethod(cryptoCode);
|
|
|
|
if (method != null)
|
|
|
|
{
|
|
|
|
return this.CreateAPIError("already-configured",
|
|
|
|
$"{cryptoCode} wallet is already configured for this store");
|
|
|
|
}
|
|
|
|
|
|
|
|
var canUseHotWallet = await CanUseHotWallet();
|
|
|
|
if (request.SavePrivateKeys && !canUseHotWallet.HotWallet)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(nameof(request.SavePrivateKeys),
|
|
|
|
"This instance forbids non-admins from having a hot wallet for your store.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.ImportKeysToRPC && !canUseHotWallet.RPCImport)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(nameof(request.ImportKeysToRPC),
|
|
|
|
"This instance forbids non-admins from having importing the wallet addresses/keys to the underlying node.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return this.CreateValidationError(ModelState);
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = _explorerClientProvider.GetExplorerClient(network);
|
|
|
|
GenerateWalletResponse response;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
response = await client.GenerateWalletAsync(request);
|
|
|
|
if (response == null)
|
|
|
|
{
|
2022-01-10 03:59:13 +01:00
|
|
|
return this.CreateAPIError(503, "not-available",
|
2021-07-27 16:53:44 +02:00
|
|
|
$"{cryptoCode} services are not currently available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-01-10 03:59:13 +01:00
|
|
|
return this.CreateAPIError(503, "not-available",
|
2021-07-27 16:53:44 +02:00
|
|
|
$"{cryptoCode} error: {e.Message}");
|
|
|
|
}
|
|
|
|
|
|
|
|
var derivationSchemeSettings = new DerivationSchemeSettings(response.DerivationScheme, network);
|
|
|
|
|
|
|
|
derivationSchemeSettings.Source =
|
|
|
|
string.IsNullOrEmpty(request.ExistingMnemonic) ? "NBXplorerGenerated" : "ImportedSeed";
|
|
|
|
derivationSchemeSettings.IsHotWallet = request.SavePrivateKeys;
|
|
|
|
|
|
|
|
var accountSettings = derivationSchemeSettings.GetSigningAccountKeySettings();
|
|
|
|
accountSettings.AccountKeyPath = response.AccountKeyPath.KeyPath;
|
|
|
|
accountSettings.RootFingerprint = response.AccountKeyPath.MasterFingerprint;
|
|
|
|
derivationSchemeSettings.AccountOriginal = response.DerivationScheme.ToString();
|
|
|
|
|
|
|
|
var store = Store;
|
|
|
|
var storeBlob = store.GetStoreBlob();
|
|
|
|
store.SetSupportedPaymentMethod(new PaymentMethodId(cryptoCode, PaymentTypes.BTCLike),
|
|
|
|
derivationSchemeSettings);
|
|
|
|
store.SetStoreBlob(storeBlob);
|
|
|
|
await _storeRepository.UpdateStore(store);
|
|
|
|
var rawResult = GetExistingBtcLikePaymentMethod(cryptoCode, store);
|
|
|
|
var result = new OnChainPaymentMethodDataWithSensitiveData(rawResult.CryptoCode, rawResult.DerivationScheme,
|
2021-09-25 07:04:34 +02:00
|
|
|
rawResult.Enabled, rawResult.Label, rawResult.AccountKeyPath, response.GetMnemonic(), derivationSchemeSettings.PaymentId.ToStringNormalized());
|
2021-07-27 16:53:44 +02:00
|
|
|
return Ok(result);
|
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-07-27 16:53:44 +02:00
|
|
|
private async Task<(bool HotWallet, bool RPCImport)> CanUseHotWallet()
|
|
|
|
{
|
2022-05-24 06:18:16 +02:00
|
|
|
return await _authorizationService.CanUseHotWallet(PoliciesSettings, User);
|
2021-07-27 16:53:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|