public class EosUtil {
//private Logger logger = Logger.getLogger(this.getClass());
private final static String url = “ip”;
private final static String from=“hpnettest111”;
private final static String to=“hpnettest222”;
private final static String quantity=“0.0001 TOK”;
private final static String memo="";
private final static String privatekey=“5JAHWaYwgzRSiJrwct7pY8m1c6Q22Vsh31pd4yD2Q9ufWNtjme3”;
private final static String transfer=“transfer”;
private final static String eosiotoken=“eosio.token”;
private final static String tok=“TOK”;
private final static SimpleDateFormat simpleDate = new SimpleDateFormat(“YYYY-MM-dd HH:MM:SS”);
public static Rpc UrlRpc() {
Rpc rpc = new Rpc(url);
return rpc;
}
/**
* 交易请求
* @param from 转出账户
* @param to 转入账户
* @param quantity 交易金额(必须保留小数点后4位数,TOK,空格不可省略:0.0001 TOK)
* @param memo 交易备注
* @param privatekey 签名账户私钥
* @param transfer 合约方法
* @param eosiotoken 合约名
* @return
*/
public String act(String from,String to,String quantity,String memo,String privatekey,String transfer,String eosiotoken) {
Rpc urlRpc = UrlRpc();
// 发起交易
Map<String, Object> args = new HashMap<String, Object>();
args.put("from", from);// 转出账户
args.put("to", to);// 转入账户
args.put("quantity", "0.0001 CFS");// 交易金额
args.put("memo", memo);// 交易备注
String res = urlRpc.pushTransaction(eosiotoken, // 合约名 eosio.token
transfer, // 合约方法 transfer
from, // 签名账户
privatekey, // 签名账户私钥
args);
//logger.debug(“交易结果:”+res);
HashMap<String,String> specMap = JSON.parseObject(res , HashMap.class);
String message = specMap.get(“message”);
//logger.debug(“操作结果:” + message);
return message;
}
/**
* 查询账户信息
* @param account 账户名
*/
private void QueryAccount(String account) {
Rpc urlRpc = UrlRpc();
String accountmessage = urlRpc.getAccount(account);
//logger.debug(“操作结果:” + accountmessage);
System.err.println(accountmessage);
}
/**
* 查询账户特定币种余额
* @param account 账户名
* @param eosiotoken 目标币合约
* @param tok 目标币名称
* @return
*/
private String QuerygetCurrencyBalance(String account,String eosiotoken,String tok) {
Rpc urlRpc = UrlRpc();
String currencyBalance = urlRpc.getCurrencyBalance(account, eosiotoken, tok);
HashMap<String,String> specMap = JSON.parseObject(currencyBalance , HashMap.class);
String data = specMap.get("data");
String substring = data.substring(1, data.length()-1);
String replaceAll = substring.replaceAll("\"", " ");
//logger.debug(“操作结果:” + currencyBalance);
return replaceAll;
}
/**
* 查询交易记录(Http请求查询)
* @param account 查询账户名
* @param pos 偏移量
* @param offset 起始位置
* @return pos,offset为空则查询所有交易记录
* 转账方每次转账操作会通知合约方,对手方,自己方,对于支出方每次支出都会产生3条交易记录
*/
public List<String> getActionsHttp(String account,int pos,int offset) {
Map<String, String> map=new HashMap<>();
map.put("account_name", account);
map.put("pos", String.valueOf(pos));
map.put("offset", String.valueOf(offset));
List<String> list=new ArrayList<>();
try {
String eoSpost = EosHttpUtils.EOSpost(url+"/v1/history/get_actions", map);
List<Map<String, Object>> toListMap = ToListMap(eoSpost);
for (Map<String, Object> map2 : toListMap) {
String from = map2.get("from").toString();
String to = map2.get("to").toString();
String quantity = map2.get("quantity").toString();
String memo = map2.get("memo").toString();
String rember="from:"+from+",to:"+to+",quantity:"+quantity+",memo:"+memo;
list.add(rember);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 查询交易记录
* @param account 查询账户名
* @param pos 偏移量
* @param offset 起始位置
* @return pos,offset为空则查询所有交易记录
* 转账方每次转账操作会通知合约方,对手方,自己方,对于支出方每次支出都会产生3条交易记录
*/
public List<String> getActionsJar(String account,int pos,int offset) {
Rpc urlRpc = UrlRpc();
String actions = urlRpc.getActions(account, pos, offset);
List<Map<String, Object>> toListMap = ToListMapJar(actions);
List<String> list=new ArrayList<>();
for (Map<String, Object> map2 : toListMap) {
String from = map2.get("from").toString();
String to = map2.get("to").toString();
String quantity = map2.get("quantity").toString();
String memo = map2.get("memo").toString();
String rember="from:"+from+",to:"+to+",quantity:"+quantity+",memo:"+memo;
list.add(rember);
}
return list;
}
/**
* 根据公钥获取账户名
* @param result
* @return
*/
public String getNameByePKey(String publickey) {
Rpc urlRpc = UrlRpc();
try {
String keyAccounts = urlRpc.getKeyAccounts(publickey);
//logger.debug(“操作结果:” + keyAccounts);
JSONObject parseObject = JSON.parseObject(keyAccounts);
String string = parseObject.getString(“data”);
JSONObject parseObjectdata = JSON.parseObject(string);
String Name = parseObjectdata.getString(“account_names”);
String substring = Name.substring(1, Name.length()-1);
String replaceAll = substring.replaceAll(""", " ");
return replaceAll;
} catch (Exception e) {
throw new IllegalArgumentException(“请检查公钥的合法性”);
}
}
/**
* 创建账户
* @param accountName 想创建的账户名(账户名必须是12位,并且只包含a-z1-5,低于12位的一般是特殊账户)
*/
public void createAccount(String accountName) {
Map<String, String> key = getKey();
String publicKey = key.get("publicKey");
Map<String, String> map=new HashMap<>();
map.put("accountName", accountName);
map.put("pk", publicKey);
try {
String eoSpost = EosHttpUtils.EOSpost("http://59.110.220.75"+"/eosSak/CreateSysAccount", map);
//logger.debug(“操作结果:” + eoSpost);
} catch (Exception e) {
throw new IllegalArgumentException(“请检查公钥的合法性,账户名必须是12位,并且只包含a-z1-5”);
}
}
/**
* 随笔生成密钥对
* @return
*/
public Map<String, String> getKey(){
IdWorker idWorker=new IdWorker();
long nextId = idWorker.nextId();
String privateKey = Ecc.seedPrivate(String.valueOf(nextId));
String publicKey = Ecc.getPublicKey(privateKey);
Map<String,String> map=new HashMap<>();
map.put("publicKey", publicKey);
map.put("privateKey", privateKey);
return map;
}
/**
* 去重
* @param list
* @return
*/
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
public static List<Map<String, Object>> ToListMap(String result) {
JSONObject parseObject = JSON.parseObject(result);
String string = parseObject.getString("actions");
JSONArray parseArray = JSON.parseArray(string);
List<Map<String, Object>> mList=new ArrayList<>();
for (Object object : parseArray) {
JSONObject parseObject2 = JSON.parseObject(object.toString());
String picPath = parseObject2.getString("action_trace");
String block_time = parseObject2.getString("block_time");
JSONObject parseObject3 = JSON.parseObject(picPath.toString());
String picPath2 = parseObject3.getString("act");
JSONObject parsereceipt = JSON.parseObject(picPath.toString());
String picPathreceipt = parseObject3.getString("receipt");
JSONObject parsereceipt2 = JSON.parseObject(picPathreceipt.toString());
String receiver = parsereceipt2.getString("receiver");
JSONObject parseObject4 = JSON.parseObject(picPath2.toString());
String picPath3 = parseObject4.getString("data");
Map map=new HashMap<String,Object>();
JSONObject JSONquantity = JSON.parseObject(picPath3.toString());
String quantity = JSONquantity.getString("quantity");
String from = JSONquantity.getString("from");
String to = JSONquantity.getString("to");
String memo = JSONquantity.getString("memo");
map.put("quantity", quantity);
map.put("from", from);
map.put("to", to);
map.put("memo", memo);
//map.put(“block_time”, block_time);
//map.put(“receiver”, receiver);
mList.add(map);
}
List removeDuplicate = removeDuplicate(mList);
return removeDuplicate;
}
public static List<Map<String, Object>> ToListMapJar(String result) {
JSONObject parseObjectdata = JSON.parseObject(result);
String stringdata = parseObjectdata.getString("data");
JSONObject parseObject = JSON.parseObject(stringdata);
String string = parseObject.getString("actions");
JSONArray parseArray = JSON.parseArray(string);
List<Map<String, Object>> mList=new ArrayList<>();
for (Object object : parseArray) {
JSONObject parseObject2 = JSON.parseObject(object.toString());
String picPath = parseObject2.getString("action_trace");
String block_time = parseObject2.getString("block_time");
JSONObject parseObject3 = JSON.parseObject(picPath.toString());
String picPath2 = parseObject3.getString("act");
JSONObject parsereceipt = JSON.parseObject(picPath.toString());
String picPathreceipt = parseObject3.getString("receipt");
JSONObject parsereceipt2 = JSON.parseObject(picPathreceipt.toString());
String receiver = parsereceipt2.getString("receiver");
JSONObject parseObject4 = JSON.parseObject(picPath2.toString());
String picPath3 = parseObject4.getString("data");
Map map=new HashMap<String,Object>();
JSONObject JSONquantity = JSON.parseObject(picPath3.toString());
String quantity = JSONquantity.getString("quantity");
String from = JSONquantity.getString("from");
String to = JSONquantity.getString("to");
String memo = JSONquantity.getString("memo");
map.put("quantity", quantity);
map.put("from", from);
map.put("to", to);
map.put("memo", memo);
//map.put(“block_time”, block_time);
//map.put(“receiver”, receiver);
mList.add(map);
}
List removeDuplicate = removeDuplicate(mList);
return removeDuplicate;
}
/**
* 随机生成EOS账户名(钱包地址)
* @return
*/
public static String regex() {
String regex = “[a-z1-5]{12}”;
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);
return result;
}
public static void main(String[] args) throws ParseException {
for (int i = 0; i < 25; i++) {
String regex = regex();
System.err.println(regex);
}
EosUtil eos=new EosUtil();
//交易请求
//String act =eos.act(eos.from, “xyyxyyxyy123”, “0.1000 TOK”, “交易”, eos.privatekey, eos.transfer, eos.eosiotoken);
//System.out.println(act);
//查询交易记录
List actionsJar = eos.getActionsJar(“hpnettest222”,-1,-100);
for (String string : actionsJar) {
System.err.println(“交易记录:”+string);
}
//查询剩余金额
String querygetCurrencyBalance = eos.QuerygetCurrencyBalance(“xyyxyyxyy123”, eos.eosiotoken, eos.tok);
System.err.println(“剩余金额:”+querygetCurrencyBalance);
}
}