@@ -76,26 +76,15 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
7676 const txCredentials =
7777 credentials . length > 0
7878 ? credentials
79- : exportTx . baseTx . inputs . map ( ( input , inputIdx ) => {
80- const transferInput = input . input as TransferInput ;
81- const inputThreshold = transferInput . sigIndicies ( ) . length || this . transaction . _threshold ;
82-
83- const utxo = this . transaction . _utxos [ inputIdx ] ;
84-
85- if ( inputThreshold === this . transaction . _threshold ) {
86- return this . createCredentialForUtxo ( utxo , this . transaction . _threshold ) ;
87- } else {
88- const sigSlots : ReturnType < typeof utils . createNewSig > [ ] = [ ] ;
89- for ( let i = 0 ; i < inputThreshold ; i ++ ) {
90- sigSlots . push ( utils . createNewSig ( '' ) ) ;
91- }
92- return new Credential ( sigSlots ) ;
93- }
79+ : this . transaction . _utxos . map ( ( utxo ) => {
80+ const utxoThreshold = utxo . threshold || this . transaction . _threshold ;
81+ return this . createCredentialForUtxo ( utxo , utxoThreshold ) ;
9482 } ) ;
9583
96- const addressMaps = txCredentials . map ( ( credential , credIdx ) =>
97- this . createAddressMapForUtxo ( this . transaction . _utxos [ credIdx ] , this . transaction . _threshold )
98- ) ;
84+ const addressMaps = this . transaction . _utxos . map ( ( utxo ) => {
85+ const utxoThreshold = utxo . threshold || this . transaction . _threshold ;
86+ return this . createAddressMapForUtxo ( utxo , utxoThreshold ) ;
87+ } ) ;
9988
10089 const unsignedTx = new UnsignedTx ( exportTx , [ ] , new FlareUtils . AddressMaps ( addressMaps ) , txCredentials ) ;
10190 this . transaction . setTransaction ( unsignedTx ) ;
@@ -167,29 +156,51 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
167156 this . transaction . _context
168157 ) ;
169158
170- this . transaction . setTransaction ( exportTx ) ;
159+ const flareUnsignedTx = exportTx as UnsignedTx ;
160+ const innerTx = flareUnsignedTx . getTx ( ) as pvmSerial . ExportTx ;
161+
162+ const utxosWithIndex = innerTx . baseTx . inputs . map ( ( input , idx ) => {
163+ const transferInput = input . input as TransferInput ;
164+ const addressesIndex = transferInput . sigIndicies ( ) ;
165+ return {
166+ ...this . transaction . _utxos [ idx ] ,
167+ addressesIndex,
168+ addresses : [ ] ,
169+ threshold : addressesIndex . length || this . transaction . _utxos [ idx ] . threshold ,
170+ } ;
171+ } ) ;
172+
173+ const txCredentials = utxosWithIndex . map ( ( utxo ) => this . createCredentialForUtxo ( utxo , utxo . threshold ) ) ;
174+
175+ const addressMaps = utxosWithIndex . map ( ( utxo ) => this . createAddressMapForUtxo ( utxo , utxo . threshold ) ) ;
176+
177+ const fixedUnsignedTx = new UnsignedTx ( innerTx , [ ] , new FlareUtils . AddressMaps ( addressMaps ) , txCredentials ) ;
178+
179+ this . transaction . setTransaction ( fixedUnsignedTx ) ;
171180 }
172181
173182 /**
174183 * Recover UTXOs from inputs
184+ * Extract addressesIndex from sigIndicies for proper signature ordering
175185 * @param inputs Array of TransferableInput
176186 * @returns Array of decoded UTXO objects
177187 */
178188 private recoverUtxos ( inputs : TransferableInput [ ] ) : DecodedUtxoObj [ ] {
179189 return inputs . map ( ( input ) => {
180190 const utxoId = input . utxoID ;
181191 const transferInput = input . input as TransferInput ;
182- const inputThreshold = transferInput . sigIndicies ( ) . length ;
183- return {
192+ const addressesIndex = transferInput . sigIndicies ( ) ;
193+
194+ const utxo : DecodedUtxoObj = {
184195 outputID : SECP256K1_Transfer_Output ,
185196 amount : input . amount ( ) . toString ( ) ,
186197 txid : utils . cb58Encode ( Buffer . from ( utxoId . txID . toBytes ( ) ) ) ,
187198 outputidx : utxoId . outputIdx . value ( ) . toString ( ) ,
188- threshold : inputThreshold || this . transaction . _threshold ,
189- addresses : this . transaction . _fromAddresses . map ( ( addr ) =>
190- utils . addressToString ( this . transaction . _network . hrp , this . transaction . _network . alias , Buffer . from ( addr ) )
191- ) ,
199+ threshold : addressesIndex . length || this . transaction . _threshold ,
200+ addresses : [ ] ,
201+ addressesIndex,
192202 } ;
203+ return utxo ;
193204 } ) ;
194205 }
195206}
0 commit comments