|
| 1 | +# Migrating from the `bitgo` Megapackage |
| 2 | + |
| 3 | +> **This package is deprecated for new projects.** Use `@bitgo/sdk-api` and `@bitgo/sdk-core` instead, |
| 4 | +> importing only the coin modules you need. |
| 5 | +
|
| 6 | +## What the Megapackage Provides |
| 7 | + |
| 8 | +The `bitgo` package (`@bitgo/bitgo` / `@bitgo-beta/bitgo`) is a convenience wrapper that re-exports: |
| 9 | + |
| 10 | +- **`@bitgo/sdk-api`** — the `BitGoAPI` class (REST client, authentication, session management) |
| 11 | +- **`@bitgo/sdk-core`** — core types, utilities, TSS primitives, wallet interfaces |
| 12 | +- **All 80+ `@bitgo/sdk-coin-*` packages** — individual coin implementations |
| 13 | + |
| 14 | +It also provides the `BitGo` class, which extends `BitGoAPI` with automatic registration of every |
| 15 | +supported coin via `GlobalCoinFactory`. This means `new BitGo().coin('btc')` works out of the box, |
| 16 | +but at the cost of bundling every coin module whether you use it or not. |
| 17 | + |
| 18 | +## When to Use This Package |
| 19 | + |
| 20 | +**Use it** if you are maintaining a legacy project that already depends on it and migration is not |
| 21 | +yet planned. |
| 22 | + |
| 23 | +**Do not use it** for new projects. Instead, use the modular approach described below. |
| 24 | + |
| 25 | +## Migration Guide |
| 26 | + |
| 27 | +### 1. Replace `BitGo` with `BitGoAPI` |
| 28 | + |
| 29 | +```typescript |
| 30 | +// Before |
| 31 | +import { BitGo, BitGoOptions } from 'bitgo' |
| 32 | +const sdk = new BitGo({ env: 'prod' }) |
| 33 | + |
| 34 | +// After |
| 35 | +import { BitGoAPI, BitGoAPIOptions } from '@bitgo/sdk-api' |
| 36 | +const sdk = new BitGoAPI({ env: 'prod' }) |
| 37 | +``` |
| 38 | + |
| 39 | +`BitGoOptions` is `BitGoAPIOptions & { useAms?: boolean }`. If you don't use the `useAms` flag, |
| 40 | +`BitGoAPIOptions` is a drop-in replacement. |
| 41 | + |
| 42 | +### 2. Move Type Imports to `@bitgo/sdk-core` |
| 43 | + |
| 44 | +All types that the megapackage re-exports from `@bitgo/sdk-core` can be imported directly: |
| 45 | + |
| 46 | +```typescript |
| 47 | +// Before |
| 48 | +import { SignatureShareRecord, CommitmentShareRecord, CustomSigningFunction } from 'bitgo' |
| 49 | + |
| 50 | +// After |
| 51 | +import { SignatureShareRecord, CommitmentShareRecord, CustomSigningFunction } from '@bitgo/sdk-core' |
| 52 | +``` |
| 53 | + |
| 54 | +Common types that move: `SignatureShareRecord`, `CommitmentShareRecord`, |
| 55 | +`EncryptedSignerShareRecord`, `SignatureShareType`, `CommitmentType`, |
| 56 | +`EncryptedSignerShareType`, `CustomSigningFunction`, `AddressFormat`, `MessageStandardType`. |
| 57 | + |
| 58 | +### 3. Replace `bitcoin.HDNode` / `bitcoin.hdPath` |
| 59 | + |
| 60 | +```typescript |
| 61 | +// Before |
| 62 | +const BitgoJS = require('bitgo') |
| 63 | +const node = BitgoJS.bitcoin.HDNode.fromBase58(xprv) |
| 64 | +const derived = BitgoJS.bitcoin.hdPath(node).derive(path) |
| 65 | + |
| 66 | +// After |
| 67 | +import { bip32 } from '@bitgo/secp256k1' |
| 68 | +const node = bip32.fromBase58(xprv) |
| 69 | +const derived = node.derivePath(path) |
| 70 | +``` |
| 71 | + |
| 72 | +### 4. Register Coins On Demand |
| 73 | + |
| 74 | +With `BitGoAPI`, coins are not pre-registered. You must register them before calling `sdk.coin()`. |
| 75 | +Use dynamic imports for lazy loading: |
| 76 | + |
| 77 | +```typescript |
| 78 | +import { BitGoAPI } from '@bitgo/sdk-api' |
| 79 | +import type { BaseCoin } from '@bitgo/sdk-core' |
| 80 | + |
| 81 | +const sdk = new BitGoAPI({ env: 'prod' }) |
| 82 | + |
| 83 | +// Register a coin family on first use |
| 84 | +async function getCoin(name: string): Promise<BaseCoin> { |
| 85 | + try { |
| 86 | + return sdk.coin(name) |
| 87 | + } catch { |
| 88 | + // Not registered yet — dynamically import and register |
| 89 | + } |
| 90 | + |
| 91 | + const { coins } = await import('@bitgo/statics') |
| 92 | + const family = coins.get(name).family |
| 93 | + |
| 94 | + switch (family) { |
| 95 | + case 'btc': { |
| 96 | + const { register } = await import('@bitgo/sdk-coin-btc') |
| 97 | + register(sdk) |
| 98 | + break |
| 99 | + } |
| 100 | + case 'eth': { |
| 101 | + const { register } = await import('@bitgo/sdk-coin-eth') |
| 102 | + register(sdk) |
| 103 | + break |
| 104 | + } |
| 105 | + // ... add cases for each coin family you need |
| 106 | + } |
| 107 | + |
| 108 | + return sdk.coin(name) |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +See `bitgo-retail/packages/retail-sdk-client/src/coinFactory.ts` for a complete reference |
| 113 | +implementation covering all coin families. |
| 114 | + |
| 115 | +### 5. Update `package.json` |
| 116 | + |
| 117 | +Remove `bitgo` (or `@bitgo-beta/bitgo`) and add the modular dependencies: |
| 118 | + |
| 119 | +```jsonc |
| 120 | +{ |
| 121 | + "dependencies": { |
| 122 | + // Remove: |
| 123 | + // "@bitgo/bitgo": "..." |
| 124 | + |
| 125 | + // Add core packages: |
| 126 | + "@bitgo/sdk-api": "...", |
| 127 | + "@bitgo/sdk-core": "...", |
| 128 | + "@bitgo/statics": "...", |
| 129 | + |
| 130 | + // Add only the coin modules you use: |
| 131 | + "@bitgo/sdk-coin-btc": "...", |
| 132 | + "@bitgo/sdk-coin-eth": "..." |
| 133 | + // ... |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
0 commit comments