6
6
//! <https://github.com/nostr-protocol/nips/blob/master/47.md>
7
7
8
8
use alloc:: string:: String ;
9
+ use alloc:: vec:: Vec ;
9
10
use alloc:: { borrow:: Cow , string:: ToString } ;
10
11
use core:: fmt;
11
12
use core:: str:: FromStr ;
@@ -125,12 +126,21 @@ pub enum Method {
125
126
/// Pay Invoice
126
127
#[ serde( rename = "pay_invoice" ) ]
127
128
PayInvoice ,
129
+ /// Pay Keysend
130
+ #[ serde( rename = "pay_keysend" ) ]
131
+ PayKeysend ,
128
132
/// Make Invoice
129
133
#[ serde( rename = "make_invoice" ) ]
130
134
MakeInvoice ,
131
135
/// Lookup Invoice
132
136
#[ serde( rename = "lookup_invoice" ) ]
133
137
LookupInvoice ,
138
+ /// List Invoices
139
+ #[ serde( rename = "list_invoices" ) ]
140
+ ListInvoices ,
141
+ /// List Payments
142
+ #[ serde( rename = "list_payments" ) ]
143
+ ListPayments ,
134
144
/// Get Balance
135
145
#[ serde( rename = "get_balance" ) ]
136
146
GetBalance ,
@@ -141,10 +151,16 @@ pub enum Method {
141
151
pub enum RequestParams {
142
152
/// Pay Invoice
143
153
PayInvoice ( PayInvoiceRequestParams ) ,
154
+ /// Pay Keysend
155
+ PayKeysend ( PayKeysendRequestParams ) ,
144
156
/// Make Invoice
145
157
MakeInvoice ( MakeInvoiceRequestParams ) ,
146
158
/// Lookup Invoice
147
159
LookupInvoice ( LookupInvoiceRequestParams ) ,
160
+ /// List Invoices
161
+ ListInvoices ( ListInvoicesRequestParams ) ,
162
+ /// List Payments
163
+ ListPayments ( ListPaymentsRequestParams ) ,
148
164
/// Get Balance
149
165
GetBalance ,
150
166
}
@@ -156,8 +172,11 @@ impl Serialize for RequestParams {
156
172
{
157
173
match self {
158
174
RequestParams :: PayInvoice ( p) => p. serialize ( serializer) ,
175
+ RequestParams :: PayKeysend ( p) => p. serialize ( serializer) ,
159
176
RequestParams :: MakeInvoice ( p) => p. serialize ( serializer) ,
160
177
RequestParams :: LookupInvoice ( p) => p. serialize ( serializer) ,
178
+ RequestParams :: ListInvoices ( p) => p. serialize ( serializer) ,
179
+ RequestParams :: ListPayments ( p) => p. serialize ( serializer) ,
161
180
RequestParams :: GetBalance => serializer. serialize_none ( ) ,
162
181
}
163
182
}
@@ -170,6 +189,35 @@ pub struct PayInvoiceRequestParams {
170
189
pub invoice : String ,
171
190
}
172
191
192
+ /// TLVs to be added to the keysend payment
193
+ #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Eq ) ]
194
+ pub struct KeysendTLVRecord {
195
+ /// TLV type
196
+ #[ serde( rename = "type" ) ]
197
+ pub type_ : u64 ,
198
+ /// TLV value
199
+ pub value : String ,
200
+ }
201
+
202
+ /// Pay Invoice Request Params
203
+ #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Eq ) ]
204
+ pub struct PayKeysendRequestParams {
205
+ /// Amount in millisatoshis
206
+ pub amount : i64 ,
207
+ /// Receiver's node id
208
+ pub pubkey : String ,
209
+ /// Optional message
210
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
211
+ pub message : Option < String > ,
212
+ /// Optional preimage
213
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
214
+ pub preimage : Option < String > ,
215
+ /// Optional TLVs to be added to the keysend payment
216
+ #[ serde( default ) ]
217
+ #[ serde( skip_serializing_if = "Vec::is_empty" ) ]
218
+ pub tlv_records : Vec < KeysendTLVRecord > ,
219
+ }
220
+
173
221
/// Make Invoice Request Params
174
222
#[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Eq ) ]
175
223
pub struct MakeInvoiceRequestParams {
@@ -179,6 +227,8 @@ pub struct MakeInvoiceRequestParams {
179
227
pub description : Option < String > ,
180
228
/// Invoice description hash
181
229
pub description_hash : Option < String > ,
230
+ /// Preimage to be used for the invoice
231
+ pub preimage : Option < String > ,
182
232
/// Invoice expiry in seconds
183
233
pub expiry : Option < i64 > ,
184
234
}
@@ -192,6 +242,43 @@ pub struct LookupInvoiceRequestParams {
192
242
pub bolt11 : Option < String > ,
193
243
}
194
244
245
+ /// List Invoice Request Params
246
+ #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Eq ) ]
247
+ pub struct ListInvoicesRequestParams {
248
+ /// Starting timestamp in seconds since epoch
249
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
250
+ pub from : Option < u64 > ,
251
+ /// Ending timestamp in seconds since epoch
252
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
253
+ pub until : Option < u64 > ,
254
+ /// Number of invoices to return
255
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
256
+ pub limit : Option < u64 > ,
257
+ /// Offset of the first invoice to return
258
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
259
+ pub offset : Option < u64 > ,
260
+ /// If true, include unpaid invoices
261
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
262
+ pub unpaid : Option < bool > ,
263
+ }
264
+
265
+ /// List Payments Request Params
266
+ #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Eq ) ]
267
+ pub struct ListPaymentsRequestParams {
268
+ /// Starting timestamp in seconds since epoch
269
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
270
+ pub from : Option < u64 > ,
271
+ /// Ending timestamp in seconds since epoch
272
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
273
+ pub until : Option < u64 > ,
274
+ /// Number of invoices to return
275
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
276
+ pub limit : Option < u64 > ,
277
+ /// Offset of the first invoice to return
278
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
279
+ pub offset : Option < u64 > ,
280
+ }
281
+
195
282
/// NIP47 Request
196
283
#[ derive( Debug , Clone , Serialize , PartialEq , Eq ) ]
197
284
pub struct Request {
@@ -233,6 +320,10 @@ impl Request {
233
320
let params: PayInvoiceRequestParams = serde_json:: from_value ( template. params ) ?;
234
321
RequestParams :: PayInvoice ( params)
235
322
}
323
+ Method :: PayKeysend => {
324
+ let params: PayKeysendRequestParams = serde_json:: from_value ( template. params ) ?;
325
+ RequestParams :: PayKeysend ( params)
326
+ }
236
327
Method :: MakeInvoice => {
237
328
let params: MakeInvoiceRequestParams = serde_json:: from_value ( template. params ) ?;
238
329
RequestParams :: MakeInvoice ( params)
@@ -241,6 +332,14 @@ impl Request {
241
332
let params: LookupInvoiceRequestParams = serde_json:: from_value ( template. params ) ?;
242
333
RequestParams :: LookupInvoice ( params)
243
334
}
335
+ Method :: ListInvoices => {
336
+ let params: ListInvoicesRequestParams = serde_json:: from_value ( template. params ) ?;
337
+ RequestParams :: ListInvoices ( params)
338
+ }
339
+ Method :: ListPayments => {
340
+ let params: ListPaymentsRequestParams = serde_json:: from_value ( template. params ) ?;
341
+ RequestParams :: ListPayments ( params)
342
+ }
244
343
Method :: GetBalance => RequestParams :: GetBalance ,
245
344
} ;
246
345
@@ -273,6 +372,15 @@ pub struct PayInvoiceResponseResult {
273
372
pub preimage : String ,
274
373
}
275
374
375
+ /// NIP47 Response Result
376
+ #[ derive( Debug , Clone , Eq , PartialEq , Serialize , Deserialize ) ]
377
+ pub struct PayKeysendResponseResult {
378
+ /// Response preimage
379
+ pub preimage : String ,
380
+ /// Payment hash
381
+ pub payment_hash : String ,
382
+ }
383
+
276
384
/// NIP47 Response Result
277
385
#[ derive( Debug , Clone , Eq , PartialEq , Serialize , Deserialize ) ]
278
386
pub struct MakeInvoiceResponseResult {
@@ -291,6 +399,15 @@ pub struct LookupInvoiceResponseResult {
291
399
pub paid : bool ,
292
400
}
293
401
402
+ /// NIP47 Response Result
403
+ #[ derive( Debug , Clone , Eq , PartialEq , Serialize , Deserialize ) ]
404
+ pub struct ListPaymentResponseResult {
405
+ /// Bolt11 invoice
406
+ pub invoice : String ,
407
+ /// Preimage for the payment
408
+ pub preimage : Option < String > ,
409
+ }
410
+
294
411
#[ derive( Debug , Clone , Eq , PartialEq , Serialize , Deserialize ) ]
295
412
#[ serde( rename_all = "lowercase" ) ]
296
413
/// Budget renewal type
@@ -323,10 +440,16 @@ pub struct GetBalanceResponseResult {
323
440
pub enum ResponseResult {
324
441
/// Pay Invoice
325
442
PayInvoice ( PayInvoiceResponseResult ) ,
443
+ /// Pay Keysend
444
+ PayKeysend ( PayKeysendResponseResult ) ,
326
445
/// Make Invoice
327
446
MakeInvoice ( MakeInvoiceResponseResult ) ,
328
447
/// Lookup Invoice
329
448
LookupInvoice ( LookupInvoiceResponseResult ) ,
449
+ /// List Invoices
450
+ ListInvoices ( Vec < LookupInvoiceResponseResult > ) ,
451
+ /// List Payments
452
+ ListPayments ( Vec < ListPaymentResponseResult > ) ,
330
453
/// Get Balance
331
454
GetBalance ( GetBalanceResponseResult ) ,
332
455
}
@@ -338,8 +461,11 @@ impl Serialize for ResponseResult {
338
461
{
339
462
match self {
340
463
ResponseResult :: PayInvoice ( p) => p. serialize ( serializer) ,
464
+ ResponseResult :: PayKeysend ( p) => p. serialize ( serializer) ,
341
465
ResponseResult :: MakeInvoice ( p) => p. serialize ( serializer) ,
342
466
ResponseResult :: LookupInvoice ( p) => p. serialize ( serializer) ,
467
+ ResponseResult :: ListInvoices ( p) => p. serialize ( serializer) ,
468
+ ResponseResult :: ListPayments ( p) => p. serialize ( serializer) ,
343
469
ResponseResult :: GetBalance ( p) => p. serialize ( serializer) ,
344
470
}
345
471
}
@@ -386,6 +512,10 @@ impl Response {
386
512
let result: PayInvoiceResponseResult = serde_json:: from_value ( result) ?;
387
513
ResponseResult :: PayInvoice ( result)
388
514
}
515
+ Method :: PayKeysend => {
516
+ let result: PayKeysendResponseResult = serde_json:: from_value ( result) ?;
517
+ ResponseResult :: PayKeysend ( result)
518
+ }
389
519
Method :: MakeInvoice => {
390
520
let result: MakeInvoiceResponseResult = serde_json:: from_value ( result) ?;
391
521
ResponseResult :: MakeInvoice ( result)
@@ -394,6 +524,14 @@ impl Response {
394
524
let result: LookupInvoiceResponseResult = serde_json:: from_value ( result) ?;
395
525
ResponseResult :: LookupInvoice ( result)
396
526
}
527
+ Method :: ListInvoices => {
528
+ let result: Vec < LookupInvoiceResponseResult > = serde_json:: from_value ( result) ?;
529
+ ResponseResult :: ListInvoices ( result)
530
+ }
531
+ Method :: ListPayments => {
532
+ let result: Vec < ListPaymentResponseResult > = serde_json:: from_value ( result) ?;
533
+ ResponseResult :: ListPayments ( result)
534
+ }
397
535
Method :: GetBalance => {
398
536
let result: GetBalanceResponseResult = serde_json:: from_value ( result) ?;
399
537
ResponseResult :: GetBalance ( result)
0 commit comments