|
| 1 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 2 | +import { Transaction } from '@bitgo/sdk-coin-sol'; |
| 3 | +import { BaseCoin, BitGoBase, PrebuildTransactionResult, Wallet } from '@bitgo/sdk-core'; |
| 4 | +import { coins } from '@bitgo/statics'; |
| 5 | + |
| 6 | +const bitgo = new BitGoAPI({ env: 'test' }); |
| 7 | + |
| 8 | +// Configuration: change these values to run the example script |
| 9 | +// const accessToken = 'v2xa2cf6160d8e30ea7892863c607411ca41c06d028036db4ef3cf4f8b2b091e472'; //'v2x70080e96706e2cfa83cf5e50dd27f5b91aa304b1dd7e01872ac3a4f85e2fa7d3'; // Your BitGo access token |
| 10 | +// const walletId = '68bafee43eb5cd22aca2afd6b13ec7ad'; //'68a8ce3dea8237d5da85d1852e370901'; // Your TSOL wallet ID |
| 11 | +// const walletRootAddress = '9tJNtvXWrtkD3NQaZY5nz8ZPc3s4ezRVX3oFfdfj5US6'; // Use wallet's root address or a dummy address |
| 12 | +// const walletPassphrase = 'Ghghjkg!455544llll'; //'0L4L"5YV@*:q_Nsv'; // Your wallet passphrase |
| 13 | + |
| 14 | +const accessToken = 'v2x70080e96706e2cfa83cf5e50dd27f5b91aa304b1dd7e01872ac3a4f85e2fa7d3'; // Your BitGo access token |
| 15 | +const walletId = '68a8ce3dea8237d5da85d1852e370901'; // Your TSOL wallet ID |
| 16 | +const walletRootAddress = '9tJNtvXWrtkD3NQaZY5nz8ZPc3s4ezRVX3oFfdfj5US6'; // Use wallet's root address or a dummy address |
| 17 | +const walletPassphrase = '0L4L"5YV@*:q_Nsv'; // Your wallet passphrase |
| 18 | +const enableTokens = [{ name: 'tsol:orca' }]; |
| 19 | + |
| 20 | +// Fake transaction data, we would expect an enabletoken but will send a transfer instead |
| 21 | +const txSendPrebuildParams = { |
| 22 | + preview: false, |
| 23 | + recipients: [ |
| 24 | + { |
| 25 | + address: walletRootAddress, |
| 26 | + amount: '10', // Small amount for testing |
| 27 | + }, |
| 28 | + ], |
| 29 | + type: 'transfer', |
| 30 | + apiVersion: 'full', |
| 31 | +}; |
| 32 | + |
| 33 | +async function main() { |
| 34 | + console.log('🔧 TSOL Token Enablement Test Script (with CoinFactory)'); |
| 35 | + |
| 36 | + checkIfPropsAreSetOrExit(); |
| 37 | + await testSendTokenEnablements(); |
| 38 | +} |
| 39 | + |
| 40 | +function checkIfPropsAreSetOrExit() { |
| 41 | + if (!accessToken || !walletId || !walletPassphrase) { |
| 42 | + console.error('❌ Please set the following required parameters:'); |
| 43 | + console.error(' - accessToken: Your BitGo access token'); |
| 44 | + console.error(' - walletId: Your TSOL wallet ID'); |
| 45 | + console.error(' - walletPassphrase: Your wallet passphrase'); |
| 46 | + console.error('\nYou can get these from your BitGo account settings.'); |
| 47 | + process.exit(1); |
| 48 | + } |
| 49 | + |
| 50 | + console.log('\n' + '='.repeat(60) + '\n'); |
| 51 | +} |
| 52 | + |
| 53 | +async function testSendTokenEnablements() { |
| 54 | + try { |
| 55 | + bitgo.authenticateWithAccessToken({ accessToken }); |
| 56 | + console.log('Getting TSOL wallet using CoinFactory...'); |
| 57 | + |
| 58 | + const { register } = await import('@bitgo/sdk-coin-sol'); |
| 59 | + register(bitgo as unknown as BitGoBase); |
| 60 | + const tsolCoin = bitgo.coin('tsol'); |
| 61 | + console.log(`✅ TSOL coin loaded: ${tsolCoin.getFullName()}`); |
| 62 | + |
| 63 | + const wallet = await tsolCoin.wallets().get({ id: walletId }); |
| 64 | + logWalletData(wallet); |
| 65 | + |
| 66 | + console.log('3️⃣ Building token enablement transactions...'); |
| 67 | + const buildParams = { |
| 68 | + enableTokens, |
| 69 | + walletPassphrase, |
| 70 | + }; |
| 71 | + |
| 72 | + const unsignedBuilds = await wallet.buildTokenEnablements(buildParams); |
| 73 | + |
| 74 | + logUnsignedBuildTokenEnablement(unsignedBuilds); |
| 75 | + logRawTxHexData(tsolCoin, unsignedBuilds); |
| 76 | + |
| 77 | + // BLIND SIGNING simulation starts here |
| 78 | + console.log('Replacing hex with prebuilt transfer transaction...'); |
| 79 | + const modifiedBuilds = await replaceTxHexWithSendTxPrebuild(wallet, unsignedBuilds); |
| 80 | + |
| 81 | + // Send token enablement transactions (they're transfers masked as the call in replaceHexWithTransferPrebuild) |
| 82 | + console.log('Sending token enablement transactions...'); |
| 83 | + const results = { |
| 84 | + success: [] as any[], |
| 85 | + failure: [] as Error[], |
| 86 | + }; |
| 87 | + |
| 88 | + for (let i = 0; i < modifiedBuilds.length; i++) { |
| 89 | + const modifiedBuild = modifiedBuilds[i]; |
| 90 | + console.log(` Processing transaction ${i + 1}/${modifiedBuilds.length}...`); |
| 91 | + |
| 92 | + try { |
| 93 | + const sendParams = { |
| 94 | + prebuildTx: modifiedBuild, |
| 95 | + walletPassphrase, |
| 96 | + apiVersion: 'full', |
| 97 | + } as any; |
| 98 | + |
| 99 | + const sendResult = await wallet.sendTokenEnablement(sendParams); |
| 100 | + results.success.push(sendResult); |
| 101 | + console.log(` ✅ Transaction ${i + 1} sent successfully`); |
| 102 | + console.log(` Result: ${JSON.stringify(sendResult, null, 2)}`); |
| 103 | + |
| 104 | + // TODO: not sure if this goes here, i'll check when I manage to do a token enablement try |
| 105 | + console.log(' You signed a non requested transfer masked as a token enablement! 💀'); |
| 106 | + } catch (error) { |
| 107 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 108 | + results.failure.push(error as Error); |
| 109 | + console.log(` ❌ Transaction ${i + 1} failed: ${errorMessage}`); |
| 110 | + |
| 111 | + // TODO: not sure if this goes here, i'll check when I manage to do a token enablement try |
| 112 | + console.log(' You catched an attempt to sign a non requested transfer masked as a token enablement! 🎉'); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + logTransactionResults(results); |
| 117 | + console.log('\n🎉 Test completed!'); |
| 118 | + } catch (error) { |
| 119 | + console.error('❌ Test failed with error:', error); |
| 120 | + if (error instanceof Error && error.stack) { |
| 121 | + console.error('Stack trace:', error.stack); |
| 122 | + } |
| 123 | + process.exit(1); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +async function replaceTxHexWithSendTxPrebuild(wallet: any, unsignedBuilds: any[]): Promise<any[]> { |
| 128 | + console.log('🔄 Replacing hex with prebuilt transfer transaction...'); |
| 129 | + |
| 130 | + try { |
| 131 | + console.log('Building prebuilt transfer transaction...'); |
| 132 | + const sendTx = await wallet.prebuildTransaction(txSendPrebuildParams); |
| 133 | + console.log(`Prebuilt transfer transaction created: Original hex length: ${sendTx.txHex?.length || 0} characters`); |
| 134 | + |
| 135 | + const modifiedBuilds = unsignedBuilds.map((build, index) => { |
| 136 | + const modifiedBuild = { ...build }; |
| 137 | + if (sendTx.txHex) { |
| 138 | + modifiedBuild.txHex = sendTx.txHex; |
| 139 | + modifiedBuild.txRequestId = sendTx.txRequestId; // Preserve txRequestId if available |
| 140 | + console.log(` Transaction ${index + 1}: Hex replaced with transfer transaction hex`); |
| 141 | + } |
| 142 | + return modifiedBuild; |
| 143 | + }); |
| 144 | + |
| 145 | + console.log(`✅ Replaced hex in ${modifiedBuilds.length} transaction(s)`); |
| 146 | + return modifiedBuilds; |
| 147 | + } catch (error) { |
| 148 | + console.error(' ❌ Error creating prebuilt transfer transaction:', error); |
| 149 | + console.log(' Falling back to original unsigned builds without hex replacement.'); |
| 150 | + return unsignedBuilds; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +function logUnsignedBuildTokenEnablement(unsignedBuilds: PrebuildTransactionResult[]) { |
| 155 | + console.log(`✅ Built ${unsignedBuilds.length} token enablement transaction(s)`); |
| 156 | + // Log details of each unsigned build |
| 157 | + unsignedBuilds.forEach((build, index) => { |
| 158 | + console.log(` Transaction ${index + 1}:`); |
| 159 | + console.log(` Wallet ID: ${build.walletId}`); |
| 160 | + console.log('Raw txHex: '); |
| 161 | + console.log(build.txHex); |
| 162 | + console.log(` TX Hex length: ${build.txHex?.length || 0} characters`); |
| 163 | + console.log(` Fee info: ${JSON.stringify(build.feeInfo)}`); |
| 164 | + console.log(` Build params: ${JSON.stringify(build.buildParams)}`); |
| 165 | + if (build.txRequestId) { |
| 166 | + console.log(` TX Request ID: ${build.txRequestId}`); |
| 167 | + } |
| 168 | + }); |
| 169 | +} |
| 170 | + |
| 171 | +function logWalletData(wallet: Wallet) { |
| 172 | + console.log(`✅ Wallet retrieved: ${wallet.id()}`); |
| 173 | + console.log(` Wallet label: ${wallet.label()}`); |
| 174 | + console.log(` Wallet type: ${wallet.type()}`); |
| 175 | + console.log(` Multisig type: ${wallet.multisigType()}`); |
| 176 | + console.log(` Balance: ${wallet.balanceString()}`); |
| 177 | + console.log(` Root address: ${wallet.coinSpecific()?.rootAddress}\n`); |
| 178 | +} |
| 179 | + |
| 180 | +function logTransactionResults(results: { success: any[]; failure: Error[] }) { |
| 181 | + console.log(`Final Results: SuccessTXs=> ${results.success.length}, FailedTXs=> ${results.failure.length}`); |
| 182 | + if (results.success.length > 0) { |
| 183 | + console.log('\n Successful transaction details:'); |
| 184 | + results.success.forEach((result, index) => { |
| 185 | + console.log(` ${index + 1}. ${JSON.stringify(result, null, 4)}`); |
| 186 | + }); |
| 187 | + } |
| 188 | + |
| 189 | + if (results.failure.length > 0) { |
| 190 | + console.log('\n Failed transaction details:'); |
| 191 | + results.failure.forEach((error, index) => { |
| 192 | + console.log(` ${index + 1}. ${error.message}`); |
| 193 | + if (error.stack) { |
| 194 | + console.log(` Stack: ${error.stack}`); |
| 195 | + } |
| 196 | + }); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +function logRawTxHexData(coin: BaseCoin, unsignedBuilds: PrebuildTransactionResult[]) { |
| 201 | + const HEX_REGEX = /^[0-9a-fA-F]+$/; |
| 202 | + const coinConfig = coins.get(coin.getChain()); |
| 203 | + |
| 204 | + unsignedBuilds.forEach((build, index) => { |
| 205 | + const transaction = new Transaction(coinConfig); |
| 206 | + const rawTx = build.txBase64 || build.txHex; |
| 207 | + |
| 208 | + let rawTxBase64 = rawTx; |
| 209 | + if (rawTx && HEX_REGEX.test(rawTx)) { |
| 210 | + rawTxBase64 = Buffer.from(rawTx, 'hex').toString('base64'); |
| 211 | + |
| 212 | + transaction.fromRawTransaction(rawTxBase64); |
| 213 | + const explainedTx = transaction.explainTransaction(); |
| 214 | + |
| 215 | + console.log('---------------------'); |
| 216 | + console.log('Explained TX:', JSON.stringify(explainedTx, null, 4)); |
| 217 | + console.log('---------------------'); |
| 218 | + } |
| 219 | + }); |
| 220 | +} |
| 221 | + |
| 222 | +if (require.main === module) { |
| 223 | + main().catch((error) => { |
| 224 | + console.error('❌ Script execution failed:', error); |
| 225 | + process.exit(1); |
| 226 | + }); |
| 227 | +} |
0 commit comments