Skip to content

fix(sdk-coin-canton): verify split transactions as a batch - #10196

Open
at31416 wants to merge 1 commit into
masterfrom
CHALO-2192
Open

at31416 wants to merge 1 commit into
masterfrom
CHALO-2192

Conversation

@at31416

@at31416 at31416 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

On EdDSA MPCv2 wallets a Canton UTXO split failed client-side, before any signing round, in two stages.

  1. wallet.signAndSendTxRequest passes no txParams, so resolveEffectiveTxParams (sdk-core) rebuilds recipients from the persisted intent. It read recipient.address.address, which is undefined when the client stores the address as a plain string (the retail split does), and Canton verifyTransaction then crashed with "Cannot read properties of undefined (reading 'split')". Accept both the { address } object and a plain string, and copy numberOfSplits from the intent into the effective txParams (like executorPartyId) so a coin verifier can validate the split count. Adds numberOfSplits to TransactionParams and PopulatedIntent.

  2. IMS builds a split as one root WalletUserProxy_BatchTransfer whose children are numberOfSplits TransferFactory_Transfer self-transfers. verifyTransaction only parsed the first transfer and compared it with the intent amount (the full value of the selected unspents), so it failed with "Tx outputs do not match with expected txParams recipients". Only the first transfer was ever inspected, so further transfers in a batch were unverified too.

When txParams.numberOfSplits > 1, verify the whole batch: parseBatchTransfers returns every transfer and throws unless the transaction has a single BatchTransfer root whose children are all TransferFactory_Transfer. Each transfer must go from the wallet's own address to itself, carry the expected token, and the transfer count must equal numberOfSplits. The total must be within less than numberOfSplits atomic units of the intent amount to allow for rounding dust. numberOfSplits of 0 or 1 keep the existing recipient-matching path. A recipient without an address now fails with a clear error instead of the TypeError.

Tests use captured staging/test txRequests for a native split, a token split and a multi-input token split, plus tampering cases (a transfer rewritten to a foreign party, wrong count, amount, wallet or token).

Ticket: CHALO-2192

On EdDSA MPCv2 wallets a Canton UTXO split failed client-side, before any
signing round, in two stages.

1. wallet.signAndSendTxRequest passes no txParams, so resolveEffectiveTxParams
   (sdk-core) rebuilds recipients from the persisted intent. It read
   recipient.address.address, which is undefined when the client stores the
   address as a plain string (the retail split does), and Canton
   verifyTransaction then crashed with "Cannot read properties of undefined
   (reading 'split')". Accept both the { address } object and a plain string,
   and copy numberOfSplits from the intent into the effective txParams (like
   executorPartyId) so a coin verifier can validate the split count. Adds
   numberOfSplits to TransactionParams and PopulatedIntent.

2. IMS builds a split as one root WalletUserProxy_BatchTransfer whose children
   are numberOfSplits TransferFactory_Transfer self-transfers. verifyTransaction
   only parsed the first transfer and compared it with the intent amount (the
   full value of the selected unspents), so it failed with "Tx outputs do not
   match with expected txParams recipients". Only the first transfer was ever
   inspected, so further transfers in a batch were unverified too.

When txParams.numberOfSplits > 1, verify the whole batch: parseBatchTransfers
returns every transfer and throws unless the transaction has a single
BatchTransfer root whose children are all TransferFactory_Transfer. Each
transfer must go from the wallet's own address to itself, carry the expected
token, and the transfer count must equal numberOfSplits. The total must be
within less than numberOfSplits atomic units of the intent amount to allow for
rounding dust. numberOfSplits of 0 or 1 keep the existing recipient-matching
path. A recipient without an address now fails with a clear error instead of
the TypeError.

Tests use captured staging/test txRequests for a native split, a token split
and a multi-input token split, plus tampering cases (a transfer rewritten to a
foreign party, wrong count, amount, wallet or token).

Ticket: CHALO-2192

Co-Authored-By: Claude <noreply@anthropic.com>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@linear-code

linear-code Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

CHALO-2192

@at31416

at31416 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @at31416's task in 18s —— View job


Review of #10196: verify Canton split transactions as a batch

I read the src changes in sdk-coin-canton and sdk-core. I did not run the tests or read the new test files. The approach is sound, and parseBatchTransfers fails closed on any unexpected transaction shape. Below are two things I'd fix and a few smaller ones.

Should fix

  1. The wallet-ownership check is skipped when rootAddress is missing (canton.ts, verifySplitTransaction).

    • The check only runs when walletRootAddress is truthy.
    • wallet comes from VerifyTransactionOptions, and signAndSendTxRequest may not supply it.
    • If it's absent, the recipient comes from the intent and every transfer is only compared to that same recipient.
    • A tampered intent and transaction that agree with each other would then pass, so a split could send funds to a foreign party.
    • On this path I'd throw when the wallet's root address can't be determined, or at least cover it with a test. The PR description says "from the wallet's own address", which the code doesn't enforce in that case.
  2. numberOfSplits is read from the same intent that gets verified (recipientUtils.ts).

    • The count and the amount both come from the persisted intent. The code then checks the transaction against them.
    • This is fine as long as the intent is trusted, which is the same assumption as for recipients.
    • The comment says the transaction "alone cannot prove" the count. It would be clearer to say the intent is the source of truth and the transaction is checked against it.

