xiaoyong931011
2020-05-31 9c8711046b00898717aa7a3b9f6dfee974d3f427
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.xcong.excoin.modules.blackchain.service;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
 
public class BtcService {
 
    
    private static BtcService service = null;
    private final static String RESULT = "result";
    private final static String METHOD_SEND_TO_ADDRESS = "sendtoaddress";
    private final static String METHOD_GET_TRANSACTION = "gettransaction";
    private final static String METHOD_LIST_TRANSACTIONS = "listtransactions";
    private final static String METHOD_GET_BLOCK_COUNT = "getblockcount";
    private final static String METHOD_NEW_ADDRESS = "getnewaddress";
    private final static String METHOD_GET_BALANCE = "getbalance";
    private final static String METHOD_GET_BALANCE_ADDRESS = "getreceivedbyaddress";
    private final static String METHOD_WALLET_PASSPHRASE = "walletpassphrase";
    private final static String METHOD_WALLET_LOCK = "walletlock";
 
    private String url = "http://120.55.86.146:1880";
    private String username = "biyi";
    private String password = "biyi1234";
 
    public static BtcService getInstance() {
        if (service != null) {
            return service;
        }
        return new BtcService();
    }
 
    private BtcService() {
    }
 
    /**
     * 创建BTC钱包 每个账户对应一个地址 方便后续操作
     * @param mId 账户
     * @return
     */
    public static Map<String, String> createWallet(String mId) {
        BtcService btcService = getInstance();
        // 创建钱包地址
        String address = btcService.getAddress(mId);
        // 私钥
        String privateKey = btcService.dumpprivkey(address);
        Map<String, String> result = new HashMap<String,String>();
        result.put("address", address);
        result.put("privateKey", privateKey);
        return result;
    }
    public String getAddress(String label) {
        try {
            JSONObject json = doRequest(METHOD_NEW_ADDRESS,label);
            if (isError(json)) {
                return "";
            }
            return json.getString(RESULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    private JSONObject doRequest(String method, Object... params) {
        JSONObject param = new JSONObject();
        param.put("id", System.currentTimeMillis() + "");
        param.put("jsonrpc", "2.0");
        param.put("method", method);
        if (params != null) {
            param.put("params", params);
        }
        String creb = Base64.encodeBase64String((username + ":" + password).getBytes());
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", "Basic " + creb);
        return JSON.parseObject(HttpUtil.jsonPost(url, headers, param.toJSONString()));
    }
 
    private boolean isError(JSONObject json) {
        if (json == null || (StringUtils.isNotEmpty(json.getString("error")) && json.get("error") != "null")) {
            return true;
        }
        return false;
    }
    
    public String dumpprivkey(String address) {
        try {
            JSONObject obj = doRequest("dumpprivkey", address);
            System.out.println(obj);
            if (!isError(obj)) {
                return obj.getString(RESULT);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 查询给定地址的总收款额(只计算接受的)
     * @param address
     * @return
     */
    public BigDecimal getBalance(String address) {
        JSONObject json = doRequest(METHOD_GET_BALANCE_ADDRESS,address,3);
        if (!isError(json)) {
            return new BigDecimal(json.getString(RESULT));
        } else {
            return null;
        }
    }
    
    public String METHOD_GET_BLOCK_COUNT() {
        JSONObject json = doRequest(METHOD_GET_BLOCK_COUNT);
        if (!isError(json)) {
            return json.getString(RESULT);
        } else {
            return null;
        }
    }
    public static void main(String[] args) {
        BtcService ser = new BtcService();
        //ser.getBalance("36RiLqCrnFfBoyUUHpGvEotXiMRtrJG8dp");
        //System.out.println(ser.METHOD_GET_BLOCK_COUNT());
       System.out.println(ser.getBalance("36RiLqCrnFfBoyUUHpGvEotXiMRtrJG8dp"));
    }
    
 
}