@@ -32,6 +32,8 @@ use bdk_wallet::miniscript::psbt::PsbtExt;
3232use bdk_wallet:: serde_json;
3333
3434use std:: fmt:: Display ;
35+ use std:: fs:: File ;
36+ use std:: io:: { BufReader , BufWriter } ;
3537use std:: ops:: Deref ;
3638use std:: str:: FromStr ;
3739use std:: sync:: { Arc , Mutex } ;
@@ -495,6 +497,15 @@ impl Psbt {
495497 Ok ( Arc :: new ( Psbt ( Mutex :: new ( psbt) ) ) )
496498 }
497499
500+ /// Create a new `Psbt` from a `.psbt` file.
501+ #[ uniffi:: constructor]
502+ pub ( crate ) fn from_file ( path : String ) -> Result < Self , PsbtError > {
503+ let file = File :: open ( path) ?;
504+ let mut buf_read = BufReader :: new ( file) ;
505+ let psbt: BdkPsbt = BdkPsbt :: deserialize_from_reader ( & mut buf_read) ?;
506+ Ok ( Psbt ( Mutex :: new ( psbt) ) )
507+ }
508+
498509 /// Serialize the PSBT into a base64-encoded string.
499510 pub ( crate ) fn serialize ( & self ) -> String {
500511 let psbt = self . 0 . lock ( ) . unwrap ( ) . clone ( ) ;
@@ -566,6 +577,15 @@ impl Psbt {
566577 }
567578 }
568579
580+ /// Write the `Psbt` to a file. Note that the file must not yet exist.
581+ pub ( crate ) fn write_to_file ( & self , path : String ) -> Result < ( ) , PsbtError > {
582+ let file = File :: create_new ( path) ?;
583+ let mut writer = BufWriter :: new ( file) ;
584+ let psbt = self . 0 . lock ( ) . unwrap ( ) ;
585+ psbt. serialize_to_writer ( & mut writer) ?;
586+ Ok ( ( ) )
587+ }
588+
569589 /// Serializes the PSBT into a JSON string representation.
570590 pub ( crate ) fn json_serialize ( & self ) -> String {
571591 let psbt = self . 0 . lock ( ) . unwrap ( ) ;
0 commit comments