Skip to content

Conversation

@rustaceanrob
Copy link
Collaborator

The Rust API makes it trivially simple to read and write PSBT to and from files. Bindings users may make use of this in air-gapped signing flows. The PsbtError already includes an Io variant, so we may make use of that error.

Notes to the reviewers

Discovered while working on a desktop app.

Changelog notice

  • Read and write PSBT to and from files

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature


/// 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(path)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wondering your thoughts on this: doc comment says "Note that the file must not yet exist." but File::create() "will truncate it if it does" (exist) per docs. Should this use File::create_new() instead to prevent overwrites? Or I could be misunderstanding workflow/behavior too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call, will swap to create new.

Copy link
Collaborator

Choose a reason for hiding this comment

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

cool, this looks good otherwise so I'll keep an eye on it and merge when swapped (and rebased if needed)

Comment on lines +1441 to +1456
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(),
}
}
}

Copy link
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 👍

let file = File::create(path)?;
let mut writer = BufWriter::new(file);
let psbt = self.0.lock().unwrap();
psbt.serialize_to_writer(&mut writer)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The Rust API makes it trivially simple to read and write PSBT to and
from files. Bindings users may make use of this in air-gapped signing
flows. The `PsbtError` already includes an `Io` variant, so we may make
use of that error.
@rustaceanrob
Copy link
Collaborator Author

Rebased and now using create_new

Copy link
Collaborator

@reez reez left a comment

Choose a reason for hiding this comment

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

ACK 4f5d092

This is great

@reez reez merged commit 010a6a9 into bitcoindevkit:master Jul 10, 2025
24 checks passed
@rustaceanrob rustaceanrob deleted the psbt-6-5 branch July 10, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants