Skip to content

Commit

Permalink
chore(ampd-minor): update signers hash for multiversx after contracts…
Browse files Browse the repository at this point in the history
… audit (#707)
  • Loading branch information
raress96 authored Dec 2, 2024
1 parent a35a0ce commit 0a1d7e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
24 changes: 17 additions & 7 deletions ampd/src/mvx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ pub struct WeightedSigners {

impl WeightedSigners {
pub fn hash(&self) -> Hash {
let mut encoded = Vec::new();
let mut encoded: Vec<Vec<u8>> = 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()
}
Expand Down Expand Up @@ -76,10 +81,15 @@ fn uint256_to_compact_vec(value: Uint256) -> Vec<u8> {
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),
};
}
}
3 changes: 2 additions & 1 deletion ampd/src/mvx/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0a1d7e1

Please sign in to comment.