9

I want to be able to use monero as a payment method in my website. It's completely written in java spark. I am unfamiliar with using curl or json rpc in java and would like someone to give an example of how to do it.

HashTables
  • 456
  • 1
  • 4
  • 11
  • I am unable to do this, but you should be able to find some JSON library for Java. Monero RPC uses standard JSON, no Monero specific oddities AFAIK. – user36303 Oct 09 '16 at 21:40
  • I managed to do it, check out my answer. Or ask me for any help or possible suggestions to improve the code. – HashTables Oct 10 '16 at 08:18

2 Answers2

8

Ok i managed to do it, here is the code:

String curlCode ="./getBalance";
System.out.println(curlCode); 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream old = System.out;
System.setOut(ps);
InputStream is = Runtime.getRuntime().exec(curlCode).getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buff = new BufferedReader (isr);

String line;
while((line = buff.readLine()) != null)
System.out.print(line); 

System.out.flush();
System.setOut(old);
System.out.println("Here: " + baos.toString());
String balance = baos.toString();
JSONObject json3 = new JSONObject(balance);
Long long1 = (json3.getJSONObject("result").getLong("balance"));
BigInteger bigInt = BigInteger.valueOf(long1);
BigDecimal one = new BigDecimal(bigInt, 12);


System.out.println("Monero balance is: " + one);

Basically what i did was run a system command with the curl code inside, convert the terminal output to a json object and printed out the balance. I also had to convert the balance with bigDecimal because it didn't have a decimal place in it.

There is one important thing to add, i added a getBalance script with the following curl code inside:

curl -X POST http://127.0.0.1:18082/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"getbalance"}' -H 'Content-Type: application/json'
HashTables
  • 456
  • 1
  • 4
  • 11
3

I probably miss the point of using an external program, but if you want to do it in Java only it can be done as follows:

public class MoneroClient {

    private int requestId = 0;

    private JSONRPC2Session rpcSession;

    public MoneroClient(String url) throws MalformedURLException {
        rpcSession = new JSONRPC2Session(new URL(url));
    }

    public BigDecimal getBalance() throws JSONRPC2SessionException {
        BigDecimal balance = null;
        JSONObject result = rpcCall("getbalance", null);
        if (result != null) {
            balance = BigDecimal.valueOf(Long.valueOf("" + result.get("balance")), 12);
        }
        return balance;
    }

    private JSONObject rpcCall(String method, List<Object> params) throws JSONRPC2SessionException {
        JSONRPC2Request request = new JSONRPC2Request(method, params, ++requestId);
        JSONRPC2Response response = rpcSession.send(request);
        JSONObject result = null;
        if (response.indicatesSuccess()) {
            result = (JSONObject) response.getResult();
        } else {
            // TODO: Throw exception
            System.err.println(response.getError().getMessage());
        }
        return result;
    }
}

It uses a library to perform the json-rpc call json-rpc-2.0-client.

Called in a simple Spark server:

public class MoneroServer {

    private static final String MONERO_URL = "http://localhost:18082/json_rpc";

    public static void main(String[] args) {
        get("/getbalance", MoneroServer::getBalance);
    }

    public static String getBalance(Request req, Response res) {
        String result = "";
        try {
            MoneroClient mc = new MoneroClient(MONERO_URL);
            result += mc.getBalance();
        } catch (Exception e) {
            result = e.toString();
        }
        return result;
    }
}

The web url: http://localhost:4567/getbalance

fatdoor
  • 589
  • 3
  • 9