Skip to content

Commit

Permalink
fix: assert EIP-712 domains are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 14, 2024
1 parent 4b053a9 commit af01579
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/dry-buses-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ox": patch
---

Assert that EIP-712 domains are valid.
16 changes: 15 additions & 1 deletion src/core/TypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ export function assert<
}

// Validate domain types.
if (types.EIP712Domain && domain) validateData(types.EIP712Domain, domain)
if (types.EIP712Domain && domain) {
if (typeof domain !== 'object') throw new InvalidDomainError({ domain })
validateData(types.EIP712Domain, domain)
}

// Validate message types.
if (primaryType !== 'EIP712Domain') {
Expand Down Expand Up @@ -749,6 +752,17 @@ export class BytesSizeMismatchError extends Errors.BaseError {
}
}

/** Thrown when the domain is invalid. */
export class InvalidDomainError extends Errors.BaseError {
override readonly name = 'TypedData.InvalidDomainError'

constructor({ domain }: { domain: unknown }) {
super(`Invalid domain "${Json.stringify(domain)}".`, {
metaMessages: ['Must be a valid EIP-712 domain.'],
})
}
}

/** Thrown when the primary type of a typed data value is invalid. */
export class InvalidPrimaryTypeError extends Errors.BaseError {
override readonly name = 'TypedData.InvalidPrimaryTypeError'
Expand Down
44 changes: 44 additions & 0 deletions src/core/_test/TypedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,49 @@ describe('assert', () => {
)
})

test('domain: invalid', () => {
expect(() =>
TypedData.assert({
domain: 'foo' as never,
primaryType: 'Mail',
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' },
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
},
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
}),
).toThrowErrorMatchingInlineSnapshot(
`
[TypedData.InvalidDomainError: Invalid domain ""foo"".
Must be a valid EIP-712 domain.]
`,
)
})

test('domain: invalid chainId', () => {
expect(() =>
TypedData.assert({
Expand Down Expand Up @@ -1412,6 +1455,7 @@ test('exports', () => {
"serialize",
"validate",
"BytesSizeMismatchError",
"InvalidDomainError",
"InvalidPrimaryTypeError",
"InvalidStructTypeError",
"encodeData",
Expand Down

0 comments on commit af01579

Please sign in to comment.