From a33c4c2db7bc5ff9d112b7b8f965e6954ebe924f Mon Sep 17 00:00:00 2001 From: Alia Aamir Date: Fri, 6 Feb 2026 12:41:00 -0500 Subject: [PATCH] feat: add go account address creation script CAAS-739 TICKET: CAAS-739 --- examples/docs/go-account-creation.md | 38 ++++++++-- .../go-account/create-go-account-address.ts | 76 +++++++++++++++++++ .../create-go-account-advanced.ts | 2 +- .../ts/{ => go-account}/create-go-account.ts | 2 +- 4 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 examples/ts/go-account/create-go-account-address.ts rename examples/ts/{ => go-account}/create-go-account-advanced.ts (99%) rename examples/ts/{ => go-account}/create-go-account.ts (98%) diff --git a/examples/docs/go-account-creation.md b/examples/docs/go-account-creation.md index b3c71e4766..97da6181e4 100644 --- a/examples/docs/go-account-creation.md +++ b/examples/docs/go-account-creation.md @@ -10,10 +10,10 @@ This guide demonstrates how to create Go Accounts (trading wallets) using only t - Full control over the wallet creation process - Production-ready code with proper error handling -## Two Approaches +## Available Scripts ### 1. SDK Approach (Recommended) -**File:** `examples/ts/create-go-account.ts` +**File:** `examples/ts/go-account/create-go-account.ts` Uses the high-level `generateWallet()` method which handles keychain creation, encryption, and wallet setup automatically. @@ -44,7 +44,7 @@ const { wallet, userKeychain, encryptedWalletPassphrase } = response; ``` ### 2. Advanced SDK Approach -**File:** `examples/ts/create-go-account-advanced.ts` +**File:** `examples/ts/go-account/create-go-account-advanced.ts` Provides manual control over keychain creation and wallet setup using SDK methods. @@ -54,8 +54,30 @@ Provides manual control over keychain creation and wallet setup using SDK method - Understanding the internals of Go Account creation - Testing and debugging +### 3. Creating Addresses for Existing Wallets +**File:** `examples/ts/go-account/create-go-account-address.ts` + +Demonstrates how to create additional addresses for an existing Go Account wallet. + +**Best for:** +- Adding new addresses to existing wallets +- Creating addresses for different tokens +- Managing multiple receiving addresses + **Example:** ```typescript +const wallet = await bitgo.coin('ofc').wallets().get({ id: walletId }); + +const address = await wallet.createAddress({ + label: 'My New Address', + onToken: 'ofctsol:usdc' // Required for OFC wallets +}); +``` + +## Detailed Examples + +### SDK Approach Example +```typescript // Step 1: Create keychain locally const keychain = bitgo.coin('ofc').keychains().create(); @@ -181,10 +203,16 @@ const usdtAddress = await wallet.createAddress({ 4. Run the script: ```bash - cd examples/ts + cd examples/ts/go-account + + # Create a new Go Account wallet (recommended) npx tsx create-go-account.ts - # or for advanced approach: + + # Create a new Go Account wallet (advanced approach) npx tsx create-go-account-advanced.ts + + # Create an address for an existing wallet + npx tsx create-go-account-address.ts ``` ## Supported Tokens diff --git a/examples/ts/go-account/create-go-account-address.ts b/examples/ts/go-account/create-go-account-address.ts new file mode 100644 index 0000000000..69a1d0d279 --- /dev/null +++ b/examples/ts/go-account/create-go-account-address.ts @@ -0,0 +1,76 @@ +/** + * Create an Address for an Existing Go Account Wallet + * + * This example demonstrates how to create a new address for an existing Go Account + * wallet. This is useful when you need to generate additional receiving addresses + * for different tokens or purposes. + * + * This does NOT use BitGo Express - it communicates directly with BitGo platform APIs. + * + * IMPORTANT: For Go Account (OFC) wallets, the onToken parameter is always required + * when creating addresses. + * + * Copyright 2025, BitGo, Inc. All Rights Reserved. + */ + +import { BitGoAPI } from '@bitgo/sdk-api'; +import { coins } from 'bitgo'; +require('dotenv').config({ path: '../../../.env' }); + +// Initialize BitGo SDK +const bitgo = new BitGoAPI({ + accessToken: process.env.TESTNET_ACCESS_TOKEN, + env: 'test', // Change this to env: 'production' when you are ready for production +}); + +// Go Accounts use the 'ofc' (Off-Chain) coin +const coin = 'ofc'; +bitgo.register(coin, coins.Ofc.createInstance); + +// Configuration - Update these values +const walletId = 'your_wallet_id'; // The ID of your existing Go Account wallet +const addressLabel = 'My New Address 2'; // Label for the new address + +// Token to create address for (required for OFC wallets) +// Examples: 'ofctsol:usdc', 'ofctsol:usdt', 'ofcttrx:usdt', 'ofctbtc' +const token = 'ofcttrx:usdt'; + +async function main() { + console.log('=== Creating Address for Go Account ===\n'); + + // Step 1: Get the existing wallet + console.log(`Retrieving wallet: ${walletId}...`); + const wallet = await bitgo.coin(coin).wallets().get({ id: walletId }); + console.log(`āœ“ Wallet retrieved: ${wallet.label()}`); + console.log(` Wallet Type: ${wallet.type()}`); + console.log(` Wallet Coin: ${wallet.coin()}`); + + // Step 2: Create a new address for the specified token + console.log(`\nCreating address for token ${token}...`); + try { + const address = await wallet.createAddress({ + label: addressLabel, + onToken: token // Required for OFC wallets + }); + + console.log(`āœ“ Address created: ${address.address}`); + console.log('\nAddress Response:'); + console.log(JSON.stringify(address, null, 2)); + + console.log('\nāœ“ Address creation complete!'); + console.log('\nNext Steps:'); + console.log('1. Use this address to receive ' + token + ' tokens'); + console.log('2. Share this address with senders'); + console.log('3. Create additional addresses: wallet.createAddress({ label: "...", onToken: "..." })'); + + } catch (error) { + console.log(` Error: Address creation failed. Token may not be enabled.`); + console.error(` Error: ${error}`); + throw error; + } +} + +main().catch((e) => { + console.error('\nāŒ Error creating address:', e); + process.exit(1); +}); diff --git a/examples/ts/create-go-account-advanced.ts b/examples/ts/go-account/create-go-account-advanced.ts similarity index 99% rename from examples/ts/create-go-account-advanced.ts rename to examples/ts/go-account/create-go-account-advanced.ts index fb3f1165bb..70633fc1bb 100644 --- a/examples/ts/create-go-account-advanced.ts +++ b/examples/ts/go-account/create-go-account-advanced.ts @@ -21,7 +21,7 @@ import { BitGoAPI } from '@bitgo/sdk-api'; import { Wallet } from '@bitgo/sdk-core'; import { coins } from 'bitgo'; -require('dotenv').config({ path: '../../.env' }); +require('dotenv').config({ path: '../../../.env' }); // Initialize BitGo SDK const bitgo = new BitGoAPI({ diff --git a/examples/ts/create-go-account.ts b/examples/ts/go-account/create-go-account.ts similarity index 98% rename from examples/ts/create-go-account.ts rename to examples/ts/go-account/create-go-account.ts index 29dfdc761e..64141e42cb 100644 --- a/examples/ts/create-go-account.ts +++ b/examples/ts/go-account/create-go-account.ts @@ -16,7 +16,7 @@ import { BitGoAPI } from '@bitgo/sdk-api'; import { coins } from 'bitgo'; -require('dotenv').config({ path: '../../.env' }); +require('dotenv').config({ path: '../../../.env' }); // Initialize BitGo SDK const bitgo = new BitGoAPI({