source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
dependencies = [
+ "indexmap",
"itoa",
"memchr",
"serde",
dirs = { version = "6.0.0" }
env_logger = "0.11.10"
log = "0.4"
-serde_json = "1.0"
+serde_json = { version = "1.0", features = ["preserve_order"] }
thiserror = "2.0.18"
tokio = { version = "1", features = ["full"] }
toml = "1.1.0"
Every command prints JSON, and that JSON is built in exactly one place: the command's own `execute` method. To find what a command prints, read its `execute`.
- Helpers in `src/utils/` return domain values (bdk/bitcoin types, `String`s, small structs such as `KeychainDescriptors`) so they can be reused. They never build JSON.
-- `execute` turns those values into the output with `serde_json::json!`.
+- `execute` turns those values into the output with `serde_json::json!`. Keys are printed in the order they are written.
##### Step 3: Implement the AppCommand Trait
use serde_json::{Value, json};
impl AppCommand<AppContext<OfflineOperations<'_>>> for WalletDetailsCommand {
-
// Implement the execute function
fn execute(&self, context: &mut AppContext<OfflineOperations<'_>>) -> Result<Value, Error> {
// Access the loaded wallet via the context state
let wallet = &context.state.wallet;
- let mut output = json!({
+ let mut output = json!({
"network": wallet.network(),
"fingerprint": wallet.fingerprint().to_string(),
});
if config.wallets.contains_key(wallet_name.as_str()) && !self.force {
return Err(Error::Generic(format!(
- "Wallet '{}' already exists. Use --force to overwrite.",
- wallet_name
+ "Wallet '{wallet_name}' already exists. Use --force to overwrite."
)));
};
&& existing.network != ctx.network.to_string()
{
return Err(Error::Generic(format!(
- "Wallet '{}' is configured for network '{}', and --force cannot change it to '{}'.",
- wallet_name, existing.network, ctx.network
+ "Wallet '{wallet_name}' is configured for network '{}', and --force cannot change it to '{}'.",
+ existing.network, ctx.network
)));
}
.map_err(|error| Error::Generic(error.to_string()))?;
Ok(json!({
- "message": format!(
- "Wallet '{}' initialized successfully in {:?}",
- wallet_name,
- ctx.datadir.join("config.toml")
- ),
+ "message": format!(
+ "Wallet '{wallet_name}' initialized successfully in {:?}",
+ ctx.datadir.join("config.toml")
+ ),
}))
}
}
Ok(json!({
"message": format!(
- "Wallet configuration '{}' deleted successfully",
- self.wallet_name
- ),
+ "Wallet configuration '{}' deleted successfully",
+ self.wallet_name
+ ),
}))
}
}
use crate::commands::KeySubCommand;
use crate::error::BDKCliError as Error;
use crate::handlers::{AppCommand, AppContext, Init};
-use crate::utils::output::FormatOutput;
+use crate::utils::{derive_master_key_from_mnemonic, output::FormatOutput};
use bdk_wallet::bip39::{Language, Mnemonic};
-use bdk_wallet::bitcoin::bip32::{DerivationPath, Fingerprint, KeySource, Xpriv};
-use bdk_wallet::bitcoin::{Network, key::Secp256k1};
+use bdk_wallet::bitcoin::bip32::{DerivationPath, KeySource, Xpriv};
+use bdk_wallet::bitcoin::key::Secp256k1;
use bdk_wallet::keys::{
- DerivableKey, DescriptorKey, ExtendedKey, GeneratableKey, GeneratedKey, bip39::WordCount,
+ DerivableKey, DescriptorKey, GeneratableKey, GeneratedKey, bip39::WordCount,
};
use bdk_wallet::miniscript::{self, Segwitv0};
use clap::{Parser, ValueEnum};
}))
}
}
-
-/// Derives the master key from a mnemonic and optional passphrase.
-fn derive_master_key_from_mnemonic(
- mnemonic: &Mnemonic,
- password: Option<String>,
- network: Network,
-) -> Result<(Xpriv, Fingerprint), Error> {
- let secp = Secp256k1::new();
- let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
- let xprv = xkey
- .into_xprv(network.into())
- .ok_or_else(|| Error::Generic("Private key info not found".to_string()))?;
- let fingerprint = xprv.fingerprint(&secp);
-
- Ok((xprv, fingerprint))
-}
.map(|tx| {
json!({
"txid": tx.tx_node.txid.to_string(),
- "is_coinbase": tx.tx_node.is_coinbase(),
+ "is_coinbase": tx.tx_node.is_coinbase(),
"wtxid": tx.tx_node.compute_wtxid().to_string(),
"version": tx.tx_node.version,
"is_rbf": tx.tx_node.is_explicitly_rbf(),
feature = "rpc"
))]
use bdk_wallet::WalletEvent;
-use bdk_wallet::bitcoin::base64::Engine;
-use bdk_wallet::bitcoin::base64::prelude::BASE64_STANDARD;
+use bdk_wallet::bip39::Mnemonic;
+use bdk_wallet::bitcoin::base64::{Engine, prelude::BASE64_STANDARD};
+use bdk_wallet::bitcoin::bip32::{Fingerprint, Xpriv};
+use bdk_wallet::bitcoin::key::Secp256k1;
use bdk_wallet::bitcoin::script::PushBytesBuf;
use bdk_wallet::bitcoin::{
Address, FeeRate, Network, OutPoint, Psbt, ScriptBuf, Transaction, Weight,
};
+use bdk_wallet::keys::{DerivableKey, ExtendedKey};
use std::{
path::{Path, PathBuf},
str::FromStr,
(12..=24).contains(&word_count) && s.chars().all(|c| c.is_alphanumeric() || c.is_whitespace())
}
+/// Derives the master key from a mnemonic and optional passphrase.
+pub fn derive_master_key_from_mnemonic(
+ mnemonic: &Mnemonic,
+ password: Option<String>,
+ network: Network,
+) -> Result<(Xpriv, Fingerprint), Error> {
+ let secp = Secp256k1::new();
+ let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
+ let xprv = xkey
+ .into_xprv(network.into())
+ .ok_or_else(|| Error::Generic("Private key info not found".to_string()))?;
+ let fingerprint = xprv.fingerprint(&secp);
+
+ Ok((xprv, fingerprint))
+}
+
#[cfg(feature = "cbf")]
pub async fn trace_logger(
mut info_subcriber: Receiver<Info>,