From 3e00ac70721dea711722e50c6b11ea43be94333d Mon Sep 17 00:00:00 2001 From: Mullapudi Pruthvik Date: Tue, 24 Nov 2026 14:14:35 +0530 Subject: [PATCH] feat(sdk-core): reject cold wallets from defi vault flows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cold wallets can neither sign client-side (TSS) nor be signed server-side (custodial/custody), so every DeFi vault flow is meaningless for them — reject with a clear client-side error before any network call, instead of an opaque prebuild failure from wallet-platform. Guards depositToVault, resumeDeposit, withdrawFromVault, wrap, and unwrap; read-only methods (getOperation, listOperations, getVaultConfig) stay unguarded since inspecting positions from any wallet is harmless. Ticket: DEFI-1359 --- modules/sdk-core/src/bitgo/defi/defiVault.ts | 25 +++++++ .../test/unit/bitgo/defi/defiVault.ts | 69 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/modules/sdk-core/src/bitgo/defi/defiVault.ts b/modules/sdk-core/src/bitgo/defi/defiVault.ts index d80a6a9794..cc74b754b9 100644 --- a/modules/sdk-core/src/bitgo/defi/defiVault.ts +++ b/modules/sdk-core/src/bitgo/defi/defiVault.ts @@ -122,6 +122,7 @@ export class DefiVault implements IDefiVault { if (!params.amount) { throw new Error('amount is required'); } + this.assertDeFiSupportedWallet(); const protocol = await this.getVaultProtocol({ vaultId: params.vaultId }); @@ -222,6 +223,7 @@ export class DefiVault implements IDefiVault { if (!params.vaultId || !params.operationId) { throw new Error('vaultId and operationId are required'); } + this.assertDeFiSupportedWallet(); // Fetch the operation to get the vault and amount details const operation = await this.getOperation({ vaultId: params.vaultId, operationId: params.operationId }); @@ -326,6 +328,7 @@ export class DefiVault implements IDefiVault { if (!params.amount) { throw new Error('amount is required'); } + this.assertDeFiSupportedWallet(); const withdrawResult = await this.wallet.sendMany({ type: 'defiWithdraw', @@ -389,6 +392,9 @@ export class DefiVault implements IDefiVault { * tracking arrives in milestone M5. */ private async sendWrapIntent(type: 'wrapNative' | 'unwrapNative', params: WrapOptions): Promise { + // Cold wallets can neither sign client-side nor be signed server-side — + // reject before the amount/EVM validation or any network call. + this.assertDeFiSupportedWallet(); const vaultId = params.vaultId?.trim(); if (!vaultId) { throw new Error('vaultId is required'); @@ -436,6 +442,25 @@ export class DefiVault implements IDefiVault { throw new Error('txRequestId not found in sendMany response'); } + /** + * DeFi vault flows require a wallet that can either sign client-side (hot + * TSS) or be signed server-side (custodial/custody). Cold wallets can do + * neither — reject before any network call. Read-only methods stay + * unguarded: inspecting positions from any wallet is harmless. + */ + private assertDeFiSupportedWallet(): void { + const walletData = this.wallet.toJSON(); + // WalletData does not type isCold; it is present on wallet data at runtime + // (see pendingApproval's _wallet.isCold read). Narrow before reading it. + const isCold = + typeof walletData === 'object' && walletData !== null && 'isCold' in walletData + ? Boolean(walletData.isCold) + : false; + if (walletData.type === 'cold' || isCold) { + throw new Error('DeFi vault flows are not supported for cold wallets'); + } + } + /** * Extract the pendingApproval id from a sendMany result, if the leg was * gated by wallet policy. Absent for un-gated legs — callers keep their diff --git a/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts b/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts index 80629d1bb0..46a6074269 100644 --- a/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts +++ b/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts @@ -1181,6 +1181,75 @@ describe('DefiVault', function () { }); }); + describe('cold wallet rejection', function () { + function makeColdWalletData(extra?: Record) { + return { + id: 'test-wallet-id', + coin: 'eth', + keys: ['user-key', 'backup-key', 'bitgo-key'], + enterprise: 'test-enterprise-id', + ...extra, + }; + } + + const coldRejection = 'DeFi vault flows are not supported for cold wallets'; + + it('rejects depositToVault for a cold wallet before any network call', async function () { + const coldDefi = new Wallet(mockBitGo, mockBaseCoin, makeColdWalletData({ type: 'cold' })).defi; + + await assert.rejects(() => coldDefi.depositToVault({ vaultId: 'vlt-galaxy-usdc', amount: '1000000' }), { + message: coldRejection, + }); + mockBitGo.get.called.should.be.false(); + mockBitGo.post.called.should.be.false(); + }); + + it('rejects resumeDeposit for a cold wallet before any network call', async function () { + const coldDefi = new Wallet(mockBitGo, mockBaseCoin, makeColdWalletData({ type: 'cold' })).defi; + + await assert.rejects(() => coldDefi.resumeDeposit({ operationId: 'op-1', vaultId: 'vlt-galaxy-usdc' }), { + message: coldRejection, + }); + mockBitGo.get.called.should.be.false(); + }); + + it('rejects withdrawFromVault for a cold wallet before any network call', async function () { + const coldDefi = new Wallet(mockBitGo, mockBaseCoin, makeColdWalletData({ type: 'cold' })).defi; + + await assert.rejects(() => coldDefi.withdrawFromVault({ vaultId: 'vlt-galaxy-usdc', amount: '1000000' }), { + message: coldRejection, + }); + mockBitGo.get.called.should.be.false(); + }); + + it('rejects wrap and unwrap for a cold wallet before any network call', async function () { + const coldDefi = new Wallet(mockBitGo, mockBaseCoin, makeColdWalletData({ type: 'cold' })).defi; + + await assert.rejects(() => coldDefi.wrap({ vaultId: 'vlt-galaxy-weth', amount: '1000' }), { + message: coldRejection, + }); + await assert.rejects(() => coldDefi.unwrap({ vaultId: 'vlt-galaxy-weth', amount: '1000' }), { + message: coldRejection, + }); + mockBitGo.get.called.should.be.false(); + }); + + it('rejects on isCold alone, even when type is not set', async function () { + const coldDefi = new Wallet(mockBitGo, mockBaseCoin, makeColdWalletData({ isCold: true })).defi; + + await assert.rejects(() => coldDefi.wrap({ vaultId: 'vlt-galaxy-weth', amount: '1000' }), { + message: coldRejection, + }); + }); + + it('does not reject read-only flows for a cold wallet', async function () { + const coldDefi = new Wallet(mockBitGo, mockBaseCoin, makeColdWalletData({ type: 'cold' })).defi; + mockBitGo.get.returns(mockRequest(makeMorphoVault('vlt-galaxy-usdc'))); + + await coldDefi.getVaultConfig({ vaultId: 'vlt-galaxy-usdc' }); + }); + }); + describe('wallet.defi getter', function () { it('should return a DefiVault instance', function () { const defi = wallet.defi;