Example #1
0
        public async Task <PaymentRequest> GetNewInvoice(NRustLightningNetwork network, InvoiceCreationOption option)
        {
            if (network == null)
            {
                throw new ArgumentNullException(nameof(network));
            }

            Primitives.PaymentPreimage paymentPreimage = Primitives.PaymentPreimage.Create(RandomUtils.GetBytes(32));

            var nodeId       = Primitives.NodeId.NewNodeId(_keysRepository.GetNodeId());
            var paymentHash  = paymentPreimage.Hash;
            var taggedFields =
                new List <TaggedField>
            {
                TaggedField.NewPaymentHashTaggedField(paymentHash),
                TaggedField.NewNodeIdTaggedField(nodeId),
                (option.EncodeDescriptionWithHash.HasValue && option.EncodeDescriptionWithHash.Value) ?
                TaggedField.NewDescriptionHashTaggedField(new uint256(Hashes.SHA256(Encoding.UTF8.GetBytes(option.Description)), false)) :
                TaggedField.NewDescriptionTaggedField(option.Description),
            };

            if (option.PaymentSecret != null)
            {
                taggedFields.Add(TaggedField.NewPaymentSecretTaggedField(option.PaymentSecret));
            }

            var t = new TaggedFields(taggedFields.ToFSharpList());
            var r = PaymentRequest.TryCreate(network.BOLT11InvoicePrefix, option.Amount.ToFSharpOption(), _systemClock.UtcNow, nodeId, t, _keysRepository.AsMessageSigner());

            if (r.IsError)
            {
                throw new InvalidDataException($"Error when creating our payment request: {r.ErrorValue}");
            }

            _logger.LogDebug($"Publish new invoice with hash {paymentHash}");

            using var tx = await _engine.OpenTransaction();

            var table = tx.GetTable(DBKeys.HashToPreimage);
            await table.Insert(paymentHash.ToBytes(false), paymentPreimage.ToByteArray());

            var table2 = tx.GetTable(DBKeys.HashToInvoice);
            await table2.Insert(paymentHash.ToBytes(false).ToHexString(), r.ResultValue.ToString());

            await tx.Commit();

            return(r.ResultValue);
        }
        public async Task <PaymentRequest> GetNewInvoice(NRustLightningNetwork network, InvoiceCreationOption option, CancellationToken ct = default)
        {
            if (network == null)
            {
                throw new ArgumentNullException(nameof(network));
            }

            Primitives.PaymentPreimage paymentPreimage = Primitives.PaymentPreimage.Create(RandomUtils.GetBytes(32));

            var nodeId       = Primitives.NodeId.NewNodeId(_keysRepository.GetNodeId());
            var paymentHash  = paymentPreimage.Hash;
            var taggedFields =
                new List <TaggedField>
            {
                TaggedField.NewPaymentHashTaggedField(paymentHash),
                TaggedField.NewNodeIdTaggedField(nodeId),
                (option.EncodeDescriptionWithHash.HasValue && option.EncodeDescriptionWithHash.Value) ?
                TaggedField.NewDescriptionHashTaggedField(new uint256(Hashes.SHA256(Encoding.UTF8.GetBytes(option.Description)), false)) :
                TaggedField.NewDescriptionTaggedField(option.Description),
            };

            if (option.PaymentSecret != null)
            {
                taggedFields.Add(TaggedField.NewPaymentSecretTaggedField(option.PaymentSecret));
            }

            var t = new TaggedFields(taggedFields.ToFSharpList());
            var r = PaymentRequest.TryCreate(network.BOLT11InvoicePrefix, option.Amount.ToFSharpOption(), DateTimeOffset.UtcNow, nodeId, t, _keysRepository.AsMessageSigner());

            if (r.IsError)
            {
                throw new InvalidDataException($"Error when creating our payment request: {r.ErrorValue}");
            }

            _logger.LogDebug($"Publish new invoice with hash {paymentHash}");
            await _repository.SetInvoice(r.ResultValue, ct);

            await _repository.SetPreimage(paymentPreimage, ct);

            return(r.ResultValue);
        }
Example #3
0
        public async Task RepositoryTest()
        {
            using var tester = DBTrieRepositoryTester.Create();

            // 1. Check invoice Equality
            var networkProvider = new NRustLightningNetworkProvider(NetworkType.Regtest);
            var network         = networkProvider.GetByCryptoCode("BTC");
            var nodeSecret      = new Key();
            var nodeId          = Primitives.NodeId.NewNodeId(nodeSecret.PubKey);

            Primitives.PaymentPreimage paymentPreimage = Primitives.PaymentPreimage.Create(RandomUtils.GetBytes(32));
            var taggedFields =
                new List <TaggedField>
            {
                TaggedField.NewPaymentHashTaggedField(paymentPreimage.Hash),
                TaggedField.NewNodeIdTaggedField((nodeId)),
                TaggedField.NewDescriptionTaggedField("Test invoice")
            };
            var t        = new TaggedFields(taggedFields.ToFSharpList());
            var invoiceR = PaymentRequest.TryCreate(network.BOLT11InvoicePrefix, FSharpOption <LNMoney> .None, DateTimeOffset.UtcNow, nodeId, t, nodeSecret);

            if (invoiceR.IsError)
            {
                throw new Exception(invoiceR.ErrorValue);
            }
            var invoice = invoiceR.ResultValue;
            await tester.Repository.SetInvoice(invoice);

            var invoice2 = await tester.Repository.GetInvoice(paymentPreimage.Hash);

            Assert.Equal(invoice.ToString(), invoice2.ToString());

            // 2. preimage
            await tester.Repository.SetPreimage(paymentPreimage);

            var preimage2 = await tester.Repository.GetPreimage(paymentPreimage.Hash);

            Assert.Equal(paymentPreimage, preimage2);

            // 3. remote endpoint
            var ipEndpoint   = IPEndPoint.Parse("192.168.0.1:9735");
            var dnsEndPointA = NBitcoin.Utils.ParseEndpoint("lightningd-a:9735", 9735);
            var dnsEndPointB = NBitcoin.Utils.ParseEndpoint("lightningd-b:8888", 9735);
            await tester.Repository.SetRemoteEndPoint(ipEndpoint);

            await tester.Repository.SetRemoteEndPoint(dnsEndPointA);

            await tester.Repository.SetRemoteEndPoint(dnsEndPointB);

            var endpoints = await tester.Repository.GetAllRemoteEndPoint().ToListAsync();

            Assert.Contains(ipEndpoint, endpoints);
            Assert.Contains(dnsEndPointA, endpoints);
            Assert.Contains(dnsEndPointB, endpoints);

            var(chanMan, readArgs, chanMon, monitorReadArgs) = GetTestChannelManager();
            // 4. channel manager
            await tester.Repository.SetChannelManager(chanMan, CancellationToken.None);

            var items = await tester.Repository.GetChannelManager(readArgs);

            Assert.True(items.HasValue);
            var(_, chanMan2) = items.Value;
            Assert.True(chanMan.Serialize(_pool).SequenceEqual(chanMan2.Serialize(_pool)));

            // 5. channel monitor
            await tester.Repository.SetManyChannelMonitor(chanMon);

            var items2 = await tester.Repository.GetManyChannelMonitor(monitorReadArgs);

            Assert.True(items2.HasValue);
            var(chanMon2, _) = items2.Value;
            Assert.True(chanMon.Serialize(_pool).SequenceEqual(chanMon2.Serialize(_pool)));
        }