我在 TRON 网络中部署合约时遇到问题,我需要以 4.. 开头的格式指定地址,或者当我收到交易历史记录时(此处 api 也以 4.. 地址响应)。
因此我有一个问题:
如何将以T LAXtqju7GKyqoP... 开头的 TRON 地址转换为4 19b6e043089843624c36f1e3b1e8572d189cbe170 ,反之亦然?
我在 TRON 网络中部署合约时遇到问题,我需要以 4.. 开头的格式指定地址,或者当我收到交易历史记录时(此处 api 也以 4.. 地址响应)。
因此我有一个问题:
如何将以T LAXtqju7GKyqoP... 开头的 TRON 地址转换为4 19b6e043089843624c36f1e3b1e8572d189cbe170 ,反之亦然?
如何将以 TLAXtqju7GKyqoP... 开头的 TRON 地址转换为 419b6e043089843624c36f1e3b1e8572d189cbe170 ,反之亦然?
const TronWeb = require('tronweb');
const tronWeb = new TronWeb(
'http://127.0.0.1:9090',
'http://127.0.0.1:9090',
'http://127.0.0.1:9090',
'd6fbbf6eecffdb32172e391363a401f89617acb9dd01897b9fa180830a8a46b2',
);
拥有 tronWeb 对象后,您可以使用 tronWeb 的地址实用程序将地址转换为反之亦然
For Example:
const addressInHexFormat = '414450cf8c8b6a8229b7f628e36b3a658e84441b6f';
const addressInBase58 = tronWeb.address.fromHex(addressInHexFormat);
> addressInBase58 = 'TGCRkw1Vq759FBCrwxkZGgqZbRX1WkBHSu'
const addressInHex = tronWeb.address.toHex(addressInBase58);
> addressInHex = '414450cf8c8b6a8229b7f628e36b3a658e84441b6f'
笔记
上面的 tronWeb 对象是使用 Tron 的 Quickstart Docker 容器创建的。通过这种方式,地址可以反过来进行转换。
地址格式在相关的 TRON 文档中有很好的解释。
在 Java 代码中(基于wallet-cli):
public String tronHex(String base58) {
byte[] decoded = decode58(base58);
String hexString = decoded == null ? "" : org.spongycastle.util.encoders.Hex.toHexString(decoded);
return hexString;
}
private 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 String hexStringTobBase58(String hexString) {
hexString = adjustHex(hexString);
byte[] decodedHex = hexString == null? new byte[0] : org.spongycastle.util.encoders.Hex.decode(hexString);
String base58 = encode58(decodedHex);
return base58;
}
private String adjustHex(String hexString) {
if (hexString.startsWith("0x")) {
hexString = "41" + hexString.substring(2);
}
if (hexString.length() % 2 == 1) {
hexString = "0" + hexString;
}
return hexString;
}
private 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);
}
在此处查找类,Base58
在此处查找类,并在Sha256Hash
此处查找对 Spongy Castle的所需依赖项。
你只需要从 Base58 解码你的 base58Address。结果,您将获得地址校验和,因此您应该从结果中删除最后 4 个字节并获得所需的地址。
address = 41||sha3[12,32): 415a523b449890854c8fc460ab602df9f31fe4293f
sha256_0 = sha256(address): 06672d677b33045c16d53dbfb1abda1902125cb3a7519dc2a6c202e3d38d3322
sha256_1 = sha256(sha256_0): 9b07d5619882ac91dbe59910499b6948eb3019fafc4f5d05d9ed589bb932a1b4
checkSum = sha256_1[0, 4): 9b07d561
addchecksum = address || checkSum: 415a523b449890854c8fc460ab602df9f31fe4293f9b07d561
base58Address = Base58(addchecksum): TJCnKsPa7y5okkXvQAidZBzqx3QyQ6sxMW
C# 示例:
public static string GetHex(this String str)
{
var sb = new StringBuilder();
var bytes = Base58.Bitcoin.Decode(str); // nuget https://www.nuget.org/packages/SimpleBase/
for (int i = 0; i < bytes.Length - 4; i++)
{
var t = bytes[i];
sb.Append(t.ToString("X2"));
}
return sb.ToString();
}