diff --git a/src/utils/hashMessageHash.test.ts b/src/utils/hashMessageHash.test.ts new file mode 100644 index 00000000..45a0e84a --- /dev/null +++ b/src/utils/hashMessageHash.test.ts @@ -0,0 +1,11 @@ +import { hashMessageHash } from './hashMessageHash' +import { test, expect } from 'vitest' + +test('returns correct source hash', async () => { + const hash = + '0xB1C3824DEF40047847145E069BF467AA67E906611B9F5EF31515338DB0AABFA2' + // checked result of same method in OP SDK + expect(hashMessageHash(hash)).toEqual( + '0x4a932049252365b3eedbc5190e18949f2ec11f39d3bef2d259764799a1b27d99', + ) +}) diff --git a/src/utils/hashMessageHash.ts b/src/utils/hashMessageHash.ts new file mode 100644 index 00000000..8a7a5ef9 --- /dev/null +++ b/src/utils/hashMessageHash.ts @@ -0,0 +1,20 @@ +import { Hex, encodeAbiParameters, keccak256, parseAbiParameters } from 'viem' + +// from https://github.com/ethereum-optimism/optimism/blob/develop/packages/sdk/src/utils/message-utils.ts#L42 +// adapted to viem + +/** + * Utility for hashing a message hash. This computes the storage slot + * where the message hash will be stored in state. HashZero is used + * because the first mapping in the contract is used. + * + * @param messageHash Message hash to hash. + * @returns Hash of the given message hash. + */ +export const hashMessageHash = (messageHash: Hex): string => { + const data = encodeAbiParameters(parseAbiParameters(['bytes32, uint256']), [ + messageHash, + 0n, + ]) + return keccak256(data) +}