因为波场的技术文档做得很不好,所以在此献丑了。
package io.gauss.bridge.server.core.service.util;
import cn.hutool.core.codec.Base58;
import cn.hutool.core.util.HexUtil;
import org.bitcoinj.core.Sha256Hash;
public class TronUtil {
public static String hexTobBase58(String hexString) {
if (hexString.startsWith("0x")) {
hexString = "41" + hexString.substring(2);
}
if (hexString.length() % 2 == 1) {
hexString = "0" + hexString;
}
return encode58(HexUtil.decodeHex(hexString));
}
private static String encode58(byte[] input) {
byte[] hash0 = Sha256Hash.hash(input);
byte[] hash1 = Sha256Hash.hash(hash0);
byte[] inputCheck = new byte[input.length + 4];
System.arraycopy(input, 0, inputCheck, 0, input.length);
System.arraycopy(hash1, 0, inputCheck, input.length, 4);
return Base58.encode(inputCheck);
}
public static String base58ToHex(String base58) {
byte[] decoded = decode58(base58);
return HexUtil.encodeHexStr(decoded);
}
private static byte[] decode58(String input) {
byte[] decodeCheck = Base58.decode(input);
if (decodeCheck.length <= 4) {
return null;
}
byte[] decodeData = new byte[decodeCheck.length - 4];
System.arraycopy(decodeCheck, 0, decodeData, 0, decodeData.length);
byte[] hash0 = Sha256Hash.hash(decodeData);
byte[] hash1 = Sha256Hash.hash(hash0);
if (hash1[0] == decodeCheck[decodeData.length] &&
hash1[1] == decodeCheck[decodeData.length + 1] &&
hash1[2] == decodeCheck[decodeData.length + 2] &&
hash1[3] == decodeCheck[decodeData.length + 3]) {
return decodeData;
}
return null;
}
public static void main(String[] args) {
String base58Address = base58ToHex("TM2WaeXaU7f9Bd7dQ3LHjAWd2BbZTBpBXT");
String hexAddress = hexTobBase58("4179494e49ed7689859663df8fe100ce1c29786ffe");
}
}