From 6e4b635ee720312be6631dee4f24fdd3c066f2eb Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Mon, 18 Nov 2024 13:52:55 +1100 Subject: [PATCH] feat: derive eip-712 type --- .changeset/thin-drinks-matter.md | 5 +++++ src/core/TypedData.ts | 4 ++-- src/core/_test/TypedData.test.ts | 10 +++++----- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 .changeset/thin-drinks-matter.md diff --git a/.changeset/thin-drinks-matter.md b/.changeset/thin-drinks-matter.md new file mode 100644 index 00000000..d435e490 --- /dev/null +++ b/.changeset/thin-drinks-matter.md @@ -0,0 +1,5 @@ +--- +"ox": patch +--- + +Derive EIP-712 Domain type if not provided in `TypedData.serialize`. diff --git a/src/core/TypedData.ts b/src/core/TypedData.ts index f9775752..2ebc1bf8 100644 --- a/src/core/TypedData.ts +++ b/src/core/TypedData.ts @@ -630,9 +630,9 @@ export function serialize< } const domain = (() => { - if (!types.EIP712Domain) return {} if (!domain_) return {} - return normalizeData(types.EIP712Domain, domain_) + const type = types.EIP712Domain ?? extractEip712DomainTypes(domain_) + return normalizeData(type, domain_) })() const message = (() => { diff --git a/src/core/_test/TypedData.test.ts b/src/core/_test/TypedData.test.ts index 73227fa2..7c217a64 100644 --- a/src/core/_test/TypedData.test.ts +++ b/src/core/_test/TypedData.test.ts @@ -979,7 +979,7 @@ describe('serialize', () => { }, }), ).toMatchInlineSnapshot( - `"{"domain":{},"message":{"address":"0xb9cab4f0e46f7f6b1024b5a7463734fa68e633f9","name":"jxom","foo":"0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9"},"primaryType":"Foo","types":{"Foo":[{"name":"address","type":"address"},{"name":"name","type":"string"},{"name":"foo","type":"string"}]}}"`, + `"{"domain":{"name":"Ether!","version":"1","chainId":1,"verifyingContract":"0xcccccccccccccccccccccccccccccccccccccccc"},"message":{"address":"0xb9cab4f0e46f7f6b1024b5a7463734fa68e633f9","name":"jxom","foo":"0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9"},"primaryType":"Foo","types":{"Foo":[{"name":"address","type":"address"},{"name":"name","type":"string"},{"name":"foo","type":"string"}]}}"`, ) }) @@ -1444,6 +1444,10 @@ test('InvalidPrimaryTypeError', () => { test('exports', () => { expect(Object.keys(TypedData)).toMatchInlineSnapshot(` [ + "BytesSizeMismatchError", + "InvalidDomainError", + "InvalidPrimaryTypeError", + "InvalidStructTypeError", "assert", "domainSeparator", "encode", @@ -1454,10 +1458,6 @@ test('exports', () => { "hashStruct", "serialize", "validate", - "BytesSizeMismatchError", - "InvalidDomainError", - "InvalidPrimaryTypeError", - "InvalidStructTypeError", "encodeData", "hashType", "encodeField",