Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor(ampd): update signers hash for multiversx after contracts audit. #707

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

raress96
Copy link
Contributor

Description

As per the Gateway contract audit, the signers hash computation has changed to use MultiversX nested encoding. Since I didn't want to add a new Rust dependency just for this and further complicate the code, a simple manual solution was used instead which yields the same result.

@raress96 raress96 requested a review from a team as a code owner November 20, 2024 13:27
Comment on lines 85 to 87
fn usize_to_u32(value: usize) -> Vec<u8> {
(value as u32).to_be_bytes().to_vec()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to move the to_vec() call out of this function and return a fixed length byte array. This being fixed length is security relevant, so I wouldn't hide it behind a vec conversion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -24,15 +25,20 @@ pub struct WeightedSigners {

impl WeightedSigners {
pub fn hash(&self) -> Hash {
let mut encoded = Vec::new();
let mut encoded: Vec<Vec<u8>> = Vec::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need Vec<Vec<u8>>? digest takes a Ref<[u8]>. If possible I'd prefer to use Vec<&[u8]> as before. Defining encoded as mutable with nested vectors allows you to modify the vector elements as well I believe, which seems dangerous

Copy link
Contributor Author

@raress96 raress96 Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't use Vec<&[u8]> is because of the needed call to usize_to_u32 which is done in the for loop. That creates a new owned variable that doesn't live long enough, hence I needed to change this to a Vec<Vec<u8>>.

The solution would have been to save the length in the WeightedSigner & WeightedSigners structs so I can reference it, but I don't think that is a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants