Skip to content

Commit 84f8750

Browse files
committed
feat(silentpayments): add SilentPaymentCode command
The input for this command are two compressed public keys. The network is obtained from the wallet current network. The silent payment code generated is independent from any of the other stateful features of bdk-cli. This command is mainly intended for experimental use, do not lock any funds to the generated code if you don't know what you are doing and don't have the keys matchin the public keys used.
1 parent d0873d1 commit 84f8750

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/commands.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ pub enum CliSubCommand {
110110
#[command(flatten)]
111111
wallet_opts: WalletOpts,
112112
},
113+
/// Silent payment code generation tool.
114+
///
115+
/// Allows the encoding of two public keys into a silent payment code.
116+
/// Useful to create silent payment transactions using fake silent payment codes.
117+
#[cfg(feature = "sp")]
118+
SilentPaymentCode {
119+
/// The scan public key to use on the silent payment code.
120+
#[arg(long = "scan_public_key")]
121+
scan: bdk_sp::bitcoin::secp256k1::PublicKey,
122+
/// The spend public key to use on the silent payment code.
123+
#[arg(long = "spend_public_key")]
124+
spend: bdk_sp::bitcoin::secp256k1::PublicKey,
125+
}
113126
}
114127

115128
/// Wallet operation subcommands.

src/handlers.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,30 @@ pub(crate) fn is_final(psbt: &Psbt) -> Result<(), Error> {
10491049
Ok(())
10501050
}
10511051

1052+
#[cfg(feature = "sp")]
1053+
pub(crate) fn handle_sp_subcommand(
1054+
scan_pubkey: bdk_sp::bitcoin::secp256k1::PublicKey,
1055+
spend_pubkey: bdk_sp::bitcoin::secp256k1::PublicKey,
1056+
network: Network,
1057+
pretty: bool,
1058+
) -> Result<String, Error> {
1059+
let sp_code = SilentPaymentCode::new_v0(scan_pubkey, spend_pubkey, network);
1060+
if pretty {
1061+
let table = vec![vec![
1062+
"sp_code".cell().bold(true),
1063+
sp_code.to_string().cell(),
1064+
]]
1065+
.table()
1066+
.display()
1067+
.map_err(|e| Error::Generic(e.to_string()))?;
1068+
Ok(format!("{table}"))
1069+
} else {
1070+
Ok(serde_json::to_string_pretty(
1071+
&json!({"sp_code": sp_code.to_string()}),
1072+
)?)
1073+
}
1074+
}
1075+
10521076
/// Handle a key sub-command
10531077
///
10541078
/// Key sub-commands are described in [`KeySubCommand`].
@@ -1348,6 +1372,14 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
13481372
let result = handle_key_subcommand(network, key_subcommand, pretty)?;
13491373
Ok(result)
13501374
}
1375+
#[cfg(feature = "sp")]
1376+
CliSubCommand::SilentPaymentCode {
1377+
scan,
1378+
spend
1379+
} => {
1380+
let result = handle_sp_subcommand(scan, spend, network, pretty)?;
1381+
Ok(result)
1382+
}
13511383
#[cfg(feature = "compiler")]
13521384
CliSubCommand::Compile {
13531385
policy,

0 commit comments

Comments
 (0)