import org.fisco.bcos.sdk.jni.common.JniLibLoader; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionStructBuilderJniObj; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV1; /** * 用官方 JNI 编码同一份 TransactionData,输出官方 TARS hex + 官方 hash。 * * 用法: java -cp bcos-sdk-jni.jar Encode */ public class Encode { static final int SM = 0; public static void main(String[] args) throws Exception { JniLibLoader.loadJniLibrary(); String text = new String(java.nio.file.Files.readAllBytes( java.nio.file.Paths.get(args[0])), "utf-8"); TransactionDataV1 d = new TransactionDataV1(); d.setVersion((int) jlong(text, "version")); d.setChainId(jstr(text, "chainId")); d.setGroupId(jstr(text, "groupId")); d.setNonce(jstr(text, "nonce")); d.setTo(jstr(text, "to")); d.setAbi(jstr(text, "abi")); d.setBlockLimit(jlong(text, "blockLimit")); d.setInput(fromHex(jstr(text, "input"))); d.setValue(jstr(text, "value")); d.setGasPrice(jstr(text, "gasPrice")); d.setGasLimit(jlong(text, "gasLimit")); d.setMaxFeePerGas(jstr(text, "maxFeePerGas")); d.setMaxPriorityFeePerGas(jstr(text, "maxPriorityFeePerGas")); String hash = TransactionStructBuilderJniObj.calcTransactionDataStructHash(SM, d); String enc = TransactionStructBuilderJniObj.encodeTransactionDataStruct(d); System.out.println("OFFICIAL_HASH=" + hash); System.out.println("OFFICIAL_TARS=" + enc); System.out.println("OFFICIAL_TARS_LEN=" + (enc.length() / 2)); } static byte[] fromHex(String h) { if (h == null) return new byte[0]; h = h.startsWith("0x") ? h.substring(2) : h; int n = h.length() / 2; byte[] out = new byte[n]; for (int i = 0; i < n; i++) { out[i] = (byte) Integer.parseInt(h.substring(i * 2, i * 2 + 2), 16); } return out; } static String jstr(String text, String key) { String pat = "\"" + key + "\""; int i = text.indexOf(pat); if (i < 0) return ""; int c = text.indexOf(':', i); int q = text.indexOf('"', c + 1); if (q < 0) return ""; int e = text.indexOf('"', q + 1); if (e < 0) return ""; return text.substring(q + 1, e); } static long jlong(String text, String key) { String pat = "\"" + key + "\""; int i = text.indexOf(pat); if (i < 0) return 0; int c = text.indexOf(':', i); int s = c + 1; while (s < text.length() && Character.isWhitespace(text.charAt(s))) s++; int e = s; while (e < text.length() && (Character.isDigit(text.charAt(e)) || text.charAt(e) == '-')) e++; return Long.parseLong(text.substring(s, e)); } }