Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions modules/sdk-core/src/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -389,6 +392,9 @@ export class DefiVault implements IDefiVault {
* tracking arrives in milestone M5.
*/
private async sendWrapIntent(type: 'wrapNative' | 'unwrapNative', params: WrapOptions): Promise<WrapResult> {
// 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');
Expand Down Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions modules/sdk-core/test/unit/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,75 @@ describe('DefiVault', function () {
});
});

describe('cold wallet rejection', function () {
function makeColdWalletData(extra?: Record<string, unknown>) {
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;
Expand Down