Skip to content

Commit 17245f7

Browse files
authored
chore: update signMessage route to v5 SDK (#720)
* fix: Return 400 on invalid wallet * chore: Migrate signMessage to v5 SDK * undo wallet * add e2e test * sign raw message, update tests
1 parent deb7908 commit 17245f7

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

src/server/routes/backend-wallet/signMessage.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { ethers } from "ethers";
3-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
43
import { StatusCodes } from "http-status-codes";
5-
import { getWallet } from "../../../utils/cache/getWallet";
4+
import { isHex, type Hex } from "thirdweb";
5+
import { getAccount } from "../../../utils/account";
6+
import { getChecksumAddress } from "../../../utils/primitiveTypes";
7+
import { createCustomError } from "../../middleware/error";
68
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
79
import { walletHeaderSchema } from "../../schemas/wallet";
810

@@ -15,7 +17,7 @@ const responseBodySchema = Type.Object({
1517
result: Type.String(),
1618
});
1719

18-
export async function signMessage(fastify: FastifyInstance) {
20+
export async function signMessageRoute(fastify: FastifyInstance) {
1921
fastify.route<{
2022
Body: Static<typeof requestBodySchema>;
2123
Reply: Static<typeof responseBodySchema>;
@@ -39,22 +41,23 @@ export async function signMessage(fastify: FastifyInstance) {
3941
const { "x-backend-wallet-address": walletAddress } =
4042
request.headers as Static<typeof walletHeaderSchema>;
4143

42-
const wallet = await getWallet({
43-
chainId: 1,
44-
walletAddress,
45-
});
46-
47-
const signer = await wallet.getSigner();
48-
49-
let signedMessage;
50-
if (isBytes) {
51-
signedMessage = await signer.signMessage(
52-
ethers.utils.arrayify(message),
44+
if (isBytes && !isHex(message)) {
45+
throw createCustomError(
46+
'"isBytes" is true but message is not hex.',
47+
StatusCodes.BAD_REQUEST,
48+
"INVALID_MESSAGE",
5349
);
54-
} else {
55-
signedMessage = await signer.signMessage(message);
5650
}
5751

52+
const account = await getAccount({
53+
chainId: 1,
54+
from: getChecksumAddress(walletAddress),
55+
});
56+
const messageToSign = isBytes ? { raw: message as Hex } : message;
57+
const signedMessage = await account.signMessage({
58+
message: messageToSign,
59+
});
60+
5861
reply.status(StatusCodes.OK).send({
5962
result: signedMessage,
6063
});

src/server/routes/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { removeBackendWallet } from "./backend-wallet/remove";
2222
import { resetBackendWalletNonces } from "./backend-wallet/resetNonces";
2323
import { sendTransaction } from "./backend-wallet/sendTransaction";
2424
import { sendTransactionBatch } from "./backend-wallet/sendTransactionBatch";
25-
import { signMessage } from "./backend-wallet/signMessage";
25+
import { signMessageRoute } from "./backend-wallet/signMessage";
2626
import { signTransaction } from "./backend-wallet/signTransaction";
2727
import { signTypedData } from "./backend-wallet/signTypedData";
2828
import { simulateTransaction } from "./backend-wallet/simulateTransaction";
@@ -123,7 +123,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
123123
await fastify.register(sendTransaction);
124124
await fastify.register(sendTransactionBatch);
125125
await fastify.register(signTransaction);
126-
await fastify.register(signMessage);
126+
await fastify.register(signMessageRoute);
127127
await fastify.register(signTypedData);
128128
await fastify.register(getTransactionsForBackendWallet);
129129
await fastify.register(getTransactionsForBackendWalletByNonce);

test/e2e/bun.lockb

-33.8 KB
Binary file not shown.

test/e2e/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"type": "module",
1212
"dependencies": {
1313
"prool": "^0.0.16",
14-
"thirdweb": "^5.43.2"
14+
"thirdweb": "^5.61.5"
1515
}
1616
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { signMessage, toHex } from "thirdweb/utils";
3+
import { ANVIL_PKEY_A } from "../../utils/wallets";
4+
import { setup } from "../setup";
5+
6+
describe("signMessageRoute", () => {
7+
test("Sign a message (string)", async () => {
8+
const { engine, backendWallet } = await setup();
9+
10+
const res = await engine.backendWallet.signMessage(backendWallet, {
11+
message: "hello world",
12+
});
13+
14+
const expected = signMessage({
15+
message: "hello world",
16+
privateKey: ANVIL_PKEY_A,
17+
});
18+
19+
expect(res.result).toEqual(expected);
20+
});
21+
22+
test("Sign a message (bytes)", async () => {
23+
const { engine, backendWallet } = await setup();
24+
25+
const res = await engine.backendWallet.signMessage(backendWallet, {
26+
message: toHex("hello world"),
27+
isBytes: true,
28+
});
29+
30+
const expected = signMessage({
31+
message: "hello world",
32+
privateKey: ANVIL_PKEY_A,
33+
});
34+
35+
expect(res.result).toEqual(expected);
36+
});
37+
});

0 commit comments

Comments
 (0)