Skip to content

Commit 29c6d0e

Browse files
committed
refactor(silentpayments): match SpInput and bitcoin::psbt::OutputType variants
1 parent 7fa6d16 commit 29c6d0e

3 files changed

Lines changed: 25 additions & 21 deletions

File tree

silentpayments/src/lib.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ pub const NUMS_H: [u8; 32] = [
2020

2121
#[derive(Debug, Clone, Copy, PartialEq)]
2222
pub enum SpInputs {
23-
P2TR,
24-
P2WPKH,
25-
WrappedSegwit,
26-
P2PKH,
23+
/// The input spends a P2TR output.
24+
Tr,
25+
/// The input spends a P2WPKH output.
26+
Wpkh,
27+
/// The input spends a P2WPKH output nested in a P2SH.
28+
ShWpkh,
29+
/// The inputs spends a P2PKH output.
30+
Pkh,
2731
}
2832

2933
pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
@@ -36,12 +40,12 @@ pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
3640
.redeem_script()
3741
.filter(|script_pubkey| script_pubkey.is_p2wpkh())
3842
// if not P2SH-P2WPKH return None
39-
.map(|_| WrappedSegwit),
43+
.map(|_| ShWpkh),
4044
// Native segwit
4145
(false, true) => {
4246
// P2WPKH
4347
if script_pubkey.is_p2wpkh() {
44-
Some(P2WPKH)
48+
Some(Wpkh)
4549
} else {
4650
// P2TR
4751
script_pubkey
@@ -51,13 +55,13 @@ pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
5155
.taproot_control_block()
5256
.filter(|control_block| control_block[1..33] == NUMS_H)
5357
// if P2TR has no internal key return None
54-
.map_or(Some(P2TR), |_| None)
58+
.map_or(Some(Tr), |_| None)
5559
})
5660
.flatten()
5761
}
5862
}
5963
// No witness, legacy P2PKH
60-
(true, false) if script_pubkey.is_p2pkh() => Some(P2PKH),
64+
(true, false) if script_pubkey.is_p2pkh() => Some(Pkh),
6165
// All other cases
6266
_ => None,
6367
}
@@ -166,7 +170,7 @@ mod tests {
166170

167171
let tagged_input = tag_txin(&txin, &script_pubkey);
168172

169-
assert_eq!(Some(SpInputs::WrappedSegwit), tagged_input);
173+
assert_eq!(Some(SpInputs::ShWpkh), tagged_input);
170174
}
171175

172176
#[test]
@@ -217,7 +221,7 @@ mod tests {
217221

218222
let tagged_input = tag_txin(&txin, &script_pubkey);
219223

220-
assert_eq!(Some(SpInputs::P2TR), tagged_input);
224+
assert_eq!(Some(SpInputs::Tr), tagged_input);
221225
}
222226

223227
#[test]
@@ -273,7 +277,7 @@ mod tests {
273277

274278
let tagged_input = tag_txin(&txin, &script_pubkey);
275279

276-
assert_eq!(Some(SpInputs::P2WPKH), tagged_input);
280+
assert_eq!(Some(SpInputs::Wpkh), tagged_input);
277281
}
278282

279283
#[test]
@@ -322,7 +326,7 @@ mod tests {
322326

323327
let tagged_input = tag_txin(&txin, &script_pubkey);
324328

325-
assert_eq!(Some(SpInputs::P2PKH), tagged_input);
329+
assert_eq!(Some(SpInputs::Pkh), tagged_input);
326330
}
327331

328332
#[test]

silentpayments/src/receive/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ pub fn extract_pubkey(txin: TxIn, script_pubkey: &ScriptBuf) -> Option<(SpInputs
5454
use SpInputs::*;
5555

5656
tag_txin(&txin, script_pubkey).and_then(|tag| match tag {
57-
WrappedSegwit | P2WPKH => {
57+
ShWpkh | Wpkh => {
5858
let maybe_pk = txin.witness.last().expect("already checked is not empty");
5959
bitcoin::PublicKey::from_slice(maybe_pk)
6060
.ok()
6161
.filter(|pubkey| pubkey.compressed)
6262
.map(|pk| (tag, pk.inner))
6363
}
64-
P2TR => XOnlyPublicKey::from_slice(&script_pubkey.as_bytes()[2..34])
64+
Tr => XOnlyPublicKey::from_slice(&script_pubkey.as_bytes()[2..34])
6565
.ok()
6666
.map(|xonly_pk| (tag, xonly_pk.public_key(Parity::Even))),
67-
P2PKH => txin
67+
Pkh => txin
6868
.script_sig
6969
.into_bytes()
7070
.windows(33)
@@ -304,7 +304,7 @@ mod tests {
304304

305305
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
306306

307-
assert_eq!(SpInputs::WrappedSegwit, input_type);
307+
assert_eq!(SpInputs::ShWpkh, input_type);
308308

309309
assert_eq!(expected_pubkey, parsed_pubkey);
310310
}
@@ -339,7 +339,7 @@ mod tests {
339339

340340
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
341341

342-
assert_eq!(SpInputs::P2WPKH, input_type);
342+
assert_eq!(SpInputs::Wpkh, input_type);
343343

344344
assert_eq!(expected_pubkey, parsed_pubkey);
345345
}
@@ -376,7 +376,7 @@ mod tests {
376376

377377
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
378378

379-
assert_eq!(SpInputs::P2TR, input_type);
379+
assert_eq!(SpInputs::Tr, input_type);
380380

381381
assert_eq!(expected_pubkey, parsed_pubkey);
382382
}
@@ -409,7 +409,7 @@ mod tests {
409409

410410
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
411411

412-
assert_eq!(SpInputs::P2PKH, input_type);
412+
assert_eq!(SpInputs::Pkh, input_type);
413413

414414
assert_eq!(expected_pubkey, parsed_pubkey);
415415
}
@@ -443,7 +443,7 @@ mod tests {
443443

444444
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
445445

446-
assert_eq!(SpInputs::P2PKH, input_type);
446+
assert_eq!(SpInputs::Pkh, input_type);
447447

448448
assert_eq!(expected_pubkey, parsed_pubkey);
449449
}

silentpayments/src/send/psbt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ where
5151
}
5252

5353
if let Some((input_type, _pk)) = extract_pubkey(full_txin, &prevout) {
54-
if let SpInputs::P2TR = input_type {
54+
if let SpInputs::Tr = input_type {
5555
for (&xonly, (_leaf_hashes, key_source)) in psbt_input.tap_key_origins.iter() {
5656
let mut internal_privkey = if let Ok(Some(privkey)) =
5757
k.get_key(KeyRequest::Bip32(key_source.clone()), secp)

0 commit comments

Comments
 (0)