gpt4 book ai didi

c# - 如何使用 Nethereum 创建 Ethereum HD 钱包实现

转载 作者:行者123 更新时间:2023-12-04 17:33:47 27 4
gpt4 key购买 nike

我一直在尝试使用 Ethereum 在 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 标准创建 HD 钱包。

您可以在 Nethereum playground 中运行此示例 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");
}
}


关于c# - 如何使用 Nethereum 创建 Ethereum HD 钱包实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57523722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com