Skip to content

Commit cd31fd8

Browse files
fix(sdk-coin-atom): normalize memo recipients in verifyTransaction
CosmosCoin.verifyTransaction compared txParams.recipients addresses as raw strings against explained outputs, which report the plain on-chain address plus a tx-level memo. Any recipient carrying its memo as address?memoId=X (the form the UI and wallet APIs send for cosmos destinations requiring a memo) failed verification even when the transaction was built correctly, so every ATOM withdrawal to a memo-required destination failed with 'Tx outputs does not match with expected txParams recipients' (COINS-2285). Normalize both sides to {address, amount, memo?} before comparing. A recipient's memo may be declared via the address ?memoId suffix, a recipient.memo field, or the tx-level txParams.memo.value; empty strings are treated as absent (the UI sends memo: '' when the field is unused, and explainTransaction() omits empty memos). Conflicting distinct memo declarations are rejected as ambiguous intent. The expected memo is derived solely from txParams (user intent), never from the prebuild, so verification only ever gets stricter. Ports the per-coin fix precedent from sdk-coin-tia (COINS-1760) and sdk-coin-zeta (CSHLD-1780) and declares the lodash dependency they use. Ticket: COINS-2285
1 parent e76eb28 commit cd31fd8

3 files changed

Lines changed: 182 additions & 3 deletions

File tree

