Skip to content

Commit

Permalink
fix: signTransaction updated to work with latest v5 SDK (#830)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
d4mr authored Jan 15, 2025
1 parent 5cc611c commit dd2d8e2
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/server/routes/backend-wallet/sign-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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()),
Expand Down Expand Up @@ -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<typeof walletHeaderSchema>;

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".',
Expand All @@ -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({
Expand Down

0 comments on commit dd2d8e2

Please sign in to comment.