merge: merge changes from bdcontract-common

This commit is contained in:
Frank.R.Wu 2021-12-09 15:57:03 +08:00
parent f59cd212dd
commit 926ef9f437
4 changed files with 82 additions and 92 deletions

View File

@ -1,13 +1,12 @@
package org.bdware.client; package org.bdware.client;
import com.google.gson.Gson; import com.google.gson.JsonElement;
import org.zz.gmhelper.SM2KeyPair; import com.google.gson.JsonPrimitive;
import java.io.*; import java.io.*;
public class ContractRequest extends SM2Verifiable public class ContractRequest extends SM2Verifiable
implements Serializable, Comparable<ContractRequest> { implements Serializable, Comparable<ContractRequest> {
private static final long serialVersionUID = 5516141428163407726L; private static final long serialVersionUID = 5516141428163407726L;
public long hasValue; public long hasValue;
public long gasLimit; public long gasLimit;
@ -25,25 +24,10 @@ public class ContractRequest extends SM2Verifiable
// action = rsaPrivKeyEncoded(aesKey); // action = rsaPrivKeyEncoded(aesKey);
String action; String action;
// arg = aesKeyEncoded({action,arg}); // arg = aesKeyEncoded({action,arg});
String arg; JsonElement arg;
String requestID; String requestID;
boolean fromDebug = false; boolean fromDebug = false;
public static void main(String[] args) {
ContractRequest req = new ContractRequest();
// req.requester = rsa.toBase64Pubkey();
req.contractID = "123";
req.action = "main";
req.arg = "arg1";
String key =
"{\"privateKey\":\"589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8\",\"publicKey\":\"04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd\"}";
SM2KeyPair pair = SM2KeyPair.fromJson(key);
req.doSignature(pair);
System.out.println(req.verifySignature());
System.out.println(new Gson().toJson(req));
System.out.println(req.verifySignature());
}
public static ContractRequest parse(byte[] content) { public static ContractRequest parse(byte[] content) {
try { try {
ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(content)); ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(content));
@ -80,7 +64,9 @@ public class ContractRequest extends SM2Verifiable
} }
public String getRequesterDOI() { public String getRequesterDOI() {
if (requesterDOI == null || requesterDOI.equals("")) return "empty"; if (null == requesterDOI || requesterDOI.isEmpty()) {
return "empty";
}
return requesterDOI; return requesterDOI;
} }
@ -88,15 +74,20 @@ public class ContractRequest extends SM2Verifiable
this.requesterDOI = requesterDOI; this.requesterDOI = requesterDOI;
} }
public String getArg() { public JsonElement getArg() {
return arg; return arg;
} }
public ContractRequest setArg(String arg) { public ContractRequest setArg(String arg) {
return setArg(new JsonPrimitive(arg));
}
public ContractRequest setArg(JsonElement arg) {
this.arg = arg; this.arg = arg;
return this; return this;
} }
public String getAction() { public String getAction() {
return action; return action;
} }
@ -177,3 +168,4 @@ public class ContractRequest extends SM2Verifiable
return Integer.compare(seq, cr.seq); return Integer.compare(seq, cr.seq);
} }
} }

View File

