Update v5 transparent signature hashes to always commit to scriptPubKey

This commit is contained in:
Kris Nuttycombe 2022-01-12 11:10:07 -07:00
parent 3c6e19f4e4
commit fc82801d3d
5 changed files with 21 additions and 16 deletions

View File

@ -94,8 +94,8 @@ impl Shl<&[u8]> for Script {
/// A transparent address corresponding to either a public key or a `Script`. /// A transparent address corresponding to either a public key or a `Script`.
#[derive(Debug, PartialEq, PartialOrd, Hash, Clone)] #[derive(Debug, PartialEq, PartialOrd, Hash, Clone)]
pub enum TransparentAddress { pub enum TransparentAddress {
PublicKey([u8; 20]), PublicKey([u8; 20]), // TODO: Rename to PublicKeyHash
Script([u8; 20]), Script([u8; 20]), // TODO: Rename to ScriptHash
} }
impl TransparentAddress { impl TransparentAddress {

View File

@ -194,7 +194,7 @@ impl TransparentAuthorizingContext for Unauthorized {
vec![] vec![]
} }
fn input_scripts(&self) -> Vec<Script> { fn input_scriptpubkeys(&self) -> Vec<Script> {
vec![] vec![]
} }
} }
@ -205,7 +205,7 @@ impl TransparentAuthorizingContext for Unauthorized {
return self.inputs.iter().map(|txin| txin.coin.value).collect(); return self.inputs.iter().map(|txin| txin.coin.value).collect();
} }
fn input_scripts(&self) -> Vec<Script> { fn input_scriptpubkeys(&self) -> Vec<Script> {
return self return self
.inputs .inputs
.iter() .iter()
@ -232,7 +232,8 @@ impl Bundle<Unauthorized> {
&SignableInput::Transparent { &SignableInput::Transparent {
hash_type: SIGHASH_ALL, hash_type: SIGHASH_ALL,
index, index,
script_code: &info.coin.script_pubkey, script_code: &info.coin.script_pubkey, // for p2pkh, always the same as script_pubkey
script_pubkey: &info.coin.script_pubkey,
value: info.coin.value, value: info.coin.value,
}, },
txid_parts_cache, txid_parts_cache,

View File

@ -27,6 +27,7 @@ pub enum SignableInput<'a> {
hash_type: u8, hash_type: u8,
index: usize, index: usize,
script_code: &'a Script, script_code: &'a Script,
script_pubkey: &'a Script,
value: Amount, value: Amount,
}, },
#[cfg(feature = "zfuture")] #[cfg(feature = "zfuture")]
@ -64,11 +65,11 @@ pub trait TransparentAuthorizingContext: transparent::Authorization {
/// without requiring the full data of the previous transactions /// without requiring the full data of the previous transactions
/// providing these inputs. /// providing these inputs.
fn input_amounts(&self) -> Vec<Amount>; fn input_amounts(&self) -> Vec<Amount>;
/// Returns the list of all transparent input scripts, provided /// Returns the list of all transparent input scriptPubKeys, provided
/// so that wallets can commit to the transparent input breakdown /// so that wallets can commit to the transparent input breakdown
/// without requiring the full data of the previous transactions /// without requiring the full data of the previous transactions
/// providing these inputs. /// providing these inputs.
fn input_scripts(&self) -> Vec<Script>; fn input_scriptpubkeys(&self) -> Vec<Script>;
} }
/// Computes the signature hash for an input to a transaction, given /// Computes the signature hash for an input to a transaction, given

View File

