Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package bitcoinassignment;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.Authenticator;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.PasswordAuthentication;
- import java.net.URL;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Bogs
- */
- public class BitcoinAssignment {
- private static final String HOST = "http://localhost:18443";
- private static final String RPCUSER = "user";
- private static final String RPCPASSWORD = "password";
- private static final String REQUESTJSON = "{\"id\":\"PARAMETER1\", \"method\":\"PARAMETER2\", \"params\":PARAMETER3}";
- public static void main(String[] args) throws MalformedURLException, IOException {
- setupAuthentication();
- Scanner scan = new Scanner(System.in);
- while (true) {
- try {
- System.out.println("AVAILABLE COMMANDS: \n* getbalance\n* getnewaddress\n* sendtoaddress [address] [amount]\n* listunspent");
- String input = scan.nextLine().toLowerCase();
- System.out.println("\n\n");
- HttpURLConnection con = setupConnection();
- if (input.contains("getbalance")) {
- fireRequest(con, "getbalance");
- } else if (input.contains("getnewaddress")) {
- fireRequest(con, "getnewaddress");
- } else if (input.contains("sendtoaddress")) {
- fireRequest(con, "sendtoaddress");
- } else if (input.contains("listunspent")) {
- fireRequest(con, "listunspent");
- }
- System.out.println("\n\n");
- } catch(Exception e) {
- System.out.println("NOTHING TO SEE HERE ");
- e.printStackTrace();
- }
- }
- }
- private static void setupAuthentication() {
- Authenticator.setDefault(new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(RPCUSER, RPCPASSWORD.toCharArray());
- }
- });
- }
- private static String handleParams() {
- String output = "[";
- int params = 0;
- Scanner scan = new Scanner(System.in);
- while(true) {
- System.out.println("tap enter to exit params");
- System.out.println("type parameter value");
- String paramValue = scan.nextLine();
- if(paramValue.equals("")) {
- break;
- } else {
- if(params > 0) {
- output += ",";
- }
- output += "\"" + paramValue + "\"";
- params++;
- }
- }
- output += "]";
- return output;
- }
- private static HttpURLConnection setupConnection() {
- HttpURLConnection con = null;
- try {
- URL url = new URL(HOST);
- con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("POST");
- con.setDoOutput(true);
- con.setRequestProperty("Content-Type", "application/json");
- } catch (Exception e) {
- Logger.getLogger(BitcoinAssignment.class.getName()).log(Level.SEVERE, null, e);
- }
- return con;
- }
- private static void fireRequest(HttpURLConnection con, String method) throws IOException {
- String request = REQUESTJSON.replaceAll("PARAMETER1", "MYID123");
- request = request.replaceFirst("PARAMETER2", method);
- request = request.replaceFirst("PARAMETER3", handleParams());
- try (OutputStream output = con.getOutputStream()) {
- output.write(request.getBytes());
- }
- InputStream response = con.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(response));
- String line;
- while ((line = br.readLine()) != null) {
- System.out.println(line);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement