zainali5120
2021-03-14 e9a3948e32f767f8147b5cd98aa85f21abc215f1
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
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 LtcService {
 
    private static LtcService 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_WALLET_PASSPHRASE = "walletpassphrase";
    private final static String METHOD_WALLET_LOCK = "walletlock";
    private final static String METHOD_GET_BALANCE_ADDRESS = "getreceivedbyaddress";
 
    private String url = "http://121.40.86.163:1888";
    private String username = "biyiltc";
    private String password = "biyiltc";
 
    public static LtcService getInstance() {
        if (service != null) {
            return service;
        }
        return new LtcService();
    }
 
    private LtcService() {
    }
 
    /**
     * 创建BTC钱包 每个账户对应一个地址 方便后续操作
     * @param mId 账户
     * @return
     */
    public static Map<String, String> createWallet(String mId) {
        LtcService 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);
            //JSONObject json = doRequest(METHOD_NEW_ADDRESS);
            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 static void main(String[] args) {
        BigDecimal s = LtcService.getInstance().getBalance("MS6UBteTkQYbbBg5xEPWUQ1PPG7zkxY368");
       System.out.println(s);
    }
}