Skip to content

Commit dd2d8e2

Browse files
authored
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
1 parent 5cc611c commit dd2d8e2

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/server/routes/backend-wallet/sign-transaction.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import {
77
maybeBigInt,
88
maybeInt,
99
} from "../../../shared/utils/primitive-types";
10-
import { toTransactionType } from "../../../shared/utils/sdk";
10+
import { thirdwebClient } from "../../../shared/utils/sdk";
1111
import { createCustomError } from "../../middleware/error";
1212
import { standardResponseSchema } from "../../schemas/shared-api-schemas";
1313
import { walletHeaderSchema } from "../../schemas/wallet";
14-
import type { Hex } from "thirdweb";
14+
import {
15+
prepareTransaction,
16+
toSerializableTransaction,
17+
type Hex,
18+
} from "thirdweb";
19+
import { getChain } from "../../../shared/utils/chain";
1520

1621
const requestBodySchema = Type.Object({
1722
transaction: Type.Object({
@@ -21,7 +26,7 @@ const requestBodySchema = Type.Object({
2126
gasPrice: Type.Optional(Type.String()),
2227
data: Type.Optional(Type.String()),
2328
value: Type.Optional(Type.String()),
24-
chainId: Type.Optional(Type.Integer()),
29+
chainId: Type.Integer(),
2530
type: Type.Optional(Type.Integer()),
2631
accessList: Type.Optional(Type.Any()),
2732
maxFeePerGas: Type.Optional(Type.String()),
@@ -54,14 +59,17 @@ export async function signTransaction(fastify: FastifyInstance) {
5459
},
5560
},
5661
handler: async (request, reply) => {
57-
const { transaction } = request.body;
5862
const { "x-backend-wallet-address": walletAddress } =
5963
request.headers as Static<typeof walletHeaderSchema>;
6064

65+
const { chainId, nonce, ...transaction } = request.body.transaction;
66+
const chain = await getChain(chainId);
67+
6168
const account = await getAccount({
62-
chainId: 1,
69+
chainId,
6370
from: getChecksumAddress(walletAddress),
6471
});
72+
6573
if (!account.signTransaction) {
6674
throw createCustomError(
6775
'This backend wallet does not support "signTransaction".',
@@ -70,23 +78,25 @@ export async function signTransaction(fastify: FastifyInstance) {
7078
);
7179
}
7280

73-
const serializableTransaction = {
74-
chainId: transaction.chainId,
75-
to: getChecksumAddress(transaction.to),
76-
nonce: maybeInt(transaction.nonce),
77-
gas: maybeBigInt(transaction.gasLimit),
78-
gasPrice: maybeBigInt(transaction.gasPrice),
81+
// const prepareTransactionOptions: StaticPrepareTransactionOptions
82+
const prepareTransactionOptions = {
83+
...transaction,
7984
data: transaction.data as Hex | undefined,
85+
client: thirdwebClient,
86+
nonce: maybeInt(nonce),
87+
chain,
8088
value: maybeBigInt(transaction.value),
81-
type: transaction.type
82-
? toTransactionType(transaction.type)
83-
: undefined,
84-
accessList: transaction.accessList,
89+
gas: maybeBigInt(transaction.gasLimit),
90+
gasPrice: maybeBigInt(transaction.gasPrice),
8591
maxFeePerGas: maybeBigInt(transaction.maxFeePerGas),
8692
maxPriorityFeePerGas: maybeBigInt(transaction.maxPriorityFeePerGas),
87-
ccipReadEnabled: transaction.ccipReadEnabled,
8893
};
8994

95+
const preparedTransaction = prepareTransaction(prepareTransactionOptions);
96+
const serializableTransaction = await toSerializableTransaction({
97+
transaction: preparedTransaction,
98+
});
99+
90100
const signature = await account.signTransaction(serializableTransaction);
91101

92102
reply.status(StatusCodes.OK).send({

0 commit comments

Comments
 (0)