Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package bitcoinassignment;
  7.  
  8. import java.io.BufferedInputStream;
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStream;
  14. import java.net.Authenticator;
  15. import java.net.HttpURLConnection;
  16. import java.net.MalformedURLException;
  17. import java.net.PasswordAuthentication;
  18. import java.net.URL;
  19. import java.util.Map;
  20. import java.util.Scanner;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23.  
  24. /**
  25.  *
  26.  * @author Bogs
  27.  */
  28. public class BitcoinAssignment {
  29.  
  30.     private static final String HOST = "http://localhost:18443";
  31.     private static final String RPCUSER = "user";
  32.     private static final String RPCPASSWORD = "password";
  33.     private static final String REQUESTJSON = "{\"id\":\"PARAMETER1\", \"method\":\"PARAMETER2\", \"params\":PARAMETER3}";
  34.  
  35.     public static void main(String[] args) throws MalformedURLException, IOException {
  36.         setupAuthentication();
  37.  
  38.         Scanner scan = new Scanner(System.in);
  39.         while (true) {
  40.             try {
  41.             System.out.println("AVAILABLE COMMANDS: \n*    getbalance\n*    getnewaddress\n*    sendtoaddress [address] [amount]\n*    listunspent");
  42.             String input = scan.nextLine().toLowerCase();
  43.             System.out.println("\n\n");
  44.             HttpURLConnection con = setupConnection();
  45.             if (input.contains("getbalance")) {
  46.                 fireRequest(con, "getbalance");
  47.             } else if (input.contains("getnewaddress")) {
  48.                 fireRequest(con, "getnewaddress");
  49.             } else if (input.contains("sendtoaddress")) {
  50.                 fireRequest(con, "sendtoaddress");
  51.             } else if (input.contains("listunspent")) {
  52.                 fireRequest(con, "listunspent");
  53.             }
  54.             System.out.println("\n\n");
  55.             } catch(Exception e) {
  56.                 System.out.println("NOTHING TO SEE HERE ");
  57.                 e.printStackTrace();
  58.             }
  59.         }
  60.     }
  61.  
  62.     private static void setupAuthentication() {
  63.         Authenticator.setDefault(new Authenticator() {
  64.             protected PasswordAuthentication getPasswordAuthentication() {
  65.                 return new PasswordAuthentication(RPCUSER, RPCPASSWORD.toCharArray());
  66.             }
  67.         });
  68.     }
  69.    
  70.     private static String handleParams() {
  71.         String output = "[";
  72.         int params = 0;
  73.         Scanner scan = new Scanner(System.in);
  74.         while(true) {
  75.             System.out.println("tap enter to exit params");
  76.             System.out.println("type parameter value");
  77.             String paramValue = scan.nextLine();
  78.             if(paramValue.equals("")) {
  79.                 break;
  80.             } else {
  81.                 if(params > 0) {
  82.                     output += ",";
  83.                 }
  84.                 output += "\"" + paramValue + "\"";
  85.                 params++;
  86.             }
  87.         }
  88.         output += "]";
  89.         return output;
  90.     }
  91.  
  92.     private static HttpURLConnection setupConnection() {
  93.         HttpURLConnection con = null;
  94.         try {
  95.             URL url = new URL(HOST);
  96.             con = (HttpURLConnection) url.openConnection();
  97.             con.setRequestMethod("POST");
  98.             con.setDoOutput(true);
  99.             con.setRequestProperty("Content-Type", "application/json");
  100.         } catch (Exception e) {
  101.             Logger.getLogger(BitcoinAssignment.class.getName()).log(Level.SEVERE, null, e);
  102.         }
  103.         return con;
  104.     }
  105.  
  106.     private static void fireRequest(HttpURLConnection con, String method) throws IOException {
  107.         String request = REQUESTJSON.replaceAll("PARAMETER1", "MYID123");
  108.         request = request.replaceFirst("PARAMETER2", method);
  109.         request = request.replaceFirst("PARAMETER3", handleParams());
  110.  
  111.         try (OutputStream output = con.getOutputStream()) {
  112.             output.write(request.getBytes());
  113.         }
  114.  
  115.         InputStream response = con.getInputStream();
  116.         BufferedReader br = new BufferedReader(new InputStreamReader(response));
  117.         String line;
  118.         while ((line = br.readLine()) != null) {
  119.             System.out.println(line);
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement