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 createWallet(String mId) { BtcService btcService = getInstance(); // 创建钱包地址 String address = btcService.getAddress(mId); // 私钥 String privateKey = btcService.dumpprivkey(address); Map result = new HashMap(); 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 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")); } }