Skip to content

Commit f80d92a

Browse files
committed
Merge bitcoindevkit#179: Feature/bip322 integration
7c33b33 chore(deps): switch bdk_bip322 from git to crates.io release (Abiodun Awoyemi) e03f123 fix: rename BIP322 commands, validate address ownership before signing and add Bip322Error variant (Abiodun) 2eab75c bip322: update proof handling for new bdk-bip322 API (aagbotemi) Pull request description: <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### Description This PR added [BIP322](https://bips.xyz/322) feature into BDK CLI. It also has a usage file for testing purposes ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ## Changelog notice <!-- Notice the release manager should include in the release tag message changelog --> <!-- See https://keepachangelog.com/en/1.0.0/ for examples --> ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk-cli/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [x] I've added docs for the new feature * [x] I've updated `CHANGELOG.md` ACKs for top commit: notmandatory: ACK 7c33b33 Tree-SHA512: a3e16b86c2259eb74f243721f9b54589322066d293ca0ba3ba74de13de264289426e5019ba16514797aa34a3f17a6bed574d0b5cc66d2ff51b68830152616696
2 parents dea7406 + 7c33b33 commit f80d92a

6 files changed

Lines changed: 110 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ shlex = { version = "1.3.0", optional = true }
3737
payjoin = { version = "=1.0.0-rc.1", features = ["v1", "v2", "io", "_test-utils"], optional = true}
3838
reqwest = { version = "0.13.2", default-features = false, optional = true }
3939
url = { version = "2.5.8", optional = true }
40+
bdk_bip322 = { version = "0.1.0", optional = true }
4041

4142
[features]
4243
default = ["repl", "sqlite"]
@@ -56,6 +57,7 @@ rpc = ["bdk_bitcoind_rpc", "_payjoin-dependencies"]
5657

5758
# Internal features
5859
_payjoin-dependencies = ["payjoin", "reqwest", "url"]
60+
bip322 = ["bdk_bip322"]
5961

6062
# Use this to consensus verify transactions at sync time
6163
verify = []

src/commands.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,35 @@ pub enum OfflineWalletSubCommand {
461461
#[arg(env = "BASE64_PSBT", required = true)]
462462
psbt: Vec<String>,
463463
},
464+
/// Sign a message using BIP322
465+
#[cfg(feature = "bip322")]
466+
SignMessage {
467+
/// The message to sign
468+
#[arg(long)]
469+
message: String,
470+
/// The signature format (e.g., Legacy, Simple, Full)
471+
#[arg(long, default_value = "simple")]
472+
signature_type: String,
473+
/// Address to sign
474+
#[arg(long)]
475+
address: String,
476+
/// Optional list of specific UTXOs for proof-of-funds (only for `FullWithProofOfFunds`)
477+
#[arg(long)]
478+
utxos: Option<Vec<OutPoint>>,
479+
},
480+
/// Verify a BIP322 signature
481+
#[cfg(feature = "bip322")]
482+
VerifyMessage {
483+
/// The signature proof to verify
484+
#[arg(long)]
485+
proof: String,
486+
/// The message that was signed
487+
#[arg(long)]
488+
message: String,
489+
/// The address associated with the signature
490+
#[arg(long)]
491+
address: String,
492+
},
464493
}
465494

466495
/// Wallet subcommands that needs a blockchain backend.

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ pub enum BDKCliError {
140140
#[cfg(feature = "payjoin")]
141141
#[error("Payjoin create request error: {0}")]
142142
PayjoinCreateRequest(#[from] payjoin::send::v2::CreateRequestError),
143+
144+
#[cfg(feature = "bip322")]
145+
#[error("BIP-322 error: {0}")]
146+
Bip322Error(#[from] bdk_bip322::error::Error),
143147
}
144148

145149
impl From<ExtractTxError> for BDKCliError {

src/handlers.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ use std::str::FromStr;
7070
))]
7171
use std::sync::Arc;
7272

73+
#[cfg(feature = "bip322")]
74+
use crate::error::BDKCliError;
75+
#[cfg(feature = "bip322")]
76+
use bdk_bip322::{BIP322, MessageProof, MessageVerificationResult};
77+
7378
#[cfg(any(
7479
feature = "electrum",
7580
feature = "esplora",
@@ -592,6 +597,47 @@ pub fn handle_offline_wallet_subcommand(
592597
&json!({ "psbt": BASE64_STANDARD.encode(final_psbt.serialize()) }),
593598
)?)
594599
}
600+
#[cfg(feature = "bip322")]
601+
SignMessage {
602+
message,
603+
signature_type,
604+
address,
605+
utxos,
606+
} => {
607+
let address: Address = parse_address(&address)?;
608+
let signature_format = parse_signature_format(&signature_type)?;
609+
610+
if !wallet.is_mine(address.script_pubkey()) {
611+
return Err(Error::Generic(format!(
612+
"Address {} does not belong to this wallet.",
613+
address
614+
)));
615+
}
616+
617+
let proof: MessageProof =
618+
wallet.sign_message(message.as_str(), signature_format, &address, utxos)?;
619+
620+
Ok(json!({"proof": proof.to_base64()}).to_string())
621+
}
622+
#[cfg(feature = "bip322")]
623+
VerifyMessage {
624+
proof,
625+
message,
626+
address,
627+
} => {
628+
let address: Address = parse_address(&address)?;
629+
let parsed_proof: MessageProof = MessageProof::from_base64(&proof)
630+
.map_err(|e| BDKCliError::Generic(format!("Invalid proof: {e}")))?;
631+
632+
let is_valid: MessageVerificationResult =
633+
wallet.verify_message(&parsed_proof, &message, &address)?;
634+
635+
Ok(json!({
636+
"valid": is_valid.valid,
637+
"proven_amount": is_valid.proven_amount.map(|a| a.to_sat()) // optional field
638+
})
639+
.to_string())
640+
}
595641
}
596642
}
597643

src/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ use bdk_wallet::descriptor::Segwitv0;
5656
use bdk_wallet::keys::{GeneratableKey, GeneratedKey, bip39::WordCount};
5757
use serde_json::{Value, json};
5858

59+
#[cfg(feature = "bip322")]
60+
use bdk_bip322::SignatureFormat;
61+
5962
/// Parse the recipient (Address,Amount) argument from cli input.
6063
pub(crate) fn parse_recipient(s: &str) -> Result<(ScriptBuf, u64), String> {
6164
let parts: Vec<_> = s.split(':').collect();
@@ -95,6 +98,21 @@ pub(crate) fn parse_address(address_str: &str) -> Result<Address, Error> {
9598
Ok(unchecked_address.assume_checked())
9699
}
97100

101+
/// Function to parse the signature format from a string
102+
#[cfg(feature = "bip322")]
103+
pub(crate) fn parse_signature_format(format_str: &str) -> Result<SignatureFormat, Error> {
104+
match format_str.to_lowercase().as_str() {
105+
"legacy" => Ok(SignatureFormat::Legacy),
106+
"simple" => Ok(SignatureFormat::Simple),
107+
"full" => Ok(SignatureFormat::Full),
108+
"fullproofoffunds" => Ok(SignatureFormat::FullProofOfFunds),
109+
_ => Err(Error::Generic(
110+
"Invalid signature format. Use 'legacy', 'simple', 'full', or 'fullproofoffunds'"
111+
.to_string(),
112+
)),
113+
}
114+
}
115+
98116
/// Prepare bdk-cli home directory
99117
///
100118
/// This function is called to check if [`crate::CliOpts`] datadir is set.

0 commit comments

Comments
 (0)