使用 Web3.js 的本地私钥
Using local private key with Web3.js
如何使用本地私钥与智能合约交互并使用 Web3.js 发送交易?私钥是硬编码的还是来自环境 (.env
) 文件?
这是 Node.js 和服务器端交互或使用 Ethereum/Polygon/Binance Smart Chain 智能合约的批处理作业所必需的。
你可能会遇到错误
Error: The method eth_sendTransaction does not exist/is not available
Infura、QuikNode 等以太坊节点提供商要求您在通过他们的节点广播之前在本地签署传出交易。
Web3.js 没有内置此功能。您需要使用 @truffle/hdwallet-provider package 作为以太坊供应商的中间件。
TypeScript 示例:
const Web3 = require('web3');
const HDWalletProvider = require("@truffle/hdwallet-provider");
import { abi } from "../../build/contracts/AnythingTruffleCompiled.json";
//
// Project secrets are hardcoded here
// - do not do this in real life
//
// No 0x prefix
const myPrivateKeyHex = "123123123";
const infuraProjectId = "123123123";
const provider = new Web3.providers.HttpProvider(`https://mainnet.infura.io/v3/${infuraProjectId}`);
// Create web3.js middleware that signs transactions locally
const localKeyProvider = new HDWalletProvider({
privateKeys: [myPrivateKeyHex],
providerOrUrl: provider,
});
const web3 = new Web3(localKeyProvider);
const myAccount = web3.eth.accounts.privateKeyToAccount(myPrivateKeyHex);
// Interact with existing, already deployed, smart contract on Ethereum mainnet
const address = '0x123123123123123123';
const myContract = new web3.eth.Contract(abi as any, address);
// Some example calls how to read data from the smart contract
const currentDuration = await myContract.methods.stakingTime().call();
const currentAmount = await myContract.methods.stakingAmount().call();
console.log('Transaction signer account is', myAccount.address, ', smart contract is', address);
console.log('Starting transaction now');
// Approve this balance to be used for the token swap
const receipt = await myContract.methods.myMethod(1, 2).send({ from: myAccount.address });
console.log('TX receipt', receipt);
您还需要避免将您的私钥提交到任何 Github 存储库。 dotenv package 是秘密管理的低门槛解决方案。
如何使用本地私钥与智能合约交互并使用 Web3.js 发送交易?私钥是硬编码的还是来自环境 (.env
) 文件?
这是 Node.js 和服务器端交互或使用 Ethereum/Polygon/Binance Smart Chain 智能合约的批处理作业所必需的。
你可能会遇到错误
Error: The method eth_sendTransaction does not exist/is not available
Infura、QuikNode 等以太坊节点提供商要求您在通过他们的节点广播之前在本地签署传出交易。
Web3.js 没有内置此功能。您需要使用 @truffle/hdwallet-provider package 作为以太坊供应商的中间件。
TypeScript 示例:
const Web3 = require('web3');
const HDWalletProvider = require("@truffle/hdwallet-provider");
import { abi } from "../../build/contracts/AnythingTruffleCompiled.json";
//
// Project secrets are hardcoded here
// - do not do this in real life
//
// No 0x prefix
const myPrivateKeyHex = "123123123";
const infuraProjectId = "123123123";
const provider = new Web3.providers.HttpProvider(`https://mainnet.infura.io/v3/${infuraProjectId}`);
// Create web3.js middleware that signs transactions locally
const localKeyProvider = new HDWalletProvider({
privateKeys: [myPrivateKeyHex],
providerOrUrl: provider,
});
const web3 = new Web3(localKeyProvider);
const myAccount = web3.eth.accounts.privateKeyToAccount(myPrivateKeyHex);
// Interact with existing, already deployed, smart contract on Ethereum mainnet
const address = '0x123123123123123123';
const myContract = new web3.eth.Contract(abi as any, address);
// Some example calls how to read data from the smart contract
const currentDuration = await myContract.methods.stakingTime().call();
const currentAmount = await myContract.methods.stakingAmount().call();
console.log('Transaction signer account is', myAccount.address, ', smart contract is', address);
console.log('Starting transaction now');
// Approve this balance to be used for the token swap
const receipt = await myContract.methods.myMethod(1, 2).send({ from: myAccount.address });
console.log('TX receipt', receipt);
您还需要避免将您的私钥提交到任何 Github 存储库。 dotenv package 是秘密管理的低门槛解决方案。