‎modules/sdk-coin-atom/package.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@
4747
"@cosmjs/amino": "^0.29.5",
4848
"@cosmjs/encoding": "^0.29.5",
4949
"@cosmjs/stargate": "^0.29.5",
50-
"bignumber.js": "^9.1.1"
50+
"bignumber.js": "^9.1.1",
51+
"lodash": "^4.18.0"
5152
},
5253
"devDependencies": {
5354
"@bitgo/sdk-api": "^3.1.6",
5455
"@bitgo/sdk-test": "^9.1.91",
55-
"axios": "1.18.0"
56+
"axios": "1.18.0",
57+
"@types/lodash": "^4.14.183"
5658
},
5759
"gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c",
5860
"files": [

‎modules/sdk-coin-atom/src/atom.ts‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { CosmosCoin, CosmosKeyPair, GasAmountDetails } from '@bitgo/abstract-cosmos';
2-
import { BaseCoin, BitGoBase, Environments } from '@bitgo/sdk-core';
2+
import _ from 'lodash';
3+
import BigNumber from 'bignumber.js';
4+
import { BaseCoin, BitGoBase, Environments, TransactionType, VerifyTransactionOptions } from '@bitgo/sdk-core';
35
import { BaseUnit, BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
46
import { KeyPair, TransactionBuilderFactory } from './lib';
57
import { GAS_AMOUNT, GAS_LIMIT } from './lib/constants';
@@ -63,4 +65,68 @@ export class Atom extends CosmosCoin {
6365
getAddressFromPublicKey(pubKey: string): string {
6466
return new KeyPair({ pub: pubKey }).getAddress();
6567
}
68+
69+
/**
70+
* @inheritDoc
71+
*
72+
* Overrides CosmosCoin.verifyTransaction because a recipient's memo can be expressed in
73+
* several ways, while explainTransaction() reports the plain on-chain address plus a single
74+
* tx-level `memo` field (stamped on every output):
75+
* 1. folded into the address as `address?memoId=X` (see getAddressDetails) — the form the
76+
* UI and wallet APIs send for cosmos destinations requiring a memo;
77+
* 2. a `recipient.memo` field (ITransactionRecipient.memo);
78+
* 3. a tx-level `txParams.memo` ({value, type}).
79+
* The base class compares raw address strings, so any memo-tagged recipient fails
80+
* verification even when the transaction was built correctly (reproduced as COINS-2285:
81+
* every ATOM withdrawal to a memo-required destination failed with 'Tx outputs does not
82+
* match with expected txParams recipients'). Normalize both sides to the same
83+
* {address, amount, memo?} shape before comparing. The expected memo is derived solely
84+
* from txParams (user intent) — never from the prebuild — so verification only ever gets
85+
* stricter: a tx whose memo deviates from the approved intent is rejected, a recipient
86+
* missing its memo when the tx carries one is rejected, empty-string memos are treated
87+
* as absent, and conflicting distinct memo declarations are rejected as ambiguous intent.
88+
*/
89+
async verifyTransaction(params: VerifyTransactionOptions): Promise<boolean> {
90+
let totalAmount = new BigNumber(0);
91+
const { txPrebuild, txParams } = params;
92+
const rawTx = txPrebuild.txHex;
93+
if (!rawTx) {
94+
throw new Error('missing required tx prebuild property txHex');
95+
}
96+
const transaction = await this.getBuilder().from(rawTx).build();
97+
const explainedTx = transaction.explainTransaction();
98+
if (txParams.recipients && txParams.recipients.length > 0) {
99+
const filteredRecipients = txParams.recipients.map((recipient) => {
100+
const { address, memoId } = this.getAddressDetails(recipient.address);
101+
// A recipient's memo may be declared via the address `?memoId=` suffix, a `recipient.memo`
102+
// field, or the tx-level `txParams.memo.value`. Empty strings mean "no memo" (the UI and
103+
// wallet APIs send memo: '' when the field is unused, and explainTransaction() omits the
104+
// memo entirely for memo-less transactions). Conflicting distinct declarations are an
105+
// ambiguous intent and are rejected rather than silently resolved by precedence.
106+
const declaredMemos = [memoId, recipient.memo, txParams.memo?.value].filter(
107+
(memo): memo is string => memo !== undefined && memo !== ''
108+
);
109+
if (declaredMemos.some((memo) => memo !== declaredMemos[0])) {
110+
throw new Error('Tx outputs does not match with expected txParams recipients');
111+
}
112+
const memo = declaredMemos[0];
113+
return { address, amount: recipient.amount, ...(memo !== undefined ? { memo } : {}) };
114+
});
115+
const filteredOutputs = explainedTx.outputs.map((output) => _.pick(output, ['address', 'amount', 'memo']));
116+
117+
if (!_.isEqual(filteredOutputs, filteredRecipients)) {
118+
throw new Error('Tx outputs does not match with expected txParams recipients');
119+
}
120+
// WithdrawDelegatorRewards and ContractCall transaction don't have amount
121+
if (transaction.type !== TransactionType.StakingWithdraw && transaction.type !== TransactionType.ContractCall) {
122+
for (const recipient of txParams.recipients) {
123+
totalAmount = totalAmount.plus(recipient.amount);
124+
}
125+
if (!totalAmount.isEqualTo(explainedTx.outputAmount)) {
126+
throw new Error('Tx total amount does not match with expected total amount field');
127+
}
128+
}
129+
}
130+
return true;
131+
}
66132
}

‎modules/sdk-coin-atom/test/unit/atom.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,117 @@ describe('ATOM', function () {
196196
isTransactionVerified.should.equal(true);
197197
});
198198

199+
it('should succeed to verify transaction with a memo-suffixed recipient address', async function () {
200+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
201+
const txParams = {
202+
recipients: [
203+
{
204+
address: `${TEST_TX_WITH_MEMO.to}?memoId=${TEST_TX_WITH_MEMO.memo}`,
205+
amount: TEST_TX_WITH_MEMO.sendAmount,
206+
},
207+
],
208+
};
209+
const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} });
210+
isTransactionVerified.should.equal(true);
211+
});
212+
213+
it('should fail to verify transaction when the recipient memoId does not match the tx memo', async function () {
214+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
215+
const txParams = {
216+
recipients: [{ address: `${TEST_TX_WITH_MEMO.to}?memoId=999999`, amount: TEST_TX_WITH_MEMO.sendAmount }],
217+
};
218+
await basecoin
219+
.verifyTransaction({ txParams, txPrebuild, verification: {} })
220+
.should.be.rejectedWith('Tx outputs does not match with expected txParams recipients');
221+
});
222+
223+
it('should fail to verify transaction when the recipient has no memoId but the tx has one', async function () {
224+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
225+
const txParams = {
226+
recipients: [{ address: TEST_TX_WITH_MEMO.to, amount: TEST_TX_WITH_MEMO.sendAmount }],
227+
};
228+
await basecoin
229+
.verifyTransaction({ txParams, txPrebuild, verification: {} })
230+
.should.be.rejectedWith('Tx outputs does not match with expected txParams recipients');
231+
});
232+
233+
it('should succeed to verify transaction when the memo is provided as a tx-level param', async function () {
234+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
235+
const txParams = {
236+
recipients: [{ address: TEST_TX_WITH_MEMO.to, amount: TEST_TX_WITH_MEMO.sendAmount }],
237+
memo: { value: TEST_TX_WITH_MEMO.memo, type: 'text' },
238+
};
239+
const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} });
240+
isTransactionVerified.should.equal(true);
241+
});
242+
243+
it('should succeed to verify transaction when the memo is provided on the recipient', async function () {
244+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
245+
const txParams = {
246+
recipients: [
247+
{
248+
address: TEST_TX_WITH_MEMO.to,
249+
amount: TEST_TX_WITH_MEMO.sendAmount,
250+
memo: TEST_TX_WITH_MEMO.memo,
251+
},
252+
],
253+
};
254+
const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} });
255+
isTransactionVerified.should.equal(true);
256+
});
257+
258+
it('should fail to verify transaction when the tx-level memo does not match the tx memo', async function () {
259+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
260+
const txParams = {
261+
recipients: [{ address: TEST_TX_WITH_MEMO.to, amount: TEST_TX_WITH_MEMO.sendAmount }],
262+
memo: { value: '999999', type: 'text' },
263+
};
264+
await basecoin
265+
.verifyTransaction({ txParams, txPrebuild, verification: {} })
266+
.should.be.rejectedWith('Tx outputs does not match with expected txParams recipients');
267+
});
268+
269+
it('should succeed to verify transaction when the tx-level memo is empty and the tx has no memo', async function () {
270+
const txPrebuild = { txHex: TEST_SEND_TX.signedTxBase64, txInfo: {} };
271+
const txParams = {
272+
recipients: [{ address: TEST_SEND_TX.recipient, amount: TEST_SEND_TX.sendAmount }],
273+
memo: { value: '', type: 'text' },
274+
};
275+
const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} });
276+
isTransactionVerified.should.equal(true);
277+
});
278+
279+
it('should succeed to verify transaction when memo sources agree with the tx memo', async function () {
280+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
281+
const txParams = {
282+
recipients: [
283+
{
284+
address: `${TEST_TX_WITH_MEMO.to}?memoId=${TEST_TX_WITH_MEMO.memo}`,
285+
amount: TEST_TX_WITH_MEMO.sendAmount,
286+
memo: TEST_TX_WITH_MEMO.memo,
287+
},
288+
],
289+
memo: { value: TEST_TX_WITH_MEMO.memo, type: 'text' },
290+
};
291+
const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} });
292+
isTransactionVerified.should.equal(true);
293+
});
294+
295+
it('should fail to verify transaction when memo sources conflict', async function () {
296+
const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} };
297+
const txParams = {
298+
recipients: [
299+
{
300+
address: `${TEST_TX_WITH_MEMO.to}?memoId=${TEST_TX_WITH_MEMO.memo}`,
301+
amount: TEST_TX_WITH_MEMO.sendAmount,
302+
},
303+
],
304+
memo: { value: '999999', type: 'text' },
305+
};
306+
await basecoin
307+
.verifyTransaction({ txParams, txPrebuild, verification: {} })
308+
.should.be.rejectedWith('Tx outputs does not match with expected txParams recipients');
309+
});
199310
it('should fail to verify transaction with invalid param', async function () {
200311
const txPrebuild = {};
201312
const txParams = { recipients: undefined };

0 commit comments

Comments
 (0)