import { pad } from "viem"; import { expect, test } from "vitest"; import { typedData } from "../../../test/src/typed-data.js"; import { toHex } from "../encoding/hex.js"; import { hashTypedData } from "./hashTypedData.js"; test("default", () => { expect( hashTypedData({ ...typedData.basic, primaryType: "Mail", }), ).toMatchInlineSnapshot( '"0x448f54762ef8ecccdc4d19bb7d467161383cd4b271617a8cee05c790eb745d74"', ); }); test("complex", () => { expect( hashTypedData({ ...typedData.complex, primaryType: "Mail", }), ).toMatchInlineSnapshot( '"0x9a74cb859ad30835ffb2da406423233c212cf6dd78e6c2c98b0c9289568954ae"', ); }); test("no domain", () => { expect( hashTypedData({ ...typedData.complex, domain: undefined, primaryType: "Mail", }), ).toMatchInlineSnapshot( '"0x14ed1dbbfecbe5de3919f7ea47daafdf3a29dfbb60dd88d85509f79773d503a5"', ); expect( hashTypedData({ ...typedData.complex, domain: {}, primaryType: "Mail", }), ).toMatchInlineSnapshot( '"0x14ed1dbbfecbe5de3919f7ea47daafdf3a29dfbb60dd88d85509f79773d503a5"', ); }); test("domain: empty name", () => { expect( hashTypedData({ ...typedData.complex, domain: { name: "" }, primaryType: "Mail", }), ).toMatchInlineSnapshot( '"0xc3f4f9ebd774352940f60aebbc83fcee20d0b17eb42bd1b20c91a748001ecb53"', ); }); test("minimal valid typed message", () => { const hash = hashTypedData({ domain: {}, primaryType: "EIP712Domain", types: { EIP712Domain: [], }, }); expect(hash).toMatchInlineSnapshot( '"0x8d4a3f4082945b7879e2b55f181c31a77c8c0a464b70669458abbaaf99de4c38"', ); }); test("typed message with a domain separator that uses all fields.", () => { const hash = hashTypedData({ domain: { chainId: 1n, name: "example.metamask.io", salt: pad(toHex(new Uint8Array([1, 2, 3])), { dir: "right" }), verifyingContract: "0x0000000000000000000000000000000000000000", version: "1", }, primaryType: "EIP712Domain", types: { EIP712Domain: [ { name: "name", type: "string", }, { name: "version", type: "string", }, { name: "chainId", type: "uint256", }, { name: "verifyingContract", type: "address", }, { name: "salt", type: "bytes32", }, ], }, }); expect(hash).toMatchInlineSnapshot( '"0x54ffed5209a17ac210ef3823740b3852ee9cd518b84ee39f0a3fa7f2f9b4205b"', ); }); test("typed message with only custom domain separator fields", () => { const hash = hashTypedData({ domain: { customChainId: 1n, customName: "example.metamask.io", customSalt: pad(toHex(new Uint8Array([1, 2, 3])), { dir: "right" }), customVerifyingContract: "0x0000000000000000000000000000000000000000", customVersion: "1", extraField: "stuff", }, primaryType: "EIP712Domain", types: { EIP712Domain: [ { name: "customName", type: "string", }, { name: "customVersion", type: "string", }, { name: "customChainId", type: "uint256", }, { name: "customVerifyingContract", type: "address", }, { name: "customSalt", type: "bytes32", }, { name: "extraField", type: "string", }, ], }, }); expect(hash).toMatchInlineSnapshot( '"0x3efa3ef0305f56ba5bba62000500e29fe82c5314bca2f958c64e31b2498560f8"', ); }); test("typed message with data", () => { const hash = hashTypedData({ domain: { chainId: 1n, name: "example.metamask.io", salt: pad(toHex(new Uint8Array([1, 2, 3])), { dir: "right" }), verifyingContract: "0x0000000000000000000000000000000000000000", version: "1", }, message: { data: "Hello!", }, primaryType: "Message", types: { EIP712Domain: [ { name: "name", type: "string", }, { name: "version", type: "string", }, { name: "chainId", type: "uint256", }, { name: "verifyingContract", type: "address", }, { name: "salt", type: "bytes32", }, ], Message: [{ name: "data", type: "string" }], }, }); expect(hash).toMatchInlineSnapshot( '"0xd2669f23b7849020ad41bcbff5b51372793f91320e0f901641945568ed7322be"', ); });