Add subcommand to print internal bitcoin wallet descriptor

Example:

 cargo run --package swap --bin swap -- export-bitcoin-wallet

{"descriptor":"wpkh(xprv9s21ZrQH143K2q85eUf2ppCtti2Zar6aDCW5dCC5DG9VvutJ3cpe3Qf9wZpQeraNf9JEuGem1RyJZQMEWCN2DpWyL5WbsvmSW6wbL7Jq53H/84'/0'/0'/0/*)","blockheight":0,"label":"cli-bitcoin"}
This commit is contained in:
rishflab 2021-10-01 10:36:08 +10:00
parent 9ea73a8e66
commit e42812ba37
6 changed files with 74 additions and 0 deletions

View File

@ -50,6 +50,8 @@ It is possible to migrate critical data from the old db to the sqlite but there
- Added a `disable-timestamp` flag to the ASB that disables timestamps from logs.
- A `config` subcommand that prints the current configuration including the data directory location.
This feature should alleviate difficulties users were having when finding where xmr-btc-swap was storing data.
- Added `export-bitcoin-wallet` subcommand to the CLI and ASB, to print the internal bitcoin wallet descriptor.
This will allow users to transact and monitor using external wallets.
## [0.8.3] - 2021-09-03

View File

@ -74,6 +74,15 @@ where
env_config: env_config(testnet),
cmd: Command::Config,
},
RawCommand::ExportBitcoinWallet => Arguments {
testnet,
json,
sled,
disable_timestamp,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::ExportBitcoinWallet,
},
RawCommand::ManualRecovery(ManualRecovery::Redeem {
redeem_params: RecoverCommandParams { swap_id },
do_not_await_finality,
@ -222,6 +231,7 @@ pub enum Command {
SafelyAbort {
swap_id: Uuid,
},
ExportBitcoinWallet,
}
#[derive(structopt::StructOpt, Debug)]
@ -296,6 +306,8 @@ pub enum RawCommand {
about = "Prints the Bitcoin and Monero balance. Requires the monero-wallet-rpc to be running."
)]
Balance,
#[structopt(about = "Print the internal bitcoin wallet descriptor.")]
ExportBitcoinWallet,
#[structopt(about = "Contains sub-commands for recovering a swap manually.")]
ManualRecovery(ManualRecovery),
}

View File

@ -301,6 +301,11 @@ async fn main() -> Result<()> {
tracing::info!("Redeem transaction successfully published with id {}", txid);
}
Command::ExportBitcoinWallet => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let wallet_export = bitcoin_wallet.wallet_export("asb").await?;
println!("{}", wallet_export.to_string())
}
}
Ok(())

View File

@ -409,6 +409,23 @@ async fn main() -> Result<()> {
println!("{}", table);
}
}
Command::ExportBitcoinWallet {
bitcoin_electrum_rpc_url,
bitcoin_target_block,
} => {
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let wallet_export = bitcoin_wallet.wallet_export("cli").await?;
println!("{}", wallet_export.to_string())
}
};
Ok(())
}

View File

@ -9,6 +9,7 @@ use bdk::database::BatchDatabase;
use bdk::descriptor::Segwitv0;
use bdk::electrum_client::{ElectrumApi, GetHistoryRes};
use bdk::keys::DerivableKey;
use bdk::wallet::export::WalletExport;
use bdk::wallet::AddressIndex;
use bdk::{FeeRate, KeychainKind, SignOptions};
use bitcoin::{Network, Script};
@ -174,6 +175,18 @@ impl Wallet {
sub
}
pub async fn wallet_export(&self, role: &str) -> Result<WalletExport> {
let wallet = self.wallet.lock().await;
match bdk::wallet::export::WalletExport::export_wallet(
&wallet,
&format!("{}-{}", role, self.network),
true,
) {
Ok(wallet_export) => Ok(wallet_export),
Err(err_msg) => Err(anyhow::Error::msg(err_msg)),
}
}
}
fn print_status_change(txid: Txid, old: Option<ScriptStatus>, new: ScriptStatus) -> ScriptStatus {

View File

@ -239,6 +239,22 @@ where
tor_socks5_port,
},
},
RawCommand::ExportBitcoinWallet { bitcoin } => {
let (bitcoin_electrum_rpc_url, bitcoin_target_block) =
bitcoin.apply_defaults(is_testnet)?;
Arguments {
env_config: env_config_from(is_testnet),
debug,
json,
sled,
data_dir: data::data_dir_from(data, is_testnet)?,
cmd: Command::ExportBitcoinWallet {
bitcoin_electrum_rpc_url,
bitcoin_target_block,
},
}
}
};
Ok(ParseResult::Arguments(arguments))
@ -289,6 +305,10 @@ pub enum Command {
namespace: XmrBtcNamespace,
tor_socks5_port: u16,
},
ExportBitcoinWallet {
bitcoin_electrum_rpc_url: Url,
bitcoin_target_block: usize,
},
}
#[derive(structopt::StructOpt, Debug)]
@ -427,6 +447,11 @@ enum RawCommand {
#[structopt(flatten)]
tor: Tor,
},
/// Print the internal bitcoin wallet descriptor
ExportBitcoinWallet {
#[structopt(flatten)]
bitcoin: Bitcoin,
},
}
#[derive(structopt::StructOpt, Debug)]