wzy
2020-06-03 c356816bb9969fc5cde71b670461d9ddf7534e8f
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.xcong.excoin.modules.blackchain.service;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
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.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
 
public class UsdtService {
//    public static void main(String[] args) throws IOException {
//        ECKey key = new ECKey();
//        //logger.info("We created a new key:\n" + key);
//        // TEST 网络
//        NetworkParameters params = MainNetParams.get();
//        Address addressFromKey = key.toAddress(params);
//        System.out.println("Public Address generated: " + addressFromKey);
//        //logger.info("Public Address generated: " + addressFromKey);
//        String privateKey = key.getPrivateKeyEncoded(params).toString();
//        System.out.println("Private key is: " + privateKey);
//        //logger.info("Private key is: " + privateKey);
//        //logger.info("Private Hex key is: " + key.getPrivateKeyAsHex());
//        Wallet wallet = new Wallet(TestNet3Params.get());
//        File walletFile = new File("D//:test.wallet");
//        wallet.importKey(key);
//        wallet.saveToFile(walletFile);
//    }
 
    private static UsdtService 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 UsdtService getInstance() {
        if (service != null) {
            return service;
        }
        return new UsdtService();
    }
 
    private UsdtService() {
    }
 
    /**
     * 创建BTC钱包 每个账户对应一个地址 方便后续操作
     *
     * @param mId 账户
     * @return
     */
    public static Map<String, String> createWallet(String mId) {
        UsdtService 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) {
        try {
            JSONObject json = doRequest("omni_getbalance", address, 31);
            if (!isError(json)) {
                return new BigDecimal(json.getJSONObject(RESULT).get("balance").toString());
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("BTC-USDT查询余额失败");
            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) {
 
        System.out.println(UsdtService.getInstance().getBalance("1PLCr8A2z7YLEtoPLJdzzqpfb3Ym87UCtt"));
    }
}