Repository navigation
fix(sdk-coin-canton): verify split transactions as a batch #10196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import { | |
| VerifyTransactionOptions, | ||
| } from '@bitgo/sdk-core'; | ||
| import { auditEddsaPrivateKey } from '@bitgo/sdk-lib-mpc'; | ||
| import BigNumber from 'bignumber.js'; | ||
| import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics'; | ||
| import { TransactionBuilderFactory } from './lib'; | ||
| import { KeyPair as CantonKeyPair } from './lib/keyPair'; | ||
|
|
@@ -221,9 +222,15 @@ export class Canton extends BaseCoin { | |
| } | ||
| return true; | ||
| case TransactionType.Send: | ||
| if (txParams.numberOfSplits !== undefined && txParams.numberOfSplits > 1) { | ||
| return this.verifySplitTransaction(transaction, txParams, params.wallet); | ||
| } | ||
| if (txParams.recipients !== undefined) { | ||
| const filteredRecipients = txParams.recipients?.map((recipient) => { | ||
| const { address, amount, tokenName } = recipient; | ||
| if (!address) { | ||
| throw new Error('Recipient address is required to verify a Canton transfer'); | ||
| } | ||
| const [addressPart, memoId] = address.split('?memoId='); | ||
| return { | ||
| address: addressPart, | ||
|
|
@@ -251,6 +258,58 @@ export class Canton extends BaseCoin { | |
| } | ||
| } | ||
| } | ||
| /** | ||
| * A split (fanout) moves value from the wallet to itself: one batch of `numberOfSplits` self-transfers. | ||
| * The intent carries the full amount of the selected unspents, so the transaction is verified as a whole | ||
| * rather than against its first transfer. | ||
| */ | ||
| private verifySplitTransaction( | ||
| transaction: BaseTransaction, | ||
| txParams: TransactionParams, | ||
| wallet: VerifyTransactionOptions['wallet'] | ||
| ): boolean { | ||
| const numberOfSplits = txParams.numberOfSplits as number; | ||
| const rawPrepared = (transaction as Transaction).prepareCommand?.preparedTransaction; | ||
| if (!rawPrepared) { | ||
| throw new Error('Split verifyTransaction: missing preparedTransaction protobuf'); | ||
| } | ||
| if (txParams.recipients?.length !== 1) { | ||
| throw new Error('Split verifyTransaction: expected exactly one recipient'); | ||
| } | ||
| const { address, amount, tokenName } = txParams.recipients[0]; | ||
| if (!address) { | ||
| throw new Error('Recipient address is required to verify a Canton split'); | ||
| } | ||
| const [expectedParty] = address.split('?memoId='); | ||
| const walletRootAddress = wallet?.coinSpecific?.()?.rootAddress; | ||
| if (walletRootAddress && walletRootAddress.split('?memoId=')[0] !== expectedParty) { | ||
| throw new Error(`Split recipient '${expectedParty}' is not the wallet's own address`); | ||
| } | ||
| // A token coin instance resolves its token from the coin itself; native Canton Coin has none. | ||
| const expectedToken = tokenName ?? (coins.get(this.getChain()).isToken ? this.getChain() : undefined); | ||
|
|
||
| const transfers = utils.parseBatchTransfers(rawPrepared); | ||
| if (transfers.length !== numberOfSplits) { | ||
| throw new Error(`Split transaction has ${transfers.length} transfers, expected ${numberOfSplits}`); | ||
| } | ||
| let total = new BigNumber(0); | ||
| for (const transfer of transfers) { | ||
| if (transfer.sender !== expectedParty || transfer.receiver !== expectedParty) { | ||
| throw new Error('Split transaction transfers funds to a party other than the wallet itself'); | ||
| } | ||
| if (transfer.token !== expectedToken) { | ||
| throw new Error(`Split transaction token '${transfer.token}' does not match expected '${expectedToken}'`); | ||
| } | ||
| total = total.plus(transfer.amount); | ||
| } | ||
| // 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); | ||
|
Comment on lines
+305
to
+306
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Why this was flaggedIn verifySplitTransaction (canton.ts:289), Verification: nit. Trigger: the split recipient's |
||
| if (unsplit.isNegative() || unsplit.gte(numberOfSplits)) { | ||
| throw new Error(`Split transaction total ${total.toFixed()} does not match expected amount ${amount}`); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private verifyBroadridgeStandingDelegationTransaction( | ||
| transaction: BaseTransaction, | ||
| executorPartyId: string | ||
|
|
||
Large diffs are not rendered by default.
There was a problem hiding this comment.
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 firstTransferFactory_TransferthatparseRawCantonTransactionDatafinds. Fix: in the regular Send path, reject any prepared transaction whose root isWalletUserProxy_BatchTransferor that has more than oneTransferFactory_Transfer, or else verify every transfer, so leaving outnumberOfSplitscannot 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.