Advertisement
prix

Coin sender

Jul 21st, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.65 KB | None | 0 0
  1. import org.apache.http.HttpEntity;
  2. import org.apache.http.auth.AuthScope;
  3. import org.apache.http.auth.UsernamePasswordCredentials;
  4. import org.apache.http.client.methods.CloseableHttpResponse;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.entity.StringEntity;
  7. import org.apache.http.impl.client.BasicCredentialsProvider;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClientBuilder;
  10. import org.apache.http.util.EntityUtils;
  11. import org.json.simple.JSONArray;
  12. import org.json.simple.JSONObject;
  13. import org.json.simple.parser.JSONParser;
  14.  
  15. import java.io.IOException;
  16. import java.util.UUID;
  17.  
  18. /**
  19. Maven:
  20.  
  21.  <dependencies>
  22.  <dependency>
  23.  <groupId>org.apache.httpcomponents</groupId>
  24.  <artifactId>httpclient</artifactId>
  25.  <version>4.3.4</version>
  26.  </dependency>
  27.  <dependency>
  28.  <groupId>com.googlecode.json-simple</groupId>
  29.  <artifactId>json-simple</artifactId>
  30.  <version>1.1.1</version>
  31.  </dependency>
  32.  </dependencies>
  33.  
  34. conf:
  35.  
  36. rpcuser=user1
  37. rpcpassword=pB341z
  38. rpcallowip=192.168.0.*
  39. rpcport=5557
  40. listen=1
  41. daemon=1
  42. server=1
  43.  
  44.  */
  45. public class CoinSender {
  46.  
  47.     public static void main(String[] args) throws Exception {
  48.         String ip = "192.168.0.41";
  49.         int port = 5557;
  50.         String targetAddress = "SDid48TEECEu7kaGFtH1i3jHEoJkiGXqPk";
  51.         String user = "user1";
  52.         String pass = "pB341z";
  53.  
  54.         RPCClient rpcClient = new RPCClient(ip, port, user, pass);
  55.  
  56.         int total = rpcClient.getBalance().intValue();
  57.         int amount = 5;
  58.         for(; total > 0; total -= amount) {
  59.             JSONObject jsonObject = rpcClient.sendCoins(targetAddress, amount);
  60.             System.out.println(total + ": " + jsonObject);
  61.             Thread.sleep(60000);
  62.         }
  63.         rpcClient.close();
  64.     }
  65.  
  66.     public static class RPCClient {
  67.  
  68.         private final CloseableHttpClient httpclient;
  69.         private String ip;
  70.         private int port;
  71.  
  72.         public RPCClient(String ip, int port, String user, String pass) {
  73.             this.ip = ip;
  74.             this.port = port;
  75.             BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  76.             credentialsProvider.setCredentials(new AuthScope(ip, port), //12789
  77.                     new UsernamePasswordCredentials(user, pass));
  78.             httpclient = HttpClientBuilder.create()
  79.                     .setDefaultCredentialsProvider(credentialsProvider)
  80.                     .build();
  81.         }
  82.  
  83.         public JSONObject invokeRPC(String id, String method, Object ...params) {
  84.             JSONObject requestJSON = new JSONObject();
  85.             requestJSON.put("id", id);
  86.             requestJSON.put("method", method);
  87.             if (null != params) {
  88.                 JSONArray array = new JSONArray();
  89.                 for (Object param : params) {
  90.                     array.add(param);
  91.                 }
  92.                 requestJSON.put("params", array);
  93.             }
  94.             System.out.println(requestJSON);
  95.             JSONObject responseJSON = null;
  96.             try {
  97.                 StringEntity myEntity = new StringEntity(requestJSON.toJSONString());
  98.                 HttpPost httppost = new HttpPost("http://" + ip + ":" + port);
  99.                 httppost.setEntity(myEntity);
  100.  
  101.                 CloseableHttpResponse response = httpclient.execute(httppost);
  102.                 HttpEntity entity = response.getEntity();
  103.  
  104.                 JSONParser parser = new JSONParser();
  105.                 responseJSON = (JSONObject) parser.parse(EntityUtils.toString(entity));
  106.                 response.close();
  107.             } catch (Exception e) {
  108.                 e.printStackTrace();
  109.             }
  110.             return responseJSON;
  111.         }
  112.  
  113.         public void close() throws IOException {
  114.             httpclient.close();
  115.         }
  116.  
  117.         public Double getBalance() {
  118.             JSONObject json = invokeRPC(UUID.randomUUID().toString(), "getbalance");
  119.             System.out.println("DEBUG, getBalance: " + json);
  120.             return (Double)json.get("result");
  121.         }
  122.  
  123.         public JSONObject sendCoins(String to, float amount) {
  124.             return invokeRPC(UUID.randomUUID().toString(), "sendtoaddress", to, amount);
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement