message-encoding.ts 1.16 KB
Newer Older
1 2
import { getContractInterface } from '@eth-optimism/contracts'
import { ethers } from 'ethers'
3
import { CoreCrossChainMessage } from '../interfaces'
4 5 6 7 8 9 10 11 12

/**
 * Returns the canonical encoding of a cross chain message. This encoding is used in various
 * locations within the Optimistic Ethereum smart contracts.
 *
 * @param message Cross chain message to encode.
 * @returns Canonical encoding of the message.
 */
export const encodeCrossChainMessage = (
13
  message: CoreCrossChainMessage
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
): string => {
  return getContractInterface('L2CrossDomainMessenger').encodeFunctionData(
    'relayMessage',
    [message.target, message.sender, message.message, message.messageNonce]
  )
}

/**
 * Returns the canonical hash of a cross chain message. This hash is used in various locations
 * within the Optimistic Ethereum smart contracts and is the keccak256 hash of the result of
 * encodeCrossChainMessage.
 *
 * @param message Cross chain message to hash.
 * @returns Canonical hash of the message.
 */
export const hashCrossChainMessage = (
30
  message: CoreCrossChainMessage
31 32 33 34 35 36
): string => {
  return ethers.utils.solidityKeccak256(
    ['bytes'],
    [encodeCrossChainMessage(message)]
  )
}