diff --git a/src/main/java/org/bdware/server/action/EventWSActions.java b/src/main/java/org/bdware/server/action/EventWSActions.java index 5737b52..dc7a4c1 100644 --- a/src/main/java/org/bdware/server/action/EventWSActions.java +++ b/src/main/java/org/bdware/server/action/EventWSActions.java @@ -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); + } }