|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { signTypedData } from "thirdweb/utils"; |
| 3 | +import { ANVIL_PKEY_A } from "../../utils/wallets"; |
| 4 | +import { setup } from "../setup"; |
| 5 | + |
| 6 | +describe("signTypedDataRoute", () => { |
| 7 | + const data = { |
| 8 | + domain: { |
| 9 | + name: "Ether Mail", |
| 10 | + version: "1", |
| 11 | + chainId: 1, |
| 12 | + verifyingContract: "0x0000000000000000000000000000000000000000", |
| 13 | + }, |
| 14 | + message: { |
| 15 | + contents: "Hello, Bob!", |
| 16 | + from: { |
| 17 | + name: "Alice", |
| 18 | + }, |
| 19 | + }, |
| 20 | + primaryType: "Mail", |
| 21 | + types: { |
| 22 | + EIP712Domain: [ |
| 23 | + { name: "name", type: "string" }, |
| 24 | + { name: "version", type: "string" }, |
| 25 | + { name: "chainId", type: "uint256" }, |
| 26 | + { name: "verifyingContract", type: "address" }, |
| 27 | + ], |
| 28 | + Mail: [ |
| 29 | + { name: "contents", type: "string" }, |
| 30 | + { name: "from", type: "Person" }, |
| 31 | + ], |
| 32 | + Person: [{ name: "name", type: "string" }], |
| 33 | + }, |
| 34 | + } as const; |
| 35 | + |
| 36 | + test("Sign typed data", async () => { |
| 37 | + const { engine, backendWallet } = await setup(); |
| 38 | + |
| 39 | + const res = await engine.backendWallet.signTypedData(backendWallet, { |
| 40 | + domain: data.domain, |
| 41 | + value: data.message, |
| 42 | + types: data.types, |
| 43 | + primaryType: data.primaryType, |
| 44 | + }); |
| 45 | + |
| 46 | + const expected = signTypedData({ |
| 47 | + // @ts-expect-error - bigint serialization |
| 48 | + domain: data.domain, |
| 49 | + message: data.message, |
| 50 | + types: data.types, |
| 51 | + primaryType: data.primaryType, |
| 52 | + privateKey: ANVIL_PKEY_A, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(res.result).toEqual(expected); |
| 56 | + }); |
| 57 | + |
| 58 | + test("Sign typed data without primary type", async () => { |
| 59 | + const { engine, backendWallet } = await setup(); |
| 60 | + |
| 61 | + const res = await engine.backendWallet.signTypedData(backendWallet, { |
| 62 | + domain: data.domain, |
| 63 | + value: data.message, |
| 64 | + types: data.types, |
| 65 | + }); |
| 66 | + |
| 67 | + const expected = signTypedData({ |
| 68 | + // @ts-expect-error - bigint serialization |
| 69 | + domain: data.domain, |
| 70 | + message: data.message, |
| 71 | + types: data.types, |
| 72 | + primaryType: data.primaryType, |
| 73 | + privateKey: ANVIL_PKEY_A, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(res.result).toEqual(expected); |
| 77 | + }); |
| 78 | +}); |
0 commit comments