@ -76,7 +76,7 @@ fn transparent_sig_digest<A: TransparentAuthorizingContext>(
if flag_anyonecanpay { if flag_anyonecanpay {
vec![] vec![]
} else { } else {
bundle.authorization.input_scripts() bundle.authorization.input_scriptpubkeys()
}, },
|w, script| script.write(w), |w, script| script.write(w),
) )
@ -114,7 +114,7 @@ fn transparent_sig_digest<A: TransparentAuthorizingContext>(
let mut ch = hasher(ZCASH_TRANSPARENT_INPUT_HASH_PERSONALIZATION); let mut ch = hasher(ZCASH_TRANSPARENT_INPUT_HASH_PERSONALIZATION);
if let SignableInput::Transparent { if let SignableInput::Transparent {
index, index,
script_code, script_pubkey,
value, value,
.. ..
} = input } = input
@ -122,7 +122,7 @@ fn transparent_sig_digest<A: TransparentAuthorizingContext>(
let txin = &bundle.vin[*index]; let txin = &bundle.vin[*index];
txin.prevout.write(&mut ch).unwrap(); txin.prevout.write(&mut ch).unwrap();
ch.write_all(&value.to_i64_le_bytes()).unwrap(); ch.write_all(&value.to_i64_le_bytes()).unwrap();
script_code.write(&mut ch).unwrap(); script_pubkey.write(&mut ch).unwrap();
ch.write_u32::<LittleEndian>(txin.sequence).unwrap(); ch.write_u32::<LittleEndian>(txin.sequence).unwrap();
} }
let per_input_digest = ch.finalize(); let per_input_digest = ch.finalize();

View File

@ -133,6 +133,7 @@ fn zip_0143() {
hash_type: tv.hash_type as u8, hash_type: tv.hash_type as u8,
index: n as usize, index: n as usize,
script_code: &tv.script_code, script_code: &tv.script_code,
script_pubkey: &tv.script_code,
value: Amount::from_nonnegative_i64(tv.amount).unwrap(), value: Amount::from_nonnegative_i64(tv.amount).unwrap(),
}, },
_ => SignableInput::Shielded, _ => SignableInput::Shielded,
@ -154,6 +155,7 @@ fn zip_0243() {
hash_type: tv.hash_type as u8, hash_type: tv.hash_type as u8,
index: n as usize, index: n as usize,
script_code: &tv.script_code, script_code: &tv.script_code,
script_pubkey: &tv.script_code,
value: Amount::from_nonnegative_i64(tv.amount).unwrap(), value: Amount::from_nonnegative_i64(tv.amount).unwrap(),
}, },
_ => SignableInput::Shielded, _ => SignableInput::Shielded,
@ -169,7 +171,7 @@ fn zip_0243() {
#[derive(Debug)] #[derive(Debug)]
struct TestTransparentAuth { struct TestTransparentAuth {
input_amounts: Vec<Amount>, input_amounts: Vec<Amount>,
input_scripts: Vec<Script>, input_scriptpubkeys: Vec<Script>,
} }
impl transparent::Authorization for TestTransparentAuth { impl transparent::Authorization for TestTransparentAuth {
@ -181,8 +183,8 @@ impl TransparentAuthorizingContext for TestTransparentAuth {
self.input_amounts.clone() self.input_amounts.clone()
} }
fn input_scripts(&self) -> Vec<Script> { fn input_scriptpubkeys(&self) -> Vec<Script> {
self.input_scripts.clone() self.input_scriptpubkeys.clone()
} }
} }
@ -214,7 +216,7 @@ fn zip_0244() {
.iter() .iter()
.map(|amount| Amount::from_nonnegative_i64(*amount).unwrap()) .map(|amount| Amount::from_nonnegative_i64(*amount).unwrap())
.collect(); .collect();
let input_scripts = tv.script_codes.iter().map(|s| Script(s.clone())).collect(); let input_scriptpubkeys = tv.script_codes.iter().map(|s| Script(s.clone())).collect();
let test_bundle = txdata let test_bundle = txdata
.transparent_bundle .transparent_bundle
@ -235,7 +237,7 @@ fn zip_0244() {
vout: b.vout.clone(), vout: b.vout.clone(),
authorization: TestTransparentAuth { authorization: TestTransparentAuth {
input_amounts, input_amounts,
input_scripts, input_scriptpubkeys,
}, },
}); });
@ -262,11 +264,12 @@ fn zip_0244() {
if let Some(index) = tv.transparent_input { if let Some(index) = tv.transparent_input {
let bundle = txdata.transparent_bundle().unwrap(); let bundle = txdata.transparent_bundle().unwrap();
let value = bundle.authorization.input_amounts[index]; let value = bundle.authorization.input_amounts[index];
let script_code = &bundle.authorization.input_scripts[index]; let script_code = &bundle.authorization.input_scriptpubkeys[index];
let signable_input = |hash_type| SignableInput::Transparent { let signable_input = |hash_type| SignableInput::Transparent {
hash_type, hash_type,
index, index,
script_code, script_code,
script_pubkey: script_code,
value, value,
}; };