@@ -5,13 +5,17 @@ import { getAccount } from "../../../shared/utils/account";
55import {
66 getChecksumAddress ,
77 maybeBigInt ,
8- maybeInt ,
98} from "../../../shared/utils/primitive-types" ;
10- import { toTransactionType } from "../../../shared/utils/sdk" ;
9+ import { thirdwebClient } from "../../../shared/utils/sdk" ;
1110import { createCustomError } from "../../middleware/error" ;
1211import { standardResponseSchema } from "../../schemas/shared-api-schemas" ;
1312import { walletHeaderSchema } from "../../schemas/wallet" ;
14- import type { Hex } from "thirdweb" ;
13+ import {
14+ prepareTransaction ,
15+ toSerializableTransaction ,
16+ type Hex ,
17+ } from "thirdweb" ;
18+ import { getChain } from "../../../shared/utils/chain" ;
1519
1620const requestBodySchema = Type . Object ( {
1721 transaction : Type . Object ( {
@@ -21,7 +25,7 @@ const requestBodySchema = Type.Object({
2125 gasPrice : Type . Optional ( Type . String ( ) ) ,
2226 data : Type . Optional ( Type . String ( ) ) ,
2327 value : Type . Optional ( Type . String ( ) ) ,
24- chainId : Type . Optional ( Type . Integer ( ) ) ,
28+ chainId : Type . Integer ( ) ,
2529 type : Type . Optional ( Type . Integer ( ) ) ,
2630 accessList : Type . Optional ( Type . Any ( ) ) ,
2731 maxFeePerGas : Type . Optional ( Type . String ( ) ) ,
@@ -54,14 +58,17 @@ export async function signTransaction(fastify: FastifyInstance) {
5458 } ,
5559 } ,
5660 handler : async ( request , reply ) => {
57- const { transaction } = request . body ;
5861 const { "x-backend-wallet-address" : walletAddress } =
5962 request . headers as Static < typeof walletHeaderSchema > ;
6063
64+ const { chainId, nonce, ...transaction } = request . body . transaction ;
65+ const chain = await getChain ( chainId ) ;
66+
6167 const account = await getAccount ( {
62- chainId : 1 ,
68+ chainId,
6369 from : getChecksumAddress ( walletAddress ) ,
6470 } ) ;
71+
6572 if ( ! account . signTransaction ) {
6673 throw createCustomError (
6774 'This backend wallet does not support "signTransaction".' ,
@@ -70,23 +77,25 @@ export async function signTransaction(fastify: FastifyInstance) {
7077 ) ;
7178 }
7279
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 ) ,
80+ // const prepareTransactionOptions: StaticPrepareTransactionOptions
81+ const prepareTransactionOptions = {
82+ ...transaction ,
7983 data : transaction . data as Hex | undefined ,
84+ client : thirdwebClient ,
85+ nonce : nonce ? Number . parseInt ( nonce ) : undefined ,
86+ chain,
8087 value : maybeBigInt ( transaction . value ) ,
81- type : transaction . type
82- ? toTransactionType ( transaction . type )
83- : undefined ,
84- accessList : transaction . accessList ,
88+ gas : maybeBigInt ( transaction . gasLimit ) ,
89+ gasPrice : maybeBigInt ( transaction . gasPrice ) ,
8590 maxFeePerGas : maybeBigInt ( transaction . maxFeePerGas ) ,
8691 maxPriorityFeePerGas : maybeBigInt ( transaction . maxPriorityFeePerGas ) ,
87- ccipReadEnabled : transaction . ccipReadEnabled ,
8892 } ;
8993
94+ const preparedTransaction = prepareTransaction ( prepareTransactionOptions ) ;
95+ const serializableTransaction = await toSerializableTransaction ( {
96+ transaction : preparedTransaction ,
97+ } ) ;
98+
9099 const signature = await account . signTransaction ( serializableTransaction ) ;
91100
92101 reply . status ( StatusCodes . OK ) . send ( {
0 commit comments