Skip to content

Commit 382cef3

Browse files
committed
Merge #171: Add remaining new NWC functions
2 parents 5919e9e + 99345e0 commit 382cef3

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

crates/nostr/src/nips/nip47.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! <https://github.com/nostr-protocol/nips/blob/master/47.md>
77
88
use alloc::string::String;
9+
use alloc::vec::Vec;
910
use alloc::{borrow::Cow, string::ToString};
1011
use core::fmt;
1112
use core::str::FromStr;
@@ -125,12 +126,21 @@ pub enum Method {
125126
/// Pay Invoice
126127
#[serde(rename = "pay_invoice")]
127128
PayInvoice,
129+
/// Pay Keysend
130+
#[serde(rename = "pay_keysend")]
131+
PayKeysend,
128132
/// Make Invoice
129133
#[serde(rename = "make_invoice")]
130134
MakeInvoice,
131135
/// Lookup Invoice
132136
#[serde(rename = "lookup_invoice")]
133137
LookupInvoice,
138+
/// List Invoices
139+
#[serde(rename = "list_invoices")]
140+
ListInvoices,
141+
/// List Payments
142+
#[serde(rename = "list_payments")]
143+
ListPayments,
134144
/// Get Balance
135145
#[serde(rename = "get_balance")]
136146
GetBalance,
@@ -141,10 +151,16 @@ pub enum Method {
141151
pub enum RequestParams {
142152
/// Pay Invoice
143153
PayInvoice(PayInvoiceRequestParams),
154+
/// Pay Keysend
155+
PayKeysend(PayKeysendRequestParams),
144156
/// Make Invoice
145157
MakeInvoice(MakeInvoiceRequestParams),
146158
/// Lookup Invoice
147159
LookupInvoice(LookupInvoiceRequestParams),
160+
/// List Invoices
161+
ListInvoices(ListInvoicesRequestParams),
162+
/// List Payments
163+
ListPayments(ListPaymentsRequestParams),
148164
/// Get Balance
149165
GetBalance,
150166
}
@@ -156,8 +172,11 @@ impl Serialize for RequestParams {
156172
{
157173
match self {
158174
RequestParams::PayInvoice(p) => p.serialize(serializer),
175+
RequestParams::PayKeysend(p) => p.serialize(serializer),
159176
RequestParams::MakeInvoice(p) => p.serialize(serializer),
160177
RequestParams::LookupInvoice(p) => p.serialize(serializer),
178+
RequestParams::ListInvoices(p) => p.serialize(serializer),
179+
RequestParams::ListPayments(p) => p.serialize(serializer),
161180
RequestParams::GetBalance => serializer.serialize_none(),
162181
}
163182
}
@@ -170,6 +189,35 @@ pub struct PayInvoiceRequestParams {
170189
pub invoice: String,
171190
}
172191

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+
173221
/// Make Invoice Request Params
174222
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
175223
pub struct MakeInvoiceRequestParams {
@@ -179,6 +227,8 @@ pub struct MakeInvoiceRequestParams {
179227
pub description: Option<String>,
180228
/// Invoice description hash
181229
pub description_hash: Option<String>,
230+
/// Preimage to be used for the invoice
231+
pub preimage: Option<String>,
182232
/// Invoice expiry in seconds
183233
pub expiry: Option<i64>,
184234
}
@@ -192,6 +242,43 @@ pub struct LookupInvoiceRequestParams {
192242
pub bolt11: Option<String>,
193243
}
194244

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+
195282
/// NIP47 Request
196283
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
197284
pub struct Request {
@@ -233,6 +320,10 @@ impl Request {
233320
let params: PayInvoiceRequestParams = serde_json::from_value(template.params)?;
234321
RequestParams::PayInvoice(params)
235322
}
323+
Method::PayKeysend => {
324+
let params: PayKeysendRequestParams = serde_json::from_value(template.params)?;
325+
RequestParams::PayKeysend(params)
326+
}
236327
Method::MakeInvoice => {
237328
let params: MakeInvoiceRequestParams = serde_json::from_value(template.params)?;
238329
RequestParams::MakeInvoice(params)
@@ -241,6 +332,14 @@ impl Request {
241332
let params: LookupInvoiceRequestParams = serde_json::from_value(template.params)?;
242333
RequestParams::LookupInvoice(params)
243334
}
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+
}
244343
Method::GetBalance => RequestParams::GetBalance,
245344
};
246345

@@ -273,6 +372,15 @@ pub struct PayInvoiceResponseResult {
273372
pub preimage: String,
274373
}
275374

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+
276384
/// NIP47 Response Result
277385
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
278386
pub struct MakeInvoiceResponseResult {
@@ -291,6 +399,15 @@ pub struct LookupInvoiceResponseResult {
291399
pub paid: bool,
292400
}
293401

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+
294411
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
295412
#[serde(rename_all = "lowercase")]
296413
/// Budget renewal type
@@ -323,10 +440,16 @@ pub struct GetBalanceResponseResult {
323440
pub enum ResponseResult {
324441
/// Pay Invoice
325442
PayInvoice(PayInvoiceResponseResult),
443+
/// Pay Keysend
444+
PayKeysend(PayKeysendResponseResult),
326445
/// Make Invoice
327446
MakeInvoice(MakeInvoiceResponseResult),
328447
/// Lookup Invoice
329448
LookupInvoice(LookupInvoiceResponseResult),
449+
/// List Invoices
450+
ListInvoices(Vec<LookupInvoiceResponseResult>),
451+
/// List Payments
452+
ListPayments(Vec<ListPaymentResponseResult>),
330453
/// Get Balance
331454
GetBalance(GetBalanceResponseResult),
332455
}
@@ -338,8 +461,11 @@ impl Serialize for ResponseResult {
338461
{
339462
match self {
340463
ResponseResult::PayInvoice(p) => p.serialize(serializer),
464+
ResponseResult::PayKeysend(p) => p.serialize(serializer),
341465
ResponseResult::MakeInvoice(p) => p.serialize(serializer),
342466
ResponseResult::LookupInvoice(p) => p.serialize(serializer),
467+
ResponseResult::ListInvoices(p) => p.serialize(serializer),
468+
ResponseResult::ListPayments(p) => p.serialize(serializer),
343469
ResponseResult::GetBalance(p) => p.serialize(serializer),
344470
}
345471
}
@@ -386,6 +512,10 @@ impl Response {
386512
let result: PayInvoiceResponseResult = serde_json::from_value(result)?;
387513
ResponseResult::PayInvoice(result)
388514
}
515+
Method::PayKeysend => {
516+
let result: PayKeysendResponseResult = serde_json::from_value(result)?;
517+
ResponseResult::PayKeysend(result)
518+
}
389519
Method::MakeInvoice => {
390520
let result: MakeInvoiceResponseResult = serde_json::from_value(result)?;
391521
ResponseResult::MakeInvoice(result)
@@ -394,6 +524,14 @@ impl Response {
394524
let result: LookupInvoiceResponseResult = serde_json::from_value(result)?;
395525
ResponseResult::LookupInvoice(result)
396526
}
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+
}
397535
Method::GetBalance => {
398536
let result: GetBalanceResponseResult = serde_json::from_value(result)?;
399537
ResponseResult::GetBalance(result)

0 commit comments

Comments
 (0)