Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: GodlyWizard on September 16, 2018, 06:49:06 PM



Title: BTCD node.js ERROR:Error: connect ECONNREFUSED
Post by: GodlyWizard on September 16, 2018, 06:49:06 PM
I am running the golang implementation of bitcoin and trying to query the JSON-RPC API remotely. I am trying with the example demonstrated here (https://github.com/btcsuite/btcd/blob/master/docs/json_rpc_api.md#ExampleNodeJsCode) but I get an error saying the connection was refused.

I have configured rpcuser and rpcpass in both btcd.conf and btcctl.conf, I have also tried generating new key and cert with the gencerts command
Code:
gencerts -d /home/user/.certsforhosts/ -H 192.168.0.104
but I still get the same error.

What might be wrong with my configuration?

btcd.conf
Code:
datadir=/home/user/blockchain/

rpcuser=user1
rpcpass=pass1
rpclimituser=user2
rpclimitpass=pass2

txindex=1
addrindex=1

rpccert=/home/user/.certsforhosts/rpc.cert
rpckey=/home/user/.certsforhosts/rpc.key

btcctl.conf
Code:
rpcuser=user1
rpcpass=pass1
rpccert=/home/user/.certsforhosts/rpc.cert

my code
Code:
var fs = require('fs');
var WebSocket = require('ws');

var cert = fs.readFileSync('path/to/my/rpc.cert');
var user = "user1";
var password = "pass1";

var ws = new WebSocket('wss://192.168.0.102:8334/ws', {
  headers: {
    'Authorization': 'Basic '+Buffer.from(user+':'+password).toString('base64')
  },
  cert: cert,
  ca: [cert]
});

ws.on('open', function() {
    console.log('CONNECTED');

    ws.send('{"jsonrpc":"1.0","id":"0","method":"notifyblocks","params":[]}');
});
ws.on('message', function(data, flags) {
    console.log(data);
});
ws.on('error', function(derp) {
  console.log('ERROR:' + derp);
})
ws.on('close', function(data) {
  console.log('DISCONNECTED');
})


Title: Re: BTCD node.js ERROR:Error: connect ECONNREFUSED
Post by: aleksej996 on September 17, 2018, 11:47:18 AM
I am not familiar with these implementations, but you should check if rpc is binding to the right address.

Check if the right port is open from the PC that you are connecting from. You can check with nmap or netcat.


Title: Re: BTCD node.js ERROR:Error: connect ECONNREFUSED
Post by: GodlyWizard on September 17, 2018, 12:20:53 PM
Thank you for your reply, the default rpc port on btcd is 8334 and I haven't changed that.
On netstat I can see that btcd is listening on that port.


Title: Re: BTCD node.js ERROR:Error: connect ECONNREFUSED
Post by: aleksej996 on September 17, 2018, 12:53:42 PM
Thank you for your reply, the default rpc port on btcd is 8334 and I haven't changed that.
On netstat I can see that btcd is listening on that port.

I would really suggest you check from the computer that you are connecting from.
You can use "nc -v 192.168.0.104 8334". (I assume that 192.168.0.104 is the IP of the host with rpc)

It is good that the program is listening on that port, but it might bind only to localhost IP address (eg. 127.0.0.1) and not to the address accessible by your other PC. Best practice is to simply check from the PC making a connection. It can rule out any firewall problems as well.


Title: Re: BTCD node.js ERROR:Error: connect ECONNREFUSED
Post by: GodlyWizard on September 17, 2018, 01:18:48 PM
You were right! It was only listening on the localhost 127.0.0.1, thank you very much!

For future reference:
I binded an IP address on my router to the MAC of my device the RPC is running on and used that IP with rpclisten option on btcd.conf, like this:

rpclisten=192.168.0.102
rpclisten=192.168.0.102:8334

My code now works and with getblockcount method I got the value I was expecting to see, the same as running btcctl getblockcount locally.


Title: Re: BTCD node.js ERROR:Error: connect ECONNREFUSED
Post by: seoincorporation on September 17, 2018, 04:01:02 PM
I am running the golang implementation of bitcoin and trying to query the JSON-RPC API remotely. I am trying with the example demonstrated here (https://github.com/btcsuite/btcd/blob/master/docs/json_rpc_api.md#ExampleNodeJsCode) but I get an error saying the connection was refused.

I have configured rpcuser and rpcpass in both btcd.conf and btcctl.conf, I have also tried generating new key and cert with the gencerts command
Code:
gencerts -d /home/user/.certsforhosts/ -H 192.168.0.104
but I still get the same error.

What might be wrong with my configuration?

btcd.conf
Code:
datadir=/home/user/blockchain/

rpcuser=user1
rpcpass=pass1
rpclimituser=user2
rpclimitpass=pass2

txindex=1
addrindex=1

rpccert=/home/user/.certsforhosts/rpc.cert
rpckey=/home/user/.certsforhosts/rpc.key

btcctl.conf
Code:
rpcuser=user1
rpcpass=pass1
rpccert=/home/user/.certsforhosts/rpc.cert

my code
Code:
var fs = require('fs');
var WebSocket = require('ws');

var cert = fs.readFileSync('path/to/my/rpc.cert');
var user = "user1";
var password = "pass1";

var ws = new WebSocket('wss://192.168.0.102:8334/ws', {
  headers: {
    'Authorization': 'Basic '+Buffer.from(user+':'+password).toString('base64')
  },
  cert: cert,
  ca: [cert]
});

ws.on('open', function() {
    console.log('CONNECTED');

    ws.send('{"jsonrpc":"1.0","id":"0","method":"notifyblocks","params":[]}');
});
ws.on('message', function(data, flags) {
    console.log(data);
});
ws.on('error', function(derp) {
  console.log('ERROR:' + derp);
})
ws.on('close', function(data) {
  console.log('DISCONNECTED');
})

Maybe is a firewall problem, did you open the bitcoin port on your firewall? Remember you can do it manually with the firewall application or you can do it by command line with the next command:

Code:
firewall-cmd --permanent --zone=public --add-port=8334/tcp

Try to fix it this way and if the problem persist, let us know.



Title: Re: BTCD node.js ERROR:Error: connect ECONNREFUSED
Post by: aleksej996 on September 17, 2018, 04:18:23 PM
Maybe is a firewall problem, did you open the bitcoin port on your firewall? Remember you can do it manually with the firewall application or you can do it by command line with the next command:

Code:
firewall-cmd --permanent --zone=public --add-port=8334/tcp

Try to fix it this way and if the problem persist, let us know.

I assume, sir, that you were too busy and haven't followed up on the posts.
The problem was solved. He was binding only to localhost which made connections over LAN impossible.

You were right! It was only listening on the localhost 127.0.0.1, thank you very much!

You are welcome, my child.