Refactoring connection to Lnd now there is HTTP support

This commit is contained in:
rockstardev 2018-05-01 20:33:43 -05:00
parent f4f9fabfd3
commit fcfba7f5e1
4 changed files with 59 additions and 30 deletions

View file

@ -55,29 +55,5 @@ namespace BTCPayServer.Tests
ExtPubKey pubkey = masterPubKey.Derive(0);
Console.WriteLine("PubKey " + 0 + " : " + pubkey.ToString(network));
}
[Fact]
public async Task TestLndAsync()
{
var tls = File.ReadAllBytes(@"c:\Users\newdawn\AppData\Local\Lnd\tls.cert");
var macroon = File.ReadAllBytes(@"c:\Users\newdawn\AppData\Local\Lnd\admin.macaroon");
var lnd = new LndClient(new Uri("https://localhost:8080"), Network.RegTest, tls, macroon);
var res = await lnd.GetInfo();
Console.WriteLine("Address: " + res.Address);
}
[Fact]
public async Task LndCreateInvoice()
{
var tls = File.ReadAllBytes(@"c:\Users\newdawn\AppData\Local\Lnd\tls.cert");
var macroon = File.ReadAllBytes(@"c:\Users\newdawn\AppData\Local\Lnd\admin.macaroon");
var lnd = new LndClient(new Uri("https://127.0.0.1:8080"), Network.RegTest, tls, macroon);
var res = await lnd.CreateInvoice(10000, "Hello world", TimeSpan.FromSeconds(3600));
Console.WriteLine("Result: "+ res.ToJson());
}
}
}

View file

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Payments.Lightning.Lnd;
using NBitcoin;
using Xunit;
using Xunit.Abstractions;
namespace BTCPayServer.Tests.UnitTests
{
public class LndTest
{
private readonly ITestOutputHelper output;
public LndTest(ITestOutputHelper output)
{
this.output = output;
}
private LndClient Client
{
get
{
var lnd = new LndClient(new Uri("http://localhost:53280"), Network.RegTest);
return lnd;
}
}
[Fact]
public async Task GetInfo()
{
var res = await Client.GetInfo();
output.WriteLine("Result: " + res.ToJson());
}
[Fact]
public async Task CreateInvoice()
{
var res = await Client.CreateInvoice(10000, "Hello world", TimeSpan.FromSeconds(3600));
output.WriteLine("Result: " + res.ToJson());
}
}
}

View file

@ -22,4 +22,4 @@ RUN go install . ./cmd/...
COPY "start-lnd.sh" .
RUN chmod +x start-lnd.sh
ENTRYPOINT ["./start-lnd.sh"]
ENTRYPOINT ["./start-lnd.sh"]

View file

@ -17,16 +17,19 @@ namespace BTCPayServer.Payments.Lightning.Lnd
{
public class LndClient : ILightningInvoiceClient, ILightningListenInvoiceSession
{
public LndClient(Uri uri, Network network, byte[] tlsCertificate, byte[] grpcMacaroon)
public LndClient(Uri uri, Network network, byte[] tlsCertificate = null, byte[] grpcMacaroon = null)
{
_HttpClient = HttpClientFactoryForLnd.Generate(tlsCertificate, grpcMacaroon);
// for now working with custom build of lnd that has no macaroons and is on http
//_HttpClient = HttpClientFactoryForLnd.Generate(tlsCertificate, grpcMacaroon);
_HttpClient = new HttpClient();
_Decorator = new LndSwaggerClient(uri.ToString().TrimEnd('/'), _HttpClient);
}
private HttpClient _HttpClient;
private LndSwaggerClient _Decorator;
public async Task<LightningInvoice> CreateInvoice(LightMoney amount, string description, TimeSpan expiry,
public async Task<LightningInvoice> CreateInvoice(LightMoney amount, string description, TimeSpan expiry,
CancellationToken cancellation = default(CancellationToken))
{
var strAmount = ConvertInv.ToString(amount.ToUnit(LightMoneyUnit.Satoshi));
@ -41,7 +44,6 @@ namespace BTCPayServer.Payments.Lightning.Lnd
var invoice = new LightningInvoice
{
// TODO: Verify id corresponds to R_hash
Id = BitString(resp.R_hash),
Amount = amount,
BOLT11 = resp.Payment_request,
@ -154,7 +156,9 @@ namespace BTCPayServer.Payments.Lightning.Lnd
private static HttpClientHandler GetCertificate(byte[] certFile)
{
var clientCertificate = new X509Certificate2(certFile);
X509Certificate2 clientCertificate = null;
if (certFile != null)
clientCertificate = new X509Certificate2(certFile);
var handler = new HttpClientHandler
{
@ -172,6 +176,9 @@ namespace BTCPayServer.Payments.Lightning.Lnd
return false;
}
if (clientCertificate == null)
return true;
X509Certificate2 remoteRoot = chain.ChainElements[chain.ChainElements.Count - 1].Certificate;
var res = clientCertificate.RawData.SequenceEqual(remoteRoot.RawData);