Bitcoin
hash – HASH160 calculation of 32-byte public key for Taproot
I know Taproot doesn’t use public key hashes, but I want to calculate HASH160 of the 32-byte Taproot X-only public key anyway. The rationale is to compare this to the existing 20-byte HASH160 of the Witness V0 ScriptPubKey to ensure that the same underlying public key is used for both outputs.
Is it correct to add 0x02 byte before 32 bytes? XOnlyPublicKey
? I sketched this using Rust-Bitcoin.
// Taproot. In the case of the key-path spend, the pub key is not tweaked.
//It is 32-byte only X-coordinate however, missing the 33-d byte Y "tie-breaker"
//https://archive.is/AsKEb
//From BIP 340: "To avoid gratuitous incompatibilities, we pick that option for P, and thus
//our X-only public keys become equivalent to a compressed public key that is the X-only key prefixed by the byte 0x02."
let witness_program_bytes = program.program().as_bytes();
//Y-coordinate absent:
assert_eq!(witness_program_bytes.len(), 32);
let mut witness_program_vec = witness_program_bytes.to_vec();
//BIP 340:
witness_program_vec.insert(0, 0x02);
// it is the actual compressed 33 byte pubkey
let pk = PublicKey::from_slice(&witness_program_vec).unwrap();