public async Task <(PaymentReceivedType, LNMoney)> PaymentReceived(Primitives.PaymentHash paymentHash, LNMoney amount, uint256?secret = null) { var tx = await _engine.OpenTransaction(); using var invoiceRow = await tx.GetTable(DBKeys.HashToInvoice).Get(paymentHash.ToBytes(false)); if (invoiceRow != null) { var res = PaymentRequest.Parse(await invoiceRow.ReadValueString()); if (res.IsError) { throw new NRustLightningException($"Failed to read payment request for {res.ErrorValue}"); } var req = res.ResultValue; if (req.AmountValue is null) { return(PaymentReceivedType.Ok, amount); } var intendedAmount = req.AmountValue.Value; if (amount.MilliSatoshi < intendedAmount.MilliSatoshi) { return(PaymentReceivedType.AmountTooLow, intendedAmount); } if (intendedAmount.MilliSatoshi * 2 < amount.MilliSatoshi) { return(PaymentReceivedType.AmountTooHigh, intendedAmount); } return(PaymentReceivedType.Ok, intendedAmount); } return(PaymentReceivedType.UnknownPaymentHash, amount); }
public async Task <IActionResult> Pay(string bolt11Invoice, long?amountMSat, CancellationToken ct) { var r = PaymentRequest.Parse(bolt11Invoice); if (r.IsError) { return(BadRequest($"Failed to parse invoice ({bolt11Invoice}): " + r.ErrorValue)); } var invoice = r.ResultValue; try { await _invoiceService.PayInvoice(invoice, amountMSat, ct); } catch (NRustLightningException ex) { return(BadRequest(ex.Message)); } catch (FFIException ex) { return(BadRequest(ex.Message)); } return(Ok()); }
public async Task <PaymentRequest?> GetInvoice(Primitives.PaymentHash hash, CancellationToken ct = default) { if (hash == null) { throw new ArgumentNullException(nameof(hash)); } using var tx = await _engine.OpenTransaction(ct); using var row = await tx.GetTable(DBKeys.HashToInvoice).Get(hash.Value.ToString()); if (row is null) { return(null); } var r = PaymentRequest.Parse((await row.ReadValueString())); if (r.IsError) { _logger.LogError($"Failed to get invoice for hash {hash}. {r.ErrorValue}"); return(null); } return(r.ResultValue); }
public void CanConvertJsonTypes() { var invoice = "lnbc1p0vhtzvpp5akajlfqdj6ek7eeh4kae6gc05fz9j99n8jadatqt4fmlwwxwx4zsnp4q2uqg2j52gxtxg5d0v928h5pll95ynsaek2csgfg26tvuzydgjrwgdqhdehjqer9wd3hy6tsw35k7msna3vtx"; var paymentRequest = PaymentRequest.Parse(invoice); var resp = new InvoiceResponse() { Invoice = paymentRequest.ResultValue }; var j = JsonSerializer.Serialize(resp); JsonSerializer.Deserialize <InvoiceResponse>(j); var invoiceResponseRaw = "{\"invoice\":\"lnbc1p0vma42pp5t2v5ehyay3x9g8769gqkrhmdlqjq0kc8ksqfxu3xjw7s2y96jegqnp4q2uqg2j52gxtxg5d0v928h5pll95ynsaek2csgfg26tvuzydgjrwgdqhdehjqer9wd3hy6tsw35k7ms3xhenl\"}"; JsonSerializer.Deserialize <InvoiceResponse>(invoiceResponseRaw); var conf = UserConfig.GetDefault(); j = JsonSerializer.Serialize(conf); var v = JsonSerializer.Deserialize <UserConfig>(j); Assert.Equal(conf.ChannelOptions.AnnouncedChannel, v.ChannelOptions.AnnouncedChannel); var openChannelRequest = new OpenChannelRequest(); j = JsonSerializer.Serialize(openChannelRequest); var conv = JsonSerializer.Deserialize <OpenChannelRequest>(j); Assert.Equal(openChannelRequest.OverrideConfig, conv.OverrideConfig); // with custom config openChannelRequest.OverrideConfig = UserConfig.GetDefault(); j = JsonSerializer.Serialize(openChannelRequest); // Don't know why but we must specify option here. var opt = new JsonSerializerOptions(); opt.Converters.Add(new NullableStructConverterFactory()); conv = JsonSerializer.Deserialize <OpenChannelRequest>(j, opt); Assert.True(conv.OverrideConfig.HasValue); Assert.Equal(openChannelRequest.OverrideConfig.Value.ChannelOptions.AnnouncedChannel, conv.OverrideConfig.Value.ChannelOptions.AnnouncedChannel); j = "{\"TheirNetworkKey\":\"024a8b7fc86957537bb365cc0242255582d3d40a5532489f67e700a89bcac2f010\",\"ChannelValueSatoshis\":100000,\"PushMSat\":1000,\"OverrideConfig\":null}"; openChannelRequest = JsonSerializer.Deserialize <OpenChannelRequest>(j, new JsonSerializerOptions() { Converters = { new HexPubKeyConverter() } }); Assert.Equal(100000UL, openChannelRequest.ChannelValueSatoshis); Assert.Equal(1000UL, openChannelRequest.PushMSat); Assert.NotNull(openChannelRequest.TheirNetworkKey); // wallet info j = "{\"DerivationStrategy\":\"tpubDBte1PdX36pt167AFbKpHwFJqZAVVRuJSadZ49LdkX5JJbJCNDc8JQ7w5GdaDZcUXm2SutgwjRuufwq4q4soePD4fPKSZCUhqDDarKRCUen\",\"OnChainBalanceSatoshis\":0}"; var networkProvider = new NRustLightningNetworkProvider(NetworkType.Regtest); var btcNetwork = networkProvider.GetByCryptoCode("BTC"); var walletInfo = JsonSerializer.Deserialize <WalletInfo>(j, new JsonSerializerOptions { Converters = { new DerivationStrategyJsonConverter(btcNetwork.NbXplorerNetwork.DerivationStrategyFactory) } }); // FeatureBit var featureBit = FeatureBit.TryParse("0b000000100100000100000000").ResultValue; var opts = new JsonSerializerOptions() { Converters = { new FeatureBitJsonConverter() } }; j = JsonSerializer.Serialize(featureBit, opts); Assert.Contains("prettyPrint", j); var featureBit2 = JsonSerializer.Deserialize <FeatureBit>(j, opts); Assert.Equal(featureBit, featureBit2); }