@@ -75,7 +75,7 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
7575 return TransactionType . Export ;
7676 }
7777
78- initBuilder ( tx : Tx ) : this {
78+ initBuilder ( tx : Tx , rawBytes ?: Buffer ) : this {
7979 const baseTx = tx as evmSerial . ExportTx ;
8080 if ( ! this . verifyTxType ( baseTx . _type ) ) {
8181 throw new NotSupported ( 'Transaction cannot be parsed or has an unsupported transaction type' ) ;
@@ -106,14 +106,41 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
106106 const outputAmount = transferOutput . amount ( ) ;
107107 const fee = inputAmount - outputAmount ;
108108 this . _amount = outputAmount ;
109- this . transaction . _fee . feeRate = Number ( fee ) - Number ( this . fixedFee ) ;
109+ // Store the actual fee directly (don't subtract fixedFee since buildFlareTransaction doesn't add it back)
110+ this . transaction . _fee . feeRate = Number ( fee ) ;
110111 this . transaction . _fee . fee = fee . toString ( ) ;
111112 this . transaction . _fee . size = 1 ;
112113 this . transaction . _fromAddresses = [ Buffer . from ( input . address . toBytes ( ) ) ] ;
113114 this . transaction . _locktime = transferOutput . getLocktime ( ) ;
114115
115116 this . _nonce = input . nonce . value ( ) ;
116- this . transaction . setTransaction ( tx ) ;
117+
118+ // Check if raw bytes contain credentials and extract them
119+ const { hasCredentials, credentials } = rawBytes
120+ ? utils . extractCredentialsFromRawBytes ( rawBytes , baseTx , 'EVM' )
121+ : { hasCredentials : false , credentials : [ ] } ;
122+
123+ // If it's a signed transaction, store the original raw bytes to preserve exact format
124+ if ( hasCredentials && rawBytes ) {
125+ this . transaction . _rawSignedBytes = rawBytes ;
126+ }
127+
128+ // Create proper UnsignedTx wrapper with credentials
129+ const fromAddress = new Address ( this . transaction . _fromAddresses [ 0 ] ) ;
130+ const addressMap = new FlareUtils . AddressMap ( [
131+ [ fromAddress , 0 ] ,
132+ [ fromAddress , 1 ] ,
133+ ] ) ;
134+ const addressMaps = new FlareUtils . AddressMaps ( [ addressMap ] ) ;
135+
136+ const unsignedTx = new UnsignedTx (
137+ baseTx ,
138+ [ ] ,
139+ addressMaps ,
140+ credentials . length > 0 ? credentials : [ new Credential ( [ utils . createNewSig ( '' ) ] ) ]
141+ ) ;
142+
143+ this . transaction . setTransaction ( unsignedTx ) ;
117144 return this ;
118145 }
119146
@@ -147,8 +174,9 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
147174 throw new Error ( 'nonce is required' ) ;
148175 }
149176
150- const txFee = BigInt ( this . fixedFee ) ;
151- const fee = BigInt ( this . transaction . _fee . feeRate ) + txFee ;
177+ // For EVM exports, feeRate represents the total fee (baseFee * gasUnits)
178+ // Don't add fixedFee as it's already accounted for in the EVM gas model
179+ const fee = BigInt ( this . transaction . _fee . feeRate ) ;
152180 this . transaction . _fee . fee = fee . toString ( ) ;
153181 this . transaction . _fee . size = 1 ;
154182
@@ -158,6 +186,15 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
158186 const amount = new BigIntPr ( this . _amount + fee ) ;
159187 const nonce = new BigIntPr ( this . _nonce ) ;
160188 const input = new evmSerial . Input ( fromAddress , amount , assetId , nonce ) ;
189+ // Map all destination P-chain addresses for multisig support
190+ // Sort addresses alphabetically by hex representation (required by Avalanche/Flare protocol)
191+ const sortedToAddresses = [ ...this . transaction . _to ] . sort ( ( a , b ) => {
192+ const aHex = Buffer . from ( a ) . toString ( 'hex' ) ;
193+ const bHex = Buffer . from ( b ) . toString ( 'hex' ) ;
194+ return aHex . localeCompare ( bHex ) ;
195+ } ) ;
196+ const toAddresses = sortedToAddresses . map ( ( addr ) => new Address ( addr ) ) ;
197+
161198 const exportTx = new evmSerial . ExportTx (
162199 new Int ( this . transaction . _networkID ) ,
163200 utils . flareIdString ( this . transaction . _blockchainID ) ,
@@ -168,9 +205,11 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
168205 assetId ,
169206 new TransferOutput (
170207 new BigIntPr ( this . _amount ) ,
171- new OutputOwners ( new BigIntPr ( this . transaction . _locktime ) , new Int ( this . transaction . _threshold ) , [
172- new Address ( this . transaction . _to [ 0 ] ) ,
173- ] )
208+ new OutputOwners (
209+ new BigIntPr ( this . transaction . _locktime ) ,
210+ new Int ( this . transaction . _threshold ) ,
211+ toAddresses
212+ )
174213 )
175214 ) ,
176215 ]
0 commit comments