|
| 1 | +/** |
| 2 | + * Create an Address for an Existing Go Account Wallet |
| 3 | + * |
| 4 | + * This example demonstrates how to create a new address for an existing Go Account |
| 5 | + * wallet. This is useful when you need to generate additional receiving addresses |
| 6 | + * for different tokens or purposes. |
| 7 | + * |
| 8 | + * This does NOT use BitGo Express - it communicates directly with BitGo platform APIs. |
| 9 | + * |
| 10 | + * IMPORTANT: For Go Account (OFC) wallets, the onToken parameter is always required |
| 11 | + * when creating addresses. |
| 12 | + * |
| 13 | + * Copyright 2025, BitGo, Inc. All Rights Reserved. |
| 14 | + */ |
| 15 | + |
| 16 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 17 | +import { coins } from 'bitgo'; |
| 18 | +require('dotenv').config({ path: '../../../.env' }); |
| 19 | + |
| 20 | +// Initialize BitGo SDK |
| 21 | +const bitgo = new BitGoAPI({ |
| 22 | + accessToken: process.env.TESTNET_ACCESS_TOKEN, |
| 23 | + env: 'test', // Change this to env: 'production' when you are ready for production |
| 24 | +}); |
| 25 | + |
| 26 | +// Go Accounts use the 'ofc' (Off-Chain) coin |
| 27 | +const coin = 'ofc'; |
| 28 | +bitgo.register(coin, coins.Ofc.createInstance); |
| 29 | + |
| 30 | +// Configuration - Update these values |
| 31 | +const walletId = '69861e14c54dbd6e00c585526f9f4152'; // The ID of your existing Go Account wallet |
| 32 | +const addressLabel = 'My New Address 2'; // Label for the new address |
| 33 | + |
| 34 | +// Token to create address for (required for OFC wallets) |
| 35 | +// Examples: 'ofctsol:usdc', 'ofctsol:usdt', 'ofcttrx:usdt', 'ofctbtc' |
| 36 | +const token = 'ofcttrx:usdt'; |
| 37 | + |
| 38 | +async function main() { |
| 39 | + console.log('=== Creating Address for Go Account ===\n'); |
| 40 | + |
| 41 | + // Step 1: Get the existing wallet |
| 42 | + console.log(`Retrieving wallet: ${walletId}...`); |
| 43 | + const wallet = await bitgo.coin(coin).wallets().get({ id: walletId }); |
| 44 | + console.log(`✓ Wallet retrieved: ${wallet.label()}`); |
| 45 | + console.log(` Wallet Type: ${wallet.type()}`); |
| 46 | + console.log(` Wallet Coin: ${wallet.coin()}`); |
| 47 | + |
| 48 | + // Step 2: Create a new address for the specified token |
| 49 | + console.log(`\nCreating address for token ${token}...`); |
| 50 | + try { |
| 51 | + const address = await wallet.createAddress({ |
| 52 | + label: addressLabel, |
| 53 | + onToken: token // Required for OFC wallets |
| 54 | + }); |
| 55 | + |
| 56 | + console.log(`✓ Address created: ${address.address}`); |
| 57 | + console.log('\nAddress Response:'); |
| 58 | + console.log(JSON.stringify(address, null, 2)); |
| 59 | + |
| 60 | + console.log('\n✓ Address creation complete!'); |
| 61 | + console.log('\nNext Steps:'); |
| 62 | + console.log('1. Use this address to receive ' + token + ' tokens'); |
| 63 | + console.log('2. Share this address with senders'); |
| 64 | + console.log('3. Create additional addresses: wallet.createAddress({ label: "...", onToken: "..." })'); |
| 65 | + |
| 66 | + } catch (error) { |
| 67 | + console.log(` Error: Address creation failed. Token may not be enabled.`); |
| 68 | + console.error(` Error: ${error}`); |
| 69 | + throw error; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +main().catch((e) => { |
| 74 | + console.error('\n❌ Error creating address:', e); |
| 75 | + process.exit(1); |
| 76 | +}); |
0 commit comments