Skip to content

Commit 2eab75c

Browse files
committed
bip322: update proof handling for new bdk-bip322 API
1 parent 1dc41d0 commit 2eab75c

6 files changed

Lines changed: 119 additions & 35 deletions

File tree

.github/workflows/audit.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ name: Audit
33
on:
44
push:
55
paths:
6+
# Run if workflow changes
7+
- '.github/workflows/audit.yml'
8+
# Run on changed dependencies
69
- '**/Cargo.toml'
710
- '**/Cargo.lock'
11+
# Run if the configuration file changes
12+
- '**/audit.toml'
813
schedule:
914
- cron: '0 0 * * 0' # Once per week
15+
# Run manually
16+
workflow_dispatch:
1017

1118
jobs:
1219

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ bdk_esplora = { version = "0.22.1", features = ["async-https", "tokio"], optiona
3434
bdk_kyoto = { version = "0.15.4", optional = true }
3535
bdk_redb = { version = "0.1.1", optional = true }
3636
shlex = { version = "1.3.0", optional = true }
37-
payjoin = { version = "=1.0.0-rc.1", features = ["v1", "v2", "io", "_test-utils"], optional = true}
38-
reqwest = { version = "0.13.2", default-features = false, optional = true }
39-
url = { version = "2.5.8", optional = true }
37+
payjoin = { version = "1.0.0-rc.1", features = ["v1", "v2", "io", "_test-utils"], optional = true}
38+
reqwest = { version = "0.12.23", default-features = false, optional = true }
39+
url = { version = "2.5.4", optional = true }
40+
bdk-bip322 = { git = "https://github.com/aagbotemi/bdk-bip322.git", branch = "master", 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,37 @@ 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+
SignBip322 {
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`) #[arg(long)]
477+
utxos: Option<Vec<OutPoint>>,
478+
},
479+
/// Verify a BIP322 signature
480+
#[cfg(feature = "bip322")]
481+
VerifyBip322 {
482+
/// The signature proof to verify
483+
#[arg(long)]
484+
proof: String,
485+
/// The message that was signed
486+
#[arg(long)]
487+
message: String,
488+
/// The signature format (e.g., Legacy, Simple, Full)
489+
#[arg(long, default_value = "simple")]
490+
signature_type: String,
491+
/// The address associated with the signature
492+
#[arg(long)]
493+
address: String,
494+
},
464495
}
465496

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

src/handlers.rs

Lines changed: 45 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, Bip322Proof, Bip322VerificationResult};
77+
7378
#[cfg(any(
7479
feature = "electrum",
7580
feature = "esplora",
@@ -592,6 +597,46 @@ pub fn handle_offline_wallet_subcommand(
592597
&json!({ "psbt": BASE64_STANDARD.encode(final_psbt.serialize()) }),
593598
)?)
594599
}
600+
#[cfg(feature = "bip322")]
601+
SignBip322 {
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+
let proof: Bip322Proof = wallet
611+
.sign_bip322(message.as_str(), signature_format, &address, utxos)
612+
.map_err(|e| {
613+
BDKCliError::Generic(format!("Failed to sign BIP-322 message: {e}"))
614+
})?;
615+
616+
Ok(json!({"proof": proof.to_base64()}).to_string())
617+
}
618+
#[cfg(feature = "bip322")]
619+
VerifyBip322 {
620+
proof,
621+
message,
622+
signature_type,
623+
address,
624+
} => {
625+
let address: Address = parse_address(&address)?;
626+
let signature_format = parse_signature_format(&signature_type)?;
627+
628+
let parsed_proof: Bip322Proof = Bip322Proof::from_base64(&proof)
629+
.map_err(|e| BDKCliError::Generic(format!("Invalid proof: {e}")))?;
630+
631+
let is_valid: Bip322VerificationResult =
632+
wallet.verify_bip322(&parsed_proof, &message, signature_format, &address)?;
633+
634+
Ok(json!({
635+
"valid": is_valid.valid,
636+
"proven_amount": is_valid.proven_amount.map(|a| a.to_sat()) // optional field
637+
})
638+
.to_string())
639+
}
595640
}
596641
}
597642

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)