自从在币市亏损之后,下决心要赚回来,想想能不能通过机器方式进行买卖操作呢,还好各大平台都提供了对应的API,故而抽个周末时间,写个简单的程序(仅提供公用的API接口调用,不提供自己的算法,如有任何疑问,可在回答区进行提问,文主尽力帮助解答)
在这里可以获取开发的私钥,公钥:
https://www.jubi.com/api/secret/keys/
这里是官方API,稍微吐槽下,比较烂:
https://www.jubi.com/help/api.html
这个是我完成的项目结构图:
client包里面包含了两个类:
BaseClient.java
作用:提供签名需要的算法
package com.standstill.client;
import com.standstill.util.DevelopUtil;
import com.standstill.util.Log4jUtil;
import com.standstill.util.MD5Util;
import org.bouncycastle.crypto.RuntimeCryptoException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.security.Security;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Created by Standstill on 17-8-19.
*/
public class BaseClient {
/**
* 通过参数进行签名操作
* @param str
* @return
*/
protected static String makeSign(String str){
Log4jUtil.logger.info("加密字符串: " + str);
try {
Security.addProvider(new BouncyCastleProvider());
SecretKey secretKey = new SecretKeySpec(getPrivateMD5().getBytes("UTF8"),
"HmacSHA256");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
byte[] digest = mac.doFinal(str.getBytes("UTF-8"));
String result = new HexBinaryAdapter().marshal(digest).toLowerCase();
Log4jUtil.logger.info("加密结果: " + result);
return result;
} catch (Exception e) {
throw new RuntimeCryptoException("加密异常");
}
}
/**
* 对私钥进行MD5加密
* @return
*/
protected static String getPrivateMD5(){
return MD5Util.MD5(DevelopUtil.privateKey);
}
/**
* 对请求参数进行连接处理,这个地方需要注意,一些接口会有参数顺序的要求
* @param dataMap
* @return
*/
protected static String combineParam(Map<String,String> dataMap){
StringBuilder rst = new StringBuilder();
Set<String> dataSet = dataMap.keySet();
Iterator<String> iterator = dataSet.iterator();
while(iterator.hasNext()){
String key = iterator.next();
if(rst.length() != 0) rst.append("&");
rst.append(key).append("=").append(dataMap.get(key));
}
return rst.toString();
}
/**
* nonce的生成算法,可以使用其他方式
* @return
*/
protected static String generateNonceStr(){
long timeStamp = System.currentTimeMillis();
long twelveTimeStamp = timeStamp % 1000000000000l;
//warning , 此处在 2033年会出现12为noncestr的bug
return String.valueOf(twelveTimeStamp);
}
}
ApiClient.java
作用:提供聚币全套的API操作(涉及到自己的私密信息,就不提供返回的json样式了,需要的可以单独找我要,或者参考官方文档)
package com.standstill.client;
import com.standstill.util.DevelopUtil;
import com.standstill.util.HttpClientUtil;
import com.standstill.util.UrlUtil;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Created by Standstill on 17-8-19.
*/
public class ApiClient extends BaseClient{
/**
* 结果JSON:
*
* @param coin
* @return
*/
public static String receiveOrders(String coin){
Map<String,String> dataMap = new HashMap<String, String>();
dataMap.put("coin" , coin);
return HttpClientUtil.doGet(UrlUtil.GET_COIN_ORDERS, dataMap);
}
/**
* 结果JSON:
*
* @param coin
* @return
*/
public static String receiveDepth(String coin){
Map<String,String> dataMap = new HashMap<String, String>();
dataMap.put("coin" , coin);
return HttpClientUtil.doGet(UrlUtil.GET_COIN_DEPTH, dataMap);
}
/**
* 结果JSON:
*
* @param coin
* @return
*/
public static String receiveDetail(String coin){
Map<String,String> dataMap = new HashMap<String, String>();
dataMap.put("coin" , coin);
return HttpClientUtil.doGet(UrlUtil.GET_COIN_DETAIL , dataMap);
}
/**
* 结果JSON:
*
* @return
*/
public static String receiveAllDetail(){
Map<String,String> dataMap = new HashMap<String, String>();
return HttpClientUtil.doGet(UrlUtil.GET_ALL_COIN_DETAIL , dataMap);
}
/**
* 结果JSON:
*
* @return
*/
public static String receiveAccount(){
Map<String,String> dataMap = new LinkedHashMap<String, String>();
dataMap.put("nonce" ,generateNonceStr());
dataMap.put("key" , DevelopUtil.publicKey);
dataMap.put("signature" , makeSign(combineParam(dataMap)));
return HttpClientUtil.doPost(UrlUtil.GET_ACCOUNT_INFO, dataMap);
}
/**
* 结果JSON:
*
* @return
*/
public static String receiveTradeList(String coin , String type ,String since){
Map<String,String> dataMap = new LinkedHashMap<String, String>();
dataMap.put("nonce" ,generateNonceStr());
dataMap.put("since" ,(since == null ? "0" : since) );
dataMap.put("coin" , coin);
dataMap.put("type" , ("open".equals(type) ? "open" : "all"));
dataMap.put("key" , DevelopUtil.publicKey);
dataMap.put("signature", makeSign(combineParam(dataMap)));
return HttpClientUtil.doPost(UrlUtil.GET_TRADE_LIST, dataMap);
}
/**
* 结果JSON:
*
* @return
*/
public static String receiveTradeView(String coin , String id){
Map<String,String> dataMap = new LinkedHashMap<String, String>();
dataMap.put("nonce" ,generateNonceStr());
dataMap.put("coin" , coin);
dataMap.put("id" , id);
dataMap.put("key" , DevelopUtil.publicKey);
dataMap.put("signature" , makeSign(combineParam(dataMap)));
return HttpClientUtil.doPost(UrlUtil.VIEW_TRADE , dataMap);
}
/**
* 结果JSON:
*
* @return
*/
public static String postTradeCancel(String coin , String id){
Map<String,String> dataMap = new LinkedHashMap<String, String>();
dataMap.put("nonce" ,generateNonceStr());
dataMap.put("coin" , coin);
dataMap.put("id" , id);
dataMap.put("key" , DevelopUtil.publicKey);
dataMap.put("signature" , makeSign(combineParam(dataMap)));
return HttpClientUtil.doPost(UrlUtil.CANCEL_TRADE , dataMap);
}
/**
* 结果JSON:
*
* @return
*/
public static String postTradeAdd(String coin , String amount , String price , String type){
Map<String,String> dataMap = new LinkedHashMap<String, String>();
dataMap.put("nonce" ,generateNonceStr());
dataMap.put("coin" , coin);
dataMap.put("key" , DevelopUtil.publicKey);
dataMap.put("amount" , amount);
dataMap.put("price" , price);
dataMap.put("type" , ("buy".equals(type) ? "buy" : "sell"));
dataMap.put("signature" , makeSign(combineParam(dataMap)));
return HttpClientUtil.doPost(UrlUtil.ADD_TRADE , dataMap);
}
}
util里面主要是工具类
CoinUtil.java
作用:币种英文简称,有些部分被注释了,看官请自行更改
package com.standstill.util;
/**
* Created by Standstill on 17-8-19.
*/
public enum CoinUtil {
/*
需要过滤掉btm + hcc ,这两个手动去操作
过滤掉cny
*/
btc,/*drk,*/blk,vrc,tfc,jbc,ltc,doge,xpm,ppc,wdc,vtc,max,ifc,zcc,zet,eac,fz,
skt,plc,mtc,qec,lkc,met,ytc,hlb,game,rss,rio,ktc,pgc,mryc,mcc,eth,etc,/*dnc,*/
gooc,xrp,nxt,lsk,xas,peb/*,nhgh*/,xsgs,ans,bts,ugt,eos,tic,ico,bcc,btk,qtum,
elc,/*hcc,*//*btm,*/act/*,cny*/
}
DevelopUtil.java
作用:开发所用的信息,包括publicKey & privateKey,这里就不贴我的了。
ErrorUtil.java
作用:官方提供的错误信息
package com.standstill.util;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Standstill on 17-8-20.
*/
public class ErrorUtil {
private static Map<String , String> errMap;
static{
errMap = new HashMap<String, String>();
errMap.put("100" , "必选参数不能为空");
errMap.put("101" , "非法参数");
errMap.put("102" , "请求的虚拟币不存在");
errMap.put("103" , "密钥不存在");
errMap.put("104" , "签名不匹配");
errMap.put("105" , "权限不足");
errMap.put("106" , "请求过期(nonce错误)");
errMap.put("200" , "余额不足");
errMap.put("201" , "买卖的数量小于最小买卖额度");
errMap.put("202" , "下单价格必须在 0 - 1000000 之间");
errMap.put("204" , "成交金额不能少于 10 元");
errMap.put("205" , "gooc限制挂单价格");
errMap.put("203" , "订单不存在");
errMap.put("401" , "系统错误");
errMap.put("402" , "请求过于频繁");
errMap.put("403" , "非开放API");
errMap.put("404" , "IP限制不能请求该资源");
errMap.put("405" , "币种交易暂时关闭");
}
public static String getErrMsg(String code){
return errMap.get(code);
}
}
HttpClientUtil.java
作用:https请求,主要用上doGet() + doPost() 方法,代码比较长,就不贴了,有需要的请单独联系我。
Log4jUtil.java
作用:Log信息
package com.standstill.util;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
* Created by Standstill on 17-8-20.
*/
public class Log4jUtil {
public static Logger logger = Logger.getRootLogger();
public Log4jUtil(){
PropertyConfigurator.configure("classpath:log4j.properties");
}
}
以及log4j.properties文件(全是摘抄来的,手动捂脸)
### 设置###
log4j.rootLogger = debug,stdout,E,I
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出INFO 级别以上的日志到=E://logs/info.log ###
log4j.appender.I = org.apache.log4j.DailyRollingFileAppender
log4j.appender.I.File = log/info.log
log4j.appender.I.Append = true
log4j.appender.I.Threshold = INFO
log4j.appender.I.layout = org.apache.log4j.PatternLayout
log4j.appender.I.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =log/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
MD5Util.java
作用:privateKey的MD5加密,网上一大堆,注意,需要小写
package com.standstill.util;
import java.security.MessageDigest;
public class MD5Util {
public final static String MD5(String s) {
char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
try {
byte[] btInput = s.getBytes();
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
ResultUtil.java
作用:处理请求结果,主要是处理请求错误的情况
package com.standstill.util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
/**
* Created by Standstill on 17-8-20.
*/
public class ResultUtil {
public static Object parse(String url , String result){
if(StringUtils.isEmpty(result)){
Log4jUtil.logger.error("API[" + url + "]请求结果为空!");
return null;
}
if(result.trim().startsWith("[")){
return JSONArray.fromObject(result);
}
JSONObject object;
try{
object = JSONObject.fromObject(result);
}catch (Exception e){
e.printStackTrace();
Log4jUtil.logger.error("API[" + url + "]请求结果解析错误!");
return null;
}
if(object.containsKey("result") && !object.getBoolean("result")){
Log4jUtil.logger.error("API[" + url + "]请求错误,原因:" + ErrorUtil.getErrMsg(object.getString("code")));
return null;
}else{
return object;
}
}
}
SSLClient.java
作用:主要是配合HttpClientUtil使用的,需要的跟我说
UrlUtil.java
作用:API请求地址
package com.standstill.util;
/**
* Created by dearest on 2017/8/18.
*/
public class UrlUtil {
public static String JUBI_HOST = "https://www.jubi.com/";
public static String GET_COIN_DETAIL = JUBI_HOST + "/api/v1/ticker/";
public static String GET_ALL_COIN_DETAIL = JUBI_HOST + "/api/v1/allticker/";
public static String GET_COIN_DEPTH = JUBI_HOST + "/api/v1/depth/";
public static String GET_COIN_ORDERS = JUBI_HOST + "/api/v1/orders/";
public static String VIEW_TRADE = JUBI_HOST + "/api/v1/trade_view/";
public static String GET_ACCOUNT_INFO = JUBI_HOST + "/api/v1/balance/";
public static String GET_TRADE_LIST = JUBI_HOST + "/api/v1/trade_list/";
public static String CANCEL_TRADE = JUBI_HOST + "/api/v1/trade_cancel/";
public static String ADD_TRADE = JUBI_HOST + "/api/v1/trade_add/";
}
额,贴完代码不知道说什么了,希望大家关注下吧,虚心接受大家的意见和建议,以及尽力帮助大家解决开发中的问题吧,烂尾的感觉,哈哈。
最近比较喜欢Coldplay的《Yellow》,送给大家: