upd bl and add libs on build

This commit is contained in:
Kukks
2023-12-11 08:41:39 +01:00
parent a8e43e9350
commit c841c5e86b
20 changed files with 122 additions and 30 deletions

View File

@@ -10,6 +10,7 @@
<Product>Blink</Product>
<Description>Brink Lightning support</Description>
<Version>1.0.0</Version>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RootNamespace>BTCPayServer.Plugins.Blink</RootNamespace>
</PropertyGroup>
<!-- Plugin development properties -->

View File

@@ -27,7 +27,7 @@ public class BlinkLightningClient : ILightningClient
{
private readonly string _apiKey;
private readonly Uri _apiEndpoint;
private readonly string _walletId;
public string? WalletId { get; set; }
private readonly Network _network;
private readonly NBXplorerDashboard _nbXplorerDashboard;
private readonly GraphQLHttpClient _client;
@@ -37,15 +37,16 @@ public class BlinkLightningClient : ILightningClient
{
_apiKey = apiKey;
_apiEndpoint = apiEndpoint;
_walletId = walletId;
WalletId = walletId;
_network = network;
_nbXplorerDashboard = nbXplorerDashboard;
_client = new GraphQLHttpClient(new GraphQLHttpClientOptions() {EndPoint = _apiEndpoint}, new NewtonsoftJsonSerializer(), httpClient);
}
public override string ToString()
{
return $"type=blink;server={_apiEndpoint};api-key={_apiKey};wallet-id={_walletId}";
return $"type=blink;server={_apiEndpoint};api-key={_apiKey}{(WalletId is null? "":$";wallet-id={WalletId}")}";
}
public async Task<LightningInvoice?> GetInvoice(string invoiceId,
@@ -73,7 +74,7 @@ query InvoiceByPaymentHash($paymentHash: PaymentHash!, $walletId: WalletId!) {
OperationName = "InvoiceByPaymentHash",
Variables = new
{
walletId = _walletId,
walletId = WalletId,
paymentHash = invoiceId
}
};
@@ -146,7 +147,7 @@ query Invoices($walletId: WalletId!) {
OperationName = "Invoices",
Variables = new
{
walletId = _walletId
walletId = WalletId
}
};
var response = await _client.SendQueryAsync<dynamic>(reques, cancellation);
@@ -192,7 +193,7 @@ query TransactionsByPaymentHash($paymentHash: PaymentHash!, $walletId: WalletId!
OperationName = "TransactionsByPaymentHash",
Variables = new
{
walletId = _walletId,
walletId = WalletId,
paymentHash = paymentHash
}
};
@@ -277,7 +278,7 @@ query Transactions($walletId: WalletId!) {
OperationName = "Transactions",
Variables = new
{
walletId = _walletId
walletId = WalletId
}
};
var response = await _client.SendQueryAsync<dynamic>(reques, cancellation);
@@ -318,7 +319,7 @@ mutation LnInvoiceCreate($input: LnInvoiceCreateInput!) {
{
input = new
{
walletId = _walletId,
walletId = WalletId,
memo = createInvoiceRequest.Description?? createInvoiceRequest.DescriptionHash?.ToString(),
amount = (long)createInvoiceRequest.Amount.ToUnit(LightMoneyUnit.Satoshi),
expiresIn = (int)createInvoiceRequest.Expiry.TotalMinutes
@@ -462,6 +463,38 @@ expiresIn = (int)createInvoiceRequest.Expiry.TotalMinutes
}
}
public async Task<(Network Network, string DefaultWalletId)> GetNetworkAndDefaultWallet(CancellationToken cancellation =default)
{
var reques = new GraphQLRequest
{
Query = @"
query GetNetworkAndDefaultWallet {
globals {
network
}
me {
defaultAccount {
defaultWalletId
}
}
}",
OperationName = "GetNetworkAndDefaultWallet"
};
var response = await _client.SendQueryAsync<dynamic>(reques, cancellation);
var defaultWalletId = (string) response.Data.me.defaultAccount.defaultWalletId;
var network = response.Data.globals.network.ToString() switch
{
"mainnet" => Network.Main,
"testnet" => Network.TestNet,
"regtest" => Network.RegTest,
_ => throw new ArgumentOutOfRangeException()
};
return (network, defaultWalletId);
}
public async Task<LightningNodeInformation> GetInfo(CancellationToken cancellation = new CancellationToken())
{
@@ -504,7 +537,7 @@ query GetWallet($walletId: WalletId!) {
}",
OperationName = "GetWallet",
Variables = new {
walletId = _walletId
walletId = WalletId
}
};
@@ -570,7 +603,7 @@ mutation LnInvoicePaymentSend($input: LnInvoicePaymentInput!) {
OperationName = "LnInvoicePaymentSend",
Variables = new {
input = new {
walletId = _walletId,
walletId = WalletId,
paymentRequest = bolt11,
}
}

View File

@@ -8,19 +8,19 @@ using Network = NBitcoin.Network;
namespace BTCPayServer.Plugins.Blink;
public class BlinkLightningConnectionStringHandler : ILightningConnectionStringHandler
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly NBXplorerDashboard _nbXplorerDashboard;
public BlinkLightningConnectionStringHandler(IHttpClientFactory httpClientFactory, NBXplorerDashboard nbXplorerDashboard)
public BlinkLightningConnectionStringHandler(IHttpClientFactory httpClientFactory,
NBXplorerDashboard nbXplorerDashboard)
{
_httpClientFactory = httpClientFactory;
_nbXplorerDashboard = nbXplorerDashboard;
}
public ILightningClient? Create(string connectionString, Network network, out string? error)
{
var kv = LightningConnectionStringHelper.ExtractValues(connectionString, out var type);
@@ -29,17 +29,20 @@ public class BlinkLightningConnectionStringHandler : ILightningConnectionStringH
error = null;
return null;
}
if (!kv.TryGetValue("server", out var server))
{
error = $"The key 'server' is mandatory for blink connection strings";
return null;
}
if (!Uri.TryCreate(server, UriKind.Absolute, out var uri)
|| uri.Scheme != "http" && uri.Scheme != "https")
{
error = "The key 'server' should be an URI starting by http:// or https://";
return null;
}
bool allowInsecure = false;
if (kv.TryGetValue("allowinsecure", out var allowinsecureStr))
{
@@ -52,40 +55,69 @@ public class BlinkLightningConnectionStringHandler : ILightningConnectionStringH
allowInsecure = allowinsecureStr.Equals("true", StringComparison.OrdinalIgnoreCase);
}
if (!LightningConnectionStringHelper.VerifySecureEndpoint(uri, allowInsecure))
{
error = "The key 'allowinsecure' is false, but server's Uri is not using https";
return null;
}
if (!kv.TryGetValue("api-key", out var apiKey))
{
error = "The key 'api-key' is not found";
return null;
}
if (!kv.TryGetValue("wallet-id", out var walletId))
{
error = "The key 'wallet-id' is not found";
return null;
}
error = null;
var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
client.BaseAddress = uri;
network = Network.Main;
var bclient = new BlinkLightningClient( apiKey, uri, walletId, network, _nbXplorerDashboard, client);
var result = bclient.GetBalance().GetAwaiter().GetResult();
if (result is null)
kv.TryGetValue("wallet-id", out var walletId);
var bclient = new BlinkLightningClient(apiKey, uri, walletId, network, _nbXplorerDashboard, client);
(Network Network, string DefaultWalletId) res;
try
{
error = "Invalid credentials";
res = bclient.GetNetworkAndDefaultWallet().GetAwaiter().GetResult();
if (res.Network != network)
{
error = $"The wallet is not on the right network ({res.Network.Name} instead of {network.Name})";
return null;
}
if (walletId is null && string.IsNullOrEmpty(res.DefaultWalletId))
{
error = $"The wallet-id is not set and no default wallet is set";
return null;
}
}
catch (Exception e)
{
error = $"Invalid server or api key";
return null;
}
return bclient;
if (walletId is null)
{
bclient.WalletId = res.DefaultWalletId;
}
else
{
try
{
bclient.GetBalance().GetAwaiter().GetResult();
}
catch (Exception e)
{
error = "Invalid wallet id";
return null;
}
}
return bclient;
}
}

View File

@@ -32,7 +32,7 @@
<code><b>type=</b>blink;<b>server=</b>https://api.blink.sv/graphql;<b>api-key</b>=blink_...;<b>wallet-id=</b>xyz</code>
</li>
</ul>
<p class="my-2">Head over to the dashboard for the wallet id and and create an api key.</p>
<p class="my-2">Head over to the Blink dashboard and create an api key. The wallet-id is optional and will use the default wallet otherwise.</p>
</div>
</div>
</div>