首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Nethereum创建Ethereum wallet实现

如何使用Nethereum创建Ethereum wallet实现
EN

Stack Overflow用户
提问于 2019-08-16 19:18:42
回答 1查看 1.1K关注 0票数 3

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

代码语言:javascript
复制
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****");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-24 18:05:33

你可以使用Nethereum来做这件事。

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

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

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

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

代码语言:javascript
复制
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");
    }
}
代码语言:javascript
复制
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57523722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档