Connect multichain using JSON

+3 votes
Hi,

I have configured multichain on two machines and successfully able to send assets between these two nodes.

Now I want to make a client (java), which can connect to the multichain using JSON RPC and execute commands. Please guide me how can I achieve this?

I found following link:

http://www.multichain.com/developers/json-rpc-api/

But not sure how to create a Java Client.

 

Regards

Anuj
asked Dec 28, 2015 by anonymous
Hello, I'm also trying to connect via JSON.

I tried to use bitcoin-json-rpc-client API, but not found a way/method to connect with the Multichain.

You could connect? We must first connect to the server machine and then in Multichain? Which means of conexion, SSH, JSON ...?
Hi Carol,

I got stuck with some issues last few days, so couldn't devote my time on this.

I haven't been able to connect to blockchain yet. Have picked up this thread again. First I will try to have blockchain and java code running on the same  machine.
Hi,

I couldn't connect with the blockchain using JSON API, so for time being I am using a workaround (though a very bad one, I must admit).

I am executing Shell scripts from Java. Java code is placed on the same machine on which the blockchain in running.

1 Answer

+2 votes
MultiChain is fully backwards compatible (with extra APIs added on top) with Bitcoin Core, the standard client for the bitcoin network, so you should be able to use an existing Java library for Bitcoin Core's JSON-RPC interface and then extend it over time to support new APIs. Here's a good place to start: https://bitbucket.org/azazar/bitcoin-json-rpc-client/wiki/Home
answered Dec 28, 2015 by MultiChain
Hi,

I have removed the JAR file from my classpath and directly imported the source code in my project.

Have made following changes to the class "BitcoinJSONRPCClient.java":
String user = "<My BlockChain username>";
String password = "<My BlockChain Password>";
String host = "<My BlockChain machine IP>";
String port = "<Machine Port>";

Now when I try to connect with the BlockChain using following method:
public static void balance() throws BitcoinException {
        System.out.println(new BitcoinJSONRPCClient().getBalance());
    }

I am getting following exception:
com.azazar.bitcoin.jsonrpcclient.BitcoinRPCException: RPC Query Failed (method: getbalance, params: [])
    at com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient.query(BitcoinJSONRPCClient.java:228)
    at com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient.getBalance(BitcoinJSONRPCClient.java:269)
    at com.test.nordea.MainClass.balance(MainClass.java:21)
    at com.test.nordea.MainClass.main(MainClass.java:14)
Caused by: java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:792)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:675)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1535)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient.query(BitcoinJSONRPCClient.java:223)
    ... 3 more



Regards
Anuj
Thanks for your reply.

I downloaded the following components from the URL provided:
1. bitcoin-json-rpc-client-1.0
2. azazar-bitcoin-json-rpc-client-dcbd61462d4b.zip - source code

I have put this JAR file in my classpath and now trying to connect with the blockchain which is running on a different machine.

I looked at the source code. In class "BitcoinJSONRPCClient.java", I couldn't find the option to set username and password (default username and password already provided in this class).

Please help me to resolve this.

Also the multichain is not running on the same machine, from which I am trying to make the JSON RPC call.


Regards
Anuj
If MultiChain is not running on the same machine, you'll need to make a change to the rpcallowip configuration setting - more info here: http://www.multichain.com/developers/runtime-parameters/
So what will be the command:
multichain-cli chain1 anyone-can-connect true
or
multichain-cli chain1 rpcallowip@<ipAddress of Machine on which java client is run>
There are various options. Please see this page for how to set runtime parameters: http://www.multichain.com/developers/runtime-parameters/
I have set the rpcallowip as:
multichaind chain1clone -rpcallowip=<IP-address>

Also in params.dat, i have modified following three elements to true:
anyone-can-connect
anyone-can-send
anyone-can-receive



Still I am getting the following exception:
sun.net.www.protocol.http.HttpURLConnection:http://chain1clone@<IP>:<Port>/
com.azazar.bitcoin.jsonrpcclient.BitcoinRPCException: RPC Query Failed (method: getbalance, params: [])
    at com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient.query(BitcoinJSONRPCClient.java:238)
    at com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient.getBalance(BitcoinJSONRPCClient.java:279)
    at com.test.nordea.MainClass.balance(MainClass.java:18)
    at com.test.nordea.MainClass.main(MainClass.java:11)
Caused by: java.net.ConnectException: Connection refused: connect



Note: chain1clone is clone of chain1.

I am getting same connection refused exception while connecting to chain1 as well.
First, the rpcallowip should be set to the IP address of the client/caller, and the JSON-RPC query should be sent to the IP address of the server/MultiChain.

If that does not help, maybe try using it on a local machine first, to check if the problem is with the IP address, or something more basic. I'm afraid it's rather difficult for us to debug this problem for you from afar.

FYI the anyone-can-* parameters are not relevant for the JSON-RPC interface, but they are rather characteristics of the blockchain itself.
...