Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = ["--cfg", "bdk_wallet_unstable"]
25 changes: 22 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "README.md"
license = "MIT"

[dependencies]
bdk_wallet = { version = "3.1.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] }
bdk_wallet = { version = "3.2.0", features = ["bdk-tx", "rusqlite", "keys-bip39", "compiler", "std"] }
clap = { version = "4.6", features = ["derive","env"] }
clap_complete = "4.6"
dirs = { version = "6.0.0" }
Expand Down
14 changes: 12 additions & 2 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod repl;
use crate::client::BlockchainClient;
use std::path::PathBuf;

use crate::persister::WalletSigners;
use crate::{error::BDKCliError as Error, utils::output::FormatOutput};
use bdk_wallet::{Wallet, bitcoin::Network};

Expand All @@ -35,6 +36,7 @@ pub struct Init;
/// Requires only a wallet.
pub struct OfflineOperations<'a> {
pub wallet: &'a mut Wallet,
pub signers: &'a WalletSigners,
}

#[cfg(any(
Expand All @@ -47,6 +49,7 @@ pub struct OfflineOperations<'a> {
/// Requires a wallet and a client.
pub struct OnlineOperations<'a> {
pub wallet: &'a mut Wallet,
pub signers: &'a WalletSigners,
pub client: &'a BlockchainClient,
pub wallet_name: String,
}
Expand All @@ -70,11 +73,16 @@ impl AppContext<Init> {
}

