From a41b255daba7ba5b4a32d99ccb65d060dbea68cc Mon Sep 17 00:00:00 2001 From: rishflab Date: Tue, 2 Mar 2021 17:10:29 +1100 Subject: [PATCH] Upgrade bitcoin wallet to use BIP84 derivation scheme Explicitly specify the change descriptor because the behaviour when it is not specified is unclear. --- swap/src/bin/asb.rs | 7 ++++--- swap/src/bin/swap_cli.rs | 2 +- swap/src/bitcoin/wallet.rs | 11 ++++++----- swap/tests/testutils/mod.rs | 3 +-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 4bc1f845..15f4e01d 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -13,6 +13,7 @@ #![allow(non_snake_case)] use anyhow::{Context, Result}; +use bdk::{descriptor::Segwitv0, keys::DerivableKey}; use prettytable::{row, Table}; use std::{path::Path, sync::Arc}; use structopt::StructOpt; @@ -88,7 +89,7 @@ async fn main() -> Result<()> { let (bitcoin_wallet, monero_wallet) = init_wallets( config.clone(), &wallet_data_dir, - seed.extended_private_key(BITCOIN_NETWORK)?.private_key, + seed.extended_private_key(BITCOIN_NETWORK)?, ) .await?; @@ -135,14 +136,14 @@ async fn main() -> Result<()> { async fn init_wallets( config: Config, bitcoin_wallet_data_dir: &Path, - private_key: ::bitcoin::PrivateKey, + key: impl DerivableKey + Clone, ) -> Result<(bitcoin::Wallet, monero::Wallet)> { let bitcoin_wallet = bitcoin::Wallet::new( config.bitcoin.electrum_rpc_url, config.bitcoin.electrum_http_url, BITCOIN_NETWORK, bitcoin_wallet_data_dir, - private_key, + key, ) .await?; diff --git a/swap/src/bin/swap_cli.rs b/swap/src/bin/swap_cli.rs index 2c23503a..7ca2b8f5 100644 --- a/swap/src/bin/swap_cli.rs +++ b/swap/src/bin/swap_cli.rs @@ -337,7 +337,7 @@ async fn init_wallets( config.bitcoin.electrum_http_url, bitcoin_network, bitcoin_wallet_data_dir, - seed.extended_private_key(bitcoin_network)?.private_key, + seed.extended_private_key(bitcoin_network)?, ) .await?; diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 76e17c98..4fe3d345 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -7,9 +7,10 @@ use anyhow::{anyhow, bail, Context, Result}; use backoff::{backoff::Constant as ConstantBackoff, future::retry}; use bdk::{ blockchain::{noop_progress, Blockchain, ElectrumBlockchain}, + descriptor::Segwitv0, electrum_client::{self, Client, ElectrumApi}, - miniscript::bitcoin::PrivateKey, - FeeRate, + keys::DerivableKey, + FeeRate, KeychainKind, }; use bitcoin::Script; use reqwest::{Method, Url}; @@ -45,7 +46,7 @@ impl Wallet { electrum_http_url: Url, network: bitcoin::Network, wallet_dir: &Path, - private_key: PrivateKey, + key: impl DerivableKey + Clone, ) -> Result { // Workaround for https://github.com/bitcoindevkit/rust-electrum-client/issues/47. let config = electrum_client::ConfigBuilder::default().retry(2).build(); @@ -56,8 +57,8 @@ impl Wallet { let db = bdk::sled::open(wallet_dir)?.open_tree(SLED_TREE_NAME)?; let bdk_wallet = bdk::Wallet::new( - bdk::template::P2WPKH(private_key), - None, + bdk::template::BIP84(key.clone(), KeychainKind::External), + Some(bdk::template::BIP84(key, KeychainKind::Internal)), network, db, ElectrumBlockchain::from(client), diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 6fe0253e..697c28c9 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -607,8 +607,7 @@ async fn init_test_wallets( bitcoin::Network::Regtest, datadir, seed.extended_private_key(bitcoin::Network::Regtest) - .expect("Could not create extended private key from seed") - .private_key, + .expect("Could not create extended private key from seed"), ) .await .expect("could not init btc wallet");