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" ;
43import { 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" ;
68import { standardResponseSchema } from "../../schemas/sharedApiSchemas" ;
79import { 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 } ) ;
0 commit comments