使用本地节点(ganache)时,我可以与智能合约进行交互。 但是当我尝试使用以下方法与公共节点交互时,我得到了
sender account not recognized
const web3 = new Web3(new HttpProvider('http://localhost:8545'));
const wallet = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(wallet);
web3.eth.defaultAccount = wallet.address;
const contractObj = new web3.eth.Contract(abi, contractAddress);
const receipt = await contractObj.methods.NameofTheMethod(x).send({
from: myAddress,
gasLimit: value
});
如何在没有像
@truffle/HDWalletProvider
这样的第三方钱包的情况下使其工作?
您的本地节点知道发送者的私钥 => 能够签署交易(在本地节点上)。
第 3 方节点不知道私钥 - 这就是您收到错误消息的原因。
您可以将私钥传递给应用程序,以便它可以在本地签署交易,并将已签名的交易发送到节点,而无需泄露私钥。
const HDWalletProvider = require("@truffle/hdwallet-provider");
const provider = new HDWalletProvider({
privateKeys: ["0x<your_private_key>"],
providerOrUrl: "https://<node_provider_url>/"
});
const web3 = new Web3(provider);
NPM 包:https://www.npmjs.com/package/@truffle/hdwallet-provider