Skip to content

Commit ee7ff5e

Browse files
committed
fix(sdk-coin-stx): remove array iteration from verifyTransaction
Replace the map() loops over recipients and outputs — and lodash isEqual — with direct index-0 access. STX enforces single-recipient at the top of the method, so iterating an array of one was misleading. Compare the single recipient address, amount, and memo directly against the single decoded output. Ticket: CSHLD-839 Session-Id: 7de30368-fc1b-4b09-9d2e-55be78e462dc Task-Id: 2203e736-35f2-4e2f-8cde-6cdbd0d3e2b6
1 parent 7984056 commit ee7ff5e

2 files changed

Lines changed: 12 additions & 42 deletions

File tree

modules/sdk-coin-stx/src/stx.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash';
21
import {
32
AuditDecryptedKeyParams,
43
BaseCoin,
@@ -116,39 +115,27 @@ export class Stx extends BaseCoin {
116115
throw new Error('missing required tx prebuild property txHex');
117116
}
118117
const explainedTx = await this.explainTransaction({ txHex: rawTx, feeInfo: { fee: '' } });
119-
if (txParams.recipients !== undefined && explainedTx) {
120-
const filteredRecipients = txParams.recipients.map((recipient) => {
121-
const addressDetails = getMemoIdAndBaseAddressFromAddress(recipient.address);
122-
return {
123-
address: addressDetails.address,
124-
amount: BigInt(recipient.amount),
125-
};
126-
});
127-
const filteredOutputs = explainedTx.outputs.map((output) => ({
128-
address: output.address,
129-
amount: BigInt(output.amount),
130-
}));
131-
if (!_.isEqual(filteredOutputs, filteredRecipients)) {
118+
const recipient = txParams.recipients?.[0];
119+
if (recipient !== undefined && explainedTx) {
120+
const txOutput = explainedTx.outputs[0];
121+
const recipientAddress = getMemoIdAndBaseAddressFromAddress(recipient.address).address;
122+
if (txOutput?.address !== recipientAddress || BigInt(txOutput?.amount) !== BigInt(recipient.amount)) {
132123
throw new Error('Tx outputs does not match with expected txParams recipients');
133124
}
134125
// compare memo
135126
let memoInput = '';
136-
let memoOutput = '';
137127
if (memo && memo.value) {
138128
memoInput = memo.value;
139-
} else if (txParams.recipients.length) {
140-
const addressDetails = getMemoIdAndBaseAddressFromAddress(txParams.recipients[0].address);
141-
memoInput = addressDetails.memoId ? addressDetails.memoId : '';
142-
}
143-
if (explainedTx.memo) {
144-
memoOutput = explainedTx.memo;
129+
} else {
130+
const addressDetails = getMemoIdAndBaseAddressFromAddress(recipient.address);
131+
memoInput = addressDetails.memoId ?? '';
145132
}
146-
if (!_.isEqual(memoInput, memoOutput)) {
133+
const memoOutput = explainedTx.memo ?? '';
134+
if (memoInput !== memoOutput) {
147135
throw new Error('Tx memo does not match with expected txParams recipient memo');
148136
}
149-
// compare send amount — STX supports only one recipient, validated above
150-
const recipientAmount = txParams.recipients.length ? new BigNumber(txParams.recipients[0].amount) : new BigNumber(0);
151-
if (!recipientAmount.isEqualTo(explainedTx.outputAmount)) {
137+
// compare amount
138+
if (!new BigNumber(recipient.amount).isEqualTo(explainedTx.outputAmount)) {
152139
throw new Error('Tx total amount does not match with expected total amount field');
153140
}
154141
}

modules/sdk-coin-stx/test/unit/stx.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -373,23 +373,6 @@ describe('STX:', function () {
373373
result.should.equal(true);
374374
});
375375

376-
it('should succeed to verify when memo is passed inside recipient address', async function () {
377-
const wallet = new Wallet(bitgo, basecoin, {});
378-
const txParams = {
379-
recipients: [
380-
{
381-
address: `${testData.txExplainedTransfer.recipient}?memoId=${encodeURIComponent(
382-
testData.txExplainedTransfer.memo
383-
)}`,
384-
amount: testData.txExplainedTransfer.outputAmount,
385-
},
386-
],
387-
wallet,
388-
};
389-
const result = await basecoin.verifyTransaction({ txPrebuild, txParams });
390-
result.should.equal(true);
391-
});
392-
393376
it('should fail to verify transaction with wrong recipient address', async function () {
394377
const wallet = new Wallet(bitgo, basecoin, {});
395378
const txParams = {

0 commit comments

Comments
 (0)