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 createWallet(String mId) { LtcService 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); //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 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); } }