|
| 1 | +/** |
| 2 | + * Verify that an address belongs to a wallet using the BitGo Express API. |
| 3 | + * |
| 4 | + * This example demonstrates the isWalletAddress endpoint which verifies: |
| 5 | + * - Forwarder addresses (deposit addresses) |
| 6 | + * - Base addresses (wallet contract addresses) |
| 7 | + * |
| 8 | + * Copyright 2024, BitGo, Inc. All Rights Reserved. |
| 9 | + */ |
| 10 | + |
| 11 | +const fetch = require('node-fetch'); |
| 12 | + |
| 13 | +const coin = 'hteth'; // change to 'eth' for production |
| 14 | + |
| 15 | +// TODO: set your access token here |
| 16 | +const accessToken = ''; |
| 17 | + |
| 18 | +// TODO: set your wallet ID here |
| 19 | +const walletId = ''; |
| 20 | + |
| 21 | +const expressUrl = ''; |
| 22 | + |
| 23 | +// Helper function to make API requests to Express |
| 24 | +async function apiRequest(method, endpoint, body = null) { |
| 25 | + const response = await fetch(`${expressUrl}${endpoint}`, { |
| 26 | + method, |
| 27 | + headers: { |
| 28 | + 'Authorization': `Bearer ${accessToken}`, |
| 29 | + 'Content-Type': 'application/json' |
| 30 | + }, |
| 31 | + body: body ? JSON.stringify(body) : null |
| 32 | + }); |
| 33 | + |
| 34 | + if (!response.ok) { |
| 35 | + throw new Error(`HTTP ${response.status}: ${await response.text()}`); |
| 36 | + } |
| 37 | + |
| 38 | + return await response.json(); |
| 39 | +} |
| 40 | + |
| 41 | +async function main() { |
| 42 | + console.log('Step 1: Fetching wallet data...'); |
| 43 | + const wallet = await apiRequest('GET', `/api/v2/${coin}/wallet/${walletId}`); |
| 44 | + |
| 45 | + const keychainIds = wallet.keys; |
| 46 | + const baseAddress = wallet.coinSpecific.baseAddress; |
| 47 | + const walletVersion = wallet.coinSpecific.walletVersion; |
| 48 | + const feeAddress = wallet.coinSpecific.feeAddress; |
| 49 | + const walletSalt = wallet.coinSpecific.salt; |
| 50 | + const addressToVerify = wallet.receiveAddress.address; |
| 51 | + |
| 52 | + console.log(' Base Address:', baseAddress); |
| 53 | + console.log(' Wallet Version:', walletVersion); |
| 54 | + |
| 55 | + console.log('Step 2: Fetching keychains...'); |
| 56 | + const keychains = []; |
| 57 | + for (const keychainId of keychainIds) { |
| 58 | + const keychain = await apiRequest('GET', `/api/v2/${coin}/key/${keychainId}`); |
| 59 | + |
| 60 | + // For TSS keychains, derive pub from commonKeychain (first 66 characters) |
| 61 | + const pub = keychain.pub || (keychain.commonKeychain && keychain.commonKeychain.slice(0, 66)); |
| 62 | + |
| 63 | + if (!pub) { |
| 64 | + throw new Error(`Unable to derive pub for keychain ${keychainId}`); |
| 65 | + } |
| 66 | + |
| 67 | + keychains.push({ |
| 68 | + pub: pub, |
| 69 | + ...(keychain.ethAddress && { ethAddress: keychain.ethAddress }), |
| 70 | + ...(keychain.commonKeychain && { commonKeychain: keychain.commonKeychain }) |
| 71 | + }); |
| 72 | + } |
| 73 | + console.log(' Retrieved', keychains.length, 'keychains'); |
| 74 | + |
| 75 | + console.log('Step 3: Fetching address details...'); |
| 76 | + const addressData = await apiRequest('GET', `/api/v2/${coin}/wallet/${walletId}/address/${addressToVerify}`); |
| 77 | + |
| 78 | + const addressIndex = addressData.index; |
| 79 | + const forwarderVersion = addressData.coinSpecific?.forwarderVersion; |
| 80 | + const forwarderSalt = addressData.coinSpecific?.salt; |
| 81 | + |
| 82 | + console.log(' Address:', addressToVerify); |
| 83 | + console.log(' Index:', addressIndex); |
| 84 | + console.log(' Forwarder Version:', forwarderVersion); |
| 85 | + |
| 86 | + console.log('Step 4: Verifying forwarder address...'); |
| 87 | + const forwarderParams = { |
| 88 | + address: addressToVerify, |
| 89 | + keychains: keychains, |
| 90 | + baseAddress: baseAddress, |
| 91 | + walletVersion: walletVersion, |
| 92 | + index: addressIndex, |
| 93 | + coinSpecific: { |
| 94 | + forwarderVersion: forwarderVersion, |
| 95 | + salt: forwarderSalt, |
| 96 | + feeAddress: feeAddress, |
| 97 | + baseAddress: baseAddress |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + const forwarderResult = await apiRequest('POST', `/api/v2/${coin}/wallet/${walletId}/iswalletaddress`, forwarderParams); |
| 102 | + console.log(' Result:', forwarderResult ? '✓ Valid' : '✗ Invalid'); |
| 103 | + |
| 104 | + console.log('Step 5: Verifying base address...'); |
| 105 | + const baseAddressParams = { |
| 106 | + address: baseAddress, |
| 107 | + keychains: keychains, |
| 108 | + baseAddress: baseAddress, |
| 109 | + walletVersion: walletVersion, |
| 110 | + index: 0, |
| 111 | + coinSpecific: { |
| 112 | + salt: walletSalt, |
| 113 | + feeAddress: feeAddress, |
| 114 | + baseAddress: baseAddress |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + const baseResult = await apiRequest('POST', `/api/v2/${coin}/wallet/${walletId}/iswalletaddress`, baseAddressParams); |
| 119 | + console.log(' Result:', baseResult ? '✓ Valid' : '✗ Invalid'); |
| 120 | +} |
| 121 | + |
| 122 | +main().catch((e) => console.error(e)); |
| 123 | + |
0 commit comments