Skip to content

Commit 2369bcc

Browse files
committed
refactor(silentpayments): match SpInput and bitcoin::psbt::OutputType variants
1 parent abc1355 commit 2369bcc

3 files changed

Lines changed: 35 additions & 31 deletions

File tree

silentpayments/src/lib.rs

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

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

2832
pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
@@ -35,12 +39,12 @@ pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
3539
.redeem_script()
3640
.filter(|script_pubkey| script_pubkey.is_p2wpkh())
3741
// if not P2SH-P2WPKH return None
38-
.map(|_| WrappedSegwit),
42+
.map(|_| ShWpkh),
3943
// Native segwit
4044
(false, true) => {
4145
// P2WPKH
4246
if script_pubkey.is_p2wpkh() {
43-
Some(P2WPKH)
47+
Some(Wpkh)
4448
} else {
4549
// P2TR
4650
script_pubkey
@@ -50,13 +54,13 @@ pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
5054
.taproot_control_block()
5155
.filter(|control_block| control_block[1..33] == NUMS_H)
5256
// if P2TR has no internal key return None
53-
.map_or(Some(P2TR), |_| None)
57+
.map_or(Some(Tr), |_| None)
5458
})
5559
.flatten()
5660
}
5761
}
5862
// No witness, legacy P2PKH
59-
(true, false) if script_pubkey.is_p2pkh() => Some(P2PKH),
63+
(true, false) if script_pubkey.is_p2pkh() => Some(Pkh),
6064
// All other cases
6165
_ => None,
6266
}
@@ -167,7 +171,7 @@ mod tests {
167171

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

170-
assert_eq!(Some(SpInputs::WrappedSegwit), tagged_input);
174+
assert_eq!(Some(SpInputs::ShWpkh), tagged_input);
171175
}
172176

173177
#[test]
@@ -219,7 +223,7 @@ mod tests {
219223

220224
let tagged_input = tag_txin(&txin, &script_pubkey);
221225

222-
assert_eq!(Some(SpInputs::P2TR), tagged_input);
226+
assert_eq!(Some(SpInputs::Tr), tagged_input);
223227
}
224228

225229
#[test]
@@ -275,7 +279,7 @@ mod tests {
275279

276280
let tagged_input = tag_txin(&txin, &script_pubkey);
277281

278-
assert_eq!(Some(SpInputs::P2WPKH), tagged_input);
282+
assert_eq!(Some(SpInputs::Wpkh), tagged_input);
279283
}
280284

281285
#[test]
@@ -324,7 +328,7 @@ mod tests {
324328

325329
let tagged_input = tag_txin(&txin, &script_pubkey);
326330

327-
assert_eq!(Some(SpInputs::P2PKH), tagged_input);
331+
assert_eq!(Some(SpInputs::Pkh), tagged_input);
328332
}
329333

330334
#[test]

silentpayments/src/receive/mod.rs

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

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

301301
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
302302

303-
assert_eq!(SpInputs::WrappedSegwit, input_type);
303+
assert_eq!(SpInputs::ShWpkh, input_type);
304304

305305
assert_eq!(expected_pubkey, parsed_pubkey);
306306
}
@@ -335,7 +335,7 @@ mod tests {
335335

336336
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
337337

338-
assert_eq!(SpInputs::P2WPKH, input_type);
338+
assert_eq!(SpInputs::Wpkh, input_type);
339339

340340
assert_eq!(expected_pubkey, parsed_pubkey);
341341
}
@@ -372,7 +372,7 @@ mod tests {
372372

373373
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
374374

375-
assert_eq!(SpInputs::P2TR, input_type);
375+
assert_eq!(SpInputs::Tr, input_type);
376376

377377
assert_eq!(expected_pubkey, parsed_pubkey);
378378
}
@@ -405,7 +405,7 @@ mod tests {
405405

406406
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
407407

408-
assert_eq!(SpInputs::P2PKH, input_type);
408+
assert_eq!(SpInputs::Pkh, input_type);
409409

410410
assert_eq!(expected_pubkey, parsed_pubkey);
411411
}
@@ -417,16 +417,16 @@ mod tests {
417417
.expect("should succeed");
418418
// only input from mainnet tx 4316fe7be359937317f42ffaf05ab02554297fb83096a0beb985a25f9e338215
419419
let txin = TxIn {
420-
previous_output: OutPoint {
421-
txid: "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
422-
.parse()
423-
.unwrap(),
424-
vout: 1,
425-
},
426-
script_sig: ScriptBuf::from_hex("0075473045022100a8c61b2d470e393279d1ba54f254b7c237de299580b7fa01ffcc940442ecec4502201afba952f4e4661c40acde7acc0341589031ba103a307b886eb867b23b850b972103782eeb913431ca6e9b8c2fd80a5f72ed2024ef72a3c6fb10263c379937323338").expect("should succeed"),
427-
sequence: Sequence::MAX,
428-
witness: Witness::new(),
429-
};
420+
previous_output: OutPoint {
421+
txid: "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
422+
.parse()
423+
.unwrap(),
424+
vout: 1,
425+
},
426+
script_sig: ScriptBuf::from_hex("0075473045022100a8c61b2d470e393279d1ba54f254b7c237de299580b7fa01ffcc940442ecec4502201afba952f4e4661c40acde7acc0341589031ba103a307b886eb867b23b850b972103782eeb913431ca6e9b8c2fd80a5f72ed2024ef72a3c6fb10263c379937323338").expect("should succeed"),
427+
sequence: Sequence::MAX,
428+
witness: Witness::new(),
429+
};
430430

431431
let expected_pubkey = PublicKey::from_str(
432432
"03782eeb913431ca6e9b8c2fd80a5f72ed2024ef72a3c6fb10263c379937323338",
@@ -439,7 +439,7 @@ mod tests {
439439

440440
let (input_type, parsed_pubkey) = maybe_pubkey.expect("is some");
441441

442-
assert_eq!(SpInputs::P2PKH, input_type);
442+
assert_eq!(SpInputs::Pkh, input_type);
443443

444444
assert_eq!(expected_pubkey, parsed_pubkey);
445445
}

silentpayments/src/send/psbt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ where
235235

236236
let maybe_secret_key = extract_pubkey(full_txin.clone(), &prevout)
237237
.map(|pubkey_data| match pubkey_data {
238-
(SpInputs::P2TR, even_tr_output_key) => {
238+
(SpInputs::Tr, even_tr_output_key) => {
239239
match get_taproot_secret(psbt_input, k, secp) {
240240
Ok(Some(secret)) => {
241241
let (xonly, _) = secret.x_only_public_key(secp);

0 commit comments

Comments
 (0)