From dd2d8e234fcfbce74661a019c41c7bc61ecfb87d Mon Sep 17 00:00:00 2001 From: Prithvish Baidya Date: Thu, 16 Jan 2025 04:58:15 +0530 Subject: [PATCH] fix: signTransaction updated to work with latest v5 SDK (#830) * fix: signTransaction updated to work with latest v5 SDK - BREAKING: pre EIP-155 transaction signing is not supported anymore * fix: use maybeInt for nonce parsing in signTransaction --- .../routes/backend-wallet/sign-transaction.ts | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/server/routes/backend-wallet/sign-transaction.ts b/src/server/routes/backend-wallet/sign-transaction.ts index 17b31292..3cab7e0a 100644 --- a/src/server/routes/backend-wallet/sign-transaction.ts +++ b/src/server/routes/backend-wallet/sign-transaction.ts @@ -7,11 +7,16 @@ import { maybeBigInt, maybeInt, } from "../../../shared/utils/primitive-types"; -import { toTransactionType } from "../../../shared/utils/sdk"; +import { thirdwebClient } from "../../../shared/utils/sdk"; import { createCustomError } from "../../middleware/error"; import { standardResponseSchema } from "../../schemas/shared-api-schemas"; import { walletHeaderSchema } from "../../schemas/wallet"; -import type { Hex } from "thirdweb"; +import { + prepareTransaction, + toSerializableTransaction, + type Hex, +} from "thirdweb"; +import { getChain } from "../../../shared/utils/chain"; const requestBodySchema = Type.Object({ transaction: Type.Object({ @@ -21,7 +26,7 @@ const requestBodySchema = Type.Object({ gasPrice: Type.Optional(Type.String()), data: Type.Optional(Type.String()), value: Type.Optional(Type.String()), - chainId: Type.Optional(Type.Integer()), + chainId: Type.Integer(), type: Type.Optional(Type.Integer()), accessList: Type.Optional(Type.Any()), maxFeePerGas: Type.Optional(Type.String()), @@ -54,14 +59,17 @@ export async function signTransaction(fastify: FastifyInstance) { }, }, handler: async (request, reply) => { - const { transaction } = request.body; const { "x-backend-wallet-address": walletAddress } = request.headers as Static; + const { chainId, nonce, ...transaction } = request.body.transaction; + const chain = await getChain(chainId); + const account = await getAccount({ - chainId: 1, + chainId, from: getChecksumAddress(walletAddress), }); + if (!account.signTransaction) { throw createCustomError( 'This backend wallet does not support "signTransaction".', @@ -70,23 +78,25 @@ export async function signTransaction(fastify: FastifyInstance) { ); } - const serializableTransaction = { - chainId: transaction.chainId, - to: getChecksumAddress(transaction.to), - nonce: maybeInt(transaction.nonce), - gas: maybeBigInt(transaction.gasLimit), - gasPrice: maybeBigInt(transaction.gasPrice), + // const prepareTransactionOptions: StaticPrepareTransactionOptions + const prepareTransactionOptions = { + ...transaction, data: transaction.data as Hex | undefined, + client: thirdwebClient, + nonce: maybeInt(nonce), + chain, value: maybeBigInt(transaction.value), - type: transaction.type - ? toTransactionType(transaction.type) - : undefined, - accessList: transaction.accessList, + gas: maybeBigInt(transaction.gasLimit), + gasPrice: maybeBigInt(transaction.gasPrice), maxFeePerGas: maybeBigInt(transaction.maxFeePerGas), maxPriorityFeePerGas: maybeBigInt(transaction.maxPriorityFeePerGas), - ccipReadEnabled: transaction.ccipReadEnabled, }; + const preparedTransaction = prepareTransaction(prepareTransactionOptions); + const serializableTransaction = await toSerializableTransaction({ + transaction: preparedTransaction, + }); + const signature = await account.signTransaction(serializableTransaction); reply.status(StatusCodes.OK).send({