Ejemplo n.º 1
0
        public async Task <JsonResult> Invoice(string cryptoCode, [FromBody] InvoiceCreationOption option)
        {
            var n       = _networkProvider.GetByCryptoCode(cryptoCode);
            var invoice = await _invoiceRepository.GetNewInvoice(n, option);

            var resp = new InvoiceResponse {
                Invoice = invoice
            };

            return(new JsonResult(resp, _repositoryProvider.GetSerializer(n).Options));
        }
Ejemplo n.º 2
0
 public Task <InvoiceResponse> GetInvoiceAsync(InvoiceCreationOption option)
 {
     return(RequestAsync <InvoiceResponse>($"/v1/payment/{cryptoCode}/invoice", HttpMethod.Post, option));
 }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        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);
        }