Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions modules/sdk-coin-flrp/src/lib/ExportInPTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
this.transaction._rawSignedBytes = rawBytes;
}

this.computeAddressesIndexFromParsed();
this.computeAddressesIndex(true);

// Use parsed credentials if available, otherwise create new ones based on sigIndices
// The sigIndices from the parsed transaction (stored in addressesIndex) determine
Expand All @@ -87,7 +87,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
const sigIndices = utxo.addressesIndex ?? [];
// Use sigIndices-based method if we have valid sigIndices from parsed transaction
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
return this.createCredentialForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
return this.createCredentialForUtxo(utxo, utxoThreshold, sigIndices);
}
return this.createCredentialForUtxo(utxo, utxoThreshold);
});
Expand All @@ -97,7 +97,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
const utxoThreshold = utxo.threshold || this.transaction._threshold;
const sigIndices = utxo.addressesIndex ?? [];
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
return this.createAddressMapForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
return this.createAddressMapForUtxo(utxo, utxoThreshold, sigIndices);
}
return this.createAddressMapForUtxo(utxo, utxoThreshold);
});
Expand Down Expand Up @@ -156,19 +156,21 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
}

const assetId = utils.flareIdString(this.transaction._assetId).toString();
const fromAddresses = this.transaction._fromAddresses.map((addr) => Buffer.from(addr));
const allFromAddresses = this.transaction._fromAddresses.map((addr) => Buffer.from(addr));
const transferableOutput = TransferableOutput.fromNative(
assetId,
this.transaction._amount,
fromAddresses,
allFromAddresses,
this.transaction._locktime,
this.transaction._threshold
);

const signingAddresses = this.getSigningAddresses();

const exportTx = pvm.e.newExportTx(
{
feeState,
fromAddressesBytes: this.transaction._fromAddresses.map((addr) => Buffer.from(addr)),
fromAddressesBytes: signingAddresses,
destinationChainId: this.transaction._network.cChainBlockchainID,
outputs: [transferableOutput],
utxos: nativeUtxos,
Expand All @@ -183,15 +185,16 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
let correctedExportTx: pvmSerial.ExportTx = innerTx;

if (changeOutputs.length > 0) {
const allWalletAddresses = this.transaction._fromAddresses.map((addr) => Buffer.from(addr));

const correctedChangeOutputs = changeOutputs.map((output) => {
const transferOut = output.output as TransferOutput;
const originalOwners = transferOut.outputOwners;

const assetIdStr = utils.flareIdString(Buffer.from(output.assetId.toBytes()).toString('hex')).toString();
return TransferableOutput.fromNative(
assetIdStr,
transferOut.amount(),
originalOwners.addrs.map((addr) => Buffer.from(addr.toBytes())),
allWalletAddresses,
this.transaction._locktime,
this.transaction._threshold
);
Expand Down Expand Up @@ -227,11 +230,11 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
this.transaction._utxos = utxosWithIndex;

const txCredentials = utxosWithIndex.map((utxo) =>
this.createCredentialForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
this.createCredentialForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
);

const addressMaps = utxosWithIndex.map((utxo) =>
this.createAddressMapForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
this.createAddressMapForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
);

const fixedUnsignedTx = new UnsignedTx(
Expand Down
15 changes: 9 additions & 6 deletions modules/sdk-coin-flrp/src/lib/ImportInCTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
fee: fee.toString(),
};

this.computeAddressesIndexFromParsed();
this.computeAddressesIndex(true);

// Create addressMaps using sigIndices from parsed transaction for consistency
const addressMaps = this.transaction._utxos.map((utxo) => {
const utxoThreshold = utxo.threshold || this.transaction._threshold;
const sigIndices = utxo.addressesIndex ?? [];
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
return this.createAddressMapForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
return this.createAddressMapForUtxo(utxo, utxoThreshold, sigIndices);
}
return this.createAddressMapForUtxo(utxo, utxoThreshold);
});
Expand All @@ -97,7 +97,7 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
const utxoThreshold = utxo.threshold || this.transaction._threshold;
const sigIndices = utxo.addressesIndex ?? [];
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
return this.createCredentialForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
return this.createCredentialForUtxo(utxo, utxoThreshold, sigIndices);
}
return this.createCredentialForUtxo(utxo, utxoThreshold);
});
Expand Down Expand Up @@ -165,10 +165,13 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
`Insufficient UTXO balance: have ${totalUtxoAmount.toString()} nFLR, need more than ${actualFeeNFlr.toString()} nFLR to cover import fee`
);
}

