Skip to content

Commit

Permalink
sup ysr
Browse files Browse the repository at this point in the history
  • Loading branch information
pengpengliu committed Apr 27, 2020
1 parent d73306c commit 051a24a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
implementation 'org.json:json:20180813'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.squareup.retrofit2:converter-jackson:2.5.0'
implementation 'com.github.pengpengliu:eos4j:v1.0.2'
implementation 'com.github.pengpengliu:eos4j:v1.0.3'
implementation 'com.github.pengpengliu.ripple-lib-java:ripple-core:v1.0'
implementation 'com.github.stellar:java-stellar-sdk:0.6.0'
implementation 'io.nebulas:nebulas:0.2'
Expand Down
91 changes: 91 additions & 0 deletions core/src/main/java/org/bitcorej/chain/eos/YTAStateProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package org.bitcorej.chain.eos;

import io.eblock.eos4j.Ecc;
import io.eblock.eos4j.api.vo.transaction.push.Tx;
import io.eblock.eos4j.api.vo.transaction.push.TxAction;
import io.eblock.eos4j.api.vo.transaction.push.TxSign;
import io.eblock.eos4j.ese.Action;
import io.eblock.eos4j.ese.DataParam;
import io.eblock.eos4j.ese.DataType;
import io.eblock.eos4j.utils.ByteUtils;
import io.eblock.eos4j.utils.Hex;
import org.bitcorej.chain.KeyPair;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class YTAStateProvider extends EOSStateProvider {
public YTAStateProvider() {
Expand All @@ -14,4 +29,80 @@ public KeyPair generateKeyPair(String secret) {

return new KeyPair(secret, eosKey.getPublicKeyAsHex("YTA"));
}

String parseYSRTransferData(String from, String to, String quantity, String memo) {
DataParam[] datas = new DataParam[] { new DataParam(from, DataType.name, Action.transfer),
new DataParam(to, DataType.name, Action.transfer),
new DataParam(quantity, DataType.asset, Action.transfer),
new DataParam("true", DataType.unit16, Action.transfer),
new DataParam(memo, DataType.string, Action.transfer), };
byte[] allbyte = new byte[] {};
for (DataParam value : datas) {
if (value.getValue().equals("true")) {
allbyte = ByteUtils.concat(allbyte, new byte[] { 1 });
} else {
allbyte = ByteUtils.concat(allbyte, value.seria());
}
}
return Hex.bytesToHexString(allbyte);
}

@Override
public String signRawTransaction(String rawTx, List<String> keys) {
JSONObject jsonObject = new JSONObject(rawTx);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

Tx tx = new Tx();
try {
tx.setExpiration(dateFormatter.parse(jsonObject.getString("expiration")).getTime() / 1000);
} catch (ParseException e) {
e.printStackTrace();
}
tx.setRef_block_num(jsonObject.getLong("ref_block_num"));
tx.setRef_block_prefix(jsonObject.getLong("ref_block_prefix"));
tx.setNet_usage_words(jsonObject.getLong("max_net_usage_words"));
tx.setMax_cpu_usage_ms(jsonObject.getLong("max_cpu_usage_ms"));
tx.setDelay_sec(jsonObject.getLong("delay_sec"));
// actions
List<TxAction> actions = new ArrayList<>();

JSONArray actionsJsonArray = jsonObject.getJSONArray("actions");
for (int i = 0; i < actionsJsonArray.length(); i++) {
JSONObject args = actionsJsonArray.getJSONObject(i).getJSONObject("args");
String code = actionsJsonArray.getJSONObject(i).getString("code");
// data
Map<String, Object> dataMap = new LinkedHashMap<>();
dataMap.put("from", args.getString("from"));
dataMap.put("to", args.getString("to"));
dataMap.put("quantity", new DataParam(args.getString("quantity"), DataType.asset, Action.transfer).getValue());
if (code.equals("ysr.ystar")) {
dataMap.put("bcreate", true);
}
dataMap.put("memo", args.getString("memo"));
// action
TxAction action = new TxAction(args.getString("from"), code, actionsJsonArray.getJSONObject(i).getString("action"), dataMap);
actions.add(action);
}
tx.setActions(actions);
// sign
String sign = Ecc.signTransaction(keys.get(0), new TxSign(this.chainId, tx));
for (int i = 0; i < actionsJsonArray.length(); i++) {
JSONObject args = actionsJsonArray.getJSONObject(i).getJSONObject("args");
String code = actionsJsonArray.getJSONObject(i).getString("code");
// data parse
String data = code.equals("ysr.ystar") ? parseYSRTransferData(args.getString("from"), args.getString("to"), args.getString("quantity"), args.getString("memo")) : Ecc.parseTransferData(args.getString("from"), args.getString("to"), args.getString("quantity"), args.getString("memo"));
// reset data
tx.getActions().get(i).setData(data);
}
// reset expiration
tx.setExpiration(dateFormatter.format(new Date(1000 * Long.parseLong(tx.getExpiration().toString()))));
try {
String packedTx = this.pushTransaction("none", tx, new String[] { sign });
return packedTx;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

0 comments on commit 051a24a

Please sign in to comment.