Skip to content

Rename SpInputs variants #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions silentpayments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ pub const NUMS_H: [u8; 32] = [

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SpInputs {
P2TR,
P2WPKH,
WrappedSegwit,
P2PKH,
/// The input spends a P2TR output.
Tr,
/// The input spends a P2WPKH output.
Wpkh,
/// The input spends a P2WPKH output nested in a P2SH.
ShWpkh,
/// The inputs spends a P2PKH output.
Pkh,
}

pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
Expand All @@ -35,12 +39,12 @@ pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
.redeem_script()
.filter(|script_pubkey| script_pubkey.is_p2wpkh())
// if not P2SH-P2WPKH return None
.map(|_| WrappedSegwit),
.map(|_| ShWpkh),
// Native segwit
(false, true) => {
// P2WPKH
if script_pubkey.is_p2wpkh() {
Some(P2WPKH)
Some(Wpkh)
} else {
// P2TR
script_pubkey
Expand All @@ -50,13 +54,13 @@ pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
.taproot_control_block()
.filter(|control_block| control_block[1..33] == NUMS_H)
// if P2TR has no internal key return None
.map_or(Some(P2TR), |_| None)
.map_or(Some(Tr), |_| None)
})
.flatten()
}
}
// No witness, legacy P2PKH
(true, false) if script_pubkey.is_p2pkh() => Some(P2PKH),
(true, false) if script_pubkey.is_p2pkh() => Some(Pkh),
// All other cases
_ => None,
}
Expand Down Expand Up @@ -167,7 +171,7 @@ mod tests {

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

assert_eq!(Some(SpInputs::WrappedSegwit), tagged_input);
assert_eq!(Some(SpInputs::ShWpkh), tagged_input);
}

#[test]
Expand Down Expand Up @@ -219,7 +223,7 @@ mod tests {

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

assert_eq!(Some(SpInputs::P2TR), tagged_input);
assert_eq!(Some(SpInputs::Tr), tagged_input);
}

#[test]
Expand Down Expand Up @@ -275,7 +279,7 @@ mod tests {

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

assert_eq!(Some(SpInputs::P2WPKH), tagged_input);
assert_eq!(Some(SpInputs::Wpkh), tagged_input);
}

#[test]
Expand Down Expand Up @@ -324,7 +328,7 @@ mod tests {

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

assert_eq!(Some(SpInputs::P2PKH), tagged_input);
assert_eq!(Some(SpInputs::Pkh), tagged_input);
}

#[test]
Expand Down
36 changes: 18 additions & 18 deletions silentpayments/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ pub fn extract_pubkey(txin: TxIn, script_pubkey: &ScriptBuf) -> Option<(SpInputs
use SpInputs::*;

tag_txin(&txin, script_pubkey).and_then(|tag| match tag {
WrappedSegwit | P2WPKH => {
ShWpkh | Wpkh => {
let maybe_pk = txin.witness.last().expect("already checked is not empty");
bitcoin::PublicKey::from_slice(maybe_pk)
.ok()
.filter(|pubkey| pubkey.compressed)
.map(|pk| (tag, pk.inner))
}
P2TR => XOnlyPublicKey::from_slice(&script_pubkey.as_bytes()[2..34])
Tr => XOnlyPublicKey::from_slice(&script_pubkey.as_bytes()[2..34])
.ok()
.map(|xonly_pk| (tag, xonly_pk.public_key(Parity::Even))),
P2PKH => txin
Pkh => txin
.script_sig
.into_bytes()
.windows(33)
Expand Down Expand Up @@ -300,7 +300,7 @@ mod tests {

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

assert_eq!(SpInputs::WrappedSegwit, input_type);
assert_eq!(SpInputs::ShWpkh, input_type);

assert_eq!(expected_pubkey, parsed_pubkey);
}
Expand Down Expand Up @@ -335,7 +335,7 @@ mod tests {

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

assert_eq!(SpInputs::P2WPKH, input_type);
assert_eq!(SpInputs::Wpkh, input_type);

assert_eq!(expected_pubkey, parsed_pubkey);
}
Expand Down Expand Up @@ -372,7 +372,7 @@ mod tests {

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

assert_eq!(SpInputs::P2TR, input_type);
assert_eq!(SpInputs::Tr, input_type);

assert_eq!(expected_pubkey, parsed_pubkey);
}
Expand Down Expand Up @@ -405,7 +405,7 @@ mod tests {

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

assert_eq!(SpInputs::P2PKH, input_type);
assert_eq!(SpInputs::Pkh, input_type);

assert_eq!(expected_pubkey, parsed_pubkey);
}
Expand All @@ -417,16 +417,16 @@ mod tests {
.expect("should succeed");
// only input from mainnet tx 4316fe7be359937317f42ffaf05ab02554297fb83096a0beb985a25f9e338215
let txin = TxIn {
previous_output: OutPoint {
txid: "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
.parse()
.unwrap(),
vout: 1,
},
script_sig: ScriptBuf::from_hex("0075473045022100a8c61b2d470e393279d1ba54f254b7c237de299580b7fa01ffcc940442ecec4502201afba952f4e4661c40acde7acc0341589031ba103a307b886eb867b23b850b972103782eeb913431ca6e9b8c2fd80a5f72ed2024ef72a3c6fb10263c379937323338").expect("should succeed"),
sequence: Sequence::MAX,
witness: Witness::new(),
};
previous_output: OutPoint {
txid: "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
.parse()
.unwrap(),
vout: 1,
},
script_sig: ScriptBuf::from_hex("0075473045022100a8c61b2d470e393279d1ba54f254b7c237de299580b7fa01ffcc940442ecec4502201afba952f4e4661c40acde7acc0341589031ba103a307b886eb867b23b850b972103782eeb913431ca6e9b8c2fd80a5f72ed2024ef72a3c6fb10263c379937323338").expect("should succeed"),
sequence: Sequence::MAX,
witness: Witness::new(),
};

let expected_pubkey = PublicKey::from_str(
"03782eeb913431ca6e9b8c2fd80a5f72ed2024ef72a3c6fb10263c379937323338",
Expand All @@ -439,7 +439,7 @@ mod tests {

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

assert_eq!(SpInputs::P2PKH, input_type);
assert_eq!(SpInputs::Pkh, input_type);

assert_eq!(expected_pubkey, parsed_pubkey);
}
Expand Down
2 changes: 1 addition & 1 deletion silentpayments/src/send/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ where

let maybe_secret_key = extract_pubkey(full_txin.clone(), &prevout)
.map(|pubkey_data| match pubkey_data {
(SpInputs::P2TR, even_tr_output_key) => {
(SpInputs::Tr, even_tr_output_key) => {
match get_taproot_secret(psbt_input, k, secp) {
Ok(Some(secret)) => {
let (xonly, _) = secret.x_only_public_key(secp);
Expand Down
Loading