Skip to content

Commit 3e19fb7

Browse files
authored
Add support for signatures and signers (#303)
* Add signature endpoint * Add support for signatures and signer
1 parent 74770cc commit 3e19fb7

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Static, Type } from "@sinclair/typebox";
2+
import { FastifyInstance } from "fastify";
3+
import { StatusCodes } from "http-status-codes";
4+
import { getWallet } from "../../../utils/cache/getWallet";
5+
import { walletAuthSchema } from "../../schemas/wallet";
6+
7+
const BodySchema = Type.Object({
8+
message: Type.String(),
9+
});
10+
11+
const ReplySchema = Type.Object({
12+
result: Type.String(),
13+
});
14+
15+
export async function signMessage(fastify: FastifyInstance) {
16+
fastify.route<{
17+
Body: Static<typeof BodySchema>;
18+
Reply: Static<typeof ReplySchema>;
19+
}>({
20+
method: "POST",
21+
url: "/backend-wallet/sign-message",
22+
schema: {
23+
summary: "Sign a message",
24+
description: "Send a message",
25+
tags: ["Backend Wallet"],
26+
operationId: "signMessage",
27+
body: BodySchema,
28+
headers: Type.Omit(walletAuthSchema, ["x-account-address"]),
29+
response: {
30+
[StatusCodes.OK]: ReplySchema,
31+
},
32+
},
33+
handler: async (req, res) => {
34+
const { message } = req.body;
35+
const walletAddress = req.headers["x-backend-wallet-address"] as string;
36+
37+
const wallet = await getWallet({
38+
chainId: 1,
39+
walletAddress,
40+
});
41+
42+
const signer = await wallet.getSigner();
43+
const signedMessage = await signer.signMessage(message);
44+
45+
res.status(200).send({
46+
result: signedMessage,
47+
});
48+
},
49+
});
50+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Static, Type } from "@sinclair/typebox";
2+
import { FastifyInstance } from "fastify";
3+
import { StatusCodes } from "http-status-codes";
4+
import { getWallet } from "../../../utils/cache/getWallet";
5+
import { walletAuthSchema } from "../../schemas/wallet";
6+
7+
const BodySchema = Type.Object({
8+
transaction: Type.Object({
9+
to: Type.Optional(Type.String()),
10+
from: Type.Optional(Type.String()),
11+
nonce: Type.Optional(Type.String()),
12+
gasLimit: Type.Optional(Type.String()),
13+
gasPrice: Type.Optional(Type.String()),
14+
data: Type.Optional(Type.String()),
15+
value: Type.Optional(Type.String()),
16+
chainId: Type.Optional(Type.Number()),
17+
type: Type.Optional(Type.Number()),
18+
accessList: Type.Optional(Type.Any()),
19+
maxFeePerGas: Type.Optional(Type.String()),
20+
maxPriorityFeePerGas: Type.Optional(Type.String()),
21+
customData: Type.Optional(Type.Record(Type.String(), Type.Any())),
22+
ccipReadEnabled: Type.Optional(Type.Boolean()),
23+
}),
24+
});
25+
26+
const ReplySchema = Type.Object({
27+
result: Type.String(),
28+
});
29+
30+
export async function signTransaction(fastify: FastifyInstance) {
31+
fastify.route<{
32+
Body: Static<typeof BodySchema>;
33+
Reply: Static<typeof ReplySchema>;
34+
}>({
35+
method: "POST",
36+
url: "/backend-wallet/sign-transaction",
37+
schema: {
38+
summary: "Sign a transaction",
39+
description: "Sign a transaction",
40+
tags: ["Backend Wallet"],
41+
operationId: "signTransaction",
42+
body: BodySchema,
43+
headers: Type.Omit(walletAuthSchema, ["x-account-address"]),
44+
response: {
45+
[StatusCodes.OK]: ReplySchema,
46+
},
47+
},
48+
handler: async (req, res) => {
49+
const { transaction } = req.body;
50+
const walletAddress = req.headers["x-backend-wallet-address"] as string;
51+
52+
const wallet = await getWallet({
53+
chainId: 1,
54+
walletAddress,
55+
});
56+
57+
const signer = await wallet.getSigner();
58+
const signedMessage = await signer.signTransaction(transaction);
59+
60+
res.status(200).send({
61+
result: signedMessage,
62+
});
63+
},
64+
});
65+
}

src/server/routes/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { createBackendWallet } from "./backend-wallet/create";
4747
import { getAll } from "./backend-wallet/getAll";
4848
import { getBalance } from "./backend-wallet/getBalance";
4949
import { importBackendWallet } from "./backend-wallet/import";
50-
import { sendTransaction } from "./backend-wallet/send";
50+
import { sendTransaction } from "./backend-wallet/sendTransaction";
5151
import { transfer } from "./backend-wallet/transfer";
5252
import { updateBackendWallet } from "./backend-wallet/update";
5353

@@ -77,6 +77,8 @@ import { updateAccessToken } from "./auth/access-tokens/update";
7777
import { getAllPermissions } from "./auth/permissions/getAll";
7878
import { grantPermissions } from "./auth/permissions/grant";
7979
import { revokePermissions } from "./auth/permissions/revoke";
80+
import { signMessage } from "./backend-wallet/signMessage";
81+
import { signTransaction } from "./backend-wallet/signTransaction";
8082
import { getAuthConfiguration } from "./configuration/auth/get";
8183
import { updateAuthConfiguration } from "./configuration/auth/update";
8284

@@ -103,6 +105,8 @@ export const withRoutes = async (fastify: FastifyInstance) => {
103105
await fastify.register(getAll);
104106
await fastify.register(transfer);
105107
await fastify.register(sendTransaction);
108+
await fastify.register(signTransaction);
109+
await fastify.register(signMessage);
106110

107111
// Configuration
108112
await fastify.register(getWalletsConfiguration);

0 commit comments

Comments
 (0)