Nethereum / Nethereum.Playground

Compile and run Nethereum snippets on the browser
MIT License
27 stars 17 forks source link

Example: Hardhat Private keys from mnemonic "test test test test test test test test test test test junk" #37

Open juanfranblanco opened 3 years ago

juanfranblanco commented 3 years ago
using System;
using System.Numerics;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Util;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.HdWallet;
using System.Threading.Tasks;
using NBitcoin;

public class Program
{
    private static async Task Main(string[] args)
    {
        // This samples shows how to derive or create accounts from a given word list seed in an hd wallet 

        // This uses Hardhat default mnemonic https://hardhat.org/hardhat-network/
        // 
        //Nethereum uses internally NBitcoin to derive the private and public keys, for more information on bip32 please check
        //https://programmingblockchain.gitbook.io/programmingblockchain/key_generation/bip_32

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

        var words = "test test test test test test test test test test test junk";
        //Note: do not confuse the password with your Metamask password, Metamask password is used to secure the storage
        var password = "";
        var wallet = new Wallet(words, password);

        //Accounts are derived using indexes so the account at 0 will have always the same private key and address.
        for (var i = 0; i < 20; i++)
        {
            var account = wallet.GetAccount(i);
            Console.WriteLine("Account index : " + i + " - Address : " + account.Address + " - Private key : " +
                              account.PrivateKey);
        }
    }
}