Skip to content

Commit 7401e94

Browse files
committed
substitute_raw_pkh in case of taproot
Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
1 parent 094f9d4 commit 7401e94

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/psbt/finalizer.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use super::{sanity_check, Error, InputError, Psbt, PsbtInputSatisfier};
2121
use crate::prelude::*;
2222
use crate::util::witness_size;
2323
use crate::{
24-
interpreter, BareCtx, Descriptor, ExtParams, Legacy, Miniscript, Satisfier, Segwitv0, Tap,
24+
interpreter, BareCtx, Descriptor, ExtParams, Legacy, Miniscript, Satisfier, Segwitv0, SigType,
25+
Tap, ToPublicKey,
2526
};
2627

2728
// Satisfy the taproot descriptor. It is not possible to infer the complete
@@ -33,6 +34,19 @@ fn construct_tap_witness(
3334
sat: &PsbtInputSatisfier,
3435
allow_mall: bool,
3536
) -> Result<Vec<Vec<u8>>, InputError> {
37+
let mut hash_map: BTreeMap<hash160::Hash, bitcoin::key::XOnlyPublicKey> = BTreeMap::new();
38+
let psbt_inputs = &sat.psbt.inputs;
39+
for psbt_input in psbt_inputs {
40+
// Use Tap Key Origins to get set of all possible keys.
41+
let public_keys = psbt_input.tap_key_origins.keys();
42+
for key in public_keys {
43+
let bitcoin_key = *key;
44+
// Convert PubKeyHash into Hash::hash160
45+
let hash = bitcoin_key.to_pubkeyhash(SigType::Schnorr);
46+
// Insert pair in HashMap
47+
hash_map.insert(hash, bitcoin_key);
48+
}
49+
}
3650
assert!(spk.is_v1_p2tr());
3751

3852
// try the key spend path first
@@ -55,7 +69,7 @@ fn construct_tap_witness(
5569
script,
5670
&ExtParams::allow_all(),
5771
) {
58-
Ok(ms) => ms,
72+
Ok(ms) => ms.substitute_raw_pkh(&hash_map),
5973
Err(..) => continue, // try another script
6074
};
6175
let mut wit = if allow_mall {

0 commit comments

Comments
 (0)