Skip to content

Commit

Permalink
hashMessageHash
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncusack committed Aug 26, 2023
1 parent 188a989 commit 967eb83
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/utils/hashMessageHash.test.ts
Original file line number Diff line number Diff line change
@@ -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',
)
})
20 changes: 20 additions & 0 deletions src/utils/hashMessageHash.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 967eb83

Please sign in to comment.