From 1f6d5768b3e76c6451a4317df0fd1cf60f8ce9c6 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Thu, 21 Sep 2023 00:18:08 -0500 Subject: [PATCH] Add remaining new NWC functions --- crates/nostr/src/nips/nip47.rs | 135 +++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/crates/nostr/src/nips/nip47.rs b/crates/nostr/src/nips/nip47.rs index 9557d0c36..1543b3eb9 100644 --- a/crates/nostr/src/nips/nip47.rs +++ b/crates/nostr/src/nips/nip47.rs @@ -125,12 +125,21 @@ pub enum Method { /// Pay Invoice #[serde(rename = "pay_invoice")] PayInvoice, + /// Pay Keysend + #[serde(rename = "pay_keysend")] + PayKeysend, /// Make Invoice #[serde(rename = "make_invoice")] MakeInvoice, /// Lookup Invoice #[serde(rename = "lookup_invoice")] LookupInvoice, + /// List Invoices + #[serde(rename = "list_invoices")] + ListInvoices, + /// List Payments + #[serde(rename = "list_payments")] + ListPayments, /// Get Balance #[serde(rename = "get_balance")] GetBalance, @@ -141,10 +150,16 @@ pub enum Method { pub enum RequestParams { /// Pay Invoice PayInvoice(PayInvoiceRequestParams), + /// Pay Keysend + PayKeysend(PayKeysendRequestParams), /// Make Invoice MakeInvoice(MakeInvoiceRequestParams), /// Lookup Invoice LookupInvoice(LookupInvoiceRequestParams), + /// List Invoices + ListInvoices(ListInvoicesRequestParams), + /// List Payments + ListPayments(ListPaymentsRequestParams), /// Get Balance GetBalance, } @@ -156,8 +171,11 @@ impl Serialize for RequestParams { { match self { RequestParams::PayInvoice(p) => p.serialize(serializer), + RequestParams::PayKeysend(p) => p.serialize(serializer), RequestParams::MakeInvoice(p) => p.serialize(serializer), RequestParams::LookupInvoice(p) => p.serialize(serializer), + RequestParams::ListInvoices(p) => p.serialize(serializer), + RequestParams::ListPayments(p) => p.serialize(serializer), RequestParams::GetBalance => serializer.serialize_none(), } } @@ -170,6 +188,35 @@ pub struct PayInvoiceRequestParams { pub invoice: String, } +/// TLVs to be added to the keysend payment +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct KeysendTLVRecord { + /// TLV type + #[serde(rename = "type")] + pub type_: u64, + /// TLV value + pub value: String, +} + +/// Pay Invoice Request Params +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct PayKeysendRequestParams { + /// Amount in millisatoshis + pub amount: i64, + /// Receiver's node id + pub pubkey: String, + /// Optional message + #[serde(skip_serializing_if = "Option::is_none")] + pub message: Option, + /// Optional preimage + #[serde(skip_serializing_if = "Option::is_none")] + pub preimage: Option, + /// Optional TLVs to be added to the keysend payment + #[serde(default)] + #[serde(skip_serializing_if = "Vec::is_empty")] + pub tlv_records: Vec, +} + /// Make Invoice Request Params #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct MakeInvoiceRequestParams { @@ -192,6 +239,43 @@ pub struct LookupInvoiceRequestParams { pub bolt11: Option, } +/// List Invoice Request Params +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct ListInvoicesRequestParams { + /// Starting timestamp in seconds since epoch + #[serde(skip_serializing_if = "Option::is_none")] + pub from: Option, + /// Ending timestamp in seconds since epoch + #[serde(skip_serializing_if = "Option::is_none")] + pub until: Option, + /// Number of invoices to return + #[serde(skip_serializing_if = "Option::is_none")] + pub limit: Option, + /// Offset of the first invoice to return + #[serde(skip_serializing_if = "Option::is_none")] + pub offset: Option, + /// If true, include unpaid invoices + #[serde(skip_serializing_if = "Option::is_none")] + pub unpaid: Option, +} + +/// List Payments Request Params +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct ListPaymentsRequestParams { + /// Starting timestamp in seconds since epoch + #[serde(skip_serializing_if = "Option::is_none")] + pub from: Option, + /// Ending timestamp in seconds since epoch + #[serde(skip_serializing_if = "Option::is_none")] + pub until: Option, + /// Number of invoices to return + #[serde(skip_serializing_if = "Option::is_none")] + pub limit: Option, + /// Offset of the first invoice to return + #[serde(skip_serializing_if = "Option::is_none")] + pub offset: Option, +} + /// NIP47 Request #[derive(Debug, Clone, Serialize, PartialEq, Eq)] pub struct Request { @@ -233,6 +317,10 @@ impl Request { let params: PayInvoiceRequestParams = serde_json::from_value(template.params)?; RequestParams::PayInvoice(params) } + Method::PayKeysend => { + let params: PayKeysendRequestParams = serde_json::from_value(template.params)?; + RequestParams::PayKeysend(params) + } Method::MakeInvoice => { let params: MakeInvoiceRequestParams = serde_json::from_value(template.params)?; RequestParams::MakeInvoice(params) @@ -241,6 +329,14 @@ impl Request { let params: LookupInvoiceRequestParams = serde_json::from_value(template.params)?; RequestParams::LookupInvoice(params) } + Method::ListInvoices => { + let params: ListInvoicesRequestParams = serde_json::from_value(template.params)?; + RequestParams::ListInvoices(params) + } + Method::ListPayments => { + let params: ListPaymentsRequestParams = serde_json::from_value(template.params)?; + RequestParams::ListPayments(params) + } Method::GetBalance => RequestParams::GetBalance, }; @@ -273,6 +369,15 @@ pub struct PayInvoiceResponseResult { pub preimage: String, } +/// NIP47 Response Result +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] +pub struct PayKeysendResponseResult { + /// Response preimage + pub preimage: String, + /// Payment hash + pub payment_hash: String, +} + /// NIP47 Response Result #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct MakeInvoiceResponseResult { @@ -291,6 +396,15 @@ pub struct LookupInvoiceResponseResult { pub paid: bool, } +/// NIP47 Response Result +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] +pub struct ListPaymentResponseResult { + /// Bolt11 invoice + pub invoice: String, + /// Preimage for the payment + pub preimage: Option, +} + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] /// Budget renewal type @@ -323,10 +437,16 @@ pub struct GetBalanceResponseResult { pub enum ResponseResult { /// Pay Invoice PayInvoice(PayInvoiceResponseResult), + /// Pay Keysend + PayKeysend(PayKeysendResponseResult), /// Make Invoice MakeInvoice(MakeInvoiceResponseResult), /// Lookup Invoice LookupInvoice(LookupInvoiceResponseResult), + /// List Invoices + ListInvoices(Vec), + /// List Payments + ListPayments(Vec), /// Get Balance GetBalance(GetBalanceResponseResult), } @@ -338,8 +458,11 @@ impl Serialize for ResponseResult { { match self { ResponseResult::PayInvoice(p) => p.serialize(serializer), + ResponseResult::PayKeysend(p) => p.serialize(serializer), ResponseResult::MakeInvoice(p) => p.serialize(serializer), ResponseResult::LookupInvoice(p) => p.serialize(serializer), + ResponseResult::ListInvoices(p) => p.serialize(serializer), + ResponseResult::ListPayments(p) => p.serialize(serializer), ResponseResult::GetBalance(p) => p.serialize(serializer), } } @@ -386,6 +509,10 @@ impl Response { let result: PayInvoiceResponseResult = serde_json::from_value(result)?; ResponseResult::PayInvoice(result) } + Method::PayKeysend => { + let result: PayKeysendResponseResult = serde_json::from_value(result)?; + ResponseResult::PayKeysend(result) + } Method::MakeInvoice => { let result: MakeInvoiceResponseResult = serde_json::from_value(result)?; ResponseResult::MakeInvoice(result) @@ -394,6 +521,14 @@ impl Response { let result: LookupInvoiceResponseResult = serde_json::from_value(result)?; ResponseResult::LookupInvoice(result) } + Method::ListInvoices => { + let result: Vec = serde_json::from_value(result)?; + ResponseResult::ListInvoices(result) + } + Method::ListPayments => { + let result: Vec = serde_json::from_value(result)?; + ResponseResult::ListPayments(result) + } Method::GetBalance => { let result: GetBalanceResponseResult = serde_json::from_value(result)?; ResponseResult::GetBalance(result)