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