fix: Stop double CBOR encoding of Plutus script used for hashing (#188)

This commit is contained in:
Harper 2022-09-12 20:47:20 +01:00 committed by GitHub
parent 30a866c799
commit 1850bfe86e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 5 deletions

View file

@ -69,6 +69,14 @@ macro_rules! common_hasher {
hasher.finalize()
}
#[inline]
pub fn hash_tagged(bytes: &[u8], tag: u8) -> Hash<{ $size / 8 }> {
let mut hasher = Self::new();
hasher.input(&[tag]);
hasher.input(bytes);
hasher.finalize()
}
/// convenient function to directly generate the hash
/// of the given [minicbor::Encode] data object
#[inline]

View file

@ -63,7 +63,7 @@ impl ToHash<28> for alonzo::NativeScript {
impl ToHash<28> for alonzo::PlutusScript {
fn to_hash(&self) -> Hash<28> {
Hasher::<224>::hash_tagged_cbor(self, 1)
Hasher::<224>::hash_tagged(&self.0, 1)
}
}
@ -93,7 +93,7 @@ impl ToHash<32> for babbage::Header {
impl ToHash<28> for babbage::PlutusV2Script {
fn to_hash(&self) -> Hash<28> {
Hasher::<224>::hash_tagged_cbor(self, 2)
Hasher::<224>::hash_tagged(&self.0, 2)
}
}
@ -120,8 +120,10 @@ impl ToHash<32> for babbage::DatumOption {
#[cfg(test)]
mod tests {
use crate::{MultiEraTx, Era};
use super::ToHash;
use pallas_codec::minicbor;
use pallas_codec::{minicbor, utils::Bytes};
use pallas_codec::utils::Int;
use pallas_crypto::hash::Hash;
use pallas_primitives::{alonzo, babbage, byron};
@ -260,7 +262,7 @@ mod tests {
fn plutus_v1_script_hashes_as_cardano_cli() {
let bytecode_hex = include_str!("../../test_data/jpgstore.plutus");
let bytecode = hex::decode(bytecode_hex).unwrap();
let script: alonzo::PlutusScript = pallas_codec::minicbor::decode(&bytecode).unwrap();
let script = alonzo::PlutusScript(Bytes::from(bytecode));
let generated = script.to_hash().to_string();
@ -276,7 +278,7 @@ mod tests {
fn plutus_v2_script_hashes_as_cardano_cli() {
let bytecode_hex = include_str!("../../test_data/v2script.plutus");
let bytecode = hex::decode(bytecode_hex).unwrap();
let script: babbage::PlutusV2Script = pallas_codec::minicbor::decode(&bytecode).unwrap();
let script = babbage::PlutusV2Script(Bytes::from(bytecode));
let generated = script.to_hash().to_string();
@ -287,4 +289,22 @@ mod tests {
"2616f3e9edb51f98ef04dbaefd042b5c731e86616e8e9172c63c39be"
);
}
#[test]
fn tx_wits_plutus_v1_script_hashes_as_cli() {
let tx_bytecode_hex = include_str!("../../test_data/scriptwit.tx");
let bytecode = hex::decode(tx_bytecode_hex).unwrap();
let tx = MultiEraTx::decode(Era::Babbage, &bytecode).unwrap();
let wits = tx.witnesses();
let script = wits.plutus_v1_script().unwrap().get(0).unwrap();
let generated = script.to_hash().to_string();
assert_eq!(
generated,
"62bdc3d04d04376d516d31664944b25ce3affa76d17f8b5e1279b49d"
);
}
}

1
test_data/scriptwit.tx Normal file
View file

@ -0,0 +1 @@
84a70082825820b0024b97f2cb61f02fee077851024d59e87aebf666d8b1aad97f0936b1336313018258206c732139de33e916342707de2aebef2252c781640326ff37b86ec99d97f1ba8d01018182581d60b6c8794e9a7a26599440a4d0fd79cd07644d15917ff13694f1f672351b00000001af2169b0021a0002a1ca0b5820ee03812c6b8c8bc136b805706a737d43fe71cf596f5eb20d8132500c3a31a6280d818258206c732139de33e916342707de2aebef2252c781640326ff37b86ec99d97f1ba8d011082581d60b6c8794e9a7a26599440a4d0fd79cd07644d15917ff13694f1f672351b00000001af10c605111a0003f2afa4008182582031ae74f8058527afb305d7495b10a99422d9337fc199e1f28044f2c477a0f9465840cdfa56b7788eb57a717ce06eec016a6c6f5ee99f51f21376de08b74e64241d9e6020c81330559d94f698689767d02c8fc6b15286fb1bd770e60f991ffda25506038149480100002221200101049fd87980ff0581840001d879808219044c1a000382d4f5f6