From 4b968996537cc8670021e8f3983fb0dc9c71c885 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Tue, 22 Mar 2022 23:48:42 +0800 Subject: [PATCH] update ledgerUtil --- .../bdware/sc/boundry/utils/LedgerUtil.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java b/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java index 451134e..9828247 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java @@ -1,11 +1,16 @@ package org.bdware.sc.boundry.utils; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.bdware.bdledger.api.grpc.Client; import org.bdware.bdledger.api.grpc.pb.CommonProto.Transaction; import org.bdware.bdledger.api.grpc.pb.CommonProto.TransactionType; import org.bdware.bdledger.api.grpc.pb.LedgerProto.SendTransactionResponse; import org.bdware.bdledger.api.grpc.pb.QueryProto.GetTransactionByHashResponse; +import org.bdware.sc.boundry.JavaScriptEntry; import org.bdware.sc.compiler.PermissionStub; +import org.bdware.sc.engine.JSONTool; import org.bdware.sc.node.Permission; import org.bdware.sc.util.HashUtil; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; @@ -19,6 +24,20 @@ import java.security.SecureRandom; public class LedgerUtil { static SecureRandom random = new SecureRandom((System.currentTimeMillis() + "").getBytes()); + public static Object getLedgerParams() { + //format:{nodes:[{ip:ip,port:port}]} + String ledgerParam = JavaScriptEntry.get.syncGet("", "getLedgerParams", ""); + JsonElement jo = JsonParser.parseString(ledgerParam); + return JSONTool.convertJsonElementToMirror(jo); + } + + public static Client getDefaultClient() { + String ledgerParam = JavaScriptEntry.get.syncGet("", "getLedgerParams", ""); + JsonElement jo = JsonParser.parseString(ledgerParam); + JsonObject param = jo.getAsJsonObject().get("nodes").getAsJsonArray().get(0).getAsJsonObject(); + return new Client(param.get("ip").getAsString(), param.get("port").getAsInt()); + } + public static Client getClient(ScriptObjectMirror str) { return new Client((String) str.get("ip"), Integer.parseInt(str.get("port").toString())); }