2021-03-03 13:22:05 -08:00
// Bitcoin Dev Kit
// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
2020-08-31 11:26:36 +02:00
//
2021-03-03 13:22:05 -08:00
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
2020-08-31 11:26:36 +02:00
//
2021-03-03 13:22:05 -08:00
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.
2020-08-31 11:26:36 +02:00
2020-09-04 15:45:11 +02:00
//! Wallet export
//!
//! This modules implements the wallet export format used by [FullyNoded](https://github.com/Fonta1n3/FullyNoded/blob/10b7808c8b929b171cca537fb50522d015168ac9/Docs/Wallets/Wallet-Export-Spec.md).
//!
//! ## Examples
//!
//! ### Import from JSON
//!
//! ```
//! # use std::str::FromStr;
//! # use bitcoin::*;
2020-09-14 14:25:38 +02:00
//! # use bdk::database::*;
//! # use bdk::wallet::export::*;
//! # use bdk::*;
2020-09-04 15:45:11 +02:00
//! let import = r#"{
//! "descriptor": "wpkh([c258d2e4\/84h\/1h\/0h]tpubDD3ynpHgJQW8VvWRzQ5WFDCrs4jqVFGHB3vLC3r49XHJSqP8bHKdK4AriuUKLccK68zfzowx7YhmDN8SiSkgCDENUFx9qVw65YyqM78vyVe\/0\/*)",
//! "blockheight":1782088,
//! "label":"testnet"
//! }"#;
//!
//! let import = WalletExport::from_str(import)?;
2020-12-23 13:48:17 +11:00
//! let wallet = Wallet::new_offline(
2020-12-16 14:19:37 -08:00
//! &import.descriptor(),
//! import.change_descriptor().as_ref(),
//! Network::Testnet,
//! MemoryDatabase::default(),
//! )?;
2020-09-14 14:25:38 +02:00
//! # Ok::<_, bdk::Error>(())
2020-09-04 15:45:11 +02:00
//! ```
//!
//! ### Export a `Wallet`
//! ```
//! # use bitcoin::*;
2020-09-14 14:25:38 +02:00
//! # use bdk::database::*;
//! # use bdk::wallet::export::*;
//! # use bdk::*;
2020-12-23 13:48:17 +11:00
//! let wallet = Wallet::new_offline(
2020-09-04 15:45:11 +02:00
//! "wpkh([c258d2e4/84h/1h/0h]tpubDD3ynpHgJQW8VvWRzQ5WFDCrs4jqVFGHB3vLC3r49XHJSqP8bHKdK4AriuUKLccK68zfzowx7YhmDN8SiSkgCDENUFx9qVw65YyqM78vyVe/0/*)",
//! Some("wpkh([c258d2e4/84h/1h/0h]tpubDD3ynpHgJQW8VvWRzQ5WFDCrs4jqVFGHB3vLC3r49XHJSqP8bHKdK4AriuUKLccK68zfzowx7YhmDN8SiSkgCDENUFx9qVw65YyqM78vyVe/1/*)"),
//! Network::Testnet,
//! MemoryDatabase::default()
//! )?;
//! let export = WalletExport::export_wallet(&wallet, "exported wallet", true)
//! .map_err(ToString::to_string)
2020-09-14 14:25:38 +02:00
//! .map_err(bdk::Error::Generic)?;
2020-09-04 15:45:11 +02:00
//!
//! println!("Exported: {}", export.to_string());
2020-09-14 14:25:38 +02:00
//! # Ok::<_, bdk::Error>(())
2020-09-04 15:45:11 +02:00
//! ```
2020-08-07 10:19:06 +02:00
use std ::str ::FromStr ;
use serde ::{ Deserialize , Serialize } ;
2021-02-02 20:06:40 -05:00
use miniscript ::descriptor ::{ ShInner , WshInner } ;
2020-11-16 22:07:38 +01:00
use miniscript ::{ Descriptor , DescriptorPublicKey , ScriptContext , Terminal } ;
2020-08-07 10:19:06 +02:00
use crate ::database ::BatchDatabase ;
use crate ::wallet ::Wallet ;
2020-09-04 15:45:11 +02:00
/// Structure that contains the export of a wallet
///
/// For a usage example see [this module](crate::wallet::export)'s documentation.
2020-08-07 10:19:06 +02:00
#[ derive(Debug, Serialize, Deserialize) ]
pub struct WalletExport {
descriptor : String ,
2020-09-04 15:45:11 +02:00
/// Earliest block to rescan when looking for the wallet's transactions
2020-08-07 10:19:06 +02:00
pub blockheight : u32 ,
2020-09-04 15:45:11 +02:00
/// Arbitrary label for the wallet
2020-08-07 10:19:06 +02:00
pub label : String ,
}
2020-08-10 13:20:48 +02:00
impl ToString for WalletExport {
fn to_string ( & self ) -> String {
serde_json ::to_string ( self ) . unwrap ( )
}
}
impl FromStr for WalletExport {
type Err = serde_json ::Error ;
fn from_str ( s : & str ) -> Result < Self , Self ::Err > {
serde_json ::from_str ( s )
}
}
2021-02-02 20:06:40 -05:00
fn remove_checksum ( s : String ) -> String {
s . splitn ( 2 , '#' ) . next ( ) . map ( String ::from ) . unwrap ( )
}
2020-08-07 10:19:06 +02:00
impl WalletExport {
2020-09-04 15:45:11 +02:00
/// Export a wallet
///
/// This function returns an error if it determines that the `wallet`'s descriptor(s) are not
/// supported by Bitcoin Core or don't follow the standard derivation paths defined by BIP44
/// and others.
///
/// If `include_blockheight` is `true`, this function will look into the `wallet`'s database
/// for the oldest transaction it knows and use that as the earliest block to rescan.
///
/// If the database is empty or `include_blockheight` is false, the `blockheight` field
/// returned will be `0`.
2020-12-23 13:48:17 +11:00
pub fn export_wallet < B , D : BatchDatabase > (
2020-08-07 10:19:06 +02:00
wallet : & Wallet < B , D > ,
label : & str ,
include_blockheight : bool ,
) -> Result < Self , & 'static str > {
2020-08-12 12:51:50 +02:00
let descriptor = wallet
. descriptor
2020-11-16 22:07:38 +01:00
. to_string_with_secret ( & wallet . signers . as_key_map ( wallet . secp_ctx ( ) ) ) ;
2021-02-02 20:06:40 -05:00
let descriptor = remove_checksum ( descriptor ) ;
2020-08-07 10:19:06 +02:00
Self ::is_compatible_with_core ( & descriptor ) ? ;
let blockheight = match wallet . database . borrow ( ) . iter_txs ( false ) {
_ if ! include_blockheight = > 0 ,
Err ( _ ) = > 0 ,
Ok ( txs ) = > {
let mut heights = txs
. into_iter ( )
. map ( | tx | tx . height . unwrap_or ( 0 ) )
. collect ::< Vec < _ > > ( ) ;
2020-10-08 16:55:49 -07:00
heights . sort_unstable ( ) ;
2020-08-07 10:19:06 +02:00
* heights . last ( ) . unwrap_or ( & 0 )
}
} ;
let export = WalletExport {
descriptor ,
label : label . into ( ) ,
blockheight ,
} ;
2020-11-16 22:07:38 +01:00
let desc_to_string = | d : & Descriptor < DescriptorPublicKey > | {
2021-02-02 20:06:40 -05:00
let descriptor =
d . to_string_with_secret ( & wallet . change_signers . as_key_map ( wallet . secp_ctx ( ) ) ) ;
remove_checksum ( descriptor )
2020-11-16 22:07:38 +01:00
} ;
if export . change_descriptor ( ) ! = wallet . change_descriptor . as_ref ( ) . map ( desc_to_string ) {
2020-08-07 10:19:06 +02:00
return Err ( " Incompatible change descriptor " ) ;
}
Ok ( export )
}
fn is_compatible_with_core ( descriptor : & str ) -> Result < ( ) , & 'static str > {
fn check_ms < Ctx : ScriptContext > (
2021-02-02 20:06:40 -05:00
terminal : & Terminal < String , Ctx > ,
2020-08-07 10:19:06 +02:00
) -> Result < ( ) , & 'static str > {
if let Terminal ::Multi ( _ , _ ) = terminal {
Ok ( ( ) )
} else {
Err ( " The descriptor contains operators not supported by Bitcoin Core " )
}
}
2021-02-02 20:06:40 -05:00
// pkh(), wpkh(), sh(wpkh()) are always fine, as well as multi() and sortedmulti()
2020-08-07 10:19:06 +02:00
match Descriptor ::< String > ::from_str ( descriptor ) . map_err ( | _ | " Invalid descriptor " ) ? {
2021-02-02 20:06:40 -05:00
Descriptor ::Pkh ( _ ) | Descriptor ::Wpkh ( _ ) = > Ok ( ( ) ) ,
Descriptor ::Sh ( sh ) = > match sh . as_inner ( ) {
ShInner ::Wpkh ( _ ) = > Ok ( ( ) ) ,
ShInner ::SortedMulti ( _ ) = > Ok ( ( ) ) ,
ShInner ::Wsh ( wsh ) = > match wsh . as_inner ( ) {
WshInner ::SortedMulti ( _ ) = > Ok ( ( ) ) ,
WshInner ::Ms ( ms ) = > check_ms ( & ms . node ) ,
} ,
ShInner ::Ms ( ms ) = > check_ms ( & ms . node ) ,
} ,
Descriptor ::Wsh ( wsh ) = > match wsh . as_inner ( ) {
WshInner ::SortedMulti ( _ ) = > Ok ( ( ) ) ,
WshInner ::Ms ( ms ) = > check_ms ( & ms . node ) ,
} ,
2020-08-07 10:19:06 +02:00
_ = > Err ( " The descriptor is not compatible with Bitcoin Core " ) ,
}
}
2020-09-04 15:45:11 +02:00
/// Return the external descriptor
2020-08-07 10:19:06 +02:00
pub fn descriptor ( & self ) -> String {
self . descriptor . clone ( )
}
2020-09-04 15:45:11 +02:00
/// Return the internal descriptor, if present
2020-08-07 10:19:06 +02:00
pub fn change_descriptor ( & self ) -> Option < String > {
let replaced = self . descriptor . replace ( " /0/* " , " /1/* " ) ;
if replaced ! = self . descriptor {
Some ( replaced )
} else {
None
}
}
}
#[ cfg(test) ]
mod test {
use std ::str ::FromStr ;
use bitcoin ::{ Network , Txid } ;
use super ::* ;
use crate ::database ::{ memory ::MemoryDatabase , BatchOperations } ;
use crate ::types ::TransactionDetails ;
2020-12-23 13:48:17 +11:00
use crate ::wallet ::Wallet ;
2020-08-07 10:19:06 +02:00
fn get_test_db ( ) -> MemoryDatabase {
let mut db = MemoryDatabase ::new ( ) ;
db . set_tx ( & TransactionDetails {
transaction : None ,
txid : Txid ::from_str (
" 4ddff1fa33af17f377f62b72357b43107c19110a8009b36fb832af505efed98a " ,
)
. unwrap ( ) ,
timestamp : 12345678 ,
received : 100_000 ,
sent : 0 ,
2020-08-10 10:49:34 +02:00
fees : 500 ,
2020-08-07 10:19:06 +02:00
height : Some ( 5000 ) ,
} )
. unwrap ( ) ;
db
}
#[ test ]
fn test_export_bip44 ( ) {
let descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/0/*) " ;
let change_descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/1/*) " ;
2020-12-23 13:48:17 +11:00
let wallet = Wallet ::new_offline (
2020-08-07 10:19:06 +02:00
descriptor ,
Some ( change_descriptor ) ,
2020-09-21 15:44:07 +02:00
Network ::Bitcoin ,
2020-08-07 10:19:06 +02:00
get_test_db ( ) ,
)
. unwrap ( ) ;
let export = WalletExport ::export_wallet ( & wallet , " Test Label " , true ) . unwrap ( ) ;
assert_eq! ( export . descriptor ( ) , descriptor ) ;
assert_eq! ( export . change_descriptor ( ) , Some ( change_descriptor . into ( ) ) ) ;
assert_eq! ( export . blockheight , 5000 ) ;
assert_eq! ( export . label , " Test Label " ) ;
}
#[ test ]
#[ should_panic(expected = " Incompatible change descriptor " ) ]
fn test_export_no_change ( ) {
// This wallet explicitly doesn't have a change descriptor. It should be impossible to
// export, because exporting this kind of external descriptor normally implies the
// existence of an internal descriptor
let descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/0/*) " ;
2020-12-23 13:48:17 +11:00
let wallet =
2020-09-21 15:44:07 +02:00
Wallet ::new_offline ( descriptor , None , Network ::Bitcoin , get_test_db ( ) ) . unwrap ( ) ;
2020-08-07 10:19:06 +02:00
WalletExport ::export_wallet ( & wallet , " Test Label " , true ) . unwrap ( ) ;
}
#[ test ]
#[ should_panic(expected = " Incompatible change descriptor " ) ]
fn test_export_incompatible_change ( ) {
// This wallet has a change descriptor, but the derivation path is not in the "standard"
// bip44/49/etc format
let descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/0/*) " ;
let change_descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/50'/0'/1/*) " ;
2020-12-23 13:48:17 +11:00
let wallet = Wallet ::new_offline (
2020-08-07 10:19:06 +02:00
descriptor ,
Some ( change_descriptor ) ,
2020-09-21 15:44:07 +02:00
Network ::Bitcoin ,
2020-08-07 10:19:06 +02:00
get_test_db ( ) ,
)
. unwrap ( ) ;
WalletExport ::export_wallet ( & wallet , " Test Label " , true ) . unwrap ( ) ;
}
#[ test ]
fn test_export_multi ( ) {
let descriptor = " wsh(multi(2, \
2020-08-12 12:51:50 +02:00
[ 73756 c7f / 48 ' / 0 ' / 0 ' / 2 ' ] tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3 / 0 /* ,\
[ f9f62194 / 48 ' / 0 ' / 0 ' / 2 ' ] tpubDDp3ZSH1yCwusRppH7zgSxq2t1VEUyXSeEp8E5aFS8m43MknUjiF1bSLo3CGWAxbDyhF1XowA5ukPzyJZjznYk3kYi6oe7QxtX2euvKWsk4 / 0 /* ,\
[ c98b1535 / 48 ' / 0 ' / 0 ' / 2 ' ] tpubDCDi5W4sP6zSnzJeowy8rQDVhBdRARaPhK1axABi8V1661wEPeanpEXj4ZLAUEoikVtoWcyK26TKKJSecSfeKxwHCcRrge9k1ybuiL71z4a / 0 /* \
2020-08-07 10:19:06 +02:00
) ) " ;
let change_descriptor = " wsh(multi(2, \
2020-08-12 12:51:50 +02:00
[ 73756 c7f / 48 ' / 0 ' / 0 ' / 2 ' ] tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3 / 1 /* ,\
[ f9f62194 / 48 ' / 0 ' / 0 ' / 2 ' ] tpubDDp3ZSH1yCwusRppH7zgSxq2t1VEUyXSeEp8E5aFS8m43MknUjiF1bSLo3CGWAxbDyhF1XowA5ukPzyJZjznYk3kYi6oe7QxtX2euvKWsk4 / 1 /* ,\
[ c98b1535 / 48 ' / 0 ' / 0 ' / 2 ' ] tpubDCDi5W4sP6zSnzJeowy8rQDVhBdRARaPhK1axABi8V1661wEPeanpEXj4ZLAUEoikVtoWcyK26TKKJSecSfeKxwHCcRrge9k1ybuiL71z4a / 1 /* \
2020-08-07 10:19:06 +02:00
) ) " ;
2020-12-23 13:48:17 +11:00
let wallet = Wallet ::new_offline (
2020-08-07 10:19:06 +02:00
descriptor ,
Some ( change_descriptor ) ,
Network ::Testnet ,
get_test_db ( ) ,
)
. unwrap ( ) ;
let export = WalletExport ::export_wallet ( & wallet , " Test Label " , true ) . unwrap ( ) ;
assert_eq! ( export . descriptor ( ) , descriptor ) ;
assert_eq! ( export . change_descriptor ( ) , Some ( change_descriptor . into ( ) ) ) ;
assert_eq! ( export . blockheight , 5000 ) ;
assert_eq! ( export . label , " Test Label " ) ;
}
#[ test ]
fn test_export_to_json ( ) {
let descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/0/*) " ;
let change_descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/1/*) " ;
2020-12-23 13:48:17 +11:00
let wallet = Wallet ::new_offline (
2020-08-07 10:19:06 +02:00
descriptor ,
Some ( change_descriptor ) ,
2020-09-21 15:44:07 +02:00
Network ::Bitcoin ,
2020-08-07 10:19:06 +02:00
get_test_db ( ) ,
)
. unwrap ( ) ;
let export = WalletExport ::export_wallet ( & wallet , " Test Label " , true ) . unwrap ( ) ;
2020-08-10 13:20:48 +02:00
assert_eq! ( export . to_string ( ) , " { \" descriptor \" : \" wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44 \' /0 \' /0 \' /0/*) \" , \" blockheight \" :5000, \" label \" : \" Test Label \" } " ) ;
2020-08-07 10:19:06 +02:00
}
#[ test ]
fn test_export_from_json ( ) {
let descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/0/*) " ;
let change_descriptor = " wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44'/0'/0'/1/*) " ;
let import_str = " { \" descriptor \" : \" wpkh(xprv9s21ZrQH143K4CTb63EaMxja1YiTnSEWKMbn23uoEnAzxjdUJRQkazCAtzxGm4LSoTSVTptoV9RbchnKPW9HxKtZumdyxyikZFDLhogJ5Uj/44 \' /0 \' /0 \' /0/*) \" , \" blockheight \" :5000, \" label \" : \" Test Label \" } " ;
2020-08-10 13:20:48 +02:00
let export = WalletExport ::from_str ( import_str ) . unwrap ( ) ;
2020-08-07 10:19:06 +02:00
assert_eq! ( export . descriptor ( ) , descriptor ) ;
assert_eq! ( export . change_descriptor ( ) , Some ( change_descriptor . into ( ) ) ) ;
assert_eq! ( export . blockheight , 5000 ) ;
assert_eq! ( export . label , " Test Label " ) ;
}
}