diff --git a/wallet/core/src/api/message.rs b/wallet/core/src/api/message.rs index b12a383952..95ddfcbdc3 100644 --- a/wallet/core/src/api/message.rs +++ b/wallet/core/src/api/message.rs @@ -535,12 +535,25 @@ pub struct AccountsPskbBroadcastRequest { pub pskb: String, } +#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] +#[serde(rename_all = "camelCase")] +pub struct PskbBroadcastRequest { + pub pskb: String, + pub network_id: NetworkId, +} + #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] #[serde(rename_all = "camelCase")] pub struct AccountsPskbBroadcastResponse { pub transaction_ids: Vec, } +#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] +#[serde(rename_all = "camelCase")] +pub struct PskbBroadcastResponse { + pub transaction_ids: Vec, +} + #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] #[serde(rename_all = "camelCase")] pub struct AccountsPskbSendRequest { diff --git a/wallet/core/src/api/traits.rs b/wallet/core/src/api/traits.rs index 47ac1e0c5f..3ab3ebdb1f 100644 --- a/wallet/core/src/api/traits.rs +++ b/wallet/core/src/api/traits.rs @@ -428,6 +428,14 @@ pub trait WalletApi: Send + Sync + AnySync { self.accounts_pskb_broadcast_call(request).await } + /// Wrapper around [`pskb_broadcast_call()`](Self::pskb_broadcast_call) + async fn pskb_broadcast(self: Arc, request: PskbBroadcastRequest) -> Result { + self.pskb_broadcast_call(request).await + } + + /// Broadcast a PSKB. + async fn pskb_broadcast_call(self: Arc, request: PskbBroadcastRequest) -> Result; + /// Broadcast a PSKB. async fn accounts_pskb_broadcast_call( self: Arc, diff --git a/wallet/core/src/api/transport.rs b/wallet/core/src/api/transport.rs index f3fda45eff..398420f777 100644 --- a/wallet/core/src/api/transport.rs +++ b/wallet/core/src/api/transport.rs @@ -101,6 +101,7 @@ impl WalletApi for WalletClient { AccountsSend, AccountsPskbSign, AccountsPskbBroadcast, + PskbBroadcast, AccountsPskbSend, AccountsGetUtxos, AccountsTransfer, @@ -187,6 +188,7 @@ impl WalletServer { AccountsSend, AccountsPskbSign, AccountsPskbBroadcast, + PskbBroadcast, AccountsPskbSend, AccountsGetUtxos, AccountsTransfer, diff --git a/wallet/core/src/wallet/api.rs b/wallet/core/src/wallet/api.rs index 409ce1b04e..28a6ae673d 100644 --- a/wallet/core/src/wallet/api.rs +++ b/wallet/core/src/wallet/api.rs @@ -2,6 +2,7 @@ //! [`WalletApi`] trait implementation for the [`Wallet`] struct. //! +use crate::account::pskb::bundle_to_finalizer_stream; use crate::api::{message::*, traits::WalletApi}; use crate::events::Events; use crate::imports::*; @@ -11,6 +12,7 @@ use crate::storage::Binding; use crate::tx::Fees; use kaspa_rpc_core::RpcFeeEstimate; use kaspa_wallet_pskt::bundle::Bundle; + use workflow_core::channel::Receiver; #[async_trait] impl WalletApi for super::Wallet { @@ -432,6 +434,36 @@ impl WalletApi for super::Wallet { Ok(AccountsPskbBroadcastResponse { transaction_ids }) } + async fn pskb_broadcast_call(self: Arc, request: PskbBroadcastRequest) -> Result { + let PskbBroadcastRequest { pskb, network_id } = request; + let pskb = Bundle::deserialize(&pskb)?; + + let mut transaction_ids = Vec::new(); + let mut stream = bundle_to_finalizer_stream(&pskb); + let rpc = self.rpc_api(); + while let Some(result) = stream.next().await { + match result { + Ok(finalized_pskt) => { + let signed_tx = match finalized_pskt.extractor() { + Ok(extractor) => match extractor.extract_tx(&network_id.into()) { + Ok(tx) => tx.tx, + Err(e) => return Err(Error::PendingTransactionFromPSKTError(e.to_string())), + }, + Err(e) => return Err(Error::PendingTransactionFromPSKTError(e.to_string())), + }; + log_info!("Submitting to rpc"); + transaction_ids.push(rpc.submit_transaction((&signed_tx).into(), false).await?); + log_info!("Submitted to rpc"); + } + Err(e) => { + log_info!("Error processing a PSKT from bundle: {:?}", e); + } + } + } + + Ok(PskbBroadcastResponse { transaction_ids }) + } + async fn accounts_get_utxos_call(self: Arc, request: AccountsGetUtxosRequest) -> Result { let AccountsGetUtxosRequest { account_id, addresses, min_amount_sompi } = request; let guard = self.guard(); diff --git a/wallet/core/src/wasm/api/message.rs b/wallet/core/src/wasm/api/message.rs index 2728227311..eed5b29b34 100644 --- a/wallet/core/src/wasm/api/message.rs +++ b/wallet/core/src/wasm/api/message.rs @@ -1578,6 +1578,47 @@ try_from! ( args: AccountsPskbBroadcastResponse, IAccountsPskbBroadcastResponse, Ok(to_value(&args)?.into()) }); +// --- + +declare! { + IPskbBroadcastRequest, + r#" + /** + * + * + * @category Wallet API + */ + export interface IPskbBroadcastRequest { + pskb : string; + networkId : NetworkId | string; + } + "#, +} + +try_from! ( args: IPskbBroadcastRequest, PskbBroadcastRequest, { + let pskb = args.get_string("pskb")?; + let network_id = args.get_network_id("networkId")?; + Ok(PskbBroadcastRequest { pskb, network_id }) +}); + +declare! { + IPskbBroadcastResponse, + r#" + /** + * + * + * @category Wallet API + */ + export interface IPskbBroadcastResponse { + transactionIds : HexString[]; + } + "#, +} + +try_from! ( args: PskbBroadcastResponse, IPskbBroadcastResponse, { + Ok(to_value(&args)?.into()) +}); + declare! { IAccountsPskbSendRequest, r#" diff --git a/wallet/core/src/wasm/api/mod.rs b/wallet/core/src/wasm/api/mod.rs index 2f34d62fb0..4d9d852f15 100644 --- a/wallet/core/src/wasm/api/mod.rs +++ b/wallet/core/src/wasm/api/mod.rs @@ -48,6 +48,7 @@ declare_wasm_handlers!([ AccountsSend, AccountsPskbSign, AccountsPskbBroadcast, + PskbBroadcast, AccountsPskbSend, AccountsGetUtxos, AccountsTransfer,