Smaller points

  • Dust tolerance: unsplit.gte(numberOfSplits) allows up to numberOfSplits - 1 atomic units of dust, which matches the description. A tampered batch could still skim that much. The effect is negligible, but it's worth a comment saying so. Check that the SDK builder's rounding cannot exceed this bound.
  • amount parsing: new BigNumber(amount) would give NaN for a malformed amount, so isNegative() and gte() would both be false and the check would pass. Add an isNaN / isFinite guard.
  • Token comparison: transfer.token !== expectedToken relies on findTokenNameByContractAddress returning undefined for unknown instruments. Check that an unknown token and native Canton Coin can't both end up as undefined. The isNativeInstrument branch seems to handle this, but a test with an unknown instrument would pin it down.
  • Non-null assertion: txParams.recipients[0] is used after an optional-chain length check. The compiler may not narrow this, so check that it type-checks without a non-null assertion.
  • getIntentRecipientAddress: the as string cast hides an undefined return. The Canton check then catches it, but other coins would see undefined as before. That is probably acceptable.
  • Style: the new BigNumber import sits between the sdk-core and statics imports. Moving it with the other third-party imports would read better.
  • Formatting: there is no blank line between the end of verifyTransaction and the new method's doc comment.

Tests: per the PR description, they cover tampering with a foreign party, count, amount, wallet and token, plus three captured fixtures. I'd add two cases: one where the wallet has no rootAddress, and one for a non-numeric amount.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Compatibility checks are failing on Node.js 27-nightly (experimental, non-blocking). This does not block merge, but it signals an incompatibility to fix before this release becomes supported.

View run

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment on lines +305 to +306
// Equal-value outputs may leave less than one atomic unit per split of rounding dust in the change holding.
const unsplit = new BigNumber(amount).minus(total);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) A Canton split passes verification for any batch total when the expected amount is missing or not numeric. The amount check fails open. new BigNumber(amount) turns an undefined or non-numeric amount into NaN. Both isNegative() and gte(numberOfSplits) return false for NaN, so no error is thrown. Fix: reject the split unless the expected amount is a finite, non-negative integer string (BigNumber.isFinite()) before comparing totals. Then a broken or missing intent amount can no longer skip the check.

Why this was flagged

In verifySplitTransaction (canton.ts:289), amount comes from txParams.recipients[0]. On the TSS path resolveEffectiveTxParams sets it to intentRecipient.amount.value (recipientUtils.ts:157), a server-supplied value, and the code never checks that it exists or is numeric. If it is undefined or non-numeric, new BigNumber(amount) is NaN, unsplit at canton.ts:305 is NaN, and unsplit.isNegative() || unsplit.gte(numberOfSplits) is false. The 'does not match expected amount' error never fires, so the batch total goes unchecked and the split is approved for signing. The check is new in this PR. The other checks (sender, receiver, token, count) do not stand in for the amount bound.

Verification: nit. Trigger: the split recipient's amount reaches verifySplitTransaction as undefined or non-numeric. At canton.ts:307-309: const unsplit = new BigNumber(amount).minus(total); if (unsplit.isNegative() || unsplit.gte(numberOfSplits)) throw .... The error never fires and the method returns true. It is lost validation, not a fund-loss path.

Comment on lines +225 to +227
if (txParams.numberOfSplits !== undefined && txParams.numberOfSplits > 1) {
return this.verifySplitTransaction(transaction, txParams, params.wallet);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟣 pre-existing, not blocking: pre-existing, security-relevant: a batch transfer with no split count is still checked only by its first transfer, so extra transfers go unverified. The batch check runs only when numberOfSplits > 1, and that value comes from the server-supplied intent. Otherwise the recipient path compares just the first TransferFactory_Transfer that parseRawCantonTransactionData finds. Fix: in the regular Send path, reject any prepared transaction whose root is WalletUserProxy_BatchTransfer or that has more than one TransferFactory_Transfer, or else verify every transfer, so leaving out numberOfSplits cannot skip the batch check.
A small fix can ride a push you are already making; otherwise a short reply is enough.

Why this was flagged

The split branch at canton.ts:225 only runs when txParams.numberOfSplits > 1. resolveEffectiveTxParams (recipientUtils.ts:183-187) fills that value in from txRequest.intent, which the server controls. Suppose the intent leaves out numberOfSplits, or sets it to 0 or 1, and the prepared transaction is a WalletUserProxy_BatchTransfer. Verification then falls through to the recipient path at canton.ts:228. explainTransaction there uses parseRawCantonTransactionData, whose default case (utils.ts findExerciseNodeFields('TransferFactory_Transfer')) reads only the first transfer node. If that first transfer matches the user's recipient, the rest of the batch can send funds to other parties and still pass. The base branch behaves the same way, and the PR description names this gap. The new parseBatchTransfers shape check is never applied on this path, so the fix is partial.

Verification: Pre-existing and security-relevant. The new split branch only runs on if (txParams.numberOfSplits !== undefined && txParams.numberOfSplits > 1) (canton.ts:225). In every other case, verification falls through to the old recipient path (canton.ts:228-257). So only the first child of a batch is checked. The base commit has the same Send path.

@kisslove-dewangan kisslove-dewangan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codeowners files lgtm

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants