Skip to content

Commit

Permalink
Add namespace to script hashing (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt authored Apr 19, 2021
1 parent 6746918 commit 59fab61
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
21 changes: 13 additions & 8 deletions rust/pkg/cardano_serialization_lib.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ declare export var NativeScriptKind: {|
+TimelockExpiry: 5, // 5
|};

/**
* Each new language uses a different namespace for hashing its script
* This is because you could have a language where the same bytes have different semantics
* So this avoids scripts in different languages mapping to the same hash
* Note that the enum value here is different than the enum value for deciding the cost model of a script
*/

declare export var ScriptHashNamespace: {|
+NativeScript: 0, // 0
|};

/**
*/

Expand Down Expand Up @@ -2091,16 +2102,10 @@ declare export class NativeScript {
static from_bytes(bytes: Uint8Array): NativeScript;

/**
* @param {number} namespace
* @returns {Ed25519KeyHash}
*/
hash(): Ed25519KeyHash;

/**
* like hash(), but prefixes 0x00 in the pre-image bytes, to match the specs
* followed by cardano-cli. See https://github.com/input-output-hk/cardano-node/issues/2593
* @returns {Ed25519KeyHash}
*/
script_hash(): Ed25519KeyHash;
hash(namespace: number): Ed25519KeyHash;

/**
* @param {ScriptPubkey} script_pubkey
Expand Down
24 changes: 15 additions & 9 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,17 +1452,21 @@ pub struct NativeScript(NativeScriptEnum);

to_from_bytes!(NativeScript);

/// Each new language uses a different namespace for hashing its script
/// This is because you could have a language where the same bytes have different semantics
/// So this avoids scripts in different languages mapping to the same hash
/// Note that the enum value here is different than the enum value for deciding the cost model of a script
#[wasm_bindgen]
impl NativeScript {
pub fn hash(&self) -> Ed25519KeyHash {
Ed25519KeyHash::from(blake2b224(self.to_bytes().as_ref()))
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ScriptHashNamespace {
NativeScript,
}

/// like hash(), but prefixes 0x00 in the pre-image bytes, to match the specs
/// followed by cardano-cli. See https://github.com/input-output-hk/cardano-node/issues/2593
pub fn script_hash(&self) -> Ed25519KeyHash {
#[wasm_bindgen]
impl NativeScript {
pub fn hash(&self, namespace: ScriptHashNamespace) -> Ed25519KeyHash {
let mut bytes = Vec::with_capacity(self.to_bytes().len() + 1);
bytes.extend_from_slice(&vec![00]);
bytes.extend_from_slice(&vec![namespace as u8]);
bytes.extend_from_slice(&self.to_bytes());
Ed25519KeyHash::from(blake2b224(bytes.as_ref()))
}
Expand Down Expand Up @@ -2439,7 +2443,9 @@ mod tests {

let script = NativeScript::new_script_pubkey(&ScriptPubkey::new(&hash));

let script_hash = ScriptHash::from_bytes(script.script_hash().to_bytes()).unwrap();
let script_hash = ScriptHash::from_bytes(
script.hash(ScriptHashNamespace::NativeScript).to_bytes()
).unwrap();

assert_eq!(hex::encode(&script_hash.to_bytes()), "187b8d3ddcb24013097c003da0b8d8f7ddcf937119d8f59dccd05a0f");
}
Expand Down

0 comments on commit 59fab61

Please sign in to comment.