|
| 1 | +import type { TypedDataSigner } from "@ethersproject/abstract-signer"; |
| 2 | +import { Static, Type } from "@sinclair/typebox"; |
| 3 | +import { FastifyInstance } from "fastify"; |
| 4 | +import { StatusCodes } from "http-status-codes"; |
| 5 | +import { getWallet } from "../../../utils/cache/getWallet"; |
| 6 | +import { standardResponseSchema } from "../../schemas/sharedApiSchemas"; |
| 7 | +import { walletAuthSchema } from "../../schemas/wallet"; |
| 8 | + |
| 9 | +const BodySchema = Type.Object({ |
| 10 | + domain: Type.Object({}, { additionalProperties: true }), |
| 11 | + types: Type.Object({}, { additionalProperties: true }), |
| 12 | + value: Type.Object({}, { additionalProperties: true }), |
| 13 | +}); |
| 14 | + |
| 15 | +const ReplySchema = Type.Object({ |
| 16 | + result: Type.String(), |
| 17 | +}); |
| 18 | + |
| 19 | +export async function signTypedData(fastify: FastifyInstance) { |
| 20 | + fastify.route<{ |
| 21 | + Body: Static<typeof BodySchema>; |
| 22 | + Reply: Static<typeof ReplySchema>; |
| 23 | + }>({ |
| 24 | + method: "POST", |
| 25 | + url: "/backend-wallet/sign-typed-data", |
| 26 | + schema: { |
| 27 | + summary: "Sign a EIP-712 message", |
| 28 | + description: "Send a EIP-712 message", |
| 29 | + tags: ["Backend Wallet"], |
| 30 | + operationId: "signTypedData", |
| 31 | + body: BodySchema, |
| 32 | + headers: Type.Omit(walletAuthSchema, ["x-account-address"]), |
| 33 | + response: { |
| 34 | + ...standardResponseSchema, |
| 35 | + [StatusCodes.OK]: ReplySchema, |
| 36 | + }, |
| 37 | + }, |
| 38 | + handler: async (req, res) => { |
| 39 | + const { domain, value, types } = req.body; |
| 40 | + const walletAddress = req.headers["x-backend-wallet-address"] as string; |
| 41 | + |
| 42 | + const wallet = await getWallet({ chainId: 1, walletAddress }); |
| 43 | + |
| 44 | + const signer = (await wallet.getSigner()) as unknown as TypedDataSigner; |
| 45 | + const result = await signer._signTypedData(domain, types, value); |
| 46 | + |
| 47 | + res.status(200).send({ |
| 48 | + result: result, |
| 49 | + }); |
| 50 | + }, |
| 51 | + }); |
| 52 | +} |
0 commit comments