JAVA 离线签名-生成波场地址
/**
* 快捷生成Trx地址
* @return
* @throws InvalidAlgorithmParameterException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
*/
public static Map<String, Object> newAddress() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
HashMap<String, Object> map = new HashMap<>();
ECKeyPair ecKeyPair = ();
String address = "41" + (ecKeyPair);
String privatekey = ().toString(16);
if(() < privateLength){
for(int i = 0 ; i < privateLength - () ; i++){
privatekey = ""+"0"+privatekey;
}
}
String publickey = ().toString(16);
("address", address);
("private", privatekey);
("public", publickey);
return map;
}
/**
* 转换地址,TGtiQ5uQzw3Z3ni3EC 转成 41 类型的地址
*
* @param addressT
* @return
*/
public static String toHexString(String addressT) {
return (decodeFromBase58Check(addressT));
}
/**
* 转换地址,415694e6807bf4685fb6 转成 TGtiQ5uQzw3Z3ni3EC 类型的地址
*
* @param addressStart41
* @return
*/
public static String fromHexString(String addressStart41) {
return encode58Check((addressStart41));
}
/**
* 查询trx的代币余额
*
* @param contractAddress 代币的合约地址
* @param address 要查询的地址
* @return 代币数量
*/
public BigDecimal getTokenBalance(String contractAddress, String address) throws Exception {
String method = "balanceOf(address)";
String contract = (contractAddress);
String parameter = fill_zero((address));
String ownerAddress = (address);
JSONObject result = TriggersmartContract(contract, method, parameter, ownerAddress);
Object constant_result = ("constant_result").get(0);
if (constant_result == null)
return new BigDecimal(0);
Integer decimals = decimals(contractAddress, address);
BigDecimal balance = new BigDecimal(new BigInteger(constant_result.toString(), 16).toString()).divide(new BigDecimal((10).pow(decimals)), 8, );
return balance;
}
/**
* 获取TRX余额
*
* @param address 地址
* @return
* @throws Exception
*/
public static BigDecimal getBalance(String address) throws Exception {
JSONObject requestBody = new JSONObject();
("address", address);
("visible", true);
HttpResponse httpResponse = (TronHttpUrl + TronCode_API.TRXBalance, (), new HttpOptions(1, 5000));
JSONObject result = (JSONObject) (httpResponse);
if (("Error"))
return new BigDecimal(0);
String balance = ("balance");
BigDecimal trxBalance = new BigDecimal(balance).divide(new BigDecimal((10).pow(6)), 8, );
return trxBalance;
}
/**
* 获取合约的单位
*
* @param contractAddress 合约地址
* @param address 拥有者地址
* @return
* @throws Exception
*/
public static Integer decimals(String contractAddress, String address) throws Exception {
String method = "decimals()";
contractAddress = (contractAddress);
String parameter = fill_zero((address));
String ownerAddress = (address);
JSONObject result = TriggersmartContract(contractAddress, method, parameter, ownerAddress);
Object constant_result = ("constant_result").get(0);
if (constant_result == null)
return 0;
BigInteger decimals = new BigInteger(constant_result.toString(), 16);
return (());
}
/**
* 调用智能合约
*
* @param contractAddress 合约地址
* @param method 方法名
* @param parameter parameter的编码需要根据合约的ABI规则
* @param ownerAddress 拥有者地址
* @return
* @throws Exception
*/
private static JSONObject TriggersmartContract(String contractAddress, String method, String parameter, String ownerAddress) throws Exception {
JSONObject requestBody = new JSONObject();
("contract_address", contractAddress);
("function_selector", method);
("parameter", parameter);
("owner_address", ownerAddress);
HttpResponse httpResponse = (TronHttpUrl + TronCode_API.TRCTOokenBalance, (), new HttpOptions(1, 5000));
JSONObject result = (JSONObject) (httpResponse);
return result;
}
/**
* 交易TRC20
*
* @param fromAddress 转账地址
* @param privateKey
* @param contractAddress 合约地址
* @param toAddress 入账地址
* @param amount 金额
* @return
* @throws Exception
*/
public static TronResult TRC20signTransaction(String fromAddress, String privateKey, String contractAddress, String toAddress, BigDecimal amount) throws Exception {
byte[] privateBytes = (privateKey);
byte[] fromAddressBytes = decodeFromBase58Check(fromAddress);
toAddress = (toAddress);
byte[] contractAddressBytes = decodeFromBase58Check(contractAddress);
Integer decimals = decimals(contractAddress, fromAddress);
amount = (new BigDecimal((10).pow(decimals)));
(());
Function transfer = new Function(
"transfer",
(new Address((2)), new Uint256(())),
(new TypeReference<Bool>() {
})
);
build = (transfer, fromAddressBytes, contractAddressBytes);
transaction = (build, , 10000000l);
byte[] transactionByte = ();
byte[] signTransaction = signTransaction(transactionByte, privateBytes);
TronResult tronResult = SendTransaction(signTransaction);
return tronResult;
}
/**
* 交易 TRX
*
* @param fromAddress 出账地址
* @param privateKey
* @param toAddress 入账地址
* @param amount 金额
* @return
* @throws Exception
*/
public static TronResult signTRXTransaction(String fromAddress, String privateKey, String toAddress, BigDecimal amount) throws Exception {
byte[] privateBytes = (privateKey);
byte[] fromAddressBytes = decodeFromBase58Check(fromAddress);
byte[] toAddressBytes = decodeFromBase58Check(toAddress);
long value = (new BigDecimal((10).pow(6))).toBigInteger().longValue();
contractTransaction = (fromAddressBytes, toAddressBytes, value);
byte[] bytes = ();
byte[] signTransaction = signTransaction(bytes, privateBytes);
TronResult tronResult = SendTransaction(signTransaction);
(tronResult);
return tronResult;
}
/**
* 获取最新区块的信息
*
* @return
* @throws Exception
*/
public static JSONObject NowBlock() throws Exception {
JSONObject block = new JSONObject();
HttpResponse response = (new HttpRequest(TronHttpUrl + TronCode_API.GetNowBlock, null, null));
(response);
JSONObject result = (JSONObject) (response);
String blockID = ("blockID");
Long timestamp = ("block_header").getJSONObject("raw_data").getLong("timestamp");
Long number = ("block_header").getJSONObject("raw_data").getLong("number");
("blockID", blockID);
("timestamp", timestamp);
("number", number);
return block;
}
/**
* 交易签名
*
* @param transactionBytes 待签名数据
* @param privateKey 交易创建者私钥
* @return 签名后的数据
* @throws InvalidProtocolBufferException 异常
*/
public static byte[] signTransaction(byte[] transactionBytes, byte[] privateKey) throws InvalidProtocolBufferException {
transaction = (transactionBytes);
byte[] rawData = ().toByteArray();
byte[] hash = (rawData);
ECKey ecKey = (privateKey);
byte[] sign = (hash).toByteArray();
return ().addSignature((sign)).build().toByteArray();
}
/**
* 广播交易
*
* @param sign 签名后的信息
* @return
* @throws Exception
*/
public static TronResult SendTransaction(byte[] sign) throws Exception {
String toHexString = (sign);
JSONObject requestBody = new JSONObject();
("transaction", toHexString);
HttpResponse httpResponse = (TronHttpUrl + TronCode_API.Send, (), new HttpOptions(1, 5000));
JSONObject result = (JSONObject) (httpResponse);
boolean status = ("result");
String code = ("code");
String hash = ("txid");
String message = ("message");
TronResult tronResult = new TronResult(hash, status, code, message);
return tronResult;
}
/**
* 补0到64位
*
* @param input
* @return
*/
public static String fill_zero(String input) {
int strLen = ();
StringBuffer sb = null;
while (strLen < 64) {
sb = new StringBuffer();
("0").append(input);// 左补0
input = ();
strLen = ();
}
return input;
}
/**
* 根据hash获取交易FEE情况
*
* @param Hash hash
* @return
* @throws Exception
*/
public static JSONObject GetTransactionInfoById(String Hash) throws Exception {
JSONObject requestBody = new JSONObject();
BigDecimal fee = new BigDecimal(0);
("value", Hash);
HttpResponse httpResponse = (TronHttpUrl + TronCode_API.GetTransactionInfoById, (), new HttpOptions(1, 5000));
Object doResult = (httpResponse);
JSONObject obj = (doResult);
return obj;
}
/**
* 根据hash获取交易状态
*
* @param Hash hash
* @return
* @throws Exception
*/
public static JSONObject GetTransactionById(String Hash) throws Exception {
JSONObject requestBody = new JSONObject();
BigDecimal fee = new BigDecimal(0);
("value", Hash);
("visible", true);
HttpResponse httpResponse = (TronHttpUrl + TronCode_API.GetTransactionById, (), new HttpOptions(1, 5000));
Object doResult = (httpResponse);
JSONObject obj = (doResult);
return obj;
}