import org.fisco.bcos.sdk.jni.common.JniLibLoader; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderJniObj; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV1; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionStructBuilderJniObj; import org.fisco.bcos.sdk.jni.utilities.tx.Transaction; import org.fisco.bcos.sdk.jni.utilities.tx.TxPair; import org.fisco.bcos.sdk.jni.utilities.signature.SignatureJniObj; /** * 官方 bcos-sdk-jni 地面真值对拍。 * * 用法: * java -cp bcos-sdk-jni.jar Oracle hash * java -cp bcos-sdk-jni.jar Oracle sign */ public class Oracle { // CryptoType: 0 = SM3/国密 (见 sdk 的 CryptoType) static final int SM = 0; public static void main(String[] args) throws Exception { JniLibLoader.loadJniLibrary(); String mode = args[0]; String jsonPath = args[1]; String text = new String(java.nio.file.Files.readAllBytes( java.nio.file.Paths.get(jsonPath)), "utf-8"); // 极简 JSON 字段读取(避免依赖外部 JSON 库):形如 "key":"value" / "key":123 String abi = jstr(text, "abi"); String to = jstr(text, "to"); String nonce = jstr(text, "nonce"); String chainId = jstr(text, "chainId"); String groupId = jstr(text, "groupId"); String inputHex = jstr(text, "input"); long blockLimit = jlong(text, "blockLimit"); long gasLimit = jlong(text, "gasLimit"); String value = jstr(text, "value"); String gasPrice = jstr(text, "gasPrice"); String maxFee = jstr(text, "maxFeePerGas"); String maxPrio = jstr(text, "maxPriorityFeePerGas"); int version = (int) jlong(text, "version"); TransactionDataV1 d = new TransactionDataV1(); d.setVersion(version); d.setChainId(chainId); d.setGroupId(groupId); d.setNonce(nonce); d.setTo(to); d.setAbi(abi); d.setBlockLimit(blockLimit); d.setInput(fromHex(inputHex)); d.setValue(value); d.setGasPrice(gasPrice); d.setGasLimit(gasLimit); d.setMaxFeePerGas(maxFee); d.setMaxPriorityFeePerGas(maxPrio); String hash = TransactionStructBuilderJniObj.calcTransactionDataStructHash(SM, d); System.out.println("OFFICIAL_HASH=" + hash); String encData = TransactionStructBuilderJniObj.encodeTransactionDataStruct(d); System.out.println("OFFICIAL_TARS=" + encData); if (mode.equals("sign")) { String keyHex = args[2]; String encodedTx = TransactionStructBuilderJniObj.createEncodedTransaction( d, keyHex, "", 1, ""); System.out.println("OFFICIAL_TX=" + encodedTx); } } 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)); } }