feat: add EventWSActions.pubEvent

add EventWSActions.pubEvent to allow clients to publish events, and return generated key pair if the client doesn't signature the event
This commit is contained in:
Frank.R.Wu 2021-12-29 20:26:21 +08:00
parent 9f30c938da
commit bc8f539c9c

View File

@ -3,7 +3,13 @@ package org.bdware.server.action;
import com.google.gson.JsonObject;
import org.bdware.sc.ContractClient;
import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.event.REvent;
import org.bdware.sc.util.HashUtil;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;
import static org.bdware.sc.event.REvent.REventSemantics.valueOf;
import static org.bdware.sc.event.REvent.REventType.PUBLISH;
public class EventWSActions {
@Action(async = true, userPermission = 0)
@ -25,7 +31,7 @@ public class EventWSActions {
ContractClient client = CMActions.manager.getClient(argCID);
if (null == client) {
ret.addProperty("data", "invalid contract ID or Name!");
rcb.onResult(ret.toString());
rcb.onResult(ret);
return;
}
String contractID = client.getContractID();
@ -36,4 +42,40 @@ public class EventWSActions {
ret.addProperty("data", topic);
rcb.onResult(ret);
}
@Action(async = true, userPermission = 0)
public void pubEvent(JsonObject args, final ResultCallback rcb) {
JsonObject ret = new JsonObject();
ret.addProperty("action", "onPubEvent");
if (!args.has("topic") || !args.has("content")) {
ret.addProperty("data", "topic or content are missed!");
rcb.onResult(ret);
return;
}
String requestID = args.has("requestID") ?
args.get("requestID").getAsString() :
HashUtil.sha3(String.valueOf(System.currentTimeMillis()), "_", String.valueOf(Math.random()));
REvent event = new REvent(args.get("topic").getAsString(),
PUBLISH,
args.get("content").getAsString(),
requestID);
if (args.has("semantics")) {
try {
event.setSemantics(valueOf(args.get("semantics").getAsString()));
} catch (Exception ignored) {
ret.addProperty("data", "unsupported semantics!");
rcb.onResult(ret);
return;
}
}
if (args.has("pubkey") && args.has("sign")) {
event.setPublicKey(args.get("pubkey").getAsString());
event.setSignature(args.get("sign").getAsString());
} else {
SM2KeyPair keyPair = SM2Util.generateSM2KeyPair();
event.doSignature(keyPair);
ret.addProperty("keys", keyPair.toJson());
}
rcb.onResult(ret);
}
}