diff --git a/modules/sdk-coin-tia/src/tia.ts b/modules/sdk-coin-tia/src/tia.ts index 1a4e8b022e6..e3220f60733 100644 --- a/modules/sdk-coin-tia/src/tia.ts +++ b/modules/sdk-coin-tia/src/tia.ts @@ -69,11 +69,16 @@ export class Tia extends CosmosCoin { /** * @inheritDoc * - * Overrides CosmosCoin.verifyTransaction because recipients can carry their memo folded into - * the address as `address?memoId=X` (see getAddressDetails), while explainTransaction() reports - * the plain on-chain address plus a separate `memo` field. The base class compares the two - * as raw strings, so any memo-tagged recipient fails verification even when the transaction was - * built correctly. Normalize both sides to the same {address, amount, memo?} shape first. + * Overrides CosmosCoin.verifyTransaction because a recipient's memo can be expressed in + * several ways, while explainTransaction() reports the plain on-chain address plus a single + * tx-level `memo` field stamped on every output: + * 1. folded into the address as `address?memoId=X` (see getAddressDetails) — the API shape; + * 2. a `recipient.memo` field; + * 3. a tx-level `txParams.memo` ({value, type}) — the web UI shape (bare address plus memo), + * also restored from intent.memo by resolveEffectiveTxParams on pending-approval re-signs. + * Normalize both sides to the same {address, amount, memo?} shape before comparing. Empty + * memos count as absent, and conflicting declarations are rejected. A declared memo must match + * the transaction memo exactly; when none is declared, the transaction must not carry one. */ async verifyTransaction(params: VerifyTransactionOptions): Promise { let totalAmount = new BigNumber(0); @@ -87,7 +92,14 @@ export class Tia extends CosmosCoin { if (txParams.recipients && txParams.recipients.length > 0) { const filteredRecipients = txParams.recipients.map((recipient) => { const { address, memoId } = this.getAddressDetails(recipient.address); - return { address, amount: recipient.amount, ...(memoId !== undefined ? { memo: memoId } : {}) }; + const declaredMemos = [memoId, recipient.memo, txParams.memo?.value].filter( + (memo): memo is string => memo !== undefined && memo !== '' + ); + if (declaredMemos.some((memo) => memo !== declaredMemos[0])) { + throw new Error('Tx outputs does not match with expected txParams recipients'); + } + const memo = declaredMemos[0]; + return { address, amount: recipient.amount, ...(memo !== undefined ? { memo } : {}) }; }); const filteredOutputs = explainedTx.outputs.map((output) => _.pick(output, ['address', 'amount', 'memo'])); diff --git a/modules/sdk-coin-tia/test/unit/tia.ts b/modules/sdk-coin-tia/test/unit/tia.ts index d91efabf424..607dcbae398 100644 --- a/modules/sdk-coin-tia/test/unit/tia.ts +++ b/modules/sdk-coin-tia/test/unit/tia.ts @@ -3,7 +3,7 @@ import { BitGoAPI } from '@bitgo/sdk-api'; import { EcdsaRangeProof, EcdsaTypes } from '@bitgo/sdk-lib-mpc'; import { mockSerializedChallengeWithProofs, TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; import { coins } from '@bitgo/statics'; -import { ErrorNoInputToRecover } from '@bitgo/sdk-core'; +import { ErrorNoInputToRecover, resolveEffectiveTxParams } from '@bitgo/sdk-core'; import BigNumber from 'bignumber.js'; import { beforeEach } from 'mocha'; import sinon from 'sinon'; @@ -234,6 +234,69 @@ describe('TIA', function () { .should.be.rejectedWith('Tx outputs does not match with expected txParams recipients'); }); + it('should succeed to verify transaction when the memo is provided as a tx-level param', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [{ address: TEST_TX_WITH_MEMO.to, amount: TEST_TX_WITH_MEMO.sendAmount }], + memo: { value: TEST_TX_WITH_MEMO.memo, type: '' }, + }; + const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} }); + isTransactionVerified.should.equal(true); + }); + + it('should fail to verify transaction when the tx-level memo does not match the tx memo', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [{ address: TEST_TX_WITH_MEMO.to, amount: TEST_TX_WITH_MEMO.sendAmount }], + memo: { value: '999999', type: '' }, + }; + await basecoin + .verifyTransaction({ txParams, txPrebuild, verification: {} }) + .should.be.rejectedWith('Tx outputs does not match with expected txParams recipients'); + }); + + it('should fail to verify transaction when the address memoId and the tx-level memo conflict', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [ + { address: `${TEST_TX_WITH_MEMO.to}?memoId=${TEST_TX_WITH_MEMO.memo}`, amount: TEST_TX_WITH_MEMO.sendAmount }, + ], + memo: { value: '999999', type: '' }, + }; + await basecoin + .verifyTransaction({ txParams, txPrebuild, verification: {} }) + .should.be.rejectedWith('Tx outputs does not match with expected txParams recipients'); + }); + + it('should succeed to verify transaction when the tx-level memo is empty and the tx has no memo', async function () { + const txPrebuild = { txHex: TEST_SEND_TX.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [{ address: TEST_SEND_TX.recipient, amount: TEST_SEND_TX.sendAmount }], + memo: { value: '', type: '' }, + }; + const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} }); + isTransactionVerified.should.equal(true); + }); + + it('should succeed to verify a memo transaction re-signed from a persisted intent', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txRequest = { + intent: { + intentType: 'payment', + recipients: [ + { + address: { address: TEST_TX_WITH_MEMO.to }, + amount: { value: TEST_TX_WITH_MEMO.sendAmount, symbol: 'ttia' }, + }, + ], + memo: TEST_TX_WITH_MEMO.memo, + }, + }; + const txParams = resolveEffectiveTxParams(txRequest as Parameters[0], undefined); + const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} }); + isTransactionVerified.should.equal(true); + }); + it('should fail to verify transaction with invalid param', async function () { const txPrebuild = {}; const txParams = { recipients: undefined }; diff --git a/modules/sdk-coin-zeta/src/zeta.ts b/modules/sdk-coin-zeta/src/zeta.ts index 2eb40953652..9066276bab6 100644 --- a/modules/sdk-coin-zeta/src/zeta.ts +++ b/modules/sdk-coin-zeta/src/zeta.ts @@ -89,15 +89,16 @@ export class Zeta extends CosmosCoin { /** * @inheritDoc * - * Overrides CosmosCoin.verifyTransaction to fix a ZETA-specific gap: recipients can carry - * their memo folded into the address as `address?memoId=X` (the BitGo combined - * address+memo convention - see getAddressDetails), but explainTransaction() always - * reports the plain on-chain address plus a separate `memo` field. The base class - * compares the two sides as raw strings, so any memo-tagged recipient fails verification - * even when the transaction was built correctly - this reproduced a live incident where a - * client could never sign a correctly-built, pending ZETA transaction to a memo'd - * recipient. Normalize both sides to the same {address, amount, memo?} shape before - * comparing. + * Overrides CosmosCoin.verifyTransaction because a recipient's memo can be expressed in + * several ways, while explainTransaction() reports the plain on-chain address plus a single + * tx-level `memo` field stamped on every output: + * 1. folded into the address as `address?memoId=X` (see getAddressDetails) — the API shape; + * 2. a `recipient.memo` field; + * 3. a tx-level `txParams.memo` ({value, type}) — the web UI shape (bare address plus memo), + * also restored from intent.memo by resolveEffectiveTxParams on pending-approval re-signs. + * Normalize both sides to the same {address, amount, memo?} shape before comparing. Empty + * memos count as absent, and conflicting declarations are rejected. A declared memo must match + * the transaction memo exactly; when none is declared, the transaction must not carry one. */ async verifyTransaction(params: VerifyTransactionOptions): Promise { let totalAmount = new BigNumber(0); @@ -111,7 +112,14 @@ export class Zeta extends CosmosCoin { if (txParams.recipients && txParams.recipients.length > 0) { const filteredRecipients = txParams.recipients.map((recipient) => { const { address, memoId } = this.getAddressDetails(recipient.address); - return { address, amount: recipient.amount, ...(memoId !== undefined ? { memo: memoId } : {}) }; + const declaredMemos = [memoId, recipient.memo, txParams.memo?.value].filter( + (memo): memo is string => memo !== undefined && memo !== '' + ); + if (declaredMemos.some((memo) => memo !== declaredMemos[0])) { + throw new Error('Tx outputs does not match with expected txParams recipients'); + } + const memo = declaredMemos[0]; + return { address, amount: recipient.amount, ...(memo !== undefined ? { memo } : {}) }; }); const filteredOutputs = explainedTx.outputs.map((output) => _.pick(output, ['address', 'amount', 'memo'])); diff --git a/modules/sdk-coin-zeta/test/unit/zeta.ts b/modules/sdk-coin-zeta/test/unit/zeta.ts index 4ff40477c50..efd49718090 100644 --- a/modules/sdk-coin-zeta/test/unit/zeta.ts +++ b/modules/sdk-coin-zeta/test/unit/zeta.ts @@ -1,6 +1,6 @@ import { CosmosTransaction, RedelegateMessage, SendMessage } from '@bitgo/abstract-cosmos'; import { BitGoAPI } from '@bitgo/sdk-api'; -import { Environments, ErrorNoInputToRecover, RecoveryProviderError } from '@bitgo/sdk-core'; +import { Environments, ErrorNoInputToRecover, RecoveryProviderError, resolveEffectiveTxParams } from '@bitgo/sdk-core'; import { EcdsaRangeProof, EcdsaTypes } from '@bitgo/sdk-lib-mpc'; import { TestBitGo, TestBitGoAPI, mockSerializedChallengeWithProofs } from '@bitgo/sdk-test'; import { coins } from '@bitgo/statics'; @@ -249,6 +249,69 @@ describe('Zeta', function () { .verifyTransaction({ txParams, txPrebuild, verification }) .should.be.rejectedWith('Tx outputs does not match with expected txParams recipients'); }); + + it('should succeed to verify transaction when the memo is provided as a tx-level param', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [{ address: TEST_TX_WITH_MEMO.to, amount: TEST_TX_WITH_MEMO.sendAmount }], + memo: { value: TEST_TX_WITH_MEMO.memo, type: '' }, + }; + const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} }); + isTransactionVerified.should.equal(true); + }); + + it('should fail to verify transaction when the tx-level memo does not match the tx memo', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [{ address: TEST_TX_WITH_MEMO.to, amount: TEST_TX_WITH_MEMO.sendAmount }], + memo: { value: '999999', type: '' }, + }; + await basecoin + .verifyTransaction({ txParams, txPrebuild, verification: {} }) + .should.be.rejectedWith('Tx outputs does not match with expected txParams recipients'); + }); + + it('should fail to verify transaction when the address memoId and the tx-level memo conflict', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [ + { address: `${TEST_TX_WITH_MEMO.to}?memoId=${TEST_TX_WITH_MEMO.memo}`, amount: TEST_TX_WITH_MEMO.sendAmount }, + ], + memo: { value: '999999', type: '' }, + }; + await basecoin + .verifyTransaction({ txParams, txPrebuild, verification: {} }) + .should.be.rejectedWith('Tx outputs does not match with expected txParams recipients'); + }); + + it('should succeed to verify transaction when the tx-level memo is empty and the tx has no memo', async function () { + const txPrebuild = { txHex: TEST_SEND_TX.signedTxBase64, txInfo: {} }; + const txParams = { + recipients: [{ address: TEST_SEND_TX.recipient, amount: TEST_SEND_TX.sendAmount }], + memo: { value: '', type: '' }, + }; + const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} }); + isTransactionVerified.should.equal(true); + }); + + it('should succeed to verify a memo transaction re-signed from a persisted intent', async function () { + const txPrebuild = { txHex: TEST_TX_WITH_MEMO.signedTxBase64, txInfo: {} }; + const txRequest = { + intent: { + intentType: 'payment', + recipients: [ + { + address: { address: TEST_TX_WITH_MEMO.to }, + amount: { value: TEST_TX_WITH_MEMO.sendAmount, symbol: 'tzeta' }, + }, + ], + memo: TEST_TX_WITH_MEMO.memo, + }, + }; + const txParams = resolveEffectiveTxParams(txRequest as Parameters[0], undefined); + const isTransactionVerified = await basecoin.verifyTransaction({ txParams, txPrebuild, verification: {} }); + isTransactionVerified.should.equal(true); + }); }); describe('Explain Transaction: ', () => {