Skip to content

Commit b877a41

Browse files
committed
fix(sdk-coin-flrp): update address index computation and signing logic in txn builders
Ticket: WIN-8746
1 parent 7719e86 commit b877a41

13 files changed

Lines changed: 318 additions & 2453 deletions

modules/sdk-coin-flrp/src/lib/ExportInPTxBuilder.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
7474
this.transaction._rawSignedBytes = rawBytes;
7575
}
7676

77-
this.computeAddressesIndexFromParsed();
77+
this.computeAddressesIndex(true);
7878

7979
// Use parsed credentials if available, otherwise create new ones based on sigIndices
8080
// The sigIndices from the parsed transaction (stored in addressesIndex) determine
@@ -87,7 +87,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
8787
const sigIndices = utxo.addressesIndex ?? [];
8888
// Use sigIndices-based method if we have valid sigIndices from parsed transaction
8989
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
90-
return this.createCredentialForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
90+
return this.createCredentialForUtxo(utxo, utxoThreshold, sigIndices);
9191
}
9292
return this.createCredentialForUtxo(utxo, utxoThreshold);
9393
});
@@ -97,7 +97,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
9797
const utxoThreshold = utxo.threshold || this.transaction._threshold;
9898
const sigIndices = utxo.addressesIndex ?? [];
9999
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
100-
return this.createAddressMapForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
100+
return this.createAddressMapForUtxo(utxo, utxoThreshold, sigIndices);
101101
}
102102
return this.createAddressMapForUtxo(utxo, utxoThreshold);
103103
});
@@ -156,19 +156,21 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
156156
}
157157

158158
const assetId = utils.flareIdString(this.transaction._assetId).toString();
159-
const fromAddresses = this.transaction._fromAddresses.map((addr) => Buffer.from(addr));
159+
const allFromAddresses = this.transaction._fromAddresses.map((addr) => Buffer.from(addr));
160160
const transferableOutput = TransferableOutput.fromNative(
161161
assetId,
162162
this.transaction._amount,
163-
fromAddresses,
163+
allFromAddresses,
164164
this.transaction._locktime,
165165
this.transaction._threshold
166166
);
167167

168+
const signingAddresses = this.getSigningAddresses();
169+
168170
const exportTx = pvm.e.newExportTx(
169171
{
170172
feeState,
171-
fromAddressesBytes: this.transaction._fromAddresses.map((addr) => Buffer.from(addr)),
173+
fromAddressesBytes: signingAddresses,
172174
destinationChainId: this.transaction._network.cChainBlockchainID,
173175
outputs: [transferableOutput],
174176
utxos: nativeUtxos,
@@ -183,15 +185,16 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
183185
let correctedExportTx: pvmSerial.ExportTx = innerTx;
184186

185187
if (changeOutputs.length > 0) {
188+
const allWalletAddresses = this.transaction._fromAddresses.map((addr) => Buffer.from(addr));
189+
186190
const correctedChangeOutputs = changeOutputs.map((output) => {
187191
const transferOut = output.output as TransferOutput;
188-
const originalOwners = transferOut.outputOwners;
189192

190193
const assetIdStr = utils.flareIdString(Buffer.from(output.assetId.toBytes()).toString('hex')).toString();
191194
return TransferableOutput.fromNative(
192195
assetIdStr,
193196
transferOut.amount(),
194-
originalOwners.addrs.map((addr) => Buffer.from(addr.toBytes())),
197+
allWalletAddresses,
195198
this.transaction._locktime,
196199
this.transaction._threshold
197200
);
@@ -227,11 +230,11 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
227230
this.transaction._utxos = utxosWithIndex;
228231

229232
const txCredentials = utxosWithIndex.map((utxo) =>
230-
this.createCredentialForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
233+
this.createCredentialForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
231234
);
232235

233236
const addressMaps = utxosWithIndex.map((utxo) =>
234-
this.createAddressMapForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
237+
this.createAddressMapForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
235238
);
236239

237240
const fixedUnsignedTx = new UnsignedTx(

modules/sdk-coin-flrp/src/lib/ImportInCTxBuilder.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
7474
fee: fee.toString(),
7575
};
7676

77-
this.computeAddressesIndexFromParsed();
77+
this.computeAddressesIndex(true);
7878

7979
// Create addressMaps using sigIndices from parsed transaction for consistency
8080
const addressMaps = this.transaction._utxos.map((utxo) => {
8181
const utxoThreshold = utxo.threshold || this.transaction._threshold;
8282
const sigIndices = utxo.addressesIndex ?? [];
8383
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
84-
return this.createAddressMapForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
84+
return this.createAddressMapForUtxo(utxo, utxoThreshold, sigIndices);
8585
}
8686
return this.createAddressMapForUtxo(utxo, utxoThreshold);
8787
});
@@ -97,7 +97,7 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
9797
const utxoThreshold = utxo.threshold || this.transaction._threshold;
9898
const sigIndices = utxo.addressesIndex ?? [];
9999
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
100-
return this.createCredentialForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
100+
return this.createCredentialForUtxo(utxo, utxoThreshold, sigIndices);
101101
}
102102
return this.createCredentialForUtxo(utxo, utxoThreshold);
103103
});
@@ -165,10 +165,13 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
165165
`Insufficient UTXO balance: have ${totalUtxoAmount.toString()} nFLR, need more than ${actualFeeNFlr.toString()} nFLR to cover import fee`
166166
);
167167
}
168+
169+
const signingAddresses = this.getSigningAddresses();
170+
168171
const importTx = evm.newImportTx(
169172
this.transaction._context,
170173
this.transaction._to[0],
171-
this.transaction._fromAddresses.map((addr) => Buffer.from(addr)),
174+
signingAddresses,
172175
nativeUtxos,
173176
sourceChain,
174177
actualFeeNFlr
@@ -204,11 +207,11 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
204207
this.transaction._utxos = utxosWithIndex;
205208

206209
const txCredentials = utxosWithIndex.map((utxo) =>
207-
this.createCredentialForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
210+
this.createCredentialForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
208211
);
209212

210213
const addressMaps = utxosWithIndex.map((utxo) =>
211-
this.createAddressMapForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
214+
this.createAddressMapForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
212215
);
213216

214217
const fixedUnsignedTx = new UnsignedTx(innerTx, [], new FlareUtils.AddressMaps(addressMaps), txCredentials);

modules/sdk-coin-flrp/src/lib/ImportInPTxBuilder.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
9393
this.transaction._rawSignedBytes = rawBytes;
9494
}
9595

