From fa54497d34a9c86190b5ece83bf076eb4b183eab Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 27 Nov 2024 12:12:13 +0100 Subject: [PATCH] nostr: remove `-Params` and `-Result` suffix from NIP47 structs Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 1 + bindings/nostr-sdk-ffi/src/nwc/mod.rs | 32 +-- .../nostr-sdk-ffi/src/protocol/nips/nip47.rs | 196 ++++++++---------- bindings/nostr-sdk-js/src/nwc/mod.rs | 37 ++-- .../nostr-sdk-js/src/protocol/nips/nip47.rs | 162 +++++++-------- crates/nostr-sdk/Cargo.toml | 4 - crates/nostr-sdk/examples/nip47.rs | 67 ------ crates/nostr/src/nips/nip47.rs | 179 ++++++++-------- crates/nwc/examples/nwc.rs | 12 +- crates/nwc/src/lib.rs | 42 ++-- 10 files changed, 318 insertions(+), 414 deletions(-) delete mode 100644 crates/nostr-sdk/examples/nip47.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index d0e9ce796..8caffa701 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ * nostr: remove `UncheckedUrl` struct ([Yuki Kishimoto]) * nostr: update `NostrConnectURI::relays` to return slice ([Yuki Kishimoto]) * nostr: update `NostrConnectURI::secret` to return string slice ([Yuki Kishimoto]) +* nostr: remove `-Params` and `-Result` suffix from NIP47 structs ([Yuki Kishimoto]) * pool: switch from async to sync message sending for `Relay` ([Yuki Kishimoto]) * connect: refactor `NostrConnectRemoteSigner` to use distinct keys for signer and user ([Yuki Kishimoto]) * connect: refactor `NostrConnectRemoteSigner` to use synchronous constructors ([Yuki Kishimoto]) diff --git a/bindings/nostr-sdk-ffi/src/nwc/mod.rs b/bindings/nostr-sdk-ffi/src/nwc/mod.rs index 03831d994..4521920d4 100644 --- a/bindings/nostr-sdk-ffi/src/nwc/mod.rs +++ b/bindings/nostr-sdk-ffi/src/nwc/mod.rs @@ -12,10 +12,9 @@ pub mod options; use self::options::NostrWalletConnectOptions; use crate::error::Result; use crate::protocol::nips::nip47::{ - GetInfoResponseResult, ListTransactionsRequestParams, LookupInvoiceRequestParams, - LookupInvoiceResponseResult, MakeInvoiceRequestParams, MakeInvoiceResponseResult, - NostrWalletConnectURI, PayInvoiceRequestParams, PayInvoiceResponseResult, - PayKeysendRequestParams, PayKeysendResponseResult, + GetInfoResponse, ListTransactionsRequest, LookupInvoiceRequest, LookupInvoiceResponse, + MakeInvoiceRequest, MakeInvoiceResponse, NostrWalletConnectURI, PayInvoiceRequest, + PayInvoiceResponse, PayKeysendRequest, PayKeysendResponse, }; use crate::relay::RelayStatus; @@ -57,42 +56,33 @@ impl NWC { } /// Pay invoice - pub async fn pay_invoice( - &self, - params: PayInvoiceRequestParams, - ) -> Result { + pub async fn pay_invoice(&self, params: PayInvoiceRequest) -> Result { Ok(self.inner.pay_invoice(params.into()).await?.into()) } /// Pay keysend - pub async fn pay_keysend( - &self, - params: PayKeysendRequestParams, - ) -> Result { + pub async fn pay_keysend(&self, params: PayKeysendRequest) -> Result { Ok(self.inner.pay_keysend(params.into()).await?.into()) } /// Create invoice - pub async fn make_invoice( - &self, - params: MakeInvoiceRequestParams, - ) -> Result { + pub async fn make_invoice(&self, params: MakeInvoiceRequest) -> Result { Ok(self.inner.make_invoice(params.into()).await?.into()) } /// Lookup invoice pub async fn lookup_invoice( &self, - params: LookupInvoiceRequestParams, - ) -> Result { + params: LookupInvoiceRequest, + ) -> Result { Ok(self.inner.lookup_invoice(params.into()).await?.into()) } /// List transactions pub async fn list_transactions( &self, - params: ListTransactionsRequestParams, - ) -> Result> { + params: ListTransactionsRequest, + ) -> Result> { let list = self.inner.list_transactions(params.into()).await?; Ok(list.into_iter().map(|l| l.into()).collect()) } @@ -103,7 +93,7 @@ impl NWC { } /// Get info - pub async fn get_info(&self) -> Result { + pub async fn get_info(&self) -> Result { Ok(self.inner.get_info().await?.into()) } } diff --git a/bindings/nostr-sdk-ffi/src/protocol/nips/nip47.rs b/bindings/nostr-sdk-ffi/src/protocol/nips/nip47.rs index c4b744bab..4e505dff8 100644 --- a/bindings/nostr-sdk-ffi/src/protocol/nips/nip47.rs +++ b/bindings/nostr-sdk-ffi/src/protocol/nips/nip47.rs @@ -157,32 +157,26 @@ impl From for nip47::Method { #[derive(Enum)] pub enum RequestParams { /// Pay Invoice - PayInvoice { - pay_invoice: PayInvoiceRequestParams, - }, + PayInvoice { pay_invoice: PayInvoiceRequest }, /// Multi Pay Invoice MultiPayInvoice { - multi_pay_invoice: MultiPayInvoiceRequestParams, + multi_pay_invoice: MultiPayInvoiceRequest, }, /// Pay Keysend - PayKeysend { - pay_keysend: PayKeysendRequestParams, - }, + PayKeysend { pay_keysend: PayKeysendRequest }, /// Multi Pay Keysend MultiPayKeysend { - multi_pay_keysend: MultiPayKeysendRequestParams, + multi_pay_keysend: MultiPayKeysendRequest, }, /// Make Invoice - MakeInvoice { - make_invoice: MakeInvoiceRequestParams, - }, + MakeInvoice { make_invoice: MakeInvoiceRequest }, /// Lookup Invoice LookupInvoice { - lookup_invoice: LookupInvoiceRequestParams, + lookup_invoice: LookupInvoiceRequest, }, /// List Transactions ListTransactions { - list_transactions: ListTransactionsRequestParams, + list_transactions: ListTransactionsRequest, }, /// Get Balance GetBalance, @@ -244,9 +238,9 @@ impl From for nip47::RequestParams { } } -/// Pay Invoice Request Params +/// Pay Invoice Request #[derive(Record)] -pub struct PayInvoiceRequestParams { +pub struct PayInvoiceRequest { /// Optional id pub id: Option, /// Request invoice @@ -255,8 +249,8 @@ pub struct PayInvoiceRequestParams { pub amount: Option, } -impl From for PayInvoiceRequestParams { - fn from(value: nip47::PayInvoiceRequestParams) -> Self { +impl From for PayInvoiceRequest { + fn from(value: nip47::PayInvoiceRequest) -> Self { Self { id: value.id, invoice: value.invoice, @@ -265,8 +259,8 @@ impl From for PayInvoiceRequestParams { } } -impl From for nip47::PayInvoiceRequestParams { - fn from(value: PayInvoiceRequestParams) -> Self { +impl From for nip47::PayInvoiceRequest { + fn from(value: PayInvoiceRequest) -> Self { Self { id: value.id, invoice: value.invoice, @@ -277,21 +271,21 @@ impl From for nip47::PayInvoiceRequestParams { /// Multi Pay Invoice Request Params #[derive(Record)] -pub struct MultiPayInvoiceRequestParams { +pub struct MultiPayInvoiceRequest { /// Invoices to pay - pub invoices: Vec, + pub invoices: Vec, } -impl From for MultiPayInvoiceRequestParams { - fn from(value: nip47::MultiPayInvoiceRequestParams) -> Self { +impl From for MultiPayInvoiceRequest { + fn from(value: nip47::MultiPayInvoiceRequest) -> Self { Self { invoices: value.invoices.into_iter().map(|i| i.into()).collect(), } } } -impl From for nip47::MultiPayInvoiceRequestParams { - fn from(value: MultiPayInvoiceRequestParams) -> Self { +impl From for nip47::MultiPayInvoiceRequest { + fn from(value: MultiPayInvoiceRequest) -> Self { Self { invoices: value.invoices.into_iter().map(|i| i.into()).collect(), } @@ -325,9 +319,9 @@ impl From for nip47::KeysendTLVRecord { } } -/// Pay Invoice Request Params +/// Pay Invoice Request #[derive(Record)] -pub struct PayKeysendRequestParams { +pub struct PayKeysendRequest { /// Optional id pub id: Option, /// Amount in millisatoshis @@ -340,8 +334,8 @@ pub struct PayKeysendRequestParams { pub tlv_records: Vec, } -impl From for PayKeysendRequestParams { - fn from(value: nip47::PayKeysendRequestParams) -> Self { +impl From for PayKeysendRequest { + fn from(value: nip47::PayKeysendRequest) -> Self { Self { id: value.id, amount: value.amount, @@ -352,8 +346,8 @@ impl From for PayKeysendRequestParams { } } -impl From for nip47::PayKeysendRequestParams { - fn from(value: PayKeysendRequestParams) -> Self { +impl From for nip47::PayKeysendRequest { + fn from(value: PayKeysendRequest) -> Self { Self { id: value.id, amount: value.amount, @@ -364,23 +358,23 @@ impl From for nip47::PayKeysendRequestParams { } } -/// Multi Pay Keysend Request Params +/// Multi Pay Keysend Request #[derive(Record)] -pub struct MultiPayKeysendRequestParams { +pub struct MultiPayKeysendRequest { /// Keysends - pub keysends: Vec, + pub keysends: Vec, } -impl From for MultiPayKeysendRequestParams { - fn from(value: nip47::MultiPayKeysendRequestParams) -> Self { +impl From for MultiPayKeysendRequest { + fn from(value: nip47::MultiPayKeysendRequest) -> Self { Self { keysends: value.keysends.into_iter().map(|i| i.into()).collect(), } } } -impl From for nip47::MultiPayKeysendRequestParams { - fn from(value: MultiPayKeysendRequestParams) -> Self { +impl From for nip47::MultiPayKeysendRequest { + fn from(value: MultiPayKeysendRequest) -> Self { Self { keysends: value.keysends.into_iter().map(|i| i.into()).collect(), } @@ -414,9 +408,9 @@ impl From for TransactionType { } } -/// Make Invoice Request Params +/// Make Invoice Request #[derive(Record)] -pub struct MakeInvoiceRequestParams { +pub struct MakeInvoiceRequest { /// Amount in millisatoshis pub amount: u64, /// Invoice description @@ -427,8 +421,8 @@ pub struct MakeInvoiceRequestParams { pub expiry: Option, } -impl From for MakeInvoiceRequestParams { - fn from(value: nip47::MakeInvoiceRequestParams) -> Self { +impl From for MakeInvoiceRequest { + fn from(value: nip47::MakeInvoiceRequest) -> Self { Self { amount: value.amount, description: value.description, @@ -438,8 +432,8 @@ impl From for MakeInvoiceRequestParams { } } -impl From for nip47::MakeInvoiceRequestParams { - fn from(value: MakeInvoiceRequestParams) -> Self { +impl From for nip47::MakeInvoiceRequest { + fn from(value: MakeInvoiceRequest) -> Self { Self { amount: value.amount, description: value.description, @@ -449,17 +443,17 @@ impl From for nip47::MakeInvoiceRequestParams { } } -/// Lookup Invoice Request Params +/// Lookup Invoice Request #[derive(Record)] -pub struct LookupInvoiceRequestParams { +pub struct LookupInvoiceRequest { /// Payment hash of invoice pub payment_hash: Option, /// Bolt11 invoice pub invoice: Option, } -impl From for LookupInvoiceRequestParams { - fn from(value: nip47::LookupInvoiceRequestParams) -> Self { +impl From for LookupInvoiceRequest { + fn from(value: nip47::LookupInvoiceRequest) -> Self { Self { payment_hash: value.payment_hash, invoice: value.invoice, @@ -467,8 +461,8 @@ impl From for LookupInvoiceRequestParams { } } -impl From for nip47::LookupInvoiceRequestParams { - fn from(value: LookupInvoiceRequestParams) -> Self { +impl From for nip47::LookupInvoiceRequest { + fn from(value: LookupInvoiceRequest) -> Self { Self { payment_hash: value.payment_hash, invoice: value.invoice, @@ -476,9 +470,9 @@ impl From for nip47::LookupInvoiceRequestParams { } } -/// List Invoice Request Params +/// List Invoice Request #[derive(Record)] -pub struct ListTransactionsRequestParams { +pub struct ListTransactionsRequest { /// Starting timestamp in seconds since epoch pub from: Option>, /// Ending timestamp in seconds since epoch @@ -493,8 +487,8 @@ pub struct ListTransactionsRequestParams { pub transaction_type: Option, } -impl From for ListTransactionsRequestParams { - fn from(value: nip47::ListTransactionsRequestParams) -> Self { +impl From for ListTransactionsRequest { + fn from(value: nip47::ListTransactionsRequest) -> Self { Self { from: value.from.map(|t| Arc::new(t.into())), until: value.until.map(|t| Arc::new(t.into())), @@ -506,8 +500,8 @@ impl From for ListTransactionsRequestParam } } -impl From for nip47::ListTransactionsRequestParams { - fn from(value: ListTransactionsRequestParams) -> Self { +impl From for nip47::ListTransactionsRequest { + fn from(value: ListTransactionsRequest) -> Self { Self { from: value.from.map(|t| **t), until: value.until.map(|t| **t), @@ -560,21 +554,21 @@ impl Request { /// NIP47 Response Result #[derive(Record)] -pub struct PayInvoiceResponseResult { +pub struct PayInvoiceResponse { /// Response preimage pub preimage: String, } -impl From for PayInvoiceResponseResult { - fn from(value: nip47::PayInvoiceResponseResult) -> Self { +impl From for PayInvoiceResponse { + fn from(value: nip47::PayInvoiceResponse) -> Self { Self { preimage: value.preimage, } } } -impl From for nip47::PayInvoiceResponseResult { - fn from(value: PayInvoiceResponseResult) -> Self { +impl From for nip47::PayInvoiceResponse { + fn from(value: PayInvoiceResponse) -> Self { Self { preimage: value.preimage, } @@ -583,21 +577,21 @@ impl From for nip47::PayInvoiceResponseResult { /// NIP47 Response Result #[derive(Record)] -pub struct PayKeysendResponseResult { +pub struct PayKeysendResponse { /// Response preimage pub preimage: String, } -impl From for PayKeysendResponseResult { - fn from(value: nip47::PayKeysendResponseResult) -> Self { +impl From for PayKeysendResponse { + fn from(value: nip47::PayKeysendResponse) -> Self { Self { preimage: value.preimage, } } } -impl From for nip47::PayKeysendResponseResult { - fn from(value: PayKeysendResponseResult) -> Self { +impl From for nip47::PayKeysendResponse { + fn from(value: PayKeysendResponse) -> Self { Self { preimage: value.preimage, } @@ -606,15 +600,15 @@ impl From for nip47::PayKeysendResponseResult { /// NIP47 Response Result #[derive(Record)] -pub struct MakeInvoiceResponseResult { +pub struct MakeInvoiceResponse { /// Bolt 11 invoice pub invoice: String, /// Invoice's payment hash pub payment_hash: String, } -impl From for MakeInvoiceResponseResult { - fn from(value: nip47::MakeInvoiceResponseResult) -> Self { +impl From for MakeInvoiceResponse { + fn from(value: nip47::MakeInvoiceResponse) -> Self { Self { invoice: value.invoice, payment_hash: value.payment_hash, @@ -622,8 +616,8 @@ impl From for MakeInvoiceResponseResult { } } -impl From for nip47::MakeInvoiceResponseResult { - fn from(value: MakeInvoiceResponseResult) -> Self { +impl From for nip47::MakeInvoiceResponse { + fn from(value: MakeInvoiceResponse) -> Self { Self { invoice: value.invoice, payment_hash: value.payment_hash, @@ -633,7 +627,7 @@ impl From for nip47::MakeInvoiceResponseResult { /// NIP47 Response Result #[derive(Record)] -pub struct LookupInvoiceResponseResult { +pub struct LookupInvoiceResponse { /// Transaction type pub transaction_type: Option, /// Bolt11 invoice @@ -660,8 +654,8 @@ pub struct LookupInvoiceResponseResult { pub metadata: Option, } -impl From for LookupInvoiceResponseResult { - fn from(value: nip47::LookupInvoiceResponseResult) -> Self { +impl From for LookupInvoiceResponse { + fn from(value: nip47::LookupInvoiceResponse) -> Self { Self { transaction_type: value.transaction_type.map(|t| t.into()), invoice: value.invoice, @@ -679,8 +673,8 @@ impl From for LookupInvoiceResponseResult { } } -impl From for nip47::LookupInvoiceResponseResult { - fn from(value: LookupInvoiceResponseResult) -> Self { +impl From for nip47::LookupInvoiceResponse { + fn from(value: LookupInvoiceResponse) -> Self { Self { transaction_type: value.transaction_type.map(|t| t.into()), invoice: value.invoice, @@ -700,21 +694,21 @@ impl From for nip47::LookupInvoiceResponseResult { /// NIP47 Response Result #[derive(Record)] -pub struct GetBalanceResponseResult { +pub struct GetBalanceResponse { /// Balance amount in msats pub balance: u64, } -impl From for GetBalanceResponseResult { - fn from(value: nip47::GetBalanceResponseResult) -> Self { +impl From for GetBalanceResponse { + fn from(value: nip47::GetBalanceResponse) -> Self { Self { balance: value.balance, } } } -impl From for nip47::GetBalanceResponseResult { - fn from(value: GetBalanceResponseResult) -> Self { +impl From for nip47::GetBalanceResponse { + fn from(value: GetBalanceResponse) -> Self { Self { balance: value.balance, } @@ -723,7 +717,7 @@ impl From for nip47::GetBalanceResponseResult { /// NIP47 Response Result #[derive(Record)] -pub struct GetInfoResponseResult { +pub struct GetInfoResponse { /// The alias of the lightning node pub alias: String, /// The color of the current node in hex code format @@ -740,8 +734,8 @@ pub struct GetInfoResponseResult { pub methods: Vec, } -impl From for GetInfoResponseResult { - fn from(value: nip47::GetInfoResponseResult) -> Self { +impl From for GetInfoResponse { + fn from(value: nip47::GetInfoResponse) -> Self { Self { alias: value.alias, color: value.color, @@ -754,8 +748,8 @@ impl From for GetInfoResponseResult { } } -impl From for nip47::GetInfoResponseResult { - fn from(value: GetInfoResponseResult) -> Self { +impl From for nip47::GetInfoResponse { + fn from(value: GetInfoResponse) -> Self { Self { alias: value.alias, color: value.color, @@ -772,39 +766,27 @@ impl From for nip47::GetInfoResponseResult { #[derive(Enum)] pub enum ResponseResult { /// Pay Invoice - PayInvoice { - pay_invoice: PayInvoiceResponseResult, - }, + PayInvoice { pay_invoice: PayInvoiceResponse }, /// Multi Pay Invoice - MultiPayInvoice { - pay_invoice: PayInvoiceResponseResult, - }, + MultiPayInvoice { pay_invoice: PayInvoiceResponse }, /// Pay Keysend - PayKeysend { - pay_keysend: PayKeysendResponseResult, - }, + PayKeysend { pay_keysend: PayKeysendResponse }, /// Multi Pay Keysend - MultiPayKeysend { - pay_keysend: PayKeysendResponseResult, - }, + MultiPayKeysend { pay_keysend: PayKeysendResponse }, /// Make Invoice - MakeInvoice { - make_invoice: MakeInvoiceResponseResult, - }, + MakeInvoice { make_invoice: MakeInvoiceResponse }, /// Lookup Invoice LookupInvoice { - lookup_invoice: LookupInvoiceResponseResult, + lookup_invoice: LookupInvoiceResponse, }, /// List Transactions ListTransactions { - list_transactions: Vec, + list_transactions: Vec, }, /// Get Balance - GetBalance { - get_balance: GetBalanceResponseResult, - }, + GetBalance { get_balance: GetBalanceResponse }, /// Get Info - GetInfo { get_info: GetInfoResponseResult }, + GetInfo { get_info: GetInfoResponse }, } impl From for ResponseResult { diff --git a/bindings/nostr-sdk-js/src/nwc/mod.rs b/bindings/nostr-sdk-js/src/nwc/mod.rs index 385afaf0b..9532a767c 100644 --- a/bindings/nostr-sdk-js/src/nwc/mod.rs +++ b/bindings/nostr-sdk-js/src/nwc/mod.rs @@ -13,18 +13,17 @@ pub mod options; use self::options::JsNostrWalletConnectOptions; use crate::error::{into_err, Result}; use crate::protocol::nips::nip47::{ - JsGetInfoResponseResult, JsListTransactionsRequestParams, JsLookupInvoiceRequestParams, - JsLookupInvoiceResponseResult, JsMakeInvoiceRequestParams, JsMakeInvoiceResponseResult, - JsNostrWalletConnectURI, JsPayInvoiceRequestParams, JsPayInvoiceResponseResult, - JsPayKeysendRequestParams, JsPayKeysendResponseResult, + JsGetInfoResponse, JsListTransactionsRequest, JsLookupInvoiceRequest, JsLookupInvoiceResponse, + JsMakeInvoiceRequest, JsMakeInvoiceResponse, JsNostrWalletConnectURI, JsPayInvoiceRequest, + JsPayInvoiceResponse, JsPayKeysendRequest, JsPayKeysendResponse, }; use crate::relay::JsRelayStatus; #[wasm_bindgen] extern "C" { /// Array - #[wasm_bindgen(typescript_type = "LookupInvoiceResponseResult[]")] - pub type JsLookupInvoiceResponseResultArray; + #[wasm_bindgen(typescript_type = "LookupInvoiceResponse[]")] + pub type JsLookupInvoiceResponseArray; } /// Nostr Wallet Connect client @@ -66,10 +65,7 @@ impl JsNwc { /// Pay invoice #[wasm_bindgen(js_name = payInvoice)] - pub async fn pay_invoice( - &self, - params: &JsPayInvoiceRequestParams, - ) -> Result { + pub async fn pay_invoice(&self, params: &JsPayInvoiceRequest) -> Result { Ok(self .inner .pay_invoice(params.to_owned().into()) @@ -80,10 +76,7 @@ impl JsNwc { /// Pay keysend #[wasm_bindgen(js_name = payKeysend)] - pub async fn pay_keysend( - &self, - params: &JsPayKeysendRequestParams, - ) -> Result { + pub async fn pay_keysend(&self, params: &JsPayKeysendRequest) -> Result { Ok(self .inner .pay_keysend(params.to_owned().into()) @@ -96,8 +89,8 @@ impl JsNwc { #[wasm_bindgen(js_name = makeInvoice)] pub async fn make_invoice( &self, - params: &JsMakeInvoiceRequestParams, - ) -> Result { + params: &JsMakeInvoiceRequest, + ) -> Result { Ok(self .inner .make_invoice(params.to_owned().into()) @@ -110,8 +103,8 @@ impl JsNwc { #[wasm_bindgen(js_name = lookupInvoice)] pub async fn lookup_invoice( &self, - params: &JsLookupInvoiceRequestParams, - ) -> Result { + params: &JsLookupInvoiceRequest, + ) -> Result { Ok(self .inner .lookup_invoice(params.to_owned().into()) @@ -124,8 +117,8 @@ impl JsNwc { #[wasm_bindgen(js_name = listTransactions)] pub async fn list_transactions( &self, - params: &JsListTransactionsRequestParams, - ) -> Result { + params: &JsListTransactionsRequest, + ) -> Result { let list = self .inner .list_transactions(params.to_owned().into()) @@ -134,7 +127,7 @@ impl JsNwc { Ok(list .into_iter() .map(|e| { - let e: JsLookupInvoiceResponseResult = e.into(); + let e: JsLookupInvoiceResponse = e.into(); JsValue::from(e) }) .collect::() @@ -149,7 +142,7 @@ impl JsNwc { /// Get info #[wasm_bindgen(js_name = getInfo)] - pub async fn get_info(&self) -> Result { + pub async fn get_info(&self) -> Result { Ok(self.inner.get_info().await.map_err(into_err)?.into()) } } diff --git a/bindings/nostr-sdk-js/src/protocol/nips/nip47.rs b/bindings/nostr-sdk-js/src/protocol/nips/nip47.rs index 65ea6cb40..7c698b6a6 100644 --- a/bindings/nostr-sdk-js/src/protocol/nips/nip47.rs +++ b/bindings/nostr-sdk-js/src/protocol/nips/nip47.rs @@ -71,10 +71,10 @@ impl From for ErrorCode { } } -/// Pay Invoice Request Params +/// Pay Invoice Request #[derive(Clone)] -#[wasm_bindgen(js_name = PayInvoiceRequestParams)] -pub struct JsPayInvoiceRequestParams { +#[wasm_bindgen(js_name = PayInvoiceRequest)] +pub struct JsPayInvoiceRequest { /// Optional id #[wasm_bindgen(getter_with_clone)] pub id: Option, @@ -85,8 +85,8 @@ pub struct JsPayInvoiceRequestParams { pub amount: Option, } -impl From for JsPayInvoiceRequestParams { - fn from(value: PayInvoiceRequestParams) -> Self { +impl From for JsPayInvoiceRequest { + fn from(value: PayInvoiceRequest) -> Self { Self { id: value.id, invoice: value.invoice, @@ -95,8 +95,8 @@ impl From for JsPayInvoiceRequestParams { } } -impl From for PayInvoiceRequestParams { - fn from(value: JsPayInvoiceRequestParams) -> Self { +impl From for PayInvoiceRequest { + fn from(value: JsPayInvoiceRequest) -> Self { Self { id: value.id, invoice: value.invoice, @@ -106,23 +106,23 @@ impl From for PayInvoiceRequestParams { } /// Multi Pay Invoice Request Params -#[wasm_bindgen(js_name = MultiPayInvoiceRequestParams)] -pub struct JsMultiPayInvoiceRequestParams { +#[wasm_bindgen(js_name = MultiPayInvoiceRequest)] +pub struct JsMultiPayInvoiceRequest { /// Invoices to pay #[wasm_bindgen(getter_with_clone)] - pub invoices: Vec, + pub invoices: Vec, } -impl From for JsMultiPayInvoiceRequestParams { - fn from(value: MultiPayInvoiceRequestParams) -> Self { +impl From for JsMultiPayInvoiceRequest { + fn from(value: MultiPayInvoiceRequest) -> Self { Self { invoices: value.invoices.into_iter().map(|i| i.into()).collect(), } } } -impl From for MultiPayInvoiceRequestParams { - fn from(value: JsMultiPayInvoiceRequestParams) -> Self { +impl From for MultiPayInvoiceRequest { + fn from(value: JsMultiPayInvoiceRequest) -> Self { Self { invoices: value.invoices.into_iter().map(|i| i.into()).collect(), } @@ -160,8 +160,8 @@ impl From for KeysendTLVRecord { /// Pay Invoice Request Params #[derive(Clone)] -#[wasm_bindgen(js_name = PayKeysendRequestParams)] -pub struct JsPayKeysendRequestParams { +#[wasm_bindgen(js_name = PayKeysendRequest)] +pub struct JsPayKeysendRequest { /// Optional id #[wasm_bindgen(getter_with_clone)] pub id: Option, @@ -178,8 +178,8 @@ pub struct JsPayKeysendRequestParams { pub tlv_records: Vec, } -impl From for JsPayKeysendRequestParams { - fn from(value: PayKeysendRequestParams) -> Self { +impl From for JsPayKeysendRequest { + fn from(value: PayKeysendRequest) -> Self { Self { id: value.id, amount: value.amount, @@ -190,8 +190,8 @@ impl From for JsPayKeysendRequestParams { } } -impl From for PayKeysendRequestParams { - fn from(value: JsPayKeysendRequestParams) -> Self { +impl From for PayKeysendRequest { + fn from(value: JsPayKeysendRequest) -> Self { Self { id: value.id, amount: value.amount, @@ -204,23 +204,23 @@ impl From for PayKeysendRequestParams { /// Multi Pay Keysend Request Params #[derive(Clone)] -#[wasm_bindgen(js_name = MultiPayKeysendRequestParams)] -pub struct JsMultiPayKeysendRequestParams { +#[wasm_bindgen(js_name = MultiPayKeysendRequest)] +pub struct JsMultiPayKeysendRequest { /// Keysends #[wasm_bindgen(getter_with_clone)] - pub keysends: Vec, + pub keysends: Vec, } -impl From for JsMultiPayKeysendRequestParams { - fn from(value: MultiPayKeysendRequestParams) -> Self { +impl From for JsMultiPayKeysendRequest { + fn from(value: MultiPayKeysendRequest) -> Self { Self { keysends: value.keysends.into_iter().map(|i| i.into()).collect(), } } } -impl From for MultiPayKeysendRequestParams { - fn from(value: JsMultiPayKeysendRequestParams) -> Self { +impl From for MultiPayKeysendRequest { + fn from(value: JsMultiPayKeysendRequest) -> Self { Self { keysends: value.keysends.into_iter().map(|i| i.into()).collect(), } @@ -257,8 +257,8 @@ impl From for JsTransactionType { /// Make Invoice Request Params #[derive(Clone)] -#[wasm_bindgen(js_name = MakeInvoiceRequestParams)] -pub struct JsMakeInvoiceRequestParams { +#[wasm_bindgen(js_name = MakeInvoiceRequest)] +pub struct JsMakeInvoiceRequest { /// Amount in millisatoshis pub amount: u64, /// Invoice description @@ -271,8 +271,8 @@ pub struct JsMakeInvoiceRequestParams { pub expiry: Option, } -impl From for JsMakeInvoiceRequestParams { - fn from(value: MakeInvoiceRequestParams) -> Self { +impl From for JsMakeInvoiceRequest { + fn from(value: MakeInvoiceRequest) -> Self { Self { amount: value.amount, description: value.description, @@ -282,8 +282,8 @@ impl From for JsMakeInvoiceRequestParams { } } -impl From for MakeInvoiceRequestParams { - fn from(value: JsMakeInvoiceRequestParams) -> Self { +impl From for MakeInvoiceRequest { + fn from(value: JsMakeInvoiceRequest) -> Self { Self { amount: value.amount, description: value.description, @@ -295,8 +295,8 @@ impl From for MakeInvoiceRequestParams { /// Lookup Invoice Request Params #[derive(Clone)] -#[wasm_bindgen(js_name = LookupInvoiceRequestParams)] -pub struct JsLookupInvoiceRequestParams { +#[wasm_bindgen(js_name = LookupInvoiceRequest)] +pub struct JsLookupInvoiceRequest { /// Payment hash of invoice #[wasm_bindgen(getter_with_clone)] pub payment_hash: Option, @@ -305,8 +305,8 @@ pub struct JsLookupInvoiceRequestParams { pub invoice: Option, } -impl From for JsLookupInvoiceRequestParams { - fn from(value: LookupInvoiceRequestParams) -> Self { +impl From for JsLookupInvoiceRequest { + fn from(value: LookupInvoiceRequest) -> Self { Self { payment_hash: value.payment_hash, invoice: value.invoice, @@ -314,8 +314,8 @@ impl From for JsLookupInvoiceRequestParams { } } -impl From for LookupInvoiceRequestParams { - fn from(value: JsLookupInvoiceRequestParams) -> Self { +impl From for LookupInvoiceRequest { + fn from(value: JsLookupInvoiceRequest) -> Self { Self { payment_hash: value.payment_hash, invoice: value.invoice, @@ -325,8 +325,8 @@ impl From for LookupInvoiceRequestParams { /// List Invoice Request Params #[derive(Clone)] -#[wasm_bindgen(js_name = ListTransactionsRequestParams)] -pub struct JsListTransactionsRequestParams { +#[wasm_bindgen(js_name = ListTransactionsRequest)] +pub struct JsListTransactionsRequest { /// Starting timestamp in seconds since epoch pub from: Option, /// Ending timestamp in seconds since epoch @@ -341,8 +341,8 @@ pub struct JsListTransactionsRequestParams { pub transaction_type: Option, } -impl From for JsListTransactionsRequestParams { - fn from(value: ListTransactionsRequestParams) -> Self { +impl From for JsListTransactionsRequest { + fn from(value: ListTransactionsRequest) -> Self { Self { from: value.from.map(|t| t.into()), until: value.until.map(|t| t.into()), @@ -354,8 +354,8 @@ impl From for JsListTransactionsRequestParams { } } -impl From for ListTransactionsRequestParams { - fn from(value: JsListTransactionsRequestParams) -> Self { +impl From for ListTransactionsRequest { + fn from(value: JsListTransactionsRequest) -> Self { Self { from: value.from.map(|t| *t), until: value.until.map(|t| *t), @@ -367,54 +367,54 @@ impl From for ListTransactionsRequestParams { } } -#[wasm_bindgen(js_name = PayInvoiceResponseResult)] -pub struct JsPayInvoiceResponseResult { +#[wasm_bindgen(js_name = PayInvoiceResponse)] +pub struct JsPayInvoiceResponse { /// Response preimage #[wasm_bindgen(getter_with_clone)] pub preimage: String, } -impl From for JsPayInvoiceResponseResult { - fn from(value: PayInvoiceResponseResult) -> Self { +impl From for JsPayInvoiceResponse { + fn from(value: PayInvoiceResponse) -> Self { Self { preimage: value.preimage, } } } -impl From for PayInvoiceResponseResult { - fn from(value: JsPayInvoiceResponseResult) -> Self { +impl From for PayInvoiceResponse { + fn from(value: JsPayInvoiceResponse) -> Self { Self { preimage: value.preimage, } } } -#[wasm_bindgen(js_name = PayKeysendResponseResult)] -pub struct JsPayKeysendResponseResult { +#[wasm_bindgen(js_name = PayKeysendResponse)] +pub struct JsPayKeysendResponse { /// Response preimage #[wasm_bindgen(getter_with_clone)] pub preimage: String, } -impl From for JsPayKeysendResponseResult { - fn from(value: PayKeysendResponseResult) -> Self { +impl From for JsPayKeysendResponse { + fn from(value: PayKeysendResponse) -> Self { Self { preimage: value.preimage, } } } -impl From for PayKeysendResponseResult { - fn from(value: JsPayKeysendResponseResult) -> Self { +impl From for PayKeysendResponse { + fn from(value: JsPayKeysendResponse) -> Self { Self { preimage: value.preimage, } } } -#[wasm_bindgen(js_name = MakeInvoiceResponseResult)] -pub struct JsMakeInvoiceResponseResult { +#[wasm_bindgen(js_name = MakeInvoiceResponse)] +pub struct JsMakeInvoiceResponse { /// Bolt 11 invoice #[wasm_bindgen(getter_with_clone)] pub invoice: String, @@ -423,8 +423,8 @@ pub struct JsMakeInvoiceResponseResult { pub payment_hash: String, } -impl From for JsMakeInvoiceResponseResult { - fn from(value: MakeInvoiceResponseResult) -> Self { +impl From for JsMakeInvoiceResponse { + fn from(value: MakeInvoiceResponse) -> Self { Self { invoice: value.invoice, payment_hash: value.payment_hash, @@ -432,8 +432,8 @@ impl From for JsMakeInvoiceResponseResult { } } -impl From for MakeInvoiceResponseResult { - fn from(value: JsMakeInvoiceResponseResult) -> Self { +impl From for MakeInvoiceResponse { + fn from(value: JsMakeInvoiceResponse) -> Self { Self { invoice: value.invoice, payment_hash: value.payment_hash, @@ -441,8 +441,8 @@ impl From for MakeInvoiceResponseResult { } } -#[wasm_bindgen(js_name = LookupInvoiceResponseResult)] -pub struct JsLookupInvoiceResponseResult { +#[wasm_bindgen(js_name = LookupInvoiceResponse)] +pub struct JsLookupInvoiceResponse { /// Transaction type pub transaction_type: Option, /// Bolt11 invoice @@ -475,8 +475,8 @@ pub struct JsLookupInvoiceResponseResult { // pub metadata: String, // TODO: this is not a string } -impl From for JsLookupInvoiceResponseResult { - fn from(value: LookupInvoiceResponseResult) -> Self { +impl From for JsLookupInvoiceResponse { + fn from(value: LookupInvoiceResponse) -> Self { Self { transaction_type: value.transaction_type.map(|t| t.into()), invoice: value.invoice, @@ -494,8 +494,8 @@ impl From for JsLookupInvoiceResponseResult { } } -impl From for LookupInvoiceResponseResult { - fn from(value: JsLookupInvoiceResponseResult) -> Self { +impl From for LookupInvoiceResponse { + fn from(value: JsLookupInvoiceResponse) -> Self { Self { transaction_type: value.transaction_type.map(|t| t.into()), invoice: value.invoice, @@ -513,30 +513,30 @@ impl From for LookupInvoiceResponseResult { } } -#[wasm_bindgen(js_name = GetBalanceResponseResult)] -pub struct JsGetBalanceResponseResult { +#[wasm_bindgen(js_name = GetBalanceResponse)] +pub struct JsGetBalanceResponse { /// Balance amount in msats pub balance: u64, } -impl From for JsGetBalanceResponseResult { - fn from(value: GetBalanceResponseResult) -> Self { +impl From for JsGetBalanceResponse { + fn from(value: GetBalanceResponse) -> Self { Self { balance: value.balance, } } } -impl From for GetBalanceResponseResult { - fn from(value: JsGetBalanceResponseResult) -> Self { +impl From for GetBalanceResponse { + fn from(value: JsGetBalanceResponse) -> Self { Self { balance: value.balance, } } } -#[wasm_bindgen(js_name = GetInfoResponseResult)] -pub struct JsGetInfoResponseResult { +#[wasm_bindgen(js_name = GetInfoResponse)] +pub struct JsGetInfoResponse { /// The alias of the lightning node #[wasm_bindgen(getter_with_clone)] pub alias: String, @@ -559,8 +559,8 @@ pub struct JsGetInfoResponseResult { pub methods: Vec, } -impl From for JsGetInfoResponseResult { - fn from(value: GetInfoResponseResult) -> Self { +impl From for JsGetInfoResponse { + fn from(value: GetInfoResponse) -> Self { Self { alias: value.alias, color: value.color, @@ -573,8 +573,8 @@ impl From for JsGetInfoResponseResult { } } -impl From for GetInfoResponseResult { - fn from(value: JsGetInfoResponseResult) -> Self { +impl From for GetInfoResponse { + fn from(value: JsGetInfoResponse) -> Self { Self { alias: value.alias, color: value.color, diff --git a/crates/nostr-sdk/Cargo.toml b/crates/nostr-sdk/Cargo.toml index 8cc839c84..f5a2609e5 100644 --- a/crates/nostr-sdk/Cargo.toml +++ b/crates/nostr-sdk/Cargo.toml @@ -125,10 +125,6 @@ required-features = ["all-nips", "lmdb"] name = "negentropy" required-features = ["all-nips", "lmdb"] -[[example]] -name = "nip47" -required-features = ["nip47"] - [[example]] name = "nip65" diff --git a/crates/nostr-sdk/examples/nip47.rs b/crates/nostr-sdk/examples/nip47.rs deleted file mode 100644 index eedbab069..000000000 --- a/crates/nostr-sdk/examples/nip47.rs +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2023-2024 Rust Nostr Developers -// Distributed under the MIT software license - -use std::str::FromStr; - -use nostr_sdk::prelude::*; - -// Check `nwc` crate for high level client library! - -#[tokio::main] -async fn main() -> Result<()> { - tracing_subscriber::fmt::init(); - - let mut nwc_uri_string = String::new(); - let mut invoice = String::new(); - - println!("Please enter a NWC string"); - std::io::stdin() - .read_line(&mut nwc_uri_string) - .expect("Failed to read line"); - - println!("Please enter a BOLT 11 invoice"); - std::io::stdin() - .read_line(&mut invoice) - .expect("Failed to read line"); - - invoice = String::from(invoice.trim()); - - let nwc_uri = - NostrWalletConnectURI::from_str(&nwc_uri_string).expect("Failed to parse NWC URI"); - - let client = Client::default(); - client.add_relay(nwc_uri.relay_url.clone()).await?; - - client.connect().await; - println!("Connected to relay {}", nwc_uri.relay_url); - - let req = nip47::Request::pay_invoice(PayInvoiceRequestParams { - id: None, - invoice, - amount: None, - }); - let req_event = req.to_event(&nwc_uri).unwrap(); - - let subscription = Filter::new() - .author(nwc_uri.public_key) - .kind(Kind::WalletConnectResponse) - .event(req_event.id) - .since(Timestamp::now()); - - client.subscribe(vec![subscription], None).await?; - - client.send_event(req_event).await.unwrap(); - - client - .handle_notifications(|notification| async { - if let RelayPoolNotification::Event { event, .. } = notification { - let res = nip47::Response::from_event(&nwc_uri, &event)?; - let PayInvoiceResponseResult { preimage } = res.to_pay_invoice()?; - println!("Payment sent. Preimage: {preimage}"); - } - Ok(true) - }) - .await?; - - Ok(()) -} diff --git a/crates/nostr/src/nips/nip47.rs b/crates/nostr/src/nips/nip47.rs index 1c231293c..44bc30db5 100644 --- a/crates/nostr/src/nips/nip47.rs +++ b/crates/nostr/src/nips/nip47.rs @@ -235,23 +235,23 @@ pub enum Method { GetInfo, } -/// Nostr Wallet Connect Request Params +/// Nostr Wallet Connect Request #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum RequestParams { /// Pay Invoice - PayInvoice(PayInvoiceRequestParams), + PayInvoice(PayInvoiceRequest), /// Multiple Pay Invoice - MultiPayInvoice(MultiPayInvoiceRequestParams), + MultiPayInvoice(MultiPayInvoiceRequest), /// Pay Keysend - PayKeysend(PayKeysendRequestParams), + PayKeysend(PayKeysendRequest), /// Multiple Pay Keysend - MultiPayKeysend(MultiPayKeysendRequestParams), + MultiPayKeysend(MultiPayKeysendRequest), /// Make Invoice - MakeInvoice(MakeInvoiceRequestParams), + MakeInvoice(MakeInvoiceRequest), /// Lookup Invoice - LookupInvoice(LookupInvoiceRequestParams), + LookupInvoice(LookupInvoiceRequest), /// List Transactions - ListTransactions(ListTransactionsRequestParams), + ListTransactions(ListTransactionsRequest), /// Get Balance GetBalance, /// Get Info @@ -277,9 +277,9 @@ impl Serialize for RequestParams { } } -/// Pay Invoice Request Params +/// Pay Invoice Request #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct PayInvoiceRequestParams { +pub struct PayInvoiceRequest { /// Optional id #[serde(skip_serializing_if = "Option::is_none")] pub id: Option, @@ -290,11 +290,26 @@ pub struct PayInvoiceRequestParams { pub amount: Option, } -/// Multiple Pay Invoice Request Params +impl PayInvoiceRequest { + /// New pay invoice request + #[inline] + pub fn new(invoice: S) -> Self + where + S: Into, + { + Self { + id: None, + invoice: invoice.into(), + amount: None, + } + } +} + +/// Multiple Pay Invoice Request #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct MultiPayInvoiceRequestParams { +pub struct MultiPayInvoiceRequest { /// Requested invoices - pub invoices: Vec, + pub invoices: Vec, } /// TLVs to be added to the keysend payment @@ -307,9 +322,9 @@ pub struct KeysendTLVRecord { pub value: String, } -/// Pay Invoice Request Params +/// Pay Invoice Request #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct PayKeysendRequestParams { +pub struct PayKeysendRequest { /// Optional id #[serde(skip_serializing_if = "Option::is_none")] pub id: Option, @@ -326,16 +341,16 @@ pub struct PayKeysendRequestParams { pub tlv_records: Vec, } -/// Multiple Pay Keysend Request Params +/// Multiple Pay Keysend Request #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct MultiPayKeysendRequestParams { +pub struct MultiPayKeysendRequest { /// Requested keysends - pub keysends: Vec, + pub keysends: Vec, } -/// Make Invoice Request Params +/// Make Invoice Request #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct MakeInvoiceRequestParams { +pub struct MakeInvoiceRequest { /// Amount in millisatoshis pub amount: u64, /// Invoice description @@ -346,9 +361,9 @@ pub struct MakeInvoiceRequestParams { pub expiry: Option, } -/// Lookup Invoice Request Params +/// Lookup Invoice Request #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct LookupInvoiceRequestParams { +pub struct LookupInvoiceRequest { /// Payment hash of invoice pub payment_hash: Option, /// Bolt11 invoice @@ -366,9 +381,9 @@ pub enum TransactionType { Outgoing, } -/// List Transactions Request Params +/// List Transactions Request #[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct ListTransactionsRequestParams { +pub struct ListTransactionsRequest { /// Starting timestamp in seconds since epoch #[serde(skip_serializing_if = "Option::is_none")] pub from: Option, @@ -411,7 +426,7 @@ struct RequestTemplate { impl Request { /// Compose `pay_invoice` request #[inline] - pub fn pay_invoice(params: PayInvoiceRequestParams) -> Self { + pub fn pay_invoice(params: PayInvoiceRequest) -> Self { Self { method: Method::PayInvoice, params: RequestParams::PayInvoice(params), @@ -420,7 +435,7 @@ impl Request { /// Compose `multi_pay_invoice` request #[inline] - pub fn multi_pay_invoice(params: MultiPayInvoiceRequestParams) -> Self { + pub fn multi_pay_invoice(params: MultiPayInvoiceRequest) -> Self { Self { method: Method::MultiPayInvoice, params: RequestParams::MultiPayInvoice(params), @@ -429,7 +444,7 @@ impl Request { /// Compose `pay_keysend` request #[inline] - pub fn pay_keysend(params: PayKeysendRequestParams) -> Self { + pub fn pay_keysend(params: PayKeysendRequest) -> Self { Self { method: Method::PayKeysend, params: RequestParams::PayKeysend(params), @@ -438,7 +453,7 @@ impl Request { /// Compose `make_invoice` request #[inline] - pub fn make_invoice(params: MakeInvoiceRequestParams) -> Self { + pub fn make_invoice(params: MakeInvoiceRequest) -> Self { Self { method: Method::MakeInvoice, params: RequestParams::MakeInvoice(params), @@ -447,7 +462,7 @@ impl Request { /// Compose `lookup_invoice` request #[inline] - pub fn lookup_invoice(params: LookupInvoiceRequestParams) -> Self { + pub fn lookup_invoice(params: LookupInvoiceRequest) -> Self { Self { method: Method::LookupInvoice, params: RequestParams::LookupInvoice(params), @@ -456,7 +471,7 @@ impl Request { /// Compose `list_transactions` request #[inline] - pub fn list_transactions(params: ListTransactionsRequestParams) -> Self { + pub fn list_transactions(params: ListTransactionsRequest) -> Self { Self { method: Method::ListTransactions, params: RequestParams::ListTransactions(params), @@ -487,32 +502,31 @@ impl Request { let params = match template.method { Method::PayInvoice => { - let params: PayInvoiceRequestParams = serde_json::from_value(template.params)?; + let params: PayInvoiceRequest = serde_json::from_value(template.params)?; RequestParams::PayInvoice(params) } Method::MultiPayInvoice => { - let params: MultiPayInvoiceRequestParams = serde_json::from_value(template.params)?; + let params: MultiPayInvoiceRequest = serde_json::from_value(template.params)?; RequestParams::MultiPayInvoice(params) } Method::PayKeysend => { - let params: PayKeysendRequestParams = serde_json::from_value(template.params)?; + let params: PayKeysendRequest = serde_json::from_value(template.params)?; RequestParams::PayKeysend(params) } Method::MultiPayKeysend => { - let params: MultiPayKeysendRequestParams = serde_json::from_value(template.params)?; + let params: MultiPayKeysendRequest = serde_json::from_value(template.params)?; RequestParams::MultiPayKeysend(params) } Method::MakeInvoice => { - let params: MakeInvoiceRequestParams = serde_json::from_value(template.params)?; + let params: MakeInvoiceRequest = serde_json::from_value(template.params)?; RequestParams::MakeInvoice(params) } Method::LookupInvoice => { - let params: LookupInvoiceRequestParams = serde_json::from_value(template.params)?; + let params: LookupInvoiceRequest = serde_json::from_value(template.params)?; RequestParams::LookupInvoice(params) } Method::ListTransactions => { - let params: ListTransactionsRequestParams = - serde_json::from_value(template.params)?; + let params: ListTransactionsRequest = serde_json::from_value(template.params)?; RequestParams::ListTransactions(params) } Method::GetBalance => RequestParams::GetBalance, @@ -552,30 +566,30 @@ impl<'de> Deserialize<'de> for Request { /// NIP47 Response Result #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct PayInvoiceResponseResult { +pub struct PayInvoiceResponse { /// Response preimage pub preimage: String, } /// NIP47 Response Result #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct PayKeysendResponseResult { +pub struct PayKeysendResponse { /// Response preimage pub preimage: String, } -/// NIP47 Response Result +/// Make Invoice Response #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct MakeInvoiceResponseResult { +pub struct MakeInvoiceResponse { /// Bolt 11 invoice pub invoice: String, /// Invoice's payment hash pub payment_hash: String, } -/// NIP47 Response Result +/// Lookup Invoice Response #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct LookupInvoiceResponseResult { +pub struct LookupInvoiceResponse { /// Transaction type #[serde(rename = "type")] #[serde(skip_serializing_if = "Option::is_none")] @@ -611,16 +625,16 @@ pub struct LookupInvoiceResponseResult { pub metadata: Option, } -/// NIP47 `get_balance` Response Result +/// Get Balance Response #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct GetBalanceResponseResult { +pub struct GetBalanceResponse { /// Balance amount in msats pub balance: u64, } -/// NIP47 `get_info` Response Result +/// Get Info Response #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct GetInfoResponseResult { +pub struct GetInfoResponse { /// The alias of the lightning node pub alias: String, /// The color of the current node in hex code format @@ -641,23 +655,23 @@ pub struct GetInfoResponseResult { #[derive(Debug, Clone, PartialEq, Eq)] pub enum ResponseResult { /// Pay Invoice - PayInvoice(PayInvoiceResponseResult), + PayInvoice(PayInvoiceResponse), /// Multiple Pay Invoice - MultiPayInvoice(PayInvoiceResponseResult), + MultiPayInvoice(PayInvoiceResponse), /// Pay Keysend - PayKeysend(PayKeysendResponseResult), + PayKeysend(PayKeysendResponse), /// Multiple Pay Keysend - MultiPayKeysend(PayKeysendResponseResult), + MultiPayKeysend(PayKeysendResponse), /// Make Invoice - MakeInvoice(MakeInvoiceResponseResult), + MakeInvoice(MakeInvoiceResponse), /// Lookup Invoice - LookupInvoice(LookupInvoiceResponseResult), + LookupInvoice(LookupInvoiceResponse), /// List Invoices - ListTransactions(Vec), + ListTransactions(Vec), /// Get Balance - GetBalance(GetBalanceResponseResult), + GetBalance(GetBalanceResponse), /// Get Info - GetInfo(GetInfoResponseResult), + GetInfo(GetInfoResponse), } impl Serialize for ResponseResult { @@ -716,27 +730,27 @@ impl Response { if let Some(result) = template.result { let result = match template.result_type { Method::PayInvoice => { - let result: PayInvoiceResponseResult = serde_json::from_value(result)?; + let result: PayInvoiceResponse = serde_json::from_value(result)?; ResponseResult::PayInvoice(result) } Method::MultiPayInvoice => { - let result: PayInvoiceResponseResult = serde_json::from_value(result)?; + let result: PayInvoiceResponse = serde_json::from_value(result)?; ResponseResult::MultiPayInvoice(result) } Method::PayKeysend => { - let result: PayKeysendResponseResult = serde_json::from_value(result)?; + let result: PayKeysendResponse = serde_json::from_value(result)?; ResponseResult::PayKeysend(result) } Method::MultiPayKeysend => { - let result: PayKeysendResponseResult = serde_json::from_value(result)?; + let result: PayKeysendResponse = serde_json::from_value(result)?; ResponseResult::MultiPayKeysend(result) } Method::MakeInvoice => { - let result: MakeInvoiceResponseResult = serde_json::from_value(result)?; + let result: MakeInvoiceResponse = serde_json::from_value(result)?; ResponseResult::MakeInvoice(result) } Method::LookupInvoice => { - let result: LookupInvoiceResponseResult = serde_json::from_value(result)?; + let result: LookupInvoiceResponse = serde_json::from_value(result)?; ResponseResult::LookupInvoice(result) } Method::ListTransactions => { @@ -744,16 +758,15 @@ impl Response { result.get("transactions").cloned().ok_or_else(|| { Error::UnexpectedResult(String::from("Missing 'transactions' field")) })?; - let result: Vec = - serde_json::from_value(transactions)?; + let result: Vec = serde_json::from_value(transactions)?; ResponseResult::ListTransactions(result) } Method::GetBalance => { - let result: GetBalanceResponseResult = serde_json::from_value(result)?; + let result: GetBalanceResponse = serde_json::from_value(result)?; ResponseResult::GetBalance(result) } Method::GetInfo => { - let result: GetInfoResponseResult = serde_json::from_value(result)?; + let result: GetInfoResponse = serde_json::from_value(result)?; ResponseResult::GetInfo(result) } }; @@ -772,8 +785,8 @@ impl Response { } } - /// Covert [Response] to [PayInvoiceResponseResult] - pub fn to_pay_invoice(self) -> Result { + /// Covert [Response] to [PayInvoiceResponse] + pub fn to_pay_invoice(self) -> Result { if let Some(e) = self.error { return Err(Error::ErrorCode(e)); } @@ -785,8 +798,8 @@ impl Response { Err(Error::UnexpectedResult(self.as_json())) } - /// Covert [Response] to [PayKeysendResponseResult] - pub fn to_pay_keysend(self) -> Result { + /// Covert [Response] to [PayKeysendResponse] + pub fn to_pay_keysend(self) -> Result { if let Some(e) = self.error { return Err(Error::ErrorCode(e)); } @@ -798,8 +811,8 @@ impl Response { Err(Error::UnexpectedResult(self.as_json())) } - /// Covert [Response] to [MakeInvoiceResponseResult] - pub fn to_make_invoice(self) -> Result { + /// Covert [Response] to [MakeInvoiceResponse] + pub fn to_make_invoice(self) -> Result { if let Some(e) = self.error { return Err(Error::ErrorCode(e)); } @@ -811,8 +824,8 @@ impl Response { Err(Error::UnexpectedResult(self.as_json())) } - /// Covert [Response] to [LookupInvoiceResponseResult] - pub fn to_lookup_invoice(self) -> Result { + /// Covert [Response] to [LookupInvoiceResponse] + pub fn to_lookup_invoice(self) -> Result { if let Some(e) = self.error { return Err(Error::ErrorCode(e)); } @@ -824,8 +837,8 @@ impl Response { Err(Error::UnexpectedResult(self.as_json())) } - /// Covert [Response] to list of [LookupInvoiceResponseResult] - pub fn to_list_transactions(self) -> Result, Error> { + /// Covert [Response] to list of [LookupInvoiceResponse] + pub fn to_list_transactions(self) -> Result, Error> { if let Some(e) = self.error { return Err(Error::ErrorCode(e)); } @@ -837,8 +850,8 @@ impl Response { Err(Error::UnexpectedResult(self.as_json())) } - /// Covert [Response] to [GetBalanceResponseResult] - pub fn to_get_balance(self) -> Result { + /// Covert [Response] to [GetBalanceResponse] + pub fn to_get_balance(self) -> Result { if let Some(e) = self.error { return Err(Error::ErrorCode(e)); } @@ -850,8 +863,8 @@ impl Response { Err(Error::UnexpectedResult(self.as_json())) } - /// Covert [Response] to [GetInfoResponseResult] - pub fn to_get_info(self) -> Result { + /// Covert [Response] to [GetInfoResponse] + pub fn to_get_info(self) -> Result { if let Some(e) = self.error { return Err(Error::ErrorCode(e)); } @@ -1065,7 +1078,7 @@ mod tests { fn serialize_request() { let request = Request { method: Method::PayInvoice, - params: RequestParams::PayInvoice(PayInvoiceRequestParams { id: None, invoice: "lnbc210n1pj99rx0pp5ehevgz9nf7d97h05fgkdeqxzytm6yuxd7048axru03fpzxxvzt7shp5gv7ef0s26pw5gy5dpwvsh6qgc8se8x2lmz2ev90l9vjqzcns6u6scqzzsxqyz5vqsp".to_string(), amount: None }), + params: RequestParams::PayInvoice(PayInvoiceRequest { id: None, invoice: "lnbc210n1pj99rx0pp5ehevgz9nf7d97h05fgkdeqxzytm6yuxd7048axru03fpzxxvzt7shp5gv7ef0s26pw5gy5dpwvsh6qgc8se8x2lmz2ev90l9vjqzcns6u6scqzzsxqyz5vqsp".to_string(), amount: None }), }; assert_eq!(Request::from_json(request.as_json()).unwrap(), request); @@ -1113,7 +1126,7 @@ mod tests { assert_eq!( result.result, Some(ResponseResult::ListTransactions(vec![ - LookupInvoiceResponseResult { + LookupInvoiceResponse { transaction_type: Some(TransactionType::Incoming), invoice: Some(String::from("abcd")), description: Some(String::from("string")), diff --git a/crates/nwc/examples/nwc.rs b/crates/nwc/examples/nwc.rs index 92654696e..305dfce26 100644 --- a/crates/nwc/examples/nwc.rs +++ b/crates/nwc/examples/nwc.rs @@ -22,13 +22,13 @@ async fn main() -> Result<()> { NostrWalletConnectURI::from_str(&nwc_uri_string).expect("Failed to parse NWC URI"); let nwc: NWC = NWC::new(uri); - // Get info - let info: GetInfoResponseResult = nwc.get_info().await?; - println!("{info:?}"); - // Get balance - let balance: u64 = nwc.get_balance().await?; - println!("Balance: {balance}"); + let balance = nwc.get_balance().await?; + println!("Balance: {balance} msat"); + + let request: PayInvoiceRequest = PayInvoiceRequest::new(""); + let response = nwc.pay_invoice(request).await?; + println!("Response: {response:?}"); Ok(()) } diff --git a/crates/nwc/src/lib.rs b/crates/nwc/src/lib.rs index 0b6f68658..07ac08532 100644 --- a/crates/nwc/src/lib.rs +++ b/crates/nwc/src/lib.rs @@ -128,9 +128,9 @@ impl NWC { /// Pay invoice pub async fn pay_invoice( &self, - params: PayInvoiceRequestParams, - ) -> Result { - let req = Request::pay_invoice(params); + request: PayInvoiceRequest, + ) -> Result { + let req = Request::pay_invoice(request); let res: Response = self.send_request(req).await?; Ok(res.to_pay_invoice()?) } @@ -138,9 +138,9 @@ impl NWC { /// Pay keysend pub async fn pay_keysend( &self, - params: PayKeysendRequestParams, - ) -> Result { - let req = Request::pay_keysend(params); + request: PayKeysendRequest, + ) -> Result { + let req = Request::pay_keysend(request); let res: Response = self.send_request(req).await?; Ok(res.to_pay_keysend()?) } @@ -148,9 +148,9 @@ impl NWC { /// Create invoice pub async fn make_invoice( &self, - params: MakeInvoiceRequestParams, - ) -> Result { - let req: Request = Request::make_invoice(params); + request: MakeInvoiceRequest, + ) -> Result { + let req: Request = Request::make_invoice(request); let res: Response = self.send_request(req).await?; Ok(res.to_make_invoice()?) } @@ -158,9 +158,9 @@ impl NWC { /// Lookup invoice pub async fn lookup_invoice( &self, - params: LookupInvoiceRequestParams, - ) -> Result { - let req = Request::lookup_invoice(params); + request: LookupInvoiceRequest, + ) -> Result { + let req = Request::lookup_invoice(request); let res: Response = self.send_request(req).await?; Ok(res.to_lookup_invoice()?) } @@ -168,23 +168,23 @@ impl NWC { /// List transactions pub async fn list_transactions( &self, - params: ListTransactionsRequestParams, - ) -> Result, Error> { + params: ListTransactionsRequest, + ) -> Result, Error> { let req = Request::list_transactions(params); let res: Response = self.send_request(req).await?; Ok(res.to_list_transactions()?) } - /// Get balance + /// Get balance (msat) pub async fn get_balance(&self) -> Result { let req = Request::get_balance(); let res: Response = self.send_request(req).await?; - let GetBalanceResponseResult { balance } = res.to_get_balance()?; + let GetBalanceResponse { balance } = res.to_get_balance()?; Ok(balance) } /// Get info - pub async fn get_info(&self) -> Result { + pub async fn get_info(&self) -> Result { let req = Request::get_info(); let res: Response = self.send_request(req).await?; Ok(res.to_get_info()?) @@ -207,12 +207,8 @@ impl NostrZapper for NWC { #[inline] async fn pay(&self, invoice: String) -> Result<(), ZapperError> { - let params = PayInvoiceRequestParams { - invoice, - id: None, - amount: None, - }; - self.pay_invoice(params) + let request: PayInvoiceRequest = PayInvoiceRequest::new(invoice); + self.pay_invoice(request) .await .map_err(ZapperError::backend)?; Ok(())