diff --git a/ampd/src/mvx/mod.rs b/ampd/src/mvx/mod.rs index 331b66aa9..dea007827 100644 --- a/ampd/src/mvx/mod.rs +++ b/ampd/src/mvx/mod.rs @@ -24,15 +24,20 @@ pub struct WeightedSigners { impl WeightedSigners { pub fn hash(&self) -> Hash { - let mut encoded = Vec::new(); + let mut encoded: Vec> = Vec::new(); + + encoded.push(usize_to_u32(self.signers.len()).to_vec()); for signer in self.signers.iter() { - encoded.push(signer.signer.as_slice()); - encoded.push(signer.weight.as_slice()); + encoded.push(signer.signer.to_vec()); + + encoded.push(usize_to_u32(signer.weight.len()).to_vec()); + encoded.push(signer.weight.to_vec()); } - encoded.push(self.threshold.as_slice()); - encoded.push(self.nonce.as_slice()); + encoded.push(usize_to_u32(self.threshold.len()).to_vec()); + encoded.push(self.threshold.to_vec()); + encoded.push(self.nonce.to_vec()); Keccak256::digest(encoded.concat()).into() } @@ -76,10 +81,15 @@ fn uint256_to_compact_vec(value: Uint256) -> Vec { bytes[slice_from..].to_vec() } +#[allow(clippy::cast_possible_truncation)] +fn usize_to_u32(value: usize) -> [u8; 4] { + (value as u32).to_be_bytes() +} + pub fn ed25519_key(pub_key: &PublicKey) -> Result<[u8; 32], Error> { - return match pub_key { + match pub_key { PublicKey::Ed25519(ed25519_key) => Ok(<[u8; 32]>::try_from(ed25519_key.as_ref()) .expect("couldn't convert pubkey to ed25519 public key")), _ => Err(Error::NotEd25519Key), - }; + } } diff --git a/ampd/src/mvx/verifier.rs b/ampd/src/mvx/verifier.rs index d2933adc8..8dce5b7f4 100644 --- a/ampd/src/mvx/verifier.rs +++ b/ampd/src/mvx/verifier.rs @@ -518,8 +518,9 @@ mod tests { // 0000000000000000000000000000000000000000000000000000000000000005 - the nonce (created_at date as uint256) let data = HexBinary::from_hex("0000000345e67eaf446e6c26eb3a2b55b64339ecf3a4d1d03180bee20eb5afdd23fa644f0000000101c387253d29085a8036d6ae2cafb1b14699751417c0ce302cfe03da279e6b5c040000000101dd9822c7fa239dda9913ebee813ecbe69e35d88ff651548d5cc42c033a8a667b000000010100000001020000000000000000000000000000000000000000000000000000000000000005") .unwrap(); + // This hash is generated externally using the MultiversX Gateway contract and is 100% correct let signers_hash = - HexBinary::from_hex("29f81aa379fa1f5973d05dd25e5ae4bc1afa2aa30156b1db5ec437a46ba4fd28") + HexBinary::from_hex("ad334409a09406b82c1d0f86bdc0aa8f68950c996643c884e6be66e6ea821654") .unwrap(); let wrong_event = Events {