Skip to content

Commit 2983117

Browse files
authored
Merge pull request #32 from slekrem/fix/nip47-payinvoice-json-property
Fix/nip47 payinvoice json property
2 parents 45dee38 + 427b51b commit 2983117

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

NNostr.Client/NNostr.Client.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<PackageReference Include="LinqKit.Core" Version="1.2.5" />
2727
<PackageReference Include="NBitcoin.Secp256k1" Version="3.1.6" />
2828
<PackageReference Include="System.Interactive.Async" Version="6.0.1" />
29-
<PackageReference Include="System.Text.Json" Version="8.0.4" />
29+
<PackageReference Include="System.Text.Json" Version="8.0.5" />
3030
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
3131
</ItemGroup>
3232
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">

NNostr.Client/Protocols/NIP47.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ public class TlvRecord
395395
public class PayInvoiceRequest : INIP47Request
396396
{
397397
public string Method => "pay_invoice";
398+
399+
[JsonPropertyName("invoice")]
398400
public string Invoice { get; set; }
399401

400402
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]

NNostr.Tests/ClientTests.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Text.Json;
77
using System.Text.Json.Nodes;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using NNostr.Client.Protocols;
1011
using Xunit;
@@ -88,12 +89,53 @@ public async Task CanParseUri()
8889
PaymentHash = "dummy"
8990
});
9091

91-
92-
var x = false;
93-
94-
95-
92+
}
93+
94+
[Fact]
95+
public void PayInvoiceRequest_SerializesCorrectly()
96+
{
97+
// Test for the bug fix - PayInvoiceRequest should include "invoice" in JSON
98+
var request = new NIP47.PayInvoiceRequest()
99+
{
100+
Invoice = "lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcqca784w",
101+
Amount = 1000
102+
};
103+
104+
var nip47Request = ((NIP47.INIP47Request)request).ToNip47Request();
105+
var json = JsonSerializer.Serialize(nip47Request);
106+
107+
// The JSON should contain "invoice" property
108+
Assert.Contains("\"invoice\"", json);
109+
110+
// Verify the structure is correct
111+
var jsonNode = JsonSerializer.Deserialize<JsonNode>(json);
112+
Assert.Equal("pay_invoice", jsonNode!["method"]!.GetValue<string>());
113+
Assert.Equal(request.Invoice, jsonNode!["params"]!["invoice"]!.GetValue<string>());
114+
Assert.Equal(1000, jsonNode!["params"]!["amount"]!.GetValue<decimal>());
115+
}
116+
117+
[Fact]
118+
public void PayInvoiceRequest_WithoutJsonPropertyName_WouldNotSerializeInvoice()
119+
{
120+
// This test demonstrates what would happen without the [JsonPropertyName("invoice")] attribute
121+
// We create a request object and verify that the JSON includes the invoice field
122+
var request = new NIP47.PayInvoiceRequest()
123+
{
124+
Invoice = "lnbc1test",
125+
Amount = 500
126+
};
127+
128+
var nip47Request = ((NIP47.INIP47Request)request).ToNip47Request();
129+
var json = JsonSerializer.Serialize(nip47Request);
130+
131+
// With the fix, "invoice" should be present in the JSON
132+
Assert.Contains("\"invoice\"", json);
96133

134+
// Parse and verify the invoice is properly serialized
135+
var jsonNode = JsonSerializer.Deserialize<JsonNode>(json);
136+
var invoiceValue = jsonNode!["params"]!["invoice"];
137+
Assert.NotNull(invoiceValue);
138+
Assert.Equal("lnbc1test", invoiceValue!.GetValue<string>());
97139
}
98140
}
99141

0 commit comments

Comments
 (0)