TON 账户创建与管理
TON 区块链中的账户管理是开发的基础,包括私钥、公钥生成和地址管理。
🔑 密钥生成
从助记词生成密钥对
import { mnemonicToPrivateKey } from '@ton/crypto';
const mnemonics = 'your twelve word mnemonic phrase here';
const keyPair = await mnemonicToPrivateKey(mnemonics.split(' '));
console.log('Public Key:', keyPair.publicKey.toString('hex'));
console.log('Private Key:', keyPair.secretKey.toString('hex'));
随机生成密钥对
import { mnemonicToPrivateKey, mnemonicNew } from '@ton/crypto';
// 生成新的助记词
const newMnemonics = await mnemonicNew();
console.log('New Mnemonics:', newMnemonics.join(' '));
// 从新助记词生成密钥对
const keyPair = await mnemonicToPrivateKey(newMnemonics);
🏦 钱包合约类型
TON 支持多种钱包合约版本,每种都有不同的特性:
WalletContractV3R2
import { WalletContractV3R2 } from '@ton/ton';
const wallet = WalletContractV3R2.create({
workchain: 0,
publicKey: keyPair.publicKey
});
WalletContractV4
import { WalletContractV4 } from '@ton/ton';
const wallet = WalletContractV4.create({
workchain: 0,
publicKey: keyPair.publicKey
});
WalletContractV5R1
import { WalletContractV5R1 } from '@ton/ton';
const wallet = WalletContractV5R1.create({
publicKey: keyPair.publicKey
});
📍 地址管理
获取钱包地址
import { TonClient } from '@ton/ton';
const client = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
});
const contract = client.open(wallet);
const address = contract.address;
// 不同格式的地址
console.log('Raw Address:', address.toString());
console.log('User-friendly (non-bounceable):', address.toString({ bounceable: false }));
console.log('URL Safe:', address.toString({ urlSafe: true }));
地址格式说明
TON 地址有多种格式:
- Raw 格式:
0:14b6a6afbdcb4fcb254f0e7d78f05888b3d222d05656fe6490563aaff3263a89
- User-friendly 格式:
UQAUtqavvctPyyVPDn148FiIs9Ii0FZW_mSQVjqv8yY6ibFJ
- Base64 格式:
EQAUtqavvctPyyVPDn148FiIs9Ii0FZW_mSQVjqv8yY6ieyM
地址标志位
地址的第一个字母表示不同的标志:
开头字母 | 二进制形式 | 可退回 | 仅测试网 |
---|---|---|---|
E... | 000100.01 | 是 | 否 |
U... | 010100.01 | 否 | 否 |
k... | 100100.01 | 是 | 是 |
0... | 110100.01 | 否 | 是 |
💰 余额查询
获取账户余额
const balance = await contract.getBalance();
console.log('Balance:', balance.toString());
console.log('Balance in TON:', Number(balance) / 1e9);
检查合约是否已部署
const isDeployed = await client.isContractDeployed(address);
console.log('Contract deployed:', isDeployed);
🔐 安全最佳实践
1. 助记词管理
- 使用强随机性生成助记词
- 安全存储,避免明文保存
- 考虑使用硬件钱包
2. 密钥管理
- 私钥永远不要暴露给第三方
- 使用环境变量存储敏感信息
- 定期轮换密钥
3. 地址验证
- 始终验证地址格式
- 使用正确的网络(主网/测试网)
- 测试小额交易
📝 完整示例
import { mnemonicToPrivateKey } from '@ton/crypto';
import { TonClient, WalletContractV4 } from '@ton/ton';
async function createAndManageAccount() {
// 创建客户端
const client = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
apiKey: 'YOUR_API_KEY'
});
// 从助记词生成密钥对
const mnemonics = process.env.TON_MNEMONIC;
const keyPair = await mnemonicToPrivateKey(mnemonics.split(' '));
// 创建钱包合约
const wallet = WalletContractV4.create({
workchain: 0,
publicKey: keyPair.publicKey
});
const contract = client.open(wallet);
// 获取地址和余额
const address = contract.address;
const balance = await contract.getBalance();
const isDeployed = await client.isContractDeployed(address);
console.log('Wallet Address:', address.toString({ bounceable: false }));
console.log('Balance:', balance.toString());
console.log('Deployed:', isDeployed);
return { contract, address, balance, isDeployed };
}
// 使用示例
createAndManageAccount().catch(console.error);