2613 large

以太坊地址和波场地址转换

thishe · 于 发布 · 407 次阅读

波场有自己优势,有些项目还是会运行在波场上面。在把合约从以太坊转移到波场的时候,发现地址转换是个麻烦事情,因此专门写了(C#)代码来实现:

using Nethereum.Hex.HexConvertors.Extensions;
using System.Linq;
using System.Security.Cryptography;
using Tron.Net.Common;

namespace TronEthAddress
{
///
/// 波场地址
///
public class TronAddress
{

public static string Eth2TronAddress(string ethAddress)
{
string fixaddress = "0x41" + ethAddress.RemoveHexPrefix();
byte[] addressBytes = fixaddress.HexToByteArray();
byte[] hash0 = SHA256(addressBytes);
byte[] hash1 = SHA256(hash0);
var checkSum = hash1.Take(4).ToArray();
return Base58.Encode(addressBytes.Concat(checkSum).ToArray());
}

private static byte[] SHA256(byte[] data)
{
using (var sha256 = new SHA256Managed())
{
return sha256.ComputeHash(data);
}
}

public static string Tron2EthAddress(string tronAddress)
{

var HexString = Base58.Decode(tronAddress).ToHexString();
string address = "0x" + HexString.Substring(2, 40);
var result = Nethereum.Util.AddressExtensions.ConvertToEthereumChecksumAddress(address);
return result;
}

}

}

  • 暂无回复。