簡體   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