Adding Lnd to connection types and supporting parsing

This commit is contained in:
rockstardev 2018-05-14 15:54:44 -05:00
parent 7c29cb62ef
commit 789b9168ad
3 changed files with 53 additions and 43 deletions

View file

@ -328,6 +328,13 @@ namespace BTCPayServer.Tests
public void CanParseLightningURL() public void CanParseLightningURL()
{ {
LightningConnectionString conn = null; 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.True(LightningConnectionString.TryParse("/test/a", out conn));
Assert.Equal("unix://test/a", conn.ToString()); Assert.Equal("unix://test/a", conn.ToString());
Assert.Equal("unix://test/a", conn.ToUri(true).AbsoluteUri); Assert.Equal("unix://test/a", conn.ToUri(true).AbsoluteUri);

View file

@ -79,7 +79,7 @@ namespace BTCPayServer.Tests.UnitTests
public async Task<LnrpcChannel> EnsureLightningChannelAsync() public async Task<LnrpcChannel> EnsureLightningChannelAsync()
{ {
var merchantInfo = await WaitLNSynched(); var merchantInfo = await WaitLNSynched();
var merchantAddress = new LnrpcLightningAddress var merchantNodeAddress = new LnrpcLightningAddress
{ {
Pubkey = merchantInfo.NodeId, Pubkey = merchantInfo.NodeId,
Host = "merchant_lnd:9735" Host = "merchant_lnd:9735"
@ -90,7 +90,7 @@ namespace BTCPayServer.Tests.UnitTests
// if channel is pending generate blocks until confirmed // if channel is pending generate blocks until confirmed
var pendingResponse = await CustomerLnd.PendingChannelsAsync(); var pendingResponse = await CustomerLnd.PendingChannelsAsync();
if (pendingResponse.Pending_open_channels? 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); ExplorerNode.Generate(1);
await WaitLNSynched(); await WaitLNSynched();
@ -100,7 +100,7 @@ namespace BTCPayServer.Tests.UnitTests
// check if channel is established // check if channel is established
var chanResponse = await CustomerLnd.ListChannelsAsync(null, null, null, null); var chanResponse = await CustomerLnd.ListChannelsAsync(null, null, null, null);
var channelToMerchant = chanResponse?.Channels var channelToMerchant = chanResponse?.Channels
.Where(a => a.Remote_pubkey == merchantAddress.Pubkey) .Where(a => a.Remote_pubkey == merchantNodeAddress.Pubkey)
.FirstOrDefault(); .FirstOrDefault();
if (channelToMerchant == null) if (channelToMerchant == null)
@ -111,7 +111,7 @@ namespace BTCPayServer.Tests.UnitTests
{ {
var connectResp = await CustomerLnd.ConnectPeerAsync(new LnrpcConnectPeerRequest var connectResp = await CustomerLnd.ConnectPeerAsync(new LnrpcConnectPeerRequest
{ {
Addr = merchantAddress Addr = merchantNodeAddress
}); });
} }

View file

@ -8,21 +8,48 @@ namespace BTCPayServer.Payments.Lightning
public enum LightningConnectionType public enum LightningConnectionType
{ {
Charge, Charge,
CLightning CLightning,
Lnd
} }
public class LightningConnectionString 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) public static bool TryParse(string str, out LightningConnectionString connectionString)
{ {
return TryParse(str, out connectionString, out var error); return TryParse(str, out connectionString, out var error);
} }
public static bool TryParse(string str, out LightningConnectionString connectionString, out string error) public static bool TryParse(string str, out LightningConnectionString connectionString, out string error)
{ {
if (str == null) if (str == null)
throw new ArgumentNullException(nameof(str)); throw new ArgumentNullException(nameof(str));
if (str.StartsWith('/')) if (str.StartsWith('/'))
str = "unix:" + str; str = "unix:" + str;
var result = new LightningConnectionString();
connectionString = null; connectionString = null;
error = null; error = null;
@ -51,7 +78,16 @@ namespace BTCPayServer.Payments.Lightning
uri = new Uri("unix://" + str, UriKind.Absolute); 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(':'); var parts = uri.UserInfo.Split(':');
if (string.IsNullOrEmpty(uri.UserInfo) || parts.Length != 2) if (string.IsNullOrEmpty(uri.UserInfo) || parts.Length != 2)
@ -67,44 +103,11 @@ namespace BTCPayServer.Payments.Lightning
error = "The url should not have user information"; error = "The url should not have user information";
return false; 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; connectionString = result;
return true; 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;
}
} }
} }