btcpayserver/BTCPayServer/App/BtcPayAppController.cs

94 lines
3.1 KiB
C#
Raw Normal View History

2023-06-23 15:10:44 +02:00
#nullable enable
using System.Linq;
using System.Threading.Tasks;
2023-07-03 09:56:00 +02:00
using BTCPayApp.CommonServer;
2023-06-23 15:10:44 +02:00
using BTCPayServer.Client;
using BTCPayServer.Data;
2023-07-03 09:56:00 +02:00
using BTCPayServer.Plugins.Shopify;
2023-06-23 15:10:44 +02:00
using BTCPayServer.Security.Greenfield;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Mvc;
using NBitcoin;
using NBitcoin.DataEncoders;
using NBXplorer;
namespace BTCPayServer.Controllers;
[Route("btcpayapp")]
public class BtcPayAppController : Controller
{
private readonly BtcPayAppService _appService;
private readonly APIKeyRepository _apiKeyRepository;
private readonly StoreRepository _storeRepository;
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
private readonly ExplorerClientProvider _explorerClientProvider;
public BtcPayAppController(BtcPayAppService appService, APIKeyRepository apiKeyRepository,
2023-07-03 09:56:00 +02:00
StoreRepository storeRepository, BTCPayNetworkProvider btcPayNetworkProvider,
ExplorerClientProvider explorerClientProvider)
2023-06-23 15:10:44 +02:00
{
_appService = appService;
_apiKeyRepository = apiKeyRepository;
_storeRepository = storeRepository;
_btcPayNetworkProvider = btcPayNetworkProvider;
_explorerClientProvider = explorerClientProvider;
}
[HttpGet("pair/{code}")]
public async Task<IActionResult> StartPair(string code)
{
var res = _appService.ConsumePairingCode(code);
if (res is null)
{
return Unauthorized();
}
var store = await _storeRepository.FindStore(res.StoreId, res.UserId);
if (store is null)
{
return NotFound();
}
var key = new APIKeyData()
{
Id = Encoders.Hex.EncodeData(RandomUtils.GetBytes(20)),
Type = APIKeyType.Permanent,
UserId = res.UserId,
Label = "BTCPay App Pairing"
};
key.SetBlob(new APIKeyBlob() {Permissions = new[] {Policies.Unrestricted}});
await _apiKeyRepository.CreateKey(key);
var onchain = store.GetDerivationSchemeSettings(_btcPayNetworkProvider, "BTC");
string? onchainSeed = null;
if (onchain is not null)
{
var explorerClient = _explorerClientProvider.GetExplorerClient("BTC");
onchainSeed = await GetSeed(explorerClient, onchain);
}
return Ok(new PairSuccessResult()
{
Key = key.Id,
StoreId = store.Id,
UserId = res.UserId,
2023-07-03 09:56:00 +02:00
ExistingWallet =
onchain?.AccountDerivation?.GetExtPubKeys()?.FirstOrDefault()
?.ToString(onchain.Network.NBitcoinNetwork),
2023-06-28 13:15:50 +02:00
ExistingWalletSeed = onchainSeed,
Network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>("BTC").NBitcoinNetwork.Name
2023-06-23 15:10:44 +02:00
});
}
2023-07-03 09:56:00 +02:00
2023-06-23 15:10:44 +02:00
private async Task<string?> GetSeed(ExplorerClient client, DerivationSchemeSettings derivation)
{
return derivation.IsHotWallet &&
2023-07-03 09:56:00 +02:00
await client.GetMetadataAsync<string>(derivation.AccountDerivation, WellknownMetadataKeys.Mnemonic) is
{ } seed &&
!string.IsNullOrEmpty(seed)
? seed
: null;
2023-06-23 15:10:44 +02:00
}
}