From 5c3e4b6b385e0497f0a1c04663c7f097c36048cd Mon Sep 17 00:00:00 2001 From: Prithvish Baidya Date: Wed, 15 Jan 2025 11:40:43 +0530 Subject: [PATCH 1/2] fix: signTransaction updated to work with latest v5 SDK --- .../routes/backend-wallet/sign-transaction.ts | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/server/routes/backend-wallet/sign-transaction.ts b/src/server/routes/backend-wallet/sign-transaction.ts index 17b31292..074975ba 100644 --- a/src/server/routes/backend-wallet/sign-transaction.ts +++ b/src/server/routes/backend-wallet/sign-transaction.ts @@ -5,13 +5,17 @@ import { getAccount } from "../../../shared/utils/account"; import { getChecksumAddress, 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 +25,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 +58,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 +77,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: nonce ? Number.parseInt(nonce) : undefined, + 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({ From 4b2c2b05a5494a2108890ae54ea8893228e28e06 Mon Sep 17 00:00:00 2001 From: Prithvish Baidya Date: Wed, 15 Jan 2025 17:23:22 +0530 Subject: [PATCH 2/2] fix: use maybeInt for nonce parsing in signTransaction --- src/server/routes/backend-wallet/sign-transaction.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/server/routes/backend-wallet/sign-transaction.ts b/src/server/routes/backend-wallet/sign-transaction.ts index 074975ba..3cab7e0a 100644 --- a/src/server/routes/backend-wallet/sign-transaction.ts +++ b/src/server/routes/backend-wallet/sign-transaction.ts @@ -5,6 +5,7 @@ import { getAccount } from "../../../shared/utils/account"; import { getChecksumAddress, maybeBigInt, + maybeInt, } from "../../../shared/utils/primitive-types"; import { thirdwebClient } from "../../../shared/utils/sdk"; import { createCustomError } from "../../middleware/error"; @@ -82,7 +83,7 @@ export async function signTransaction(fastify: FastifyInstance) { ...transaction, data: transaction.data as Hex | undefined, client: thirdwebClient, - nonce: nonce ? Number.parseInt(nonce) : undefined, + nonce: maybeInt(nonce), chain, value: maybeBigInt(transaction.value), gas: maybeBigInt(transaction.gasLimit),