先复习一下基础知识
连接波场网络:
// tronConnector.ts
import {
TronWeb } from 'tronweb';
export function connectToTronNetwork(fullNodeUrl: string, eventServerUrl: string): TronWeb {
const tronWeb = new TronWeb({
fullHost: fullNodeUrl || 'https://api.trongrid.io',
headers: {
'TRON-PRO-API-KEY': 'your api key' },
privateKey: 'your private key'
});
return tronWeb;
}
其中APIKEY可以在官网获取;
可以使用tronWeb.isConnected()判断是否连接成功
创建离线波场地址:
tronWeb.createAccount();
该地址未激活,如果需要激活, 通常需要一定数量的 TRX(TRON 的本地代币)用于支付激活费用;
const toAddress = '新账户地址'; // 新创建的账户地址
const amount = 1000; // 转账的 TRX 数量
const transaction = await tronWeb.trx.sendTransaction(toAddress, amount);
console.log(transaction);
等待区块确定
const accountInfo = await tronWeb.trx.getAccount('新账户地址');
console.log(accountInfo);
就可以查看激活信息;
创建随机助记词与私钥:
const tronWeb = require('tronweb');
// 创建随机的助记词和私钥
const {
mnemonic, privateKey } = tronWeb.createRandom();
console.log('Mnemonic:', mnemonic);
console.log('Private Key:', privateKey);
如何让其助记词与波场地址关联:
//使用上述的privateKey
const address = tronWeb.address.fromPrivateKey(privateKey);
console.log('Address:', address);
这就关联了;
根据提供的助记词获取地址和私钥
const tronWeb = require('tronweb');
// 替换为实际的助记词
const mnemonic = 'your twelve words mnemonic here';
// 从助记词生成 TRON 账户的地址和私钥
const account = tronWeb.fromMnemonic(mnemonic);