版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sail331x/article/details/112972860
前言
由于在项目中需要,我们又为了节省服务器资源,决定不同步节点数据。也就说说,很多的一些API,我们是不能直接用的了,最直接的有创建地址、签名交易等等
相关API修改
地址生成
TronUtils.java
/**
* 离线创建地址
*
* @return
*/
public static Map<String, String> createAddress() {
ECKey eCkey = new ECKey(random);
String privateKey = ByteArray.toHexString(eCkey.getPrivKeyBytes());
byte[] addressBytes = eCkey.getAddress();
String hexAddress = ByteArray.toHexString(addressBytes);
Map<String, String> addressInfo = new HashMap<>();
addressInfo.put("address", toViewAddress(hexAddress));
addressInfo.put("hexAddress", hexAddress);
addressInfo.put("privateKey", privateKey);
return addressInfo;
}
离线签名并广播交易
/**
* 广播交易信息 返回交易id
* @param tronUrl
* @param transaction
* @return
*/
public static String signAndBroadcast(String tronUrl,String privateKey,JSONObject transaction)throws Throwable{
if(tronUrl.endsWith("/")){
tronUrl= tronUrl.substring(0,tronUrl.length() - 1);
}
Protocol.Transaction tx = packTransaction(transaction.toJSONString());
byte[] bytes = signTransactionByte(tx.toByteArray(), ByteArray.fromHexString(privateKey));
String signTransation = Hex.toHexString(bytes);
JSONObject jsonObjectGB = new JSONObject();
jsonObjectGB.put("transaction", signTransation);
String url = tronUrl + "/wallet/broadcasthex";
String transationCompelet1 =