org.bdware.ypkdeploy.SmartContractClientExt Maven / Gradle / Ivy
The newest version!
package org.bdware.ypkdeploy;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.client.ResultCallback;
import org.bdware.client.SmartContractClient;
import org.bdware.client.ws.Action;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
public class SmartContractClientExt extends SmartContractClient {
private static final Logger LOGGER = LogManager.getLogger(SmartContractClientExt.class);
public int count = 0;
public boolean isLoggedIn = false;
public String nodePubkey = null;
SecureRandom secureRandom = new SecureRandom();
public SmartContractClientExt(String uri, SM2KeyPair pair) {
super(uri, pair);
}
public void startContract(String project) {
startContract(project, null, 0, null);
}
public void startContract(String project, JsonElement createParam) {
startContract(project, createParam, 0, null);
}
public void startContractByYpk(String path, boolean isPrivate, JsonElement createParam,
ResultCallback rc) {
String reqID = System.currentTimeMillis() + "";
Map ret = new HashMap<>();
ret.put("action", "startContractByYPK");
ret.put("isPrivate", isPrivate);
ret.put("owner", getKeyPair().getPublicKeyStr());
ret.put("requestID", reqID);
ret.put("createParam", createParam);
ret.put("path", path);
String content =
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
String sig;
try {
sig = ByteUtils.toHexString(
SM2Util.sign(getKeyPair().getPrivateKeyParameter(), content.getBytes()));
ret.put("signature", sig);
} catch (CryptoException e) {
e.printStackTrace();
}
if (rc != null) {
cbs.put(reqID, rc);
}
this.sendMsg(new Gson().toJson(ret));
}
public void startPublicContract(String project, JsonElement createParam, ResultCallback rc) {
String reqID = System.currentTimeMillis() + "";
Map ret = new HashMap<>();
ret.put("action", "startContract");
ret.put("isPrivate", false);
ret.put("owner", getKeyPair().getPublicKeyStr());
ret.put("requestID", reqID);
ret.put("path", "/" + project + "/manifest.json");
ret.put("createParam", createParam);
String content =
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
String sig;
try {
sig = ByteUtils.toHexString(
SM2Util.sign(getKeyPair().getPrivateKeyParameter(), content.getBytes()));
ret.put("signature", sig);
} catch (CryptoException e) {
e.printStackTrace();
}
ret.put("script", "empty");
if (rc != null) {
cbs.put(reqID, rc);
}
this.sendMsg(new Gson().toJson(ret));
}
public void startContractAsDebug(String project, JsonElement createParam, int remoteDebugPort,
ResultCallback rc) {
String reqID = System.currentTimeMillis() + "";
Map ret = new HashMap<>();
ret.put("action", "startContractAsDebug");
ret.put("isPrivate", true);
ret.put("owner", getKeyPair().getPublicKeyStr());
ret.put("requestID", reqID);
ret.put("path", "/" + project + "/manifest.json");
ret.put("createParam", createParam);
if (remoteDebugPort != 0)
ret.put("remoteDebugPort", remoteDebugPort);
String content =
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
String sig;
try {
sig = ByteUtils.toHexString(
SM2Util.sign(getKeyPair().getPrivateKeyParameter(), content.getBytes()));
ret.put("signature", sig);
} catch (CryptoException e) {
e.printStackTrace();
}
ret.put("script", "empty");
if (rc != null) {
cbs.put(reqID, rc);
}
this.sendMsg(new Gson().toJson(ret));
}
public void startContract(String project, JsonElement createParam, int remoteDebugPort,
ResultCallback rc) {
String reqID = System.currentTimeMillis() + "";
Map ret = new HashMap<>();
ret.put("action", "startContract");
ret.put("isPrivate", true);
ret.put("owner", getKeyPair().getPublicKeyStr());
ret.put("requestID", reqID);
ret.put("path", "/" + project + "/manifest.json");
ret.put("createParam", createParam);
if (remoteDebugPort != 0)
ret.put("remoteDebugPort", remoteDebugPort);
String content =
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
String sig;
try {
sig = ByteUtils.toHexString(
SM2Util.sign(getKeyPair().getPrivateKeyParameter(), content.getBytes()));
ret.put("signature", sig);
} catch (CryptoException e) {
e.printStackTrace();
}
ret.put("script", "empty");
if (rc != null) {
cbs.put(reqID, rc);
}
this.sendMsg(new Gson().toJson(ret));
}
@Override
public void onLogin(JsonObject obj) {
// LOGGER.info(obj.toString());
isLoggedIn = true;
}
public void startMultiContractByScript(String script, String peersID, JsonElement createParam,
ResultCallback rc) {
String reqID = System.currentTimeMillis() + "" + secureRandom.nextInt();
Map ret = new HashMap<>();
ret.put("action", "startContractMultiPoint");
ret.put("script", script);
ret.put("type", 5);
ret.put("peersID", peersID);
ret.put("owner", getKeyPair().getPublicKeyStr());
ret.put("requestID", reqID);
ret.put("createParam", createParam);
// "Algorithm|" + request.script + "|" + global.sm2Key.publicKey
ret.put("signature", doSignature(getKeyPair().getPrivateKeyParameter(),
"Sole|" + script + "|" + ret.get("owner")));
if (rc != null) {
cbs.put(reqID, rc);
}
String msg = new Gson().toJson(ret);
LOGGER.debug(msg);
this.sendMsg(msg);
}
public String doSignature(ECPrivateKeyParameters privateKey, String content) {
try {
return ByteUtils.toHexString(SM2Util.sign(privateKey, content.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static class ContractExecType {
public static final int Sole = 0; // 单点锚定合约
public static final int RequestOnce = 1;// 多点不同步合约
public static final int ResponseOnce = 2; // 多点不同步合约,超时的时候再请求一个, 直到获得一个response
public static final int RequestAllResponseFirst = 3;
public static final int RequestAllResponseHalf = 4;
public static final int RequestAllResponseAll = 5;
public static final int Sharding = 6;
public static final int SelfAdaptiveSharding = 7;
public static final int PBFT = 8;
}
public void startMultiContract(boolean isPrivate, String ypkName, String peersID,
int contractExecType, JsonElement createParam, ResultCallback rc) {
String reqID = System.currentTimeMillis() + "" + secureRandom.nextInt();
Map ret = new HashMap<>();
ret.put("action", "startContractMultiPoint");
ret.put("projectName", ypkName);
ret.put("isPrivate", isPrivate);
ret.put("type", contractExecType);
ret.put("peersID", peersID);
ret.put("owner", getKeyPair().getPublicKeyStr());
ret.put("requestID", reqID);
ret.put("createParam", createParam);
// "Algorithm|" + request.script + "|" + global.sm2Key.publicKey
if (rc != null) {
cbs.put(reqID, rc);
}
String msg = new Gson().toJson(ret);
LOGGER.debug(msg);
this.sendMsg(msg);
}
public void startContractByScript(String script, ResultCallback rc) {
String reqID = System.currentTimeMillis() + "" + Math.random();
Map ret = new HashMap<>();
ret.put("action", "startContract");
ret.put("script", script);
ret.put("owner", getKeyPair().getPublicKeyStr());
ret.put("requestID", reqID);
// "Algorithm|" + request.script + "|" + global.sm2Key.publicKey
ret.put("signature", doSignature(getKeyPair().getPrivateKeyParameter(),
"Algorithm|" + script + "|" + ret.get("owner")));
if (rc != null) {
cbs.put(reqID, rc);
}
String msg = new Gson().toJson(ret);
LOGGER.debug(msg);
this.sendMsg(msg);
}
public void kill(String name, ResultCallback rc) {
Map ret = new HashMap<>();
ret.put("action", "killContractProcess");
ret.put("verifiedPubKey", getKeyPair().getPublicKeyStr());
ret.put("name", name);
String requestID = "kill_" + System.currentTimeMillis();
ret.put("requestID", requestID);
if (rc != null) {
cbs.put(requestID, rc);
}
this.sendMsg(new Gson().toJson(ret));
}
public void killAllContractProcess() {
Map ret = new HashMap<>();
ret.put("action", "killAllContract");
this.sendMsg(new Gson().toJson(ret));
}
// public void onExecuteResult(JsonObject obj) {
// }
public void genBDCoin(String name) {
Map ret = new HashMap<>();
ret.put("action", "generateBDCoinProject");
ret.put("ContractName", name);
ret.put("name", name);
}
public void ping() {
this.sendMsg("{\"action\":\"ping\"}");
}
@Action
public void pong(JsonObject obj) {
count++;
}
@Action
public void onStartContractTrustfullyResult(JsonObject jo) {
String responseID = jo.get("responseID").getAsString();
ResultCallback cb = cbs.get(responseID);
if (cb != null)
cb.onResult(jo);
}
@Action
public void onStartContractAsDebug(JsonObject obj) {
invokeCallback(obj);
}
public void listProjects(boolean isPrivate, ResultCallback resultCallback) {
JsonObject request = new JsonObject();
request.addProperty("action", "listProjects");
request.addProperty("isPrivate", isPrivate);
String requestID = System.currentTimeMillis() + "";
request.addProperty("requestID", requestID);
cbs.put(requestID, resultCallback);
sendMsg(request.toString());
}
@Action
public void onListProjects(JsonObject jo) {
String responseID = jo.get("responseID").getAsString();
ResultCallback cb = cbs.get(responseID);
if (cb != null)
cb.onResult(jo);
}
@Action
public void onKillContractProcess(JsonObject jo) {
if (!jo.has("responseID")) {
LOGGER.error("can't get responseID:" + jo.toString());
return;
}
String responseID = jo.get("responseID").getAsString();
ResultCallback cb = cbs.get(responseID);
if (cb != null)
cb.onResult(jo);
}
public void requestNodeInfo() {
this.sendMsg("{\"action\":\"loadNodeConfig\"}");
}
@Action
public void onLoadNodeConfig(JsonObject obj) {
JsonObject data = obj.get("data").getAsJsonObject();
nodePubkey = data.get("nodePubKey").getAsString();
}
public void loginSync() {
login();
for (; !isLoggedIn;)
Thread.yield();
}
@Action
public void onQueryAEState(JsonObject obj) {
System.out.println("[AEState] " + obj.toString());
}
@Action
public void onQueryAECaller(JsonObject obj) {
System.out.println("[AECaller] " + obj.toString());
}
public void sendMsgTricky(String msg) {
this.sendMsg(msg);
}
}