Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/types/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{ops::Deref, str::FromStr};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsError;

use bdk_wallet::bitcoin::consensus::{deserialize, serialize};
use bdk_wallet::bitcoin::{Transaction as BdkTransaction, Txid as BdkTxid};

use crate::result::JsResult;
Expand Down Expand Up @@ -118,6 +120,24 @@ impl Transaction {
Ok(output.into())
}

/// Serialize the transaction to consensus-encoded bytes (BIP144 format).
///
/// Returns the raw transaction as a `Uint8Array` suitable for broadcasting,
/// passing to other WASM modules (e.g. LDK), or storing in IndexedDB.
pub fn to_bytes(&self) -> Vec<u8> {
serialize(&self.0)
}

/// Deserialize a transaction from consensus-encoded bytes.
///
/// Accepts a `Uint8Array` of raw transaction bytes (as produced by `to_bytes()`
/// or any standard Bitcoin serializer).
pub fn from_bytes(bytes: &[u8]) -> JsResult<Transaction> {
let tx: BdkTransaction =
deserialize(bytes).map_err(|e| JsError::new(&format!("Failed to deserialize transaction: {e}")))?;
Ok(Transaction(tx))
}

#[wasm_bindgen(js_name = clone)]
pub fn js_clone(&self) -> Transaction {
self.clone()
Expand Down
Loading