[Ethereum] Invalid JSON RPC response error for sendTransaction on Infura + Ropsten node + Truffle console

infuratransactionstruffleweb3js

Calls work but transactions throw the error

Error: Invalid JSON RPC response: ""

I am using web3 v0.19.0 & Truffle v3.4.9. Deploying contract using truffle,
truffle migrate --network ropsten
which successfully provides api and contract address.

My web3 provider and ropsten network (infura node) are defined in truffle.js on top of react-auth-box project.

I open truffle console --network ropsten, and define web3 –

var Web3 = require('web3')
let web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider('https://ropsten.infura.io/my_access_token_here')) 

There is no default account
web3.eth.defaultAccount (returns null)
web3.eth.accounts (returns [])

Set default account,
web3.eth.defaultAccount = '0xpersonalaccount'

Define contract instance,
let contract = web3.eth.contract(abi).at(address)

All good so far and Calls work
contract.checkIdExists.call(1, {'from': account, 'to': address})
(Returns '0x0000000000')

contract.fetchDataById.call(1, {'from': account, 'to': address})
(Returns '0x')

1. Transactions fail –
contract.addRecord.sendTransaction(1, 'fjdnjsnkjnsd', '03:00:21 12-12-12', 'true', '', {'from': account, 'to': address})

Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/var/www/html/react-auth-box/node_modules/web3/lib/web3/errors.js:38:16)
at HttpProvider.send (/var/www/html/react-auth-box/node_modules/web3/lib/web3/httpprovider.js:91:22)
at RequestManager.send (/var/www/html/react-auth-box/node_modules/web3/lib/web3/requestmanager.js:58:32)
at Eth.send [as sendTransaction] (/var/www/html/react-auth-box/node_modules/web3/lib/web3/method.js:145:58)
at SolidityFunction.sendTransaction (/var/www/html/react-auth-box/node_modules/web3/lib/web3/function.js:167:26)
at evalmachine.:1:20
at ContextifyScript.Script.runInContext (vm.js:53:29)
at Object.runInContext (vm.js:108:6)
at TruffleInterpreter.interpret (/home/shivam/.npm-global/lib/node_modules/truffle/build/cli.bundled.js:213786:17)
at bound (domain.js:301:14)

2. Makes me think I probably have to unlock account first (do I?)
web3.personal.unlockAccount(account, password)

Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/var/www/html/react-auth-box/node_modules/web3/lib/web3/errors.js:38:16)
at HttpProvider.send (/var/www/html/react-auth-box/node_modules/web3/lib/web3/httpprovider.js:91:22)
at RequestManager.send (/var/www/html/react-auth-box/node_modules/web3/lib/web3/requestmanager.js:58:32)
at Personal.send [as unlockAccount] (/var/www/html/react-auth-box/node_modules/web3/lib/web3/method.js:145:58)
at evalmachine.:1:15
at ContextifyScript.Script.runInContext (vm.js:53:29)
at Object.runInContext (vm.js:108:6)
at TruffleInterpreter.interpret (/home/shivam/.npm-global/lib/node_modules/truffle/build/cli.bundled.js:213786:17)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)

Running pretty clueless by now. Any support will be appreciated. Thanks!

Best Answer

Let me post the complete answer here (Credits to @Ismael).

Relevant packages -
web3@0.18.2
ethereumjs-tx@1.3.3
crypto-js

const Web3 = require('web3')  
let web3 = new Web3()  
web3.providers.HttpProvider('https://ropsten.infura.io/my_access_token_here'))  
let contract = web3.eth.contract(abi).at(address)  
var coder = require('web3/lib/solidity/coder')  
var CryptoJS = require('crypto-js')  
var privateKey = new Buffer(myPrivateKey, 'hex')  

var functionName = 'addRecord'  
var types = ['uint','bytes32','bytes20','bytes5','bytes']  
var args = [1, 'fjdnjsnkjnsd', '03:00:21 12-12-12', 'true', '']  
var fullName = functionName + '(' + types.join() + ')'  
var signature = CryptoJS.SHA3(fullName,{outputLength:256}).toString(CryptoJS.enc.Hex).slice(0, 8)  
var dataHex = signature + coder.encodeParams(types, args)  
var data = '0x'+dataHex  

var nonce = web3.toHex(web3.eth.getTransactionCount(account))  
var gasPrice = web3.toHex(web3.eth.gasPrice)  
var gasLimitHex = web3.toHex(300000) (user defined)  
var rawTx = { 'nonce': nonce, 'gasPrice': gasPrice, 'gasLimit': gasLimitHex, 'from': account, 'to': address, 'data': data}  
var tx = new Tx(rawTx)  
tx.sign(privateKey)  
var serializedTx = '0x'+tx.serialize().toString('hex')  
web3.eth.sendRawTransaction(serializedTx, function(err, txHash){ console.log(err, txHash) })   

(Returns '0xf802614fd6a53cb372752634630265063d0b48fec12ea8f5ed363de1d4bd372d')

web3.eth.getTransaction('0xf802614fd6a53cb372752634630265063d0b48fec12ea8f5ed363de1d4bd372d', console.log)

(Prints transaction data)
(Refer here)

Related Topic