const signingAddresses = this.getSigningAddresses();

const importTx = evm.newImportTx(
this.transaction._context,
this.transaction._to[0],
this.transaction._fromAddresses.map((addr) => Buffer.from(addr)),
signingAddresses,
nativeUtxos,
sourceChain,
actualFeeNFlr
Expand Down Expand Up @@ -204,11 +207,11 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
this.transaction._utxos = utxosWithIndex;

const txCredentials = utxosWithIndex.map((utxo) =>
this.createCredentialForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
this.createCredentialForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
);

const addressMaps = utxosWithIndex.map((utxo) =>
this.createAddressMapForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
this.createAddressMapForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
);

const fixedUnsignedTx = new UnsignedTx(innerTx, [], new FlareUtils.AddressMaps(addressMaps), txCredentials);
Expand Down
20 changes: 7 additions & 13 deletions modules/sdk-coin-flrp/src/lib/ImportInPTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
this.transaction._rawSignedBytes = rawBytes;
}

this.computeAddressesIndexFromParsed();
this.computeAddressesIndex(true);

// Use parsed credentials if available, otherwise create new ones based on sigIndices
// The sigIndices from the parsed transaction (stored in addressesIndex) determine
Expand All @@ -106,7 +106,7 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
const sigIndices = utxo.addressesIndex ?? [];
// Use sigIndices-based method if we have valid sigIndices from parsed transaction
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
return this.createCredentialForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
return this.createCredentialForUtxo(utxo, utxoThreshold, sigIndices);
}
return this.createCredentialForUtxo(utxo, utxoThreshold);
});
Expand All @@ -116,7 +116,7 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
const utxoThreshold = utxo.threshold || this.transaction._threshold;
const sigIndices = utxo.addressesIndex ?? [];
if (sigIndices.length >= utxoThreshold && sigIndices.every((idx) => idx >= 0)) {
return this.createAddressMapForUtxoWithSigIndices(utxo, utxoThreshold, sigIndices);
return this.createAddressMapForUtxo(utxo, utxoThreshold, sigIndices);
}
return this.createAddressMapForUtxo(utxo, utxoThreshold);
});
Expand Down Expand Up @@ -180,24 +180,18 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
}

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

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

const invalidFromAddress = fromAddresses.find((addr) => addr.length !== 20);
if (invalidFromAddress) {
throw new BuildTransactionError(
`Invalid fromAddress length: expected 20 bytes, got ${invalidFromAddress.length}`
);
}
const signingAddresses = this.getSigningAddresses();

const importTx = pvm.e.newImportTx(
{
feeState: this.transaction._feeState,
fromAddressesBytes: fromAddresses,
fromAddressesBytes: signingAddresses,
sourceChainId: this.transaction._network.cChainBlockchainID,
toAddressesBytes: toAddresses,
utxos: nativeUtxos,
Expand Down Expand Up @@ -237,11 +231,11 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
this.transaction._utxos = utxosWithIndex;

const txCredentials = utxosWithIndex.map((utxo) =>
this.createCredentialForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
this.createCredentialForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
);

const addressMaps = utxosWithIndex.map((utxo) =>
this.createAddressMapForUtxoWithSigIndices(utxo, utxo.threshold, utxo.actualSigIndices)
this.createAddressMapForUtxo(utxo, utxo.threshold, utxo.actualSigIndices)
);

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