波场TRX在.Net Core C#中实现助记词和HDWallet

本文章主要通过将ETH钱包地址转换成TRX地址实现,也就是说ETH怎么用HdWallet的通过转换地址的方式在TRX也能用,私钥也是。

主要用到了以下nuget包

  • Nethereum.Hex
  • Nethereum.HdWallet
  • Tron.Net.Client

核心代码:

public static string GetBase58CheckAddress(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());
}

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

示例:

//生成助记词
Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.Twelve);

//使用助记词
Nethereum.HdWallet.Wallet wallet = new Nethereum.HdWallet.Wallet(mnemonic.ToString(), "助记词密码");

for (int i = 0; i < 5; i++)
{
    //移除私钥的0x开头
    string privateKey = wallet.GetAccount(i).PrivateKey.RemoveHexPrefix();
string address = GetBase58CheckAddress(wallet.GetAccount(i).Address); Console.WriteLine("TRX地址:{0},私钥:{1}", address, privateKey); }

生成5个ETH地址和对应的私钥,然后把ETH地址转换成TRX

结果:

TRX地址:TREyTqjmzjBFqnWGQZ1q1G2WpGmz7dvbPm,私钥:d687aced7f1c9fc47587b87a457b876b768377e8524b9243a7643ce2be90f938
TRX地址:TQs6vEqL4Vf4ckzmeXWi4q1p5VevkA5Yxi,私钥:c0eb70683e9aeb077286384ed94b9ae8c55d64b6ccd87fa460c932d7aa38c373
TRX地址:THAG1RcHvM6AQzJsPYxuV5PNYrUsfYvMBh,私钥:bfc0e0185ea3b1fdc861af0b44545a1fb7b42a7defda709fc8e92391654d1fe2
TRX地址:TKnmzam2uj6SQ1kUpkEbUDN81Rct9PdQ4z,私钥:96d6ff282991c7700aae0ed132ae598c5325a53e63c71eb8a9749d7626fdee23
TRX地址:TE6S8h3cuxhWkfd7JBGU99vghq4bBJiK6i,私钥:efbb87c615e1a243e770eaedc4d473ab5586f5751b75104615e2415dc1670c66

 

波场TRX在.Net Core C#中实现助记词和HDWallet

上一篇:LOJ#2586. 「APIO2018」选圆圈 KDtree+剪枝


下一篇:AtCoder Grand Contest 044 A Pay to Win 贪心