Skip to content

Commit 967eb83

Browse files
committed
hashMessageHash
1 parent 188a989 commit 967eb83

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/utils/hashMessageHash.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { hashMessageHash } from './hashMessageHash'
2+
import { test, expect } from 'vitest'
3+
4+
test('returns correct source hash', async () => {
5+
const hash =
6+
'0xB1C3824DEF40047847145E069BF467AA67E906611B9F5EF31515338DB0AABFA2'
7+
// checked result of same method in OP SDK
8+
expect(hashMessageHash(hash)).toEqual(
9+
'0x4a932049252365b3eedbc5190e18949f2ec11f39d3bef2d259764799a1b27d99',
10+
)
11+
})

src/utils/hashMessageHash.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Hex, encodeAbiParameters, keccak256, parseAbiParameters } from 'viem'
2+
3+
// from https://github.com/ethereum-optimism/optimism/blob/develop/packages/sdk/src/utils/message-utils.ts#L42
4+
// adapted to viem
5+
6+
/**
7+
* Utility for hashing a message hash. This computes the storage slot
8+
* where the message hash will be stored in state. HashZero is used
9+
* because the first mapping in the contract is used.
10+
*
11+
* @param messageHash Message hash to hash.
12+
* @returns Hash of the given message hash.
13+
*/
14+
export const hashMessageHash = (messageHash: Hex): string => {
15+
const data = encodeAbiParameters(parseAbiParameters(['bytes32, uint256']), [
16+
messageHash,
17+
0n,
18+
])
19+
return keccak256(data)
20+
}

0 commit comments

Comments
 (0)