繁体   English   中英

如何使用 Nethereum 创建以太坊 HD 钱包实现

[英]How do I create an Ethereum HD wallet implementation with Nethereum

我一直在尝试使用以太坊在 C# 中创建 HD 钱包实现,到目前为止,我看到的实现是针对智能合约的。 下面是 NBitcoin 中的类似实现

ExtKey masterKey = ExtKey.Parse("***MasterKey***"); 
ExtPubKey masterPubKey = masterKey.Neuter();
ExtPubKey pubkey1 = masterPubKey.Derive((uint)id);
BitcoinAddress address = pubkey1.PubKey.GetAddress("****Bitcoin network type test or live****");

您可以为此使用 Nethereum。

Nethereum 内部使用 NBitcoin 来派生私钥和公钥,有关 BIP32 的更多信息,请查看https://programmingblockchain.gitbook.io/programmingblockchain/key_generation/bip_32

尽管以太坊使用不同的方法来创建它们的地址和路径。

下面的示例展示了如何在以太坊中使用 BIP32 标准创建高清钱包。

您可以在 Nethereum 游乐场http://playground.nethereum.com/csharp/id/1043 中运行此示例

using System;
using System.Numerics;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Util;
using System.Threading.Tasks;
using NBitcoin;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.HdWallet;

public class Program
{
    private static async Task Main(string[] args)
    {

        //Initiating a HD Wallet requires a list of words and an optional password to add further entropy (randomness)

        var words = "ripple scissors kick mammal hire column oak again sun offer wealth tomorrow wagon turn fatal";
        //Note: do not confuse the password with your Metamask password, Metamask password is used to secure the storage
        var password = "password";
        var wallet = new Wallet(words, password);

        // An HD Wallet is deterministic, it will derive the same number of addresses 
        // given the same seed (wordlist + optional password).

        // All the created accounts can be loaded in a Web3 instance and used as any other account, 
        // we can for instance check the balance of one of them:

        var account = new Wallet(words, password).GetAccount(0);
        Console.WriteLine("The account address is: " + account.Address);

        var web3 = new Web3(account, "http://testchain.nethereum.com:8545");
        //we connect to the Nethereum testchain which has already the account preconfigured with some Ether balance.
        var balance = await web3.Eth.GetBalance.SendRequestAsync(account.Address);
        Console.WriteLine("The account balance is: " + balance.Value);

        //Or transfer some Ether, as the account already has the private key required to sign the transactions.

        var toAddress = "0x13f022d72158410433cbd66f5dd8bf6d2d129924";
        var transactionReceipt = await web3.Eth.GetEtherTransferService()
            .TransferEtherAndWaitForReceiptAsync(toAddress, 2.11m, 2);
        Console.WriteLine($"Transaction {transactionReceipt.TransactionHash} for amount of 2.11 Ether completed");
    }
}


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM