mirror of
https://gitee.com/BDWare/sdk-java
synced 2025-01-09 17:34:19 +00:00
merge: merge changes from bdcontract-common
This commit is contained in:
parent
f59cd212dd
commit
926ef9f437
@ -1,13 +1,12 @@
|
||||
package org.bdware.client;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class ContractRequest extends SM2Verifiable
|
||||
implements Serializable, Comparable<ContractRequest> {
|
||||
|
||||
private static final long serialVersionUID = 5516141428163407726L;
|
||||
public long hasValue;
|
||||
public long gasLimit;
|
||||
@ -25,25 +24,10 @@ public class ContractRequest extends SM2Verifiable
|
||||
// action = rsaPrivKeyEncoded(aesKey);
|
||||
String action;
|
||||
// arg = aesKeyEncoded({action,arg});
|
||||
String arg;
|
||||
JsonElement arg;
|
||||
String requestID;
|
||||
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) {
|
||||
try {
|
||||
ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(content));
|
||||
@ -80,7 +64,9 @@ public class ContractRequest extends SM2Verifiable
|
||||
}
|
||||
|
||||
public String getRequesterDOI() {
|
||||
if (requesterDOI == null || requesterDOI.equals("")) return "empty";
|
||||
if (null == requesterDOI || requesterDOI.isEmpty()) {
|
||||
return "empty";
|
||||
}
|
||||
return requesterDOI;
|
||||
}
|
||||
|
||||
@ -88,15 +74,20 @@ public class ContractRequest extends SM2Verifiable
|
||||
this.requesterDOI = requesterDOI;
|
||||
}
|
||||
|
||||
public String getArg() {
|
||||
public JsonElement getArg() {
|
||||
return arg;
|
||||
}
|
||||
|
||||
public ContractRequest setArg(String arg) {
|
||||
return setArg(new JsonPrimitive(arg));
|
||||
}
|
||||
|
||||
public ContractRequest setArg(JsonElement arg) {
|
||||
this.arg = arg;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
@ -177,3 +168,4 @@ public class ContractRequest extends SM2Verifiable
|
||||
return Integer.compare(seq, cr.seq);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
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.math.ec.ECPoint;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
@ -12,7 +10,6 @@ import org.zz.gmhelper.SM2Util;
|
||||
import java.math.BigInteger;
|
||||
|
||||
public abstract class SM2Verifiable {
|
||||
private static final Logger LOGGER = LogManager.getLogger(SM2Verifiable.class);
|
||||
// signature is der encoded
|
||||
public String signature;
|
||||
|
||||
@ -23,9 +20,9 @@ public abstract class SM2Verifiable {
|
||||
public abstract String getContentStr();
|
||||
|
||||
public void doSignature(String privateKey) {
|
||||
BigInteger privkey = new BigInteger(privateKey, 16);
|
||||
ECPoint p = SM2Util.G_POINT.multiply(privkey).normalize();
|
||||
doSignature(new SM2KeyPair(new ECPublicKeyParameters(p, SM2Util.DOMAIN_PARAMS), privkey));
|
||||
BigInteger pKey = new BigInteger(privateKey, 16);
|
||||
ECPoint p = SM2Util.G_POINT.multiply(pKey).normalize();
|
||||
doSignature(new SM2KeyPair(new ECPublicKeyParameters(p, SM2Util.DOMAIN_PARAMS), pKey));
|
||||
}
|
||||
|
||||
public void doSignature(SM2KeyPair pair) {
|
||||
@ -47,10 +44,6 @@ public abstract class SM2Verifiable {
|
||||
ECPublicKeyParameters pubKey =
|
||||
BCECUtil.createECPublicKeyFromStrParameters(
|
||||
getPublicKey(), SM2Util.CURVE, SM2Util.DOMAIN_PARAMS);
|
||||
LOGGER.info(verifiedStr);
|
||||
LOGGER.info(getPublicKey());
|
||||
LOGGER.info(signature);
|
||||
|
||||
return SM2Util.verify(
|
||||
pubKey, verifiedStr.getBytes(), ByteUtils.fromHexString(signature));
|
||||
} catch (Exception e) {
|
||||
|
@ -223,8 +223,11 @@ public class SmartContractClient extends SmartContractHandler {
|
||||
req.put("signature", cr.signature);
|
||||
req.put("pubkey", cr.getPublicKey());
|
||||
req.put("operation", cr.getAction());
|
||||
if (cr.getArg() == null) req.put("arg", "undefined");
|
||||
else req.put("arg", cr.getArg());
|
||||
if (null == cr.getArg()) {
|
||||
req.put("arg", "undefined");
|
||||
} else {
|
||||
req.put("arg", cr.getArg().toString());
|
||||
}
|
||||
return req;
|
||||
}
|
||||
|
||||
|
@ -4,10 +4,7 @@ import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintStream;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
@ -30,64 +27,24 @@ public class SmartContractHttpClient {
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
this.keyPair = pair;
|
||||
this.isPost = method.toUpperCase().equals("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;
|
||||
}
|
||||
this.isPost = method.equalsIgnoreCase("POST");
|
||||
}
|
||||
|
||||
public static String getExecuteUrl(String ip, int port, ContractRequest cr) {
|
||||
return String.format(
|
||||
"http://%s:%d/SCIDE/CMManager?action=executeContract&contractID=%s&operation=%s&arg=%s&pubkey=%s&signature=%s",
|
||||
ip,
|
||||
port,
|
||||
cr.getContractID(),
|
||||
cr.getAction(),
|
||||
URLEncoder.encode(cr.getArg()),
|
||||
cr.getPublicKey(),
|
||||
cr.signature);
|
||||
}
|
||||
|
||||
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;
|
||||
try {
|
||||
return String.format(
|
||||
"http://%s:%d/SCIDE/CMManager?action=executeContract&contractID=%s&operation=%s&arg=%s&pubkey=%s&signature=%s",
|
||||
ip,
|
||||
port,
|
||||
cr.getContractID(),
|
||||
cr.getAction(),
|
||||
URLEncoder.encode(cr.getArg().toString(), "utf-8"),
|
||||
cr.getPublicKey(),
|
||||
cr.signature);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Object> httpPost(String str, String body) {
|
||||
@ -153,4 +110,49 @@ public class SmartContractHttpClient {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user