96-
this.computeAddressesIndexFromParsed();
96+
this.computeAddressesIndex(true);
9797

9898
// Use parsed credentials if available, otherwise create new ones based on sigIndices
9999
// The sigIndices from the parsed transaction (stored in addressesIndex) determine
@@ -106,7 +106,7 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
106106
const sigIndices = utxo.addressesIndex ?? [];
107107
// Use sigIndices-based method if we have valid sigIndices from parsed transaction
108108
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
109-
return this.createCredentialForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
109+
return this.createCredentialForUtxo(utxo, utxoThreshold, sigIndices);
110110
}
111111
return this.createCredentialForUtxo(utxo, utxoThreshold);
112112
});
@@ -116,7 +116,7 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
116116
const utxoThreshold = utxo.threshold || this.transaction._threshold;
117117
const sigIndices = utxo.addressesIndex ?? [];
118118
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
119-
return this.createAddressMapForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
119+
return this.createAddressMapForUtxo(utxo, utxoThreshold, sigIndices);
120120
}
121121
return this.createAddressMapForUtxo(utxo, utxoThreshold);
122122
});
@@ -180,24 +180,18 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
180180
}
181181

182182
const toAddresses = this.transaction._to.map((addr) => Buffer.from(addr));
183-
const fromAddresses = this.transaction._fromAddresses.map((addr) => Buffer.from(addr));
184183

185184
const invalidToAddress = toAddresses.find((addr) => addr.length !== 20);
186185
if (invalidToAddress) {
187186
throw new BuildTransactionError(`Invalid toAddress length: expected 20 bytes, got ${invalidToAddress.length}`);
188187
}
189188

190-
const invalidFromAddress = fromAddresses.find((addr) => addr.length !== 20);
191-
if (invalidFromAddress) {
192-
throw new BuildTransactionError(
193-
`Invalid fromAddress length: expected 20 bytes, got ${invalidFromAddress.length}`
194-
);
195-
}
189+
const signingAddresses = this.getSigningAddresses();
196190

197191
const importTx = pvm.e.newImportTx(
198192
{
199193
feeState: this.transaction._feeState,
200-
fromAddressesBytes: fromAddresses,
194+
fromAddressesBytes: signingAddresses,
201195
sourceChainId: this.transaction._network.cChainBlockchainID,
202196
toAddressesBytes: toAddresses,
203197
utxos: nativeUtxos,
@@ -237,11 +231,11 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
237231
this.transaction._utxos = utxosWithIndex;
238232

239233
const txCredentials = utxosWithIndex.map((utxo) =>
240-
this.createCredentialForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
234+
this.createCredentialForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
241235
);
242236

243237
const addressMaps = utxosWithIndex.map((utxo) =>
244-
this.createAddressMapForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
238+
this.createAddressMapForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
245239
);
246240

247241
const fixedUnsignedTx = new UnsignedTx(innerTx, [], new FlareUtils.AddressMaps(addressMaps), txCredentials);

0 commit comments

Comments
 (0)