const bip39 = require('bip39'); const crypto_ts = require('crypto');
// 1. 生成 128 位随机熵 const entropy = crypto_ts.randomBytes(16); // 128 位是 16 字节
// 2. 计算校验和 (SHA-256) const hash = crypto_ts.createHash('sha256').update(entropy).digest(); const checksum = hash[0] >> 4; // 取前 4 位
// 3. 组合熵和校验和 let bits = ''; for (let i = 0; i < entropy.length; i++) { bits += entropy[i].toString(2).padStart(8, '0'); } bits += checksum.toString(2).padStart(4, '0');
// 4. 分割为助记词索引 const indices = []; for (let i = 0; i < bits.length; i += 11) { const index = parseInt(bits.slice(i, i + 11), 2); indices.push(index); }
// 5. 映射为助记词 const wordlist = bip39.wordlists.english; const mnemonic = indices.map(index => wordlist[index]).join(' ');
请看图片显示的结果