@ -1,7 +1,5 @@
package org.bdware.client; package org.bdware.client;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
@ -12,7 +10,6 @@ import org.zz.gmhelper.SM2Util;
import java.math.BigInteger; import java.math.BigInteger;
public abstract class SM2Verifiable { public abstract class SM2Verifiable {
private static final Logger LOGGER = LogManager.getLogger(SM2Verifiable.class);
// signature is der encoded // signature is der encoded
public String signature; public String signature;
@ -23,9 +20,9 @@ public abstract class SM2Verifiable {
public abstract String getContentStr(); public abstract String getContentStr();
public void doSignature(String privateKey) { public void doSignature(String privateKey) {
BigInteger privkey = new BigInteger(privateKey, 16); BigInteger pKey = new BigInteger(privateKey, 16);
ECPoint p = SM2Util.G_POINT.multiply(privkey).normalize(); ECPoint p = SM2Util.G_POINT.multiply(pKey).normalize();
doSignature(new SM2KeyPair(new ECPublicKeyParameters(p, SM2Util.DOMAIN_PARAMS), privkey)); doSignature(new SM2KeyPair(new ECPublicKeyParameters(p, SM2Util.DOMAIN_PARAMS), pKey));
} }
public void doSignature(SM2KeyPair pair) { public void doSignature(SM2KeyPair pair) {
@ -47,10 +44,6 @@ public abstract class SM2Verifiable {
ECPublicKeyParameters pubKey = ECPublicKeyParameters pubKey =
BCECUtil.createECPublicKeyFromStrParameters( BCECUtil.createECPublicKeyFromStrParameters(
getPublicKey(), SM2Util.CURVE, SM2Util.DOMAIN_PARAMS); getPublicKey(), SM2Util.CURVE, SM2Util.DOMAIN_PARAMS);
LOGGER.info(verifiedStr);
LOGGER.info(getPublicKey());
LOGGER.info(signature);
return SM2Util.verify( return SM2Util.verify(
pubKey, verifiedStr.getBytes(), ByteUtils.fromHexString(signature)); pubKey, verifiedStr.getBytes(), ByteUtils.fromHexString(signature));
} catch (Exception e) { } catch (Exception e) {

View File

@ -223,8 +223,11 @@ public class SmartContractClient extends SmartContractHandler {
req.put("signature", cr.signature); req.put("signature", cr.signature);
req.put("pubkey", cr.getPublicKey()); req.put("pubkey", cr.getPublicKey());
req.put("operation", cr.getAction()); req.put("operation", cr.getAction());
if (cr.getArg() == null) req.put("arg", "undefined"); if (null == cr.getArg()) {
else req.put("arg", cr.getArg()); req.put("arg", "undefined");
} else {
req.put("arg", cr.getArg().toString());
}
return req; return req;
} }

View File

@ -4,10 +4,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.zz.gmhelper.SM2KeyPair; import org.zz.gmhelper.SM2KeyPair;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -30,64 +27,24 @@ public class SmartContractHttpClient {
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
this.keyPair = pair; this.keyPair = pair;
this.isPost = method.toUpperCase().equals("POST"); this.isPost = method.equalsIgnoreCase("POST");
}
public JsonObject executeContract(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
String url = getExecuteUrl(ip, port, cr);
String str = (String) httpGet(url).get("response");
return JsonParser.parseString(str).getAsJsonObject();
}
public String executeContractAsString(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
if (isPost) {
JsonObject jo = new JsonObject();
String url = String.format("http://%s:%d/SCIDE/CMManager", ip, port);
jo.addProperty("action", "executeContract");
jo.addProperty("contractID", cr.getContractID());
jo.addProperty("operation", cr.getAction());
jo.addProperty("arg", cr.getArg());
jo.addProperty("pubkey", cr.getPublicKey());
jo.addProperty("signature", cr.getSignature());
String str = (String) httpPost(url, jo.toString()).get("response");
return str;
} else {
String url = getExecuteUrl(ip, port, cr);
String str = (String) httpGet(url).get("response");
return str;
}
} }
public static String getExecuteUrl(String ip, int port, ContractRequest cr) { public static String getExecuteUrl(String ip, int port, ContractRequest cr) {
try {
return String.format( return String.format(
"http://%s:%d/SCIDE/CMManager?action=executeContract&contractID=%s&operation=%s&arg=%s&pubkey=%s&signature=%s", "http://%s:%d/SCIDE/CMManager?action=executeContract&contractID=%s&operation=%s&arg=%s&pubkey=%s&signature=%s",
ip, ip,
port, port,
cr.getContractID(), cr.getContractID(),
cr.getAction(), cr.getAction(),
URLEncoder.encode(cr.getArg()), URLEncoder.encode(cr.getArg().toString(), "utf-8"),
cr.getPublicKey(), cr.getPublicKey(),
cr.signature); cr.signature);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} }
public String getExecuteUrl(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
String url = getExecuteUrl(ip, port, cr);
return url;
} }
public static Map<String, Object> httpPost(String str, String body) { public static Map<String, Object> httpPost(String str, String body) {
@ -153,4 +110,49 @@ public class SmartContractHttpClient {
} }
return ret; return ret;
} }
public JsonObject executeContract(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
String url = getExecuteUrl(ip, port, cr);
String str = (String) httpGet(url).get("response");
return JsonParser.parseString(str).getAsJsonObject();
}
public String executeContractAsString(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
if (isPost) {
JsonObject jo = new JsonObject();
String url = String.format("http://%s:%d/SCIDE/CMManager", ip, port);
jo.addProperty("action", "executeContract");
jo.addProperty("contractID", cr.getContractID());
jo.addProperty("operation", cr.getAction());
jo.add("arg", cr.getArg());
jo.addProperty("pubkey", cr.getPublicKey());
jo.addProperty("signature", cr.getSignature());
String str = (String) httpPost(url, jo.toString()).get("response");
return str;
} else {
String url = getExecuteUrl(ip, port, cr);
String str = (String) httpGet(url).get("response");
return str;
}
}
public String getExecuteUrl(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
String url = getExecuteUrl(ip, port, cr);
return url;
}
} }