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

Fix EIP 712 - Data of the struct should be concatenated after type hash #1054

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions batcher/aligned-sdk/src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,17 @@ impl Eip712 for NoncedVerificationData {

let mut hasher = Keccak256::new();

// As per the EIP, first we generate the type hash
// hashStruct(s : 𝕊) = keccak256(typeHash ‖ encodeData(s))

// We first generate the type hash
hasher.update(NONCED_VERIFICATION_DATA_TYPE);
let type_hash = hasher.finalize_reset();

// Then hash is resetted, so we start hashing the second term of the encodedData(s)
// Then we hash it with the rest of the data in the struct
hasher.update(type_hash);
hasher.update(verification_data_hash);
hasher.update(nonce_bytes);
hasher.update(max_fee_bytes);
let encoded_data_hash = hasher.finalize_reset();

// Now we do the actual final
// keccak256(typeHash ‖ encodeData(s))
hasher.update(type_hash);
hasher.update(encoded_data_hash);
let hash_struct = hasher.finalize_reset();

Ok(hash_struct.into())
Expand Down

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions contracts/src/core/BatcherPaymentService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ contract BatcherPaymentService is
noncedVerificationDataTypeHash = _noncedVerificationDataTypeHash;
}

// Defined in types.rs
// keccak256("NoncedVerificationData(bytes32 verification_data_hash,uint256 nonce,uint256 max_fee)")
function initializeNoncedVerificationDataTypeHash(
bytes32 _noncedVerificationDataTypeHash
) public reinitializer(2) onlyOwner {
Expand Down Expand Up @@ -280,15 +282,12 @@ contract BatcherPaymentService is
revert InvalidMaxFee(signatureData.maxFee, feePerProof);
}

bytes32 encodedDataHash = keccak256(
abi.encode(
leaf,
signatureData.nonce,
signatureData.maxFee
)
);

bytes32 structHash = keccak256(abi.encode(noncedVerificationDataTypeHash, encodedDataHash));
bytes32 structHash = keccak256(abi.encode(
noncedVerificationDataTypeHash,
leaf,
signatureData.nonce,
signatureData.maxFee
));

bytes32 hash = _hashTypedDataV4(structHash);

Expand Down
Loading