From 789b9168add80e2bdd3c7dcdffafaf878e0b3ab3 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Mon, 14 May 2018 15:54:44 -0500 Subject: [PATCH] Adding Lnd to connection types and supporting parsing --- BTCPayServer.Tests/UnitTest1.cs | 7 ++ BTCPayServer.Tests/UnitTests/LndTest.cs | 8 +- .../Lightning/LightningConnectionString.cs | 81 ++++++++++--------- 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 64afa1d28..b61f07339 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -328,6 +328,13 @@ namespace BTCPayServer.Tests public void CanParseLightningURL() { LightningConnectionString conn = null; + + Assert.True(LightningConnectionString.TryParse("http://127.0.0.1:53280?type=lnd", out conn)); + Assert.Equal("http://127.0.0.1:53280/", conn.ToString()); + Assert.Equal("http://127.0.0.1:53280/", conn.ToUri(true).AbsoluteUri); + Assert.Equal("http://127.0.0.1:53280/", conn.ToUri(false).AbsoluteUri); + Assert.Equal(LightningConnectionType.Lnd, conn.ConnectionType); + Assert.True(LightningConnectionString.TryParse("/test/a", out conn)); Assert.Equal("unix://test/a", conn.ToString()); Assert.Equal("unix://test/a", conn.ToUri(true).AbsoluteUri); diff --git a/BTCPayServer.Tests/UnitTests/LndTest.cs b/BTCPayServer.Tests/UnitTests/LndTest.cs index 418b73f48..4f4d0310a 100644 --- a/BTCPayServer.Tests/UnitTests/LndTest.cs +++ b/BTCPayServer.Tests/UnitTests/LndTest.cs @@ -79,7 +79,7 @@ namespace BTCPayServer.Tests.UnitTests public async Task EnsureLightningChannelAsync() { var merchantInfo = await WaitLNSynched(); - var merchantAddress = new LnrpcLightningAddress + var merchantNodeAddress = new LnrpcLightningAddress { Pubkey = merchantInfo.NodeId, Host = "merchant_lnd:9735" @@ -90,7 +90,7 @@ namespace BTCPayServer.Tests.UnitTests // if channel is pending generate blocks until confirmed var pendingResponse = await CustomerLnd.PendingChannelsAsync(); if (pendingResponse.Pending_open_channels? - .Any(a => a.Channel?.Remote_node_pub == merchantAddress.Pubkey) == true) + .Any(a => a.Channel?.Remote_node_pub == merchantNodeAddress.Pubkey) == true) { ExplorerNode.Generate(1); await WaitLNSynched(); @@ -100,7 +100,7 @@ namespace BTCPayServer.Tests.UnitTests // check if channel is established var chanResponse = await CustomerLnd.ListChannelsAsync(null, null, null, null); var channelToMerchant = chanResponse?.Channels - .Where(a => a.Remote_pubkey == merchantAddress.Pubkey) + .Where(a => a.Remote_pubkey == merchantNodeAddress.Pubkey) .FirstOrDefault(); if (channelToMerchant == null) @@ -111,7 +111,7 @@ namespace BTCPayServer.Tests.UnitTests { var connectResp = await CustomerLnd.ConnectPeerAsync(new LnrpcConnectPeerRequest { - Addr = merchantAddress + Addr = merchantNodeAddress }); } diff --git a/BTCPayServer/Payments/Lightning/LightningConnectionString.cs b/BTCPayServer/Payments/Lightning/LightningConnectionString.cs index fe7fc8a6a..be9f952e2 100644 --- a/BTCPayServer/Payments/Lightning/LightningConnectionString.cs +++ b/BTCPayServer/Payments/Lightning/LightningConnectionString.cs @@ -8,21 +8,48 @@ namespace BTCPayServer.Payments.Lightning public enum LightningConnectionType { Charge, - CLightning + CLightning, + Lnd } public class LightningConnectionString { + public LightningConnectionString() { } + + public string Username { get; set; } + public string Password { get; set; } + public Uri BaseUri { get; set; } + + public LightningConnectionType ConnectionType { get; private set; } + + public Uri ToUri(bool withCredentials) + { + if (withCredentials) + { + return new UriBuilder(BaseUri) { UserName = Username ?? "", Password = Password ?? "" }.Uri; + } + else + { + return BaseUri; + } + } + + public override string ToString() + { + return ToUri(true).AbsoluteUri; + } + + // public static bool TryParse(string str, out LightningConnectionString connectionString) { return TryParse(str, out connectionString, out var error); } + public static bool TryParse(string str, out LightningConnectionString connectionString, out string error) { if (str == null) throw new ArgumentNullException(nameof(str)); if (str.StartsWith('/')) str = "unix:" + str; - var result = new LightningConnectionString(); connectionString = null; error = null; @@ -51,7 +78,16 @@ namespace BTCPayServer.Payments.Lightning uri = new Uri("unix://" + str, UriKind.Absolute); } - if (uri.Scheme == "http" || uri.Scheme == "https") + var result = new LightningConnectionString(); + + result.ConnectionType = uri.Scheme == "http" || uri.Scheme == "https" ? + LightningConnectionType.Charge : + LightningConnectionType.CLightning; + + if (uri.Query.Contains("type=lnd")) + result.ConnectionType = LightningConnectionType.Lnd; + + if (result.ConnectionType == LightningConnectionType.Charge) { var parts = uri.UserInfo.Split(':'); if (string.IsNullOrEmpty(uri.UserInfo) || parts.Length != 2) @@ -67,44 +103,11 @@ namespace BTCPayServer.Payments.Lightning error = "The url should not have user information"; return false; } - result.BaseUri = new UriBuilder(uri) { UserName = "", Password = "" }.Uri; + + var uriWithoutQuery = uri.AbsoluteUri.Split('?')[0]; + result.BaseUri = new UriBuilder(uriWithoutQuery) { UserName = "", Password = "" }.Uri; connectionString = result; return true; } - - public LightningConnectionString() - { - - } - - public string Username { get; set; } - public string Password { get; set; } - public Uri BaseUri { get; set; } - - public LightningConnectionType ConnectionType - { - get - { - return BaseUri.Scheme == "http" || BaseUri.Scheme == "https" ? LightningConnectionType.Charge - : LightningConnectionType.CLightning; - } - } - - public Uri ToUri(bool withCredentials) - { - if (withCredentials) - { - return new UriBuilder(BaseUri) { UserName = Username ?? "", Password = Password ?? "" }.Uri; - } - else - { - return BaseUri; - } - } - - public override string ToString() - { - return ToUri(true).AbsoluteUri; - } } }