Skip to content

Commit

Permalink
Support lnurl channel requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Nov 29, 2024
1 parent d1405d6 commit 386dd80
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions BTCPayApp.UI/BTCPayApp.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="blazor-dragdrop" Version="2.6.1" />
<PackageReference Include="Fluxor.Blazor.Web" Version="6.1.0" />
<PackageReference Include="Fluxor.Blazor.Web.ReduxDevTools" Version="6.1.0" />
<PackageReference Include="LNURL" Version="0.0.36" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="8.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="8.0.11" />
Expand Down
57 changes: 52 additions & 5 deletions BTCPayApp.UI/Pages/Settings/LightningChannelsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@using BTCPayApp.Core.LDK
@using BTCPayApp.Core.Wallet
@using BTCPayServer.Lightning
@using LNURL
@using NBitcoin
@using org.ldk.enums
@using org.ldk.structs
Expand All @@ -17,6 +18,7 @@
@inject IState<StoreState> StoreState
@inject IState<UIState> UIState
@inject IDispatcher Dispatcher
@inject IHttpClientFactory HttpClientFactory

<PageTitle>Peers and Channels</PageTitle>

Expand Down Expand Up @@ -61,9 +63,9 @@
</button>
<button type="button" class="btn btn-outline-primary btn-sm" @onclick="() => UpdatePeer(peer.nodeId, peerConfig with { Trusted = !peerConfig.Trusted })">@(peerConfig.Trusted? "Trusted": "Untrusted")</button>
}
else if (peer.socket is not null)
else if (peer.socket is not null && EndPointParser.TryParse(peer.socket, 9735, out var endpoint))
{
<button type="button" class="btn btn-outline-primary btn-sm" @onclick="() => UpdatePeer(peer.nodeId, new PeerInfo { Endpoint = peer.socket })">Remember peer</button>
<button type="button" class="btn btn-outline-primary btn-sm" @onclick="() => UpdatePeer(peer.nodeId, new PeerInfo { Endpoint = endpoint })">Remember peer</button>
}
</div>
</div>
Expand All @@ -79,7 +81,7 @@
<label>Node ID</label>
</div>
<div class="form-floating">
<TruncateCenter Text="@peerConfig.Endpoint" Padding="30" Copy="true" Elastic="true" class="form-control-plaintext"/>
<TruncateCenter Text="@peerConfig.Endpoint?.ToEndpointString()" Padding="30" Copy="true" Elastic="true" class="form-control-plaintext"/>
<label>Socket</label>
</div>
@if (!string.IsNullOrEmpty(peerConfig.Label))
Expand Down Expand Up @@ -456,8 +458,7 @@
await InvokeAsync(StateHasChanged);
await _semaphore.WaitAsync();

EndPointParser.TryParse(peerConfig.Endpoint, 9735, out var endpoint);
if (endpoint != null) await Node.PeerHandler.ConnectAsync(new PubKey(peer), endpoint);
if (peerConfig.Endpoint != null) await Node.PeerHandler.ConnectAsync(new PubKey(peer), peerConfig.Endpoint);
}
finally
{
Expand Down Expand Up @@ -504,6 +505,52 @@
_ = FetchData();
}

private async Task ParseChannelRequest(string lnurl, string name, bool trusted)
{
if (Loading || Node == null || ChannelModel?.Amount is null || string.IsNullOrEmpty(ChannelModel.PeerId)) return;
try
{
ChannelSuccessMessage = ChannelErrorMessage = null;
ChannelOpening = true;
await InvokeAsync(StateHasChanged);

var uri = LNURL.Parse(lnurl, out var tag);
var http = HttpClientFactory.CreateClient();
var channelRequest = (LNURLChannelRequest) await LNURL.FetchInformation(uri, tag, http, new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);
if (channelRequest is null)
{
ChannelErrorMessage = "The channel request is invalid";
return;
}


if (!EndPointParser.TryParse($"{channelRequest.Uri.Host}:{channelRequest.Uri.Port}", 9735, out var endpoint))
{
ChannelErrorMessage = "The channel request provided an invalid endpoint for the peer";
return;
}

await Node.Peer(channelRequest.Uri.NodeId, new PeerInfo {Endpoint = endpoint, Label = name, Trusted = trusted});
await ConnectPeer(channelRequest.Uri.NodeId.ToString(), new PeerInfo {Endpoint = endpoint, Label = name, Trusted = trusted});
await channelRequest.SendRequest(Node.NodeId, true, http, new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token);
ChannelSuccessMessage = "Successfully initiated channel creation. Please wait for the peer to execute.";
}
catch (Exception e)
{
ChannelErrorMessage = e.Message;
}
finally
{
if(ChannelSuccessMessage is null && ChannelErrorMessage is null)
ChannelErrorMessage = "Error opening channel";
_semaphore.Release();
ChannelOpening = false;
await InvokeAsync(StateHasChanged);
}
if (string.IsNullOrEmpty(ChannelErrorMessage))
_ = FetchData();
}

private void ToggleDisplayCurrency()
{
if (ChannelModel?.Amount.HasValue is true)
Expand Down

0 comments on commit 386dd80

Please sign in to comment.