Private key length is invalid

I have a hard time to get my dApp’s frontend working with Infura. Previously I’ve only built my dApps with ganache-cli, but now I wanted to try it with Kovan and Infura. There’s absolutely no problem with the deployment, the problem comes when I try to call functions, since I now need to sign transactions offline. Using truffle unbox react, in my getWeb3.js file, you can see my code in one of the the pictures. I’ve tried to add 0x at the beginning, as well as deleting/adding 'hex', but it simply doesn’t work out for me. Can someone please see if anything in my code is wrong, or give suggestions of what I might try instead?

Either the error message says “private key length is invalid” (when not using “hex” after privateKey) or “No “from” address specified in neither the given options, nor the default options” (with only the privateKey).

Thank you all very much!

We are looking into this, and I’ve reached out to the Truffle team for their input. Will post back here with more when ready.

It looks like
No "from" address specified in neither the given options, nor the default options
is coming from web3.js, the from field isn’t required with signed txns since it can be recalculated from the txHash and vars, it looks like you are properly signing the txn using ethereumjs-tx however it is unclear how that transaction is being sent to Infura since it is not visible in the screenshot.

Can you share the rest of the code?

Thanks for looking into this. Here is the rest of the code that I have in getWeb3.js, which I then import in my file App.js, which is where the actual frontend takes part. Perhaps I miss to send it to Infura somehow? And as I said, everything works well with ganache-cli and without ethereumjs-tx.

App.js:

(Could only upload one pic per post)

It looks like the screen shots are still missing the code we need. Unfortunately, I don’t think the issue is with ganache or infura, but rather with web3.js. After looking closer, that error originates from web3.eth.Contract which sometimes expects a from field; for example on a .call or .send. It’s hard to say without reading the rest of his code.

1 Like

Thank you. Don’t know exactly what code you need, but I send you a function of how I’ve always done developing dApps with ganache, where I send it from my Metamask account (where “privateKey” and “account” come from). Can you please provide a code example of how to do my setup with Infura/the part you think I miss? Since I haven’t sent you the correct code snippet, perhaps I just simply miss out the part of sending the transaction to Infura?

And to be clear, I’ve tested this dApp with ganache and there’s been no problem at all. It’s when I include Infura and deploy it to Kovan that the frontend won’t work out. I managed to send some funds to the contract through the fallback function, and I can also update its state with call-functions. It’s the setters and the “send functions” that don’t work.

If I instead as accounts[0] hard code the actual address, it gives me the error The method eth_sendTransaction does not exist/is not available. Don’t know if that helps. Thanks once again.

I’ve resolved my issue now and everything works fine with Infura. As you said, it had nothing to do with Infura but was a web3 related problem. Thanks again for your help!

How did you solve the problem with web3 @filip ? I am running into the same issue…

@klivin I rebuilt everything from scratch as I knew it from before, and then tried to include Metamask with Infura (on Kovan) just as I’ve previously done on localhost. For me, it was totally unnecessary to even use ethereumtx-js, as Metamask solves that problem for you. I’ll send you a picture of my getWeb3.js file.

Then in your frontend building file (like app.js or index.js), just do as usual with this.state.contract.methods.randomFunction().send({ from: accounts[0] });. There’s no need to send or sign any raw transactions, which I first thought was the problem when I got all the error messages. Metamask will take care of that also on Kovan, but you need to change the getWeb3.js first. See the picture and then tell me how it went :slight_smile: