Skip to content

Commit

Permalink
fix: don't use chainId when calculating domain hash of <=1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Dec 5, 2024
1 parent fa245b7 commit 505c459
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { faker } from '@faker-js/faker'
import { getDomainHash } from '.'
import { AbiCoder, keccak256 } from 'ethers'

// <= 1.2.0
// keccak256("EIP712Domain(address verifyingContract)");
const OLD_DOMAIN_TYPEHASH = '0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749'

// >= 1.3.0
// keccak256("EIP712Domain(uint256 chainId,address verifyingContract)");
const NEW_DOMAIN_TYPEHASH = '0x47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218'

describe('SafeTxHashDataRow', () => {
describe('getDomainHash', () => {
it.each(['1.0.0' as const, '1.1.1' as const, '1.2.0' as const])(
'should return the domain hash without chain ID for version %s',
(version) => {
const chainId = faker.string.numeric()
const safeAddress = faker.finance.ethereumAddress()

const result = getDomainHash({ chainId, safeAddress, safeVersion: version })

expect(result).toEqual(
keccak256(AbiCoder.defaultAbiCoder().encode(['bytes32', 'address'], [OLD_DOMAIN_TYPEHASH, safeAddress])),
)
},
)

it.each(['1.3.0' as const, '1.4.1' as const])(
'should return the domain hash with chain ID for version %s',
(version) => {
const chainId = faker.string.numeric()
const safeAddress = faker.finance.ethereumAddress()

const result = getDomainHash({ chainId, safeAddress, safeVersion: version })

expect(result).toEqual(
keccak256(
AbiCoder.defaultAbiCoder().encode(
['bytes32', 'uint256', 'address'],
[NEW_DOMAIN_TYPEHASH, chainId, safeAddress],
),
),
)
},
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import { type SafeTransactionData, type SafeVersion } from '@safe-global/safe-co
import { getEip712TxTypes } from '@safe-global/protocol-kit/dist/src/utils'
import useSafeAddress from '@/hooks/useSafeAddress'
import useChainId from '@/hooks/useChainId'
import semverSatisfies from 'semver/functions/satisfies'

const NEW_DOMAIN_TYPE_HASH_VERSION = '>=1.3.0'

export function getDomainHash({
chainId,
safeAddress,
safeVersion,
}: {
chainId: string
safeAddress: string
safeVersion: SafeVersion
}): string {
return TypedDataEncoder.hashDomain({
...(semverSatisfies(safeVersion, NEW_DOMAIN_TYPE_HASH_VERSION) && { chainId }),
verifyingContract: safeAddress,
})
}

export const SafeTxHashDataRow = ({
safeTxHash,
Expand All @@ -17,10 +35,7 @@ export const SafeTxHashDataRow = ({
const chainId = useChainId()
const safeAddress = useSafeAddress()

const domainHash = TypedDataEncoder.hashDomain({
chainId,
verifyingContract: safeAddress,
})
const domainHash = getDomainHash({ chainId, safeAddress, safeVersion })
const messageHash = safeTxData
? TypedDataEncoder.hashStruct('SafeTx', { SafeTx: getEip712TxTypes(safeVersion).SafeTx }, safeTxData)
: undefined
Expand Down

0 comments on commit 505c459

Please sign in to comment.