Skip to content
Merged
Show file tree
Hide file tree
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 bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use bdk_wallet::miniscript::psbt::PsbtExt;
use bdk_wallet::serde_json;

use std::fmt::Display;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -495,6 +497,15 @@ impl Psbt {
Ok(Arc::new(Psbt(Mutex::new(psbt))))
}

/// Create a new `Psbt` from a `.psbt` file.
#[uniffi::constructor]
pub(crate) fn from_file(path: String) -> Result<Self, PsbtError> {
let file = File::open(path)?;
let mut buf_read = BufReader::new(file);
let psbt: BdkPsbt = BdkPsbt::deserialize_from_reader(&mut buf_read)?;
Ok(Psbt(Mutex::new(psbt)))
}

/// Serialize the PSBT into a base64-encoded string.
pub(crate) fn serialize(&self) -> String {
let psbt = self.0.lock().unwrap().clone();
Expand Down Expand Up @@ -566,6 +577,15 @@ impl Psbt {
}
}

/// Write the `Psbt` to a file. Note that the file must not yet exist.
pub(crate) fn write_to_file(&self, path: String) -> Result<(), PsbtError> {
let file = File::create_new(path)?;
let mut writer = BufWriter::new(file);
let psbt = self.0.lock().unwrap();
psbt.serialize_to_writer(&mut writer)?;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok(())
}

/// Serializes the PSBT into a JSON string representation.
pub(crate) fn json_serialize(&self) -> String {
let psbt = self.0.lock().unwrap();
Expand Down
16 changes: 16 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,22 @@ impl From<BdkPsbtParseError> for PsbtParseError {
}
}

impl From<std::io::Error> for PsbtError {
fn from(error: std::io::Error) -> Self {
PsbtError::Io {
error_message: error.to_string(),
}
}
}

impl From<bdk_wallet::bitcoin::io::Error> for PsbtError {
fn from(error: bdk_wallet::bitcoin::io::Error) -> Self {
PsbtError::Io {
error_message: error.to_string(),
}
}
}

Comment on lines +1441 to +1456
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these 👍

impl From<BdkPsbtFinalizeError> for PsbtFinalizeError {
fn from(value: BdkPsbtFinalizeError) -> Self {
match value {
Expand Down
Loading