Ejemplo n.º 1
0
        public static ILightningClient CreateLightningClient(this LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network, LightningNetworkOptions options, LightningClientFactoryService lightningClientFactory)
        {
            var external = supportedPaymentMethod.GetExternalLightningUrl();

            if (external != null)
            {
                return(lightningClientFactory.Create(external, network));
            }
            else
            {
                if (!options.InternalLightningByCryptoCode.TryGetValue(network.CryptoCode, out var connectionString))
                {
                    throw new PaymentMethodUnavailableException("No internal node configured");
                }
                return(lightningClientFactory.Create(connectionString, network));
            }
        }
        public ILightningInvoiceClient CreateClient(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network)
        {
            var uri = supportedPaymentMethod.GetLightningUrl();

            if (uri.ConnectionType == LightningConnectionType.Charge)
            {
                return(new ChargeClient(uri.ToUri(true), network.NBitcoinNetwork));
            }
            else if (uri.ConnectionType == LightningConnectionType.CLightning)
            {
                return(new CLightningRPCClient(uri.ToUri(false), network.NBitcoinNetwork));
            }
            else
            {
                throw new NotSupportedException($"Unsupported connection string for lightning server ({uri.ConnectionType})");
            }
        }
Ejemplo n.º 3
0
        private async Task Listen(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network)
        {
            ILightningInvoiceListener session = null;

            try
            {
                Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Start listening {supportedPaymentMethod.GetLightningUrl().BaseUri}");
                var lightningClient = supportedPaymentMethod.CreateClient(network);
                session = await lightningClient.Listen(_Cts.Token);

                while (true)
                {
                    var notification = await session.WaitInvoice(_Cts.Token);

                    ListenedInvoice listenedInvoice = GetListenedInvoice(notification.Id);
                    if (listenedInvoice == null)
                    {
                        continue;
                    }
                    if (notification.Id == listenedInvoice.PaymentMethodDetails.InvoiceId &&
                        notification.BOLT11 == listenedInvoice.PaymentMethodDetails.BOLT11)
                    {
                        if (notification.Status == LightningInvoiceStatus.Paid &&
                            notification.PaidAt.HasValue && notification.Amount != null)
                        {
                            await AddPayment(network, notification, listenedInvoice);

                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                        if (notification.Status == LightningInvoiceStatus.Expired)
                        {
                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                    }
                }
            }
            catch when(_Cts.IsCancellationRequested)
            {
            }
Ejemplo n.º 4
0
        private async Task Listen(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network)
        {
            try
            {
                Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Start listening {supportedPaymentMethod.GetLightningChargeUrl(false)}");
                var charge  = GetChargeClient(supportedPaymentMethod, network);
                var session = await charge.Listen(_Cts.Token);

                while (true)
                {
                    var notification = await session.NextEvent(_Cts.Token);

                    ListenedInvoice listenedInvoice = GetListenedInvoice(notification.Id);
                    if (listenedInvoice == null)
                    {
                        continue;
                    }

                    if (notification.Id == listenedInvoice.PaymentMethodDetails.InvoiceId &&
                        notification.PaymentRequest == listenedInvoice.PaymentMethodDetails.BOLT11)
                    {
                        if (notification.Status == "paid" && notification.PaidAt.HasValue)
                        {
                            await AddPayment(network, notification, listenedInvoice);

                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                        if (notification.Status == "expired")
                        {
                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                    }
                }
            }
            catch when(_Cts.IsCancellationRequested)
            {
            }
Ejemplo n.º 5
0
        public ILightningInvoiceClient CreateClient(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network)
        {
            var uri = supportedPaymentMethod.GetLightningUrl();

            return(CreateClient(uri, network.NBitcoinNetwork));
        }