简体   繁体   English

私有 ethereum.network 无法识别发件人帐户

[英]Sender account not recognized on private ethereum network

I'm currently developing a dApp in Solidity and am working on a web3 library to handle communication with it.我目前正在用 Solidity 开发一个 dApp,并且正在开发一个 web3 库来处理与它的通信。

I struggle with the process of new account creation and transaction signing in web3.我在 web3 中为新帐户创建和交易签名的过程而苦恼。 Before I continue it worth noting that I'm running my own local, private blockchain (currently with Ganache).在我继续之前,值得注意的是我正在运行我自己的本地私有区块链(目前使用 Ganache)。

My code looks as follows:我的代码如下所示:

try{
    let a = web3.eth.accounts.create()
    let dataTx = someContract.methods.someMethod().encodeABI()
    let rawTx = {
        to: someContract._address,
        from: account.address,
        data: dataTx,
        gas: 10000000000
    }


    const transaction = web3.eth.accounts.signTransaction(rawTx, util.toBuffer(account.privateKey))
    web3.eth.sendTransaction(rawTx).then(console.log)
}
catch(e){
    console.log(e)
}

The problem here is that the web3.eth.sendTransaction() method raises the following exception: Error: Returned error: sender account not recognized .这里的问题是web3.eth.sendTransaction()方法引发了以下异常: Error: Returned error: sender account not recognized

My understanding is that web3.eth.accounts is used for managing local accounts and web3.eth.personal is used to communicate with a client (eg Geth).我的理解是web3.eth.accounts用于管理本地帐户,而web3.eth.personal用于与客户端(例如 Geth)通信。 I wish to keep the private keys of accounts my app creates locally on the device of the web3 client, but it raises this exception.我希望保留我的应用程序在 web3 客户端设备上本地创建的帐户的私钥,但它会引发此异常。

Where am I going wrong?我哪里错了? Should I register the newly created accounts somewhere before running transactions with it?我应该在运行交易之前在某处注册新创建的帐户吗? Is there some vital information I'm missing here?我在这里缺少一些重要信息吗?

Thanks!谢谢!

You are refering to functionality in web3 1.0.0 which is not yet fully released. 您指的是尚未完全发布的web3 1.0.0中的功能。 If you go to https://web3js.readthedocs.io/en/1.0/getting-started.html you would see that they state the following: 如果您访问https://web3js.readthedocs.io/en/1.0/getting-started.html,您会看到它们指出以下内容:

This documentation is work in progress and web3.js 1.0 is not yet released! 本文档正在开发中,尚未发布web3.js 1.0! You can find the current documentation for web3 0.xx at github.com/ethereum/wiki/wiki/JavaScript-API . 您可以在github.com/ethereum/wiki/wiki/JavaScript-API上找到web3 0.xx的当前文档。

Most probably you are using a version 0.20.x or something like that so check this first. 很可能您正在使用0.20.x或类似的版本,因此请先检查一下。 To check this open the dApp in the browser and type in the console the following: 要对此进行检查,请在浏览器中打开dApp,然后在控制台中键入以下内容:

web3.version.api

This should show you which version you are using. 这应该显示您正在使用哪个版本。

I don't think there is a way to create accounts with web3js 0.20.x directly but you can try to update the web3js to the 1.0.0-beta and try to run your code again. 我认为没有办法直接使用web3js 0.20.x创建帐户,但是您可以尝试将web3js更新为1.0.0-beta,然后尝试再次运行代码。 You can find it in NPM here - https://www.npmjs.com/package/web3 您可以在NPM中找到它-https: //www.npmjs.com/package/web3

If you would want to learn more about Solidity and smart contracts creation, check out my course that I did on that specifically - Solidity Smart Contracts: Build Dapps In Ethereum Blockchain 如果您想了解有关Solidity和智能合约创建的更多信息,请查看我专门为此做的课程-Solidity Smart Contracts:在以太坊区块链中构建Dapps

Here is a coupon for discount: QAUS8657 这是折价券: QAUS8657

If you want to use an account other than Ganache provided you, you have to start Ganache providing your accounts data in the form private_key,initial_balance : 如果要使用Ganache提供的帐户以外的其他帐户,则必须启动Ganache以private_key,initial_balance形式提供帐户数据:

Example command: ganache-cli --account 0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56,123456789 示例命令: ganache-cli --account 0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56,123456789

And it's output: 它的输出是:

Ganache CLI v6.1.8 (ganache-core: 2.2.1) Ganache CLI v6.1.8(ganache-core:2.2.1)

Available Accounts ================== (0) 0x44fa41e453654ccb365a358e994c764a37eea91f (~0 ETH) 可用帐户=================(0)0x44fa41e453654ccb365a358e994c764a37eea91f(〜0 ETH)

Private Keys ================== (0) 0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56 私钥=================(0)0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56

Gas Price ================== 20000000000 汽油价格================== 20000000000

Gas Limit ================== 6721975 气体限制================== 6721975

Listening on 127.0.0.1:8545 聆听127.0.0.1:8545

I am having same issue in my project.我在我的项目中遇到了同样的问题。 The problem in my case is beacuse i am not using same web3 provider to create contract variable.我的问题是因为我没有使用相同的 web3 提供程序来创建合同变量。 example code:示例代码:

const providerEth= new Web3.providers.HttpProvider(
    'HTTP://127.0.0.1:8545'
);
const web3Eth = new Web3(providerEth);
const contract= new web3Eth.eth.Contract(abi,address);

Here, we are not using metamask provider although both on same network Still it not recognize the account.在这里,我们没有使用元掩码提供程序,尽管它们都在同一网络上,但它仍然无法识别该帐户。 So you should create contract like this所以你应该像这样创建合同

const web3Eth = new Web3(window.web3.currentProvider);
const contract= new web3Eth.eth.Contract(abi,address);

const accounts = await web3.eth.getAccounts();
var receipt=await contract.methods.transfer(receiver,amount).send({from:accounts[0]});

Now, you can able to call smart contract function with account address.现在,您可以使用帐户地址调用智能合约功能。

I had the same issue.我遇到过同样的问题。 It happened when I already opened my truffle console and after that I did a restart of my ganache because I wanted to start clean.它发生在我已经打开松露控制台的时候,之后我重新启动了我的甘纳许,因为我想开始干净。 What fixed it for me was stopping the truffle console job and starting it again.为我修复的是停止松露控制台作业并重新启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM