ethereumweb3jsgo-ethereum

web3 json rpc error "" when attempting transactions


We're using web3 to connect to the rinkeby test ethereum network. When doing so through geth, via localhost, with the below web3 command:

var web3 = new Web3('http://localhost:8545');

We don't get any errors. We use this command to start geth:

geth --rinkeby --rpc --rpcapi="personal,eth,network,web3,net" --ipcpath "~/Library/Ethereum/geth.ipc"

However when we try using the rinkeby test network directly:

var web3 = new Web3('https://rinkeby.infura.io/');

We get this error:

Error: Invalid JSON RPC response: ""
   at Object.InvalidResponse (errors.js:42)
   at XMLHttpRequest.request.onreadystatechange (index.js:73)
   at XMLHttpRequest.dispatchEvent (event-target.js:172)
   at XMLHttpRequest.setReadyState (XMLHttpRequest.js:546)
   at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:387)
   at XMLHttpRequest.js:493
   at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
   at MessageQueue.__callFunction (MessageQueue.js:353)
   at MessageQueue.js:118
   at MessageQueue.__guardSafe (MessageQueue.js:316)

Most of the operations work on both networks, but .send() calls fail when connecting to the rinkeby network directly.

We think it's an issue with authentication, since other commands succeed that don't perform transactions. However, we tried using the HDWalletProvider and none of our accounts created via geth have mnemonics.


Solution

  • Transactions have to be signed. When you send a transaction via your local geth node, it knows the private key corresponding to the address you're sending from, so it can sign the transaction for you (once you unlock the account).

    A public node like Infura (fortunately!) doesn't know your private key, so it can't sign transactions for you. You'll need to sign them locally and then send them using sendSignedTransaction.