To be able to use your account on TON, you need to have an initialized wallet. Wallet initialization means deploying a TON wallet contract for your account.
While this is a great feature of the TON blockchain, you might find it cumbersome dealing with wallet deployments during your development stage.
So here’s a couple of scripts in JavaScript using tonweb that can help you:
GenerateAndInitialize.js — a script that generates a key pair for an account and then keeps running until you top it up with TON; once the account has enough TON, the script deploys a wallet contract.
CheckAndDepoy.js — a script that checks whether an account is initialized; if it’s not initialized, the script deploys a wallet contract.
Generates a key pair and saves it to wallet_keys.json
Keeps running and checking the balance of the generated account until it’s topped up
Once the account is topped up, the script deploys a v4R2 wallet contract
constTonWeb=require('tonweb');const fs =require('fs');const httpProvider =newTonWeb.HttpProvider('CHAINSTACK_NODE_WITH_jsonRPC');const tonweb =newTonWeb(httpProvider);asyncfunctiongenerateAndInitializeWallet(){// Generate a new key pairconst keyPair =TonWeb.utils.nacl.sign.keyPair();// Create a v4R2 wallet instanceconstWalletClass= tonweb.wallet.all['v4R2'];const wallet =newWalletClass(tonweb.provider,{publicKey: keyPair.publicKey,wc:0});// Get the wallet addressconst walletAddress =await wallet.getAddress();const addressString = walletAddress.toString(true,true,true);console.log('Generated wallet address:', addressString);console.log('Please send some TON to this address to initialize the wallet.');// Save the key pair to a fileconst keyPairData ={publicKey:TonWeb.utils.bytesToHex(keyPair.publicKey),secretKey:TonWeb.utils.bytesToHex(keyPair.secretKey)}; fs.writeFileSync('wallet_keys.json',JSON.stringify(keyPairData,null,2));console.log('Key pair saved to wallet_keys.json');// Wait for the wallet to be topped upawaitwaitForBalance(walletAddress);// Initialize the walletconsole.log('Initializing wallet...');const deployResult =await wallet.deploy(keyPair.secretKey).send();console.log('Deployment transaction sent:', deployResult);console.log('Waiting for deployment to complete...');awaitnewPromise(resolve=>setTimeout(resolve,10000));// Wait for 10 secondsconst seqno =await wallet.methods.seqno().call();if(seqno !==null){console.log('Wallet successfully initialized!');}else{console.log('Wallet initialization might have failed. Please check the address on a block explorer.');}}asyncfunctionwaitForBalance(address){while(true){const balance =await tonweb.getBalance(address);if(balance && balance !=='0'){console.log('Balance detected:',TonWeb.utils.fromNano(balance),'TON');break;}awaitnewPromise(resolve=>setTimeout(resolve,5000));// Check every 5 seconds}}generateAndInitializeWallet().catch(console.error);
Make sure you put your Chainstack TON node endpoint with a /jsonRPC at the end instead of CHAINSTACK_NODE_WITH_jsonRPC.
Make sure you put your Chainstack TON node endpoint with a /jsonRPC at the end instead of CHAINSTACK_NODE_WITH_jsonRPC; and put your private key inPRIVATE_KEY.