为什么需要在 Node 中创建以太坊钱包?

            以太坊是目前最为流行的区块链平台之一,很多应用都是基于它开发的。如果你需要在应用中使用以太坊,那么就需要一个钱包来管理以太币。在 Node 中创建钱包有很多好处,比如你可以使用以太坊的 JavaScript API 与其进行交互。此外,你也可以使用纯 JavaScript 库来生成公私钥对,而无需使用任何外部软件。

            如何生成公私钥对?

            如何在 Node 中创建以太坊钱包

            生成公私钥对需要使用以太坊的加密算法 secp256k1。在 Node 中,你可以使用 crypto 模块来生成这个算法的密钥。首先,需要生成一个随机数作为私钥,然后使用 secp256k1 将其转换为公钥。

            const crypto = require('crypto');
            const secp256k1 = require('secp256k1');
            
            const privateKey = crypto.randomBytes(32);
            const publicKey = secp256k1.publicKeyCreate(privateKey);
            
            console.log('私钥:'   privateKey.toString('hex'));
            console.log('公钥:'   publicKey.toString('hex'));

            如何从私钥生成地址?

            在以太坊中,钱包地址是基于公钥生成的。因此,如果你已经有了一个私钥和公钥,那么就可以使用这些信息来生成地址。要生成地址,需要先对公钥进行 Keccak-256 哈希运算,然后取结果的后 20 字节。

            const crypto = require('crypto');
            const secp256k1 = require('secp256k1');
            const { keccak256 } = require('js-sha3');
            
            const privateKey = crypto.randomBytes(32);
            const publicKey = secp256k1.publicKeyCreate(privateKey);
            
            const address = '0x'   keccak256(publicKey.slice(1)).slice(-20).toString('hex');
            
            console.log('地址:'   address);

            如何将钱包信息存储在本地?

            如何在 Node 中创建以太坊钱包

            在实际使用中,你需要将钱包信息存储在本地,以便后续使用。你可以使用 fs 模块将其写入到一个 JSON 文件中。例如:

            const fs = require('fs');
            
            const privateKey = crypto.randomBytes(32);
            const publicKey = secp256k1.publicKeyCreate(privateKey);
            const address = '0x'   keccak256(publicKey.slice(1)).slice(-20).toString('hex');
            
            const wallet = {
              privateKey: privateKey.toString('hex'),
              publicKey: publicKey.toString('hex'),
              address: address
            };
            
            fs.writeFileSync('wallet.json', JSON.stringify(wallet));
            
            console.log('钱包信息已保存到 wallet.json 中。');

            如何读取本地钱包信息?

            如果你需要使用已经保存在本地的钱包信息,可以使用 fs 模块读取 JSON 文件,并将其中的私钥转换为 Buffer 类型。

            const fs = require('fs');
            
            const walletData = fs.readFileSync('wallet.json');
            const wallet = JSON.parse(walletData);
            
            const privateKey = Buffer.from(wallet.privateKey, 'hex');
            const publicKey = secp256k1.publicKeyCreate(privateKey);
            const address = '0x'   keccak256(publicKey.slice(1)).slice(-20).toString('hex');
            
            console.log('地址:'   address);

            如何使用钱包进行交易?

            要使用钱包进行交易,需要使用以太坊的 JavaScript API web3.js。首先需要连接到一个以太坊节点,然后使用钱包信息构造一个账户。然后就可以使用 web3.js 提供的方法来构造、发送交易了。

            const Web3 = require('web3');
            
            const web3 = new Web3('https://mainnet.infura.io/v3/');
            
            const fromAccount = web3.eth.accounts.privateKeyToAccount(wallet.privateKey);
            web3.eth.accounts.wallet.add(fromAccount);
            
            const toAddress = ''; // 填写接收方地址
            const value = ''; // 填写转账金额
            
            const gasPrice = await web3.eth.getGasPrice();
            const gasLimit = 21000;
            
            const tx = {
              from: fromAccount.address,
              to: toAddress,
              value: value,
              gasPrice: gasPrice,
              gasLimit: gasLimit,
              nonce: await web3.eth.getTransactionCount(fromAccount.address)
            };
            
            const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
            await web3.eth.sendSignedTransaction(signedTx.rawTransaction);