@@ -5,13 +5,17 @@ import { getAccount } from "../../../shared/utils/account";
5
5
import {
6
6
getChecksumAddress ,
7
7
maybeBigInt ,
8
- maybeInt ,
9
8
} from "../../../shared/utils/primitive-types" ;
10
- import { toTransactionType } from "../../../shared/utils/sdk" ;
9
+ import { thirdwebClient } from "../../../shared/utils/sdk" ;
11
10
import { createCustomError } from "../../middleware/error" ;
12
11
import { standardResponseSchema } from "../../schemas/shared-api-schemas" ;
13
12
import { 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" ;
15
19
16
20
const requestBodySchema = Type . Object ( {
17
21
transaction : Type . Object ( {
@@ -21,7 +25,7 @@ const requestBodySchema = Type.Object({
21
25
gasPrice : Type . Optional ( Type . String ( ) ) ,
22
26
data : Type . Optional ( Type . String ( ) ) ,
23
27
value : Type . Optional ( Type . String ( ) ) ,
24
- chainId : Type . Optional ( Type . Integer ( ) ) ,
28
+ chainId : Type . Integer ( ) ,
25
29
type : Type . Optional ( Type . Integer ( ) ) ,
26
30
accessList : Type . Optional ( Type . Any ( ) ) ,
27
31
maxFeePerGas : Type . Optional ( Type . String ( ) ) ,
@@ -54,14 +58,17 @@ export async function signTransaction(fastify: FastifyInstance) {
54
58
} ,
55
59
} ,
56
60
handler : async ( request , reply ) => {
57
- const { transaction } = request . body ;
58
61
const { "x-backend-wallet-address" : walletAddress } =
59
62
request . headers as Static < typeof walletHeaderSchema > ;
60
63
64
+ const { chainId, nonce, ...transaction } = request . body . transaction ;
65
+ const chain = await getChain ( chainId ) ;
66
+
61
67
const account = await getAccount ( {
62
- chainId : 1 ,
68
+ chainId,
63
69
from : getChecksumAddress ( walletAddress ) ,
64
70
} ) ;
71
+
65
72
if ( ! account . signTransaction ) {
66
73
throw createCustomError (
67
74
'This backend wallet does not support "signTransaction".' ,
@@ -70,23 +77,25 @@ export async function signTransaction(fastify: FastifyInstance) {
70
77
) ;
71
78
}
72
79
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 ,
79
83
data : transaction . data as Hex | undefined ,
84
+ client : thirdwebClient ,
85
+ nonce : nonce ? Number . parseInt ( nonce ) : undefined ,
86
+ chain,
80
87
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 ) ,
85
90
maxFeePerGas : maybeBigInt ( transaction . maxFeePerGas ) ,
86
91
maxPriorityFeePerGas : maybeBigInt ( transaction . maxPriorityFeePerGas ) ,
87
- ccipReadEnabled : transaction . ccipReadEnabled ,
88
92
} ;
89
93
94
+ const preparedTransaction = prepareTransaction ( prepareTransactionOptions ) ;
95
+ const serializableTransaction = await toSerializableTransaction ( {
96
+ transaction : preparedTransaction ,
97
+ } ) ;
98
+
90
99
const signature = await account . signTransaction ( serializableTransaction ) ;
91
100
92
101
reply . status ( StatusCodes . OK ) . send ( {
0 commit comments