|
| 1 | +# Creating Go Accounts without BitGo Express |
| 2 | + |
| 3 | +This guide demonstrates how to create Go Accounts (trading wallets) using only the BitGo SDK, without requiring BitGo Express. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +**BitGo Express is optional middleware** - you can interact directly with the BitGo platform using the SDK. This approach gives you: |
| 8 | +- Direct API communication with BitGo |
| 9 | +- No need to run a separate Express server |
| 10 | +- Full control over the wallet creation process |
| 11 | +- Production-ready code with proper error handling |
| 12 | + |
| 13 | +## Two Approaches |
| 14 | + |
| 15 | +### 1. SDK Approach (Recommended) |
| 16 | +**File:** `examples/ts/create-go-account.ts` |
| 17 | + |
| 18 | +Uses the high-level `generateWallet()` method which handles keychain creation, encryption, and wallet setup automatically. |
| 19 | + |
| 20 | +**Best for:** |
| 21 | +- Most production use cases |
| 22 | +- Quick integration |
| 23 | +- Users who don't need manual key management |
| 24 | + |
| 25 | +**Example:** |
| 26 | +```typescript |
| 27 | +const bitgo = new BitGoAPI({ |
| 28 | + accessToken: process.env.TESTNET_ACCESS_TOKEN, |
| 29 | + env: 'test', |
| 30 | +}); |
| 31 | + |
| 32 | +const coin = 'ofc'; |
| 33 | +bitgo.register(coin, coins.Ofc.createInstance); |
| 34 | + |
| 35 | +const response = await bitgo.coin(coin).wallets().generateWallet({ |
| 36 | + label: 'My Go Account', |
| 37 | + passphrase: 'wallet_passphrase', |
| 38 | + passcodeEncryptionCode: 'encryption_code', |
| 39 | + enterprise: 'your_enterprise_id', |
| 40 | + type: 'trading', // Required for Go Accounts |
| 41 | +}); |
| 42 | + |
| 43 | +const { wallet, userKeychain, encryptedWalletPassphrase } = response; |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Advanced SDK Approach |
| 47 | +**File:** `examples/ts/create-go-account-advanced.ts` |
| 48 | + |
| 49 | +Provides manual control over keychain creation and wallet setup using SDK methods. |
| 50 | + |
| 51 | +**Best for:** |
| 52 | +- Advanced users needing custom key management |
| 53 | +- Integration with custom key storage systems |
| 54 | +- Understanding the internals of Go Account creation |
| 55 | +- Testing and debugging |
| 56 | + |
| 57 | +**Example:** |
| 58 | +```typescript |
| 59 | +// Step 1: Create keychain locally |
| 60 | +const keychain = bitgo.coin('ofc').keychains().create(); |
| 61 | + |
| 62 | +// Step 2: Encrypt private key |
| 63 | +const encryptedPrv = bitgo.encrypt({ |
| 64 | + password: passphrase, |
| 65 | + input: keychain.prv |
| 66 | +}); |
| 67 | + |
| 68 | +// Step 3: Add keychain to BitGo |
| 69 | +const addedKeychain = await bitgo.coin('ofc').keychains().add({ |
| 70 | + pub: keychain.pub, |
| 71 | + encryptedPrv: encryptedPrv, |
| 72 | + originalPasscodeEncryptionCode: passcodeEncryptionCode, |
| 73 | + keyType: 'independent', |
| 74 | + source: 'user', |
| 75 | + enterprise: enterpriseId, |
| 76 | +}); |
| 77 | + |
| 78 | +// Step 4: Create wallet |
| 79 | +const walletResponse = await bitgo.coin('ofc').wallets().add({ |
| 80 | + label: 'My Go Account', |
| 81 | + m: 1, |
| 82 | + n: 1, |
| 83 | + keys: [addedKeychain.id], |
| 84 | + type: 'trading', |
| 85 | + enterprise: enterpriseId, |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +## Complete Workflow |
| 90 | + |
| 91 | +Both examples demonstrate the complete Go Account creation flow: |
| 92 | + |
| 93 | +### 1. Create Wallet |
| 94 | +```typescript |
| 95 | +// SDK generates keychain and wallet in one call |
| 96 | +const response = await bitgo.coin('ofc').wallets().generateWallet({...}); |
| 97 | +``` |
| 98 | + |
| 99 | +### 2. Wait for Initialization |
| 100 | +Go Accounts require system initialization, which may take a few seconds: |
| 101 | + |
| 102 | +```typescript |
| 103 | +async function waitForWalletInitialization(wallet, maxRetries = 30, delayMs = 2000) { |
| 104 | + for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 105 | + const walletData = await bitgo.coin('ofc').wallets().get({ id: wallet.id() }); |
| 106 | + const coinSpecific = walletData._wallet.coinSpecific; |
| 107 | + |
| 108 | + if (!coinSpecific.pendingSystemInitialization) { |
| 109 | + return; // Wallet ready |
| 110 | + } |
| 111 | + |
| 112 | + await sleep(delayMs); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### 3. Create Addresses |
| 118 | +Create default and token-specific addresses: |
| 119 | + |
| 120 | +```typescript |
| 121 | +// Default address |
| 122 | +const defaultAddress = await wallet.createAddress({ |
| 123 | + label: 'Default Receive Address' |
| 124 | +}); |
| 125 | + |
| 126 | +// Token-specific address |
| 127 | +const tokenAddress = await wallet.createAddress({ |
| 128 | + label: 'USDT Address', |
| 129 | + onToken: 'ofcttrx:usdt' |
| 130 | +}); |
| 131 | +``` |
| 132 | + |
| 133 | +### 4. Create Bank Account (Optional) |
| 134 | +For fiat operations: |
| 135 | + |
| 136 | +```typescript |
| 137 | +const bankAccount = await bitgo.post(bitgo.url('/bankAccounts')) |
| 138 | + .send({ |
| 139 | + enterpriseId, |
| 140 | + name: 'US Bank', |
| 141 | + accountNumber: '123456789', |
| 142 | + type: 'wire', |
| 143 | + accountType: 'checking', |
| 144 | + currency: 'usd', |
| 145 | + routingNumber: '123456789', |
| 146 | + // ... other parameters |
| 147 | + }) |
| 148 | + .result(); |
| 149 | +``` |
| 150 | + |
| 151 | +## Key Differences from Express Approach |
| 152 | + |
| 153 | +| Aspect | Express | Direct SDK | |
| 154 | +|--------|---------|------------| |
| 155 | +| **Server Required** | Yes (Express server) | No | |
| 156 | +| **API Calls** | HTTP to Express → Express to BitGo | Direct SDK → BitGo | |
| 157 | +| **Setup** | More complex (server + SDK) | Simple (SDK only) | |
| 158 | +| **Security** | Keys never leave Express server | Keys managed in your application | |
| 159 | +| **Performance** | Extra network hop | Direct communication | |
| 160 | +| **Use Case** | Shared signing server | Embedded integration | |
| 161 | + |
| 162 | +## Security Best Practices |
| 163 | + |
| 164 | +1. **Backup Critical Information:** |
| 165 | + - Encrypted private key (`userKeychain.encryptedPrv`) |
| 166 | + - Keychain ID (`userKeychain.id`) |
| 167 | + - Encrypted wallet passphrase (`encryptedWalletPassphrase`) |
| 168 | + - Passphrase encryption code (stored separately) |
| 169 | + |
| 170 | +2. **Secure Storage:** |
| 171 | + - Store encrypted keys in secure database |
| 172 | + - Never log unencrypted private keys |
| 173 | + - Use environment variables for sensitive config |
| 174 | + |
| 175 | +3. **Access Control:** |
| 176 | + - Limit access token permissions |
| 177 | + - Use enterprise-scoped tokens |
| 178 | + - Implement proper authentication |
| 179 | + |
| 180 | +## Running the Examples |
| 181 | + |
| 182 | +1. Install dependencies: |
| 183 | + ```bash |
| 184 | + cd /path/to/BitGoJS |
| 185 | + yarn install |
| 186 | + ``` |
| 187 | + |
| 188 | +2. Set up environment: |
| 189 | + ```bash |
| 190 | + cp examples/.env.example examples/.env |
| 191 | + # Edit .env with your access token and enterprise ID |
| 192 | + ``` |
| 193 | + |
| 194 | +3. Update script configuration: |
| 195 | + - Set your `enterprise` ID |
| 196 | + - Choose wallet `label` and `passphrase` |
| 197 | + - Optional: Set `token` for specific crypto assets |
| 198 | + |
| 199 | +4. Run the script: |
| 200 | + ```bash |
| 201 | + cd examples/ts |
| 202 | + npx tsx create-go-account.ts |
| 203 | + # or for advanced approach: |
| 204 | + npx tsx create-go-account-advanced.ts |
| 205 | + ``` |
| 206 | + |
| 207 | +## Supported Tokens |
| 208 | + |
| 209 | +Common tokens for Go Accounts (use with `onToken` parameter): |
| 210 | +- `ofctbtc` - Bitcoin |
| 211 | +- `ofcteth` - Ethereum |
| 212 | +- `ofctsol` - Solana |
| 213 | +- `ofcttrx:usdt` - USDT on Tron |
| 214 | +- `ofcteth:usdt` - USDT on Ethereum |
| 215 | + |
| 216 | +For testnet, use the test prefix: `ofcttbtc`, `ofctteth`, etc. |
| 217 | + |
| 218 | +## Troubleshooting |
| 219 | + |
| 220 | +### Wallet initialization timeout |
| 221 | +**Issue:** Wallet stuck in `pendingSystemInitialization` |
| 222 | + |
| 223 | +**Solution:** Increase the retry count and delay in `waitForWalletInitialization()`. |
| 224 | + |
| 225 | +### Token address creation fails |
| 226 | +**Issue:** Cannot create address for specific token |
| 227 | + |
| 228 | +**Solution:** Verify the token is enabled for your enterprise and the token identifier is correct. |
| 229 | + |
| 230 | +### Bank account creation fails |
| 231 | +**Issue:** 403 or permission error |
| 232 | + |
| 233 | +**Solution:** Bank account creation requires specific enterprise permissions. Contact BitGo support if needed. |
| 234 | + |
| 235 | +## Additional Resources |
| 236 | + |
| 237 | +- [BitGo API Documentation](https://developers.bitgo.com/) |
| 238 | +- [Go Accounts Overview](https://developers.bitgo.com/docs/crypto-as-a-service-go-accounts) |
| 239 | +- [SDK Reference](https://github.com/BitGo/BitGoJS) |
| 240 | + |
| 241 | +## Support |
| 242 | + |
| 243 | +For questions or issues: |
| 244 | +1. Check the [BitGo Developer Documentation](https://developers.bitgo.com/) |
| 245 | +2. Open an issue on [GitHub](https://github.com/BitGo/BitGoJS/issues) |
| 246 | +3. Contact BitGo support |
0 commit comments