var Crypto = require('crypto') var secp256k1 = require('secp256k1') var createKeccakHash = require('keccak')
// 一个32字节的随机数(1~2^256-1),直接把他当私钥 var privateKey = Crypto.randomBytes(32);
// 由secp256k1椭圆曲线算法先计算出公钥 var pubKey = secp256k1.publicKeyCreate(privateKey, false).slice(1);
// 进行keccak256 hash运算再取后40位得到 var address = createKeccakHash('keccak256').update(pubKey).digest().slice(-20);
console.log(privateKey.toString('hex')); console.log("0x" + address.toString('hex'));
执行报错 PS D:\webprojects\wallet> node raw.js D:\webprojects\wallet\node_modules\keccak\lib\api\keccak.js:41 if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer') ^
TypeError: Data must be a string or a buffer at Keccak.update (D:\webprojects\wallet\node_modules\?[4mkeccak?[24m\lib\api\keccak.js:41:67) at Object.<anonymous> (D:\webprojects\wallet\raw.js:12:45) ?[90m at Module._compile (internal/modules/cjs/loader.js:1156:30)?[39m ?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)?[39m ?[90m at Module.load (internal/modules/cjs/loader.js:1000:32)?[39m ?[90m at Function.Module._load (internal/modules/cjs/loader.js:899:14)?[39m ?[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)?[39m ?[90m at internal/main/run_main_module.js:18:47?[39m