/// <summary> /// Connect a customer LN node to the merchant LN node /// </summary> public void PrepareLightning(LightningConnectionType lndBackend) { ILightningInvoiceClient client = MerchantCharge.Client; if (lndBackend == LightningConnectionType.LndREST) { client = MerchantLnd.Client; } PrepareLightningAsync(client).GetAwaiter().GetResult(); }
/// <summary> /// Connect a customer LN node to the merchant LN node /// </summary> /// <returns></returns> private async Task PrepareLightningAsync(ILightningInvoiceClient client) { bool awaitingLocking = false; while (true) { var merchantInfo = await WaitLNSynched(client, CustomerLightningD, MerchantLightningD); var peers = await CustomerLightningD.ListPeersAsync(); var filteringToTargetedPeers = peers.Where(a => a.Id == merchantInfo.NodeId); var channel = filteringToTargetedPeers .SelectMany(p => p.Channels) .Where(c => !SKIPPED_STATES.Contains(c.State ?? "")) .FirstOrDefault(); switch (channel?.State) { case null: var address = await CustomerLightningD.NewAddressAsync(); await ExplorerNode.SendToAddressAsync(address, Money.Coins(0.5m)); ExplorerNode.Generate(1); await WaitLNSynched(client, CustomerLightningD, MerchantLightningD); await Task.Delay(1000); var merchantNodeInfo = new NodeInfo(merchantInfo.NodeId, merchantInfo.Address, merchantInfo.P2PPort); await CustomerLightningD.ConnectAsync(merchantNodeInfo); await CustomerLightningD.FundChannelAsync(merchantNodeInfo, Money.Satoshis(16777215)); break; case "CHANNELD_AWAITING_LOCKIN": ExplorerNode.Generate(awaitingLocking ? 1 : 10); await WaitLNSynched(client, CustomerLightningD, MerchantLightningD); awaitingLocking = true; break; case "CHANNELD_NORMAL": return; default: throw new NotSupportedException(channel?.State ?? ""); } } }
private async Task <LightningNodeInformation> WaitLNSynchedCore(int blockCount, ILightningInvoiceClient client) { var merchantInfo = await client.GetInfo(); if (merchantInfo.BlockHeight == blockCount) { return(merchantInfo); } return(null); }