Skip to content

Commit

Permalink
Adds signTypedData Route to Sign EIP712 Messages with Backend Wallets (
Browse files Browse the repository at this point in the history
…#414)

* feat: adds signTypedData route

* fix(signTypedData): remove unused body param
  • Loading branch information
gregfromstl authored Feb 22, 2024
1 parent 9f6d5fa commit ce609ee
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/server/routes/backend-wallet/signTypedData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { TypedDataSigner } from "@ethersproject/abstract-signer";
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getWallet } from "../../../utils/cache/getWallet";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
import { walletAuthSchema } from "../../schemas/wallet";

const BodySchema = Type.Object({
domain: Type.Object({}, { additionalProperties: true }),
types: Type.Object({}, { additionalProperties: true }),
value: Type.Object({}, { additionalProperties: true }),
});

const ReplySchema = Type.Object({
result: Type.String(),
});

export async function signTypedData(fastify: FastifyInstance) {
fastify.route<{
Body: Static<typeof BodySchema>;
Reply: Static<typeof ReplySchema>;
}>({
method: "POST",
url: "/backend-wallet/sign-typed-data",
schema: {
summary: "Sign a EIP-712 message",
description: "Send a EIP-712 message",
tags: ["Backend Wallet"],
operationId: "signTypedData",
body: BodySchema,
headers: Type.Omit(walletAuthSchema, ["x-account-address"]),
response: {
...standardResponseSchema,
[StatusCodes.OK]: ReplySchema,
},
},
handler: async (req, res) => {
const { domain, value, types } = req.body;
const walletAddress = req.headers["x-backend-wallet-address"] as string;

const wallet = await getWallet({ chainId: 1, walletAddress });

const signer = (await wallet.getSigner()) as unknown as TypedDataSigner;
const result = await signer._signTypedData(domain, types, value);

res.status(200).send({
result: result,
});
},
});
}
4 changes: 3 additions & 1 deletion src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import { grantPermissions } from "./auth/permissions/grant";
import { revokePermissions } from "./auth/permissions/revoke";
import { signMessage } from "./backend-wallet/signMessage";
import { signTransaction } from "./backend-wallet/signTransaction";
import { signTypedData } from "./backend-wallet/signTypedData";
import { getAuthConfiguration } from "./configuration/auth/get";
import { updateAuthConfiguration } from "./configuration/auth/update";

Expand All @@ -102,6 +103,7 @@ import { revokeRelayer } from "./relayer/revoke";
import { getAllTransactions } from "./backend-wallet/getTransactions";
import { resetBackendWalletNonces } from "./backend-wallet/resetNonces";
import { sendTransactionBatch } from "./backend-wallet/sendTransactionBatch";
import { simulateTransaction } from "./backend-wallet/simulateTransaction";
import { withdraw } from "./backend-wallet/withdraw";
import { home } from "./home";
import { updateRelayer } from "./relayer/update";
Expand All @@ -110,7 +112,6 @@ import { queueStatus } from "./system/queue";
import { checkGroupStatus } from "./transaction/group";
import { sendSignedTransaction } from "./transaction/sendSignedTx";
import { sendSignedUserOp } from "./transaction/sendSignedUserOp";
import { simulateTransaction } from "./backend-wallet/simulateTransaction";

export const withRoutes = async (fastify: FastifyInstance) => {
// Backend Wallets
Expand All @@ -125,6 +126,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
await fastify.register(sendTransactionBatch);
await fastify.register(signTransaction);
await fastify.register(signMessage);
await fastify.register(signTypedData);
await fastify.register(getAllTransactions);
await fastify.register(resetBackendWalletNonces);
await fastify.register(getBackendWalletNonce);
Expand Down

0 comments on commit ce609ee

Please sign in to comment.