impl<'a> AppContext<OfflineOperations<'a>> {
pub fn new_offline_wallet(network: Network, datadir: PathBuf, wallet: &'a mut Wallet) -> Self {
pub fn new_offline_wallet(
network: Network,
datadir: PathBuf,
wallet: &'a mut Wallet,
signers: &'a WalletSigners,
) -> Self {
Self {
network,
datadir,
state: OfflineOperations { wallet },
state: OfflineOperations { wallet, signers },
}
}
}
Expand All @@ -90,6 +98,7 @@ impl<'a> AppContext<OnlineOperations<'a>> {
network: Network,
datadir: PathBuf,
wallet: &'a mut Wallet,
signers: &'a WalletSigners,
client: &'a BlockchainClient,
wallet_name: String,
) -> Self {
Expand All @@ -98,6 +107,7 @@ impl<'a> AppContext<OnlineOperations<'a>> {
datadir,
state: OnlineOperations {
wallet,
signers,
client,
wallet_name,
},
Expand Down
67 changes: 40 additions & 27 deletions src/handlers/offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use bdk_wallet::bitcoin::base64::Engine;
use bdk_wallet::bitcoin::base64::prelude::BASE64_STANDARD;
use bdk_wallet::bitcoin::script::PushBytesBuf;
use bdk_wallet::bitcoin::{Address, Amount, FeeRate, OutPoint, Psbt, ScriptBuf, Sequence, Txid};
use bdk_wallet::descriptor::ExtractPolicy;
use bdk_wallet::descriptor::policy::{BuildSatisfaction, Condition};
use bdk_wallet::signer::SignersContainer;
use bdk_wallet::{KeychainKind, SignOptions};
use clap::Parser;
use serde_json::json;
Expand Down Expand Up @@ -255,6 +258,32 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateTxCommand {
type Output = PsbtResult;

fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
let mut condition = Condition::default();
for (keychain, descriptor) in ctx.state.wallet.keychains() {
let policy = descriptor
.extract_policy(
&SignersContainer::default(),
BuildSatisfaction::None,
ctx.state.wallet.secp_ctx(),
)?
.expect("wallet descriptors must have a policy");

let policy_path = match keychain {
KeychainKind::External => self.external_policy.as_deref(),
KeychainKind::Internal => self.internal_policy.as_deref(),
}
.map(serde_json::from_str::<BTreeMap<String, Vec<usize>>>)
.transpose()?
.unwrap_or_default();

let policy_condition = policy
.get_condition(&policy_path)
.map_err(|e| Error::Generic(e.to_string()))?;
condition = condition
.merge(&policy_condition)
.map_err(|e| Error::Generic(e.to_string()))?;
}

let mut tx_builder = ctx.state.wallet.build_tx();

if self.send_all {
Expand Down Expand Up @@ -301,19 +330,7 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateTxCommand {
tx_builder.add_data(data);
}

let policies = vec![
self.external_policy
.clone()
.map(|p| (p, KeychainKind::External)),
self.internal_policy
.clone()
.map(|p| (p, KeychainKind::Internal)),
];

for (policy, keychain) in policies.into_iter().flatten() {
let policy = serde_json::from_str::<BTreeMap<String, Vec<usize>>>(&policy)?;
tx_builder.policy_path(policy, keychain);
}
tx_builder.set_condition(condition);

let psbt = tx_builder.finish()?;

Expand Down Expand Up @@ -472,7 +489,9 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateSpTxCommand {

let unsigned_psbt = psbt.clone();

let finalized = ctx.state.wallet.sign(&mut psbt, SignOptions::default())?;
let wallet = &ctx.state.wallet;
ctx.state.signers.sign_psbt(wallet, &mut psbt)?;
let finalized = wallet.finalize_psbt(&mut psbt, SignOptions::default())?;

if !finalized {
return Err(Error::Generic(
Expand All @@ -488,16 +507,8 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateSpTxCommand {
}

let secp = Secp256k1::new();
let mut external_signers = ctx
.state
.wallet
.get_signers(KeychainKind::External)
.as_key_map(&secp);
let internal_signers = ctx
.state
.wallet
.get_signers(KeychainKind::Internal)
.as_key_map(&secp);
let mut external_signers = ctx.state.signers.external.clone();
let internal_signers = ctx.state.signers.internal.clone();
external_signers.extend(internal_signers);

match external_signers.iter().next().expect("not empty") {
Expand Down Expand Up @@ -526,7 +537,8 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateSpTxCommand {
psbt_input.final_script_witness = None;
}

let _resigned = ctx.state.wallet.sign(&mut psbt, SignOptions::default())?;
ctx.state.signers.sign_psbt(wallet, &mut psbt)?;
let _resigned = wallet.finalize_psbt(&mut psbt, SignOptions::default())?;

let raw_tx = psbt.extract_tx()?;

Expand Down Expand Up @@ -653,7 +665,7 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for SignCommand {
type Output = PsbtResult;

fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
let wallet = &mut ctx.state.wallet;
let wallet = &ctx.state.wallet;
let psbt_bytes = BASE64_STANDARD
.decode(&self.psbt)
.map_err(|e| Error::Generic(e.to_string()))?;
Expand All @@ -664,7 +676,8 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for SignCommand {
trust_witness_utxo: self.trust_witness_utxo.unwrap_or(false),
..Default::default()
};
let finalized = wallet.sign(&mut psbt, signopt)?;
ctx.state.signers.sign_psbt(wallet, &mut psbt)?;
let finalized = wallet.finalize_psbt(&mut psbt, signopt)?;
Ok(PsbtResult::new(&psbt, Some(finalized)))
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/handlers/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use {
crate::commands::ReplSubCommand,
crate::handlers::{AppCommand, AppContext},
crate::persister::WalletSigners,
crate::utils::output::FormatOutput,
bdk_wallet::{Wallet, bitcoin::Network},
clap::Parser,
Expand All @@ -24,6 +25,7 @@ use {crate::commands::WalletSubCommand, crate::error::BDKCliError as Error, std:
pub(crate) async fn respond(
network: Network,
wallet: &mut Wallet,
signers: &WalletSigners,
#[cfg(any(
feature = "electrum",
feature = "esplora",
Expand Down Expand Up @@ -54,7 +56,7 @@ pub(crate) async fn respond(
let response = match repl_subcommand {
ReplSubCommand::Wallet { subcommand } => match subcommand {
WalletSubCommand::OfflineWalletSubCommand(cmd) => {
let mut ctx = AppContext::new_offline_wallet(network, datadir, wallet);
let mut ctx = AppContext::new_offline_wallet(network, datadir, wallet, signers);
cmd.execute(&mut ctx).map_err(|e| e.to_string())?;
Some(())
}
Expand All @@ -70,6 +72,7 @@ pub(crate) async fn respond(
network,
datadir,
wallet,
signers,
client_ref,
wallet_name.to_string(),
);
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
WalletSubCommand::OnlineWalletSubCommand(cmd) => {
let runtime = WalletRuntime::load(&home_dir, &wallet_name)?;
let mut wallet = runtime.build_wallet(true)?;
let signers = runtime.build_signers()?;
let client = runtime.build_client(&wallet)?;
{
let mut ctx = AppContext::new_online_wallet(
runtime.network,
runtime.home_dir.clone(),
&mut wallet,
&signers,
&client,
runtime.wallet_name.clone(),
);
Expand All @@ -86,12 +88,14 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
WalletSubCommand::OfflineWalletSubCommand(cmd) => {
let runtime = WalletRuntime::load(&home_dir, &wallet_name)?;
let mut wallet = runtime.build_wallet(command_requires_db(&cmd))?;
let signers = runtime.build_signers()?;

{
let mut ctx = AppContext::new_offline_wallet(
runtime.network,
runtime.home_dir.clone(),
&mut wallet,
&signers,
);

match cmd {
Expand Down Expand Up @@ -119,11 +123,13 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
WalletSubCommand::Hwi(cmd) => {
let runtime = WalletRuntime::load(&home_dir, &wallet_name)?;
let mut wallet = runtime.build_wallet(false)?;
let signers = runtime.build_signers()?;

let mut ctx = AppContext::new_offline_wallet(
runtime.network,
runtime.home_dir.clone(),
&mut wallet,
&signers,
);

cmd.run(&mut ctx, &wallet_name)
Expand Down Expand Up @@ -157,6 +163,7 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
let runtime = WalletRuntime::load(&home_dir, &wallet_name)?;

let mut wallet = runtime.build_wallet(true)?;
let signers = runtime.build_signers()?;

#[cfg(any(
feature = "electrum",
Expand All @@ -182,6 +189,7 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
let should_exit = crate::handlers::repl::respond(
runtime.network,
&mut wallet,
&signers,
#[cfg(any(
feature = "electrum",
feature = "esplora",
Expand Down
Loading
Loading