update ledgerUtil

This commit is contained in:
CaiHQ 2022-03-22 23:48:42 +08:00
parent 8db5c8e2d7
commit 4b96899653

View File

@ -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()));
}