This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add e2e test for EIP712 legacy TypedData (#154)
- Loading branch information
1 parent
76ea63e
commit 753cbdf
Showing
13 changed files
with
176 additions
and
93 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { _TypedDataEncoder as TypedDataEncoder } from '@ethersproject/hash' | ||
import { keccak256 } from '@ethersproject/keccak256' | ||
import { EIP712ToSign } from '@evmos/transactions' | ||
import { hexToBytes } from './common' | ||
|
||
type EIP712Types = Record<string, { name: string; type: string }[]> | ||
type EIP712Domain = Record<string, number | string> | ||
type EIP712Message = Record<string, unknown> | ||
|
||
const hashDomain = (payload: { | ||
types: EIP712Types | ||
domain: EIP712Domain | ||
}): string => | ||
TypedDataEncoder.hashStruct( | ||
'EIP712Domain', | ||
{ EIP712Domain: payload.types.EIP712Domain }, | ||
payload.domain, | ||
) | ||
|
||
const hashMessage = (payload: { | ||
types: EIP712Types | ||
primaryType: string | ||
message: EIP712Message | ||
}): string => | ||
TypedDataEncoder.from( | ||
(() => { | ||
const types = { ...payload.types } | ||
delete types.EIP712Domain | ||
|
||
// EthersJS assumes the first type is the primary type | ||
const primary = types[payload.primaryType] | ||
if (!primary) { | ||
throw new Error(`No matched primary type: ${payload.primaryType}`) | ||
} | ||
delete types[payload.primaryType] | ||
|
||
return { | ||
[payload.primaryType]: primary, | ||
...types, | ||
} | ||
})(), | ||
).hash(payload.message) | ||
|
||
export const eip712Digest = (payload: EIP712ToSign) => { | ||
const typedPayload = payload as { | ||
types: EIP712Types | ||
primaryType: string | ||
domain: EIP712Domain | ||
message: EIP712Message | ||
} | ||
|
||
const raw = Buffer.concat([ | ||
Buffer.from('19', 'hex'), | ||
Buffer.from('01', 'hex'), | ||
hexToBytes(hashDomain(typedPayload)), | ||
hexToBytes(hashMessage(typedPayload)), | ||
]) | ||
|
||
const hashAsHex = keccak256(raw) | ||
|
||
return hexToBytes(hashAsHex) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.