Skip to content

Commit ce609ee

Browse files
authored
Adds signTypedData Route to Sign EIP712 Messages with Backend Wallets (#414)
* feat: adds signTypedData route * fix(signTypedData): remove unused body param
1 parent 9f6d5fa commit ce609ee

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

src/server/routes/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import { grantPermissions } from "./auth/permissions/grant";
8585
import { revokePermissions } from "./auth/permissions/revoke";
8686
import { signMessage } from "./backend-wallet/signMessage";
8787
import { signTransaction } from "./backend-wallet/signTransaction";
88+
import { signTypedData } from "./backend-wallet/signTypedData";
8889
import { getAuthConfiguration } from "./configuration/auth/get";
8990
import { updateAuthConfiguration } from "./configuration/auth/update";
9091

@@ -102,6 +103,7 @@ import { revokeRelayer } from "./relayer/revoke";
102103
import { getAllTransactions } from "./backend-wallet/getTransactions";
103104
import { resetBackendWalletNonces } from "./backend-wallet/resetNonces";
104105
import { sendTransactionBatch } from "./backend-wallet/sendTransactionBatch";
106+
import { simulateTransaction } from "./backend-wallet/simulateTransaction";
105107
import { withdraw } from "./backend-wallet/withdraw";
106108
import { home } from "./home";
107109
import { updateRelayer } from "./relayer/update";
@@ -110,7 +112,6 @@ import { queueStatus } from "./system/queue";
110112
import { checkGroupStatus } from "./transaction/group";
111113
import { sendSignedTransaction } from "./transaction/sendSignedTx";
112114
import { sendSignedUserOp } from "./transaction/sendSignedUserOp";
113-
import { simulateTransaction } from "./backend-wallet/simulateTransaction";
114115

115116
export const withRoutes = async (fastify: FastifyInstance) => {
116117
// Backend Wallets
@@ -125,6 +126,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
125126
await fastify.register(sendTransactionBatch);
126127
await fastify.register(signTransaction);
127128
await fastify.register(signMessage);
129+
await fastify.register(signTypedData);
128130
await fastify.register(getAllTransactions);
129131
await fastify.register(resetBackendWalletNonces);
130132
await fastify.register(getBackendWalletNonce);

0 commit comments

Comments
 (0)