0

I'm using Infura as my web3 provider, and have MetaMask installed to connect to the blockchain. I'm trying to send a transaction, but have learned that Infura is read-only, and doesn't support the eth_sendTransaction method. I found this thread talking about it.

I'm trying to figure out how to sign a transaction locally then send it. This is my code that deals with signing and sending a transaction

...
let encodedData = await this.props.users.methods.registerUser(this.state.signUpUserName).encodeABI()

let signedData = await this.web3.eth.accounts.sign(encodedData, this.state.userAccount)

await this.web3.eth.sendSignedTransaction("0x" + signedData,function(err, result) {
    if(!err){
        console.log(result)
    }
})
...

I'm getting an error when I try to send the transaction, I think it's due to signedData not being RLP encoded, but I don't know much about it.

invalid argument 0: json: cannot unmarshal hex string of odd length into Go value of type hexutil.Bytes

signedData is an object containing the following data:

Object { message: "0x704f1b94000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000064a61726574680000000000000000000000000000000000000000000000000000", 
messageHash: "0x61f631cd8d21c0c03ca6548c35ec63b00aa41f53c4f3a89f1ef20ccc90b333e7", 
v: "0x1b", 
r: "0x5ed963bd343bdb044faf9a8f4074c074db6055cc72dd9d3e1032851e8d709dd2", 
s: "0x6fb58f55543c473e61dba4556021115c5aff041195453307a579eac5fa313685", 
signature: "0x5ed963bd343bdb044faf9a8f4074c074db6055cc72dd9d3e1032851e8d709dd26fb58f55543c473e61dba4556021115c5aff041195453307a579eac5fa3136851b" }

I was reading up on the ethereumjs-tx library, but that requires a private key to sign data, and as far as I know, you can't get the private key from metamask. Do I need to hardcode my private key from MetaMask into my code? How would one sign data locally and then proceed to send a transaction?

  • Is this in the browser? If so, why aren't you just using MetaMask? – smarx Jan 18 at 5:05
  • How would I do that? When I call web3.eth.sendTransaction or user .send after a contract function call I get the error, Returned error: The method eth_sendTransaction does not exist/is not available – JarethRader Jan 18 at 7:36
  • What web3 provider are you using? If you're using MetaMask's injected web3 provider, web3.eth.sendTransaction should work and pop up the MetaMask UI to have the user authorize the transaction. – smarx Jan 18 at 7:38
  • I'm using Infura as my web3 provider. I'll try using MetaMask instead. – JarethRader Jan 18 at 8:17
  • I switched to using MetaMask as my web3 provider and it works the way you said it would! – JarethRader Jan 23 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.