Example #1
0
        public void Slip77Tests()
        {
            //test vector: https://github.com/vulpemventures/slip77/commit/bc0fa0c712512d27cf1e2d6e1aaee5c36a3d38fa
            var allMnemonic = new Mnemonic("all all all all all all all all all all all all");
            var master      = Slip21Node.FromSeed(allMnemonic.DeriveSeed());

            Assert.Equal("dbf12b44133eaab506a740f6565cc117228cbf1dd70635cfa8ddfdc9af734756", master.Key.ToHex());
            var slip77                = Slip21Node.FromSeed(allMnemonic.DeriveSeed()).GetSlip77Node();
            var script                = Script.FromHex("76a914a579388225827d9f2fe9014add644487808c695d88ac");
            var privateBlindingKey    = slip77.DeriveSlip77BlindingKey(script);
            var unconfidentialAddress =
                BitcoinAddress.Create("2dpWh6jbhAowNsQ5agtFzi7j6nKscj6UnEr", Altcoins.Liquid.Instance.Regtest);
            var publicBlindingKey = privateBlindingKey.PubKey;

            Assert.Equal("4e6e94df28448c7bb159271fe546da464ea863b3887d2eec6afd841184b70592",
                         privateBlindingKey.ToHex());
            Assert.Equal("0223ef5cf5d1185f86204b9386c8541061a24b6f72fa4a29e3a0b60e1c20ffaf5b",
                         publicBlindingKey.ToHex());
            Assert.Equal("CTEkf75DFff5ReB7juTg2oehrj41aMj21kvvJaQdWsEAQohz1EDhu7Ayh6goxpz3GZRVKidTtaXaXYEJ",
                         new BitcoinBlindedAddress(publicBlindingKey, unconfidentialAddress).ToString());


            //test vector: https://github.com/trezor/trezor-firmware/pull/398/files#diff-afc9a622fb2281269983493a9da47a364e94f8bd338e5322f4a7cef99f98ee69R119
            var abusiveWords =
                new Mnemonic("alcohol woman abuse must during monitor noble actual mixed trade anger aisle");
            var derived       = abusiveWords.DeriveExtKey().Derive(new KeyPath("44'/1'/0'/0/0"));
            var addr          = derived.PrivateKey.ScriptPubKey.GetDestinationAddress(Liquid.Instance.Regtest);
            var abusiveslip77 = Slip21Node.FromSeed(abusiveWords.DeriveSeed()).GetSlip77Node();

            Assert.Equal("26f1dc2c52222394236d76e0809516255cfcca94069fd5187c0f090d18f42ad6",
                         abusiveslip77.DeriveSlip77BlindingKey(addr.ScriptPubKey).ToHex());
        }
Example #2
0
        void StoreCredentials(string mnemo, string password)
        {
            Mnemonic mymnemo = new Mnemonic(mnemo);

            Seed = mymnemo.DeriveSeed(password).ToHex();
            var EthKey = GetEthereumKey(1);

            pkey = GetAccount(1);
            var address = EthKey.GetPublicAddress();

            account = address;

            Keystore.Add(password, mnemo);

            JSON = JsonConvert.SerializeObject(Keystore, Formatting.Indented);

            string path     = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, "info.json");

            using (var file = File.Open(filePath, FileMode.Create, FileAccess.Write))
                using (var strm = new StreamWriter(file))
                {
                    strm.Write(JSON);
                }
        }
Example #3
0
        private void InitialiseSeed(string words, string seedPassword = null)
        {
            var mneumonic = new Mnemonic(words);

            Seed  = mneumonic.DeriveSeed((string)seedPassword).ToHex();
            Words = mneumonic.Words;
        }
        static (byte[] seed, string sentence) CreateHdSecrets(byte[] sourceKeyMaterial, int startIndex)
        {
            WordList wordList = WordList.English;

            var usedKeyMaterial = new byte[32];

            Buffer.BlockCopy(sourceKeyMaterial, startIndex, usedKeyMaterial, 0, 32);

            var mnemonic = new Mnemonic(wordList, usedKeyMaterial);

            if (!mnemonic.IsValidChecksum)
            {
                throw new InvalidOperationException("Invalid checksum.");
            }

            var seed = mnemonic.DeriveSeed();

            seed.Check(64);

            var sentence = mnemonic.ToString();

            CheckNumberOfWords(sentence, WordListLength);

            var testRecovery = new Mnemonic(sentence, WordList.English);

            if (testRecovery.ToString() != sentence ||
                !ByteArrays.AreAllBytesEqual(testRecovery.DeriveSeed(), seed))
            {
                throw new InvalidOperationException("This seed cannot be recovered.");
            }

            return(seed, sentence);
        }
Example #5
0
        private void InitialiseSeed(Wordlist wordlist, WordCount wordCount, string seedPassword = null)
        {
            var mneumonic = new Mnemonic(wordlist, wordCount);

            Seed  = mneumonic.DeriveSeed((string)seedPassword).ToHex();
            Words = mneumonic.Words;
        }
Example #6
0
    public void NewImportAccount()
    {
        string DEFAULT_PATH = "m/44'/60'/0'/0/0";
        // var wordstr = "girl arrange include shadow long energy aim harsh arm then mercy race";
        var wordstr    = "sibling edge false start marriage valid actress average tennis office trip protect";
        var mneumonic  = new Mnemonic(wordstr);
        var seed       = mneumonic.DeriveSeed(null).ToHex();
        var words      = mneumonic.Words;
        var masterKey  = new ExtKey(seed);
        var keyPath    = new KeyPath(DEFAULT_PATH);
        var extkey     = masterKey.Derive(keyPath);
        var privateKey = extkey.PrivateKey.ToBytes();
        var stringPK   = privateKey.ToHex();
        var ecKey      = new EthECKey(privateKey, true);
        var address    = ecKey.GetPublicAddress();

        Debug.Log("address: " + address + "\n privateKey: " + stringPK.ToString());

        var password        = "******";
        var keystoreservice = new Nethereum.KeyStore.KeyStoreService();
        var encryptedJson   = keystoreservice.EncryptAndGenerateDefaultKeyStoreAsJson(password, privateKey, address);

        PlayerPrefs.SetString("encryptedJson", encryptedJson);
        Debug.Log("encryptedJson " + encryptedJson);
    }
Example #7
0
    public void NewCreateAccount()
    {
        var wordlist  = Wordlist.English;
        var wordCount = WordCount.Twelve;
        var mneumonic = new Mnemonic(wordlist, wordCount);
        var seed      = mneumonic.DeriveSeed(null).ToHex();
        var words     = mneumonic.Words;

        Debug.Log("seed " + seed.ToString() + "  words " + words.ToString());
        var wordsstr = "";

        foreach (var item in words)
        {
            wordsstr = wordsstr + item + " ";
        }
        Debug.Log(wordsstr);

        string DEFAULT_PATH = "m/44'/60'/0'/0/0";

        var masterKey  = new ExtKey(seed);
        var keyPath    = new KeyPath(DEFAULT_PATH);
        var extkey     = masterKey.Derive(keyPath);
        var privateKey = extkey.PrivateKey.ToBytes();
        var ecKey      = new EthECKey(privateKey, true);
        var address    = ecKey.GetPublicAddress();

        Debug.Log("address: " + address + "\n privateKey: " + privateKey.ToString());
    }
        public static Wallet RestoreWalletFromMnemonic(string mnemonicStr, Network env)
        {
            Mnemonic mnemonic = new Mnemonic(mnemonicStr);

            var seed = mnemonic.DeriveSeed();
            var pk   = new ExtKey(seed);
            var kp   = KeyPath.Parse(keyPath);
            var key  = pk.Derive(kp);

            var privKey = key.PrivateKey;
            var bytes   = privKey.ToBytes();

            Wallet w = new Wallet();

            w._env            = BinanceEnvironment.GetEnvironment(env);
            w._privateKey     = privKey;
            w._privateKeyStr  = BitConverter.ToString(bytes).Replace("-", string.Empty);
            w._publicKey      = w._privateKey.PubKey.Compress();
            w._publicKeyBytes = w._publicKey.ToBytes();
            w._AddressBytes   = w._publicKey.Hash.ToBytes();
            w._addressStr     = Bech32Engine.Encode(w.Env.Hrp, w._AddressBytes);


            w.Init();

            return(w);
        }
Example #9
0
        public Task <bool> UnlockWalletAsync(string password)
        {
            return(Task.Run(delegate
            {
                string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                string filePath = Path.Combine(path, "info.json");
                if (File.Exists(filePath))
                {
                    JSON = "";
                    JSON = File.ReadAllText(filePath);
                    this.Keystore = JsonConvert.DeserializeObject <Dictionary <string, string> >(JSON);

                    string mymnemo;

                    if (Keystore.TryGetValue(password, out mymnemo))
                    {
                        mnemo = new Mnemonic(mymnemo, Wordlist.English);
                        Seed = mnemo.DeriveSeed(password).ToHex();
                        var EthKey = GetEthereumKey(1);
                        pkey = GetAccount(1);
                        var address = EthKey.GetPublicAddress();
                        account = address;
                    }
                    else
                    {
                        return false;
                    }

                    return true;
                }
                return false;
            }));
        }
Example #10
0
        private void InitialiseSeed(string words, string seedPassword = null)
        {
            var mneumonic = new Mnemonic(words);

            Seed  = mneumonic.DeriveSeed(seedPassword);
            Words = mneumonic.Words;
            IsMneumonicValidChecksum = mneumonic.IsValidChecksum;
        }
Example #11
0
        private void InitialiseSeed(Wordlist wordlist, WordCount wordCount, string seedPassword = null)
        {
            var mneumonic = new Mnemonic(wordlist, wordCount);

            Seed  = mneumonic.DeriveSeed(seedPassword).ToHex();
            Words = mneumonic.Words;
            IsMneumonicValidChecksum = mneumonic.IsValidChecksum;
        }
Example #12
0
    private void InitialiseSeed()
    {
        var mneumonic = new Mnemonic(Wordlist.English, WordCount.Twelve);

        Seed  = mneumonic.DeriveSeed(null).ToHex();
        Words = mneumonic.Words;
        IsMneumonicValidChecksum = mneumonic.IsValidChecksum;
        GetPrivateKeyAndAddress();
    }
Example #13
0
    private void InitialiseSeed(string words)
    {
        var mneumonic = new Mnemonic(words);

        Seed  = mneumonic.DeriveSeed(null).ToHex();
        Words = mneumonic.Words;
        IsMneumonicValidChecksum = mneumonic.IsValidChecksum;
        GetPrivateKeyAndAddress();
    }
Example #14
0
        public void ShouldAllowDeriviationSeed()
        {
            var mnemo     = new Mnemonic("stem true medal chronic lion machine mask road rabbit process movie account", Wordlist.English);
            var seed      = mnemo.DeriveSeed();
            var ethWallet = new Wallet(seed);
            var account   = new Account(ethWallet.GetPrivateKey(0));

            account = ethWallet.GetAccount(account.Address);
            Assert.True(account.Address.IsTheSameAddress("0x03dd02C038e15fcFbBdc372c71D595BD241E0898"));
        }
Example #15
0
        public HdWalletBase(string words, string seedPassword)
        {
            if (string.IsNullOrEmpty(words))
            {
                throw new NullReferenceException(nameof(words));
            }

            var mneumonic = new Mnemonic(words);

            BIP39Seed = mneumonic.DeriveSeed(seedPassword).ToHex();
        }
Example #16
0
        public static Wallet FromMnemonicCode(string mnemonicCode, BinanceDexEnvironmentData env)
        {
            Mnemonic mnemonic = new Mnemonic(mnemonicCode);

            byte[]  seed    = mnemonic.DeriveSeed();
            ExtKey  extKey  = new ExtKey(seed);
            KeyPath keyPath = KeyPath.Parse(hdPath);
            ExtKey  derived = extKey.Derive(keyPath);
            Wallet  wallet  = new Wallet(derived.PrivateKey, env);

            wallet.Mnemonic = mnemonicCode;
            return(wallet);
        }
Example #17
0
        private EthECKey GetECKeyPair(string mneumonicWords, string passphrase, long addressIndex)
        {
            Mnemonic mneumonic = new Mnemonic(mneumonicWords);
            string   seed      = mneumonic.DeriveSeed(passphrase).ToHex();
            ExtKey   masterKey = new ExtKey(seed);

            NBitcoin.KeyPath keyPath  = new NBitcoin.KeyPath(GetIndexPath(addressIndex));
            ExtKey           childkey = masterKey.Derive(keyPath);

            byte[]   privateKeyBytes = childkey.PrivateKey.ToBytes();
            EthECKey ethECKey        = new EthECKey(privateKeyBytes, true);

            return(ethECKey);
        }
Example #18
0
        public WalletDto CreateWallet(int index)
        {
            var mnemonic = new Mnemonic(Wordlist.English, WordCount.Twelve);
            var seed     = mnemonic.DeriveSeed();

            var key   = new Key(seed, index);
            var words = string.Join(" ", mnemonic.Words);

            return(new WalletDto
            {
                Mnemonic = words,
                Seed = seed.ToHex(),
                PrivateKey = key.PrivateKey.ToHex(),
                PublicKey = key.PublicKey.ToHex(),
                Address = key.ToWallet().Base58Address,
            });
        }
Example #19
0
        private static void TestMethod()
        {
            Key    privateKey = new Key();
            PubKey publicKey  = privateKey.PubKey;

            string mnemonicString = "quantum tobacco key they maid mean crime youth chief jungle mind design broken tilt bus shoulder leaf good forward erupt split divert bread kitten";
            var    mnemonic       = new Mnemonic(mnemonicString);
            var    seed           = mnemonic.DeriveSeed();

            var xprv = mnemonic.DeriveExtKey().Derive(new KeyPath("m/44\'/0\'/0\'/0"));

            var xpub = xprv.Neuter();

            Console.WriteLine("xpub: " + xpub.ToString(Network.Main));

            Console.WriteLine("Private Key Test net: " + privateKey.ToString(Network.TestNet));
            Console.WriteLine("Secret Test net" + privateKey.GetBitcoinSecret(Network.TestNet).ToString());
            //Console.WriteLine("address Main net: " + publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main));
            Console.WriteLine("address Test net: " + publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.TestNet));
            Console.WriteLine("Segwit address Test net: " + publicKey.GetAddress(ScriptPubKeyType.Segwit, Network.TestNet));

            var publicKeyHash = publicKey.Hash;

            Console.WriteLine("public key hash:" + publicKeyHash);
            var mainNetAddress = publicKeyHash.GetAddress(Network.Main);
            var testNetAddress = publicKeyHash.GetAddress(Network.TestNet);

            Console.WriteLine("address Main net: " + mainNetAddress);
            Console.WriteLine("address Test net: " + testNetAddress);

            Console.WriteLine("----------------------");
            var publicKeyHash2 = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf");

            var testNetAddress2 = publicKeyHash2.GetAddress(Network.TestNet);
            var mainNetAddress2 = publicKeyHash2.GetAddress(Network.Main);

            Console.WriteLine(mainNetAddress2.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG
            Console.WriteLine(testNetAddress2.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG

            Console.WriteLine("-----------------------");
            var paymentScript      = publicKeyHash.ScriptPubKey;
            var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main);

            Console.WriteLine(mainNetAddress == sameMainNetAddress); // True
        }
Example #20
0
        public static MnemonicKeyPair RecoverMnemonicKeyPair(List <string> mnemonicSeedWords, string mnemonicSeedPassword)
        {
            if (mnemonicSeedPassword == null)
            {
                mnemonicSeedPassword = "";
            }

            Mnemonic mn = new Mnemonic(string.Join(" ", mnemonicSeedWords), Wordlist.English);

            // generate the seed from words and password
            byte[] seed = mn.DeriveSeed(mnemonicSeedPassword);
            // generate the master key from the seed using bitcoinj implementation of hd
            // wallets
            DeterministicKeyPair master           = new DeterministicKeyPair(seed);
            RawKeyPair           generatedKeyPair = new RawKeyPair(Hex.ToHexString(master.PrivateKey.ToBytes()));

            return(new MnemonicKeyPair(generatedKeyPair, mnemonicSeedWords, master));
        }
Example #21
0
        public void WordTest()
        {
            //1、通过助记词 得到私钥
            Mnemonic mnemo           = new Mnemonic("累 妙 清 董 贮 程 异 瞧 敲 发 拍 虾", Wordlist.ChineseSimplified);
            ExtKey   hdRoot          = mnemo.DeriveExtKey("123456");
            var      privateKey      = hdRoot.PrivateKey.ToBytes(); //字节--》对应不同私钥体系
            var      ethKey          = new EthECKey(privateKey, true);
            var      hdEthPrivateKey = ethKey.GetPrivateKey();      //得到hdEthPrivateKey

            Mnemonic tempMnemonic = new Mnemonic("累 妙 清 董 贮 程 异 瞧 敲 发 拍 虾", Wordlist.ChineseSimplified);
            string   Seed         = tempMnemonic.DeriveSeed("123456").ToHex();
            //2、通过计算得到种子,获得私钥
            var masterKey = new ExtKey(Seed);
            //var masterKeyString = masterKey.PrivateKey.GetBitcoinSecret(Network.Main);
            var tempEthKey     = new EthECKey(masterKey.PrivateKey.ToBytes(), true);
            var tempPrivateKey = tempEthKey.GetPrivateKey();

            Assert.Equal(hdEthPrivateKey, tempPrivateKey);
        }
Example #22
0
        public void Slip21Tests()
        {
            var allMnemonic = new Mnemonic("all all all all all all all all all all all all");
            var master      = Slip21Node.FromSeed(allMnemonic.DeriveSeed());

            Assert.Equal("dbf12b44133eaab506a740f6565cc117228cbf1dd70635cfa8ddfdc9af734756", master.Key.ToHex());

            var child1 = master.DeriveChild("SLIP-0021");

            Assert.Equal("1d065e3ac1bbe5c7fad32cf2305f7d709dc070d672044a19e610c77cdf33de0d", child1.Key.ToHex());

            var child2 = child1.DeriveChild("Master encryption key");

            Assert.Equal("ea163130e35bbafdf5ddee97a17b39cef2be4b4f390180d65b54cf05c6a82fde", child2.Key.ToHex());

            var child3 = child1.DeriveChild("Authentication key");

            Assert.Equal("47194e938ab24cc82bfa25f6486ed54bebe79c40ae2a5a32ea6db294d81861a6", child3.Key.ToHex());
        }
        public void CreateMnemonics(string masterSentence)
        {
            CheckNumberOfWords(masterSentence, 3 * WordListLength);

            this.WalletSentence = string.Join(" ", masterSentence.Split(" ").ToArray().Take(WordListLength));

            var firstMnemonic = new Mnemonic(this.WalletSentence, WordList.English);

            this.WalletSeed = firstMnemonic.DeriveSeed();

            this.WalletSentence = firstMnemonic.ToString();

            this.SecondSentence = string.Join(" ", masterSentence.Split(" ").ToArray().Skip(WordListLength).Take(WordListLength));

            var secondMnemonic = new Mnemonic(this.SecondSentence, WordList.English);

            this.SecondSeed = secondMnemonic.DeriveSeed();

            this.SecondSentence = secondMnemonic.ToString();



            this.ThirdSentence = string.Join(" ", masterSentence.Split(" ").ToArray().Skip(WordListLength * 2).Take(WordListLength));

            var thirdMnemonic = new Mnemonic(this.ThirdSentence, WordList.English);

            this.ThirdSeed = thirdMnemonic.DeriveSeed();

            this.ThirdSentence = secondMnemonic.ToString();

            if (masterSentence != $"{this.WalletSentence} {this.SecondSentence} {this.ThirdSentence}")
            {
                throw new InvalidOperationException("Error in recovery.");
            }

            this.MasterSentence = masterSentence;
            this.MasterKey      = new byte[3 * 64];
            Buffer.BlockCopy(this.WalletSeed, 0, this.MasterKey, 0 * 64, 64);
            Buffer.BlockCopy(this.SecondSeed, 0, this.MasterKey, 1 * 64, 64);
            Buffer.BlockCopy(this.ThirdSeed, 0, this.MasterKey, 2 * 64, 64);
            this.MasterKey.Check(3 * 64);
        }
Example #24
0
        public static CreateRandomAccountResponse CreateRandomAccount(Network network)
        {
            var env      = BinanceEnvironment.GetEnvironment(network);
            var response = new CreateRandomAccountResponse();

            response.Network = network;
            Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.TwentyFour);

            response.Mnemonic = mnemonic.ToString();
            var seed    = mnemonic.DeriveSeed();
            var pk      = new ExtKey(seed);
            var kp      = KeyPath.Parse(keyPath);
            var key     = pk.Derive(kp);
            var privKey = key.PrivateKey;

            response.PrivateKey = BitConverter.ToString(privKey.ToBytes()).Replace("-", string.Empty);
            response.Address    = Bech32Engine.Encode(env.Hrp, privKey.PubKey.Hash.ToBytes());

            return(response);
        }
Example #25
0
        public void EngTest()
        {
            var test = JObject.Parse(File.ReadAllText("data/bip39_vectors.json"));

            foreach (var language in test.Properties())
            {
                var lang = GetList(language.Name);
                foreach (var langTest in ((JArray)language.Value).OfType <JArray>().Take(2))
                {
                    var    entropy     = Encoders.Hex.DecodeData(langTest[0].ToString());
                    string mnemonicStr = langTest[1].ToString();
                    string seed        = langTest[2].ToString();
                    var    mnemonic    = new Mnemonic(mnemonicStr, lang);
                    Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed("TREZOR")));

                    mnemonic = new Mnemonic(lang, entropy);
                    Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed("TREZOR")));
                }
            }
        }
Example #26
0
        public void JapTest()
        {
            var test = JArray.Parse(File.ReadAllText("data/bip39_JP.json", Encoding.UTF32));

            foreach (var unitTest in test.OfType <JObject>())
            {
                var    entropy     = Encoders.Hex.DecodeData(unitTest["entropy"].ToString());
                string mnemonicStr = unitTest["mnemonic"].ToString();
                string seed        = unitTest["seed"].ToString();
                string passphrase  = unitTest["passphrase"].ToString();
                var    mnemonic    = new Mnemonic(mnemonicStr, Wordlist.Japanese);
                Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed(passphrase)));
                var bip32       = unitTest["bip32_xprv"].ToString();
                var bip32Actual = mnemonic.DeriveExtKey(passphrase).ToString(Network.Main);
                Assert.Equal(bip32, bip32Actual.ToString());

                mnemonic    = new Mnemonic(Wordlist.Japanese, entropy);
                bip32Actual = mnemonic.DeriveExtKey(passphrase).ToString(Network.Main);
                Assert.Equal(bip32, bip32Actual.ToString());
            }
        }
Example #27
0
        public static KeyInformation GetKeyFromMnemonic(string mnenonicWords, string password, string hrp)
        {
            Mnemonic mnemonic = new Mnemonic(mnenonicWords, Wordlist.English);

            ExtKey key    = new ExtKey(mnemonic.DeriveSeed(password));
            ExtKey extKey = key.Derive(new KeyPath("m/44'/714'/0'/0/0"));

            Key    privateKey = extKey.PrivateKey;
            PubKey publicKey  = privateKey.PubKey;
            string address    = GetAddress(publicKey.Hash.ToBytes(), hrp);

            KeyInformation keyInformation = new KeyInformation
            {
                Address    = address,
                Mnemonic   = mnemonic.ToString(),
                PrivateKey = privateKey.ToBytes().ToHexString(),
                PublicKey  = publicKey.ToString()
            };

            return(keyInformation);
        }
Example #28
0
        public void JapTest()
        {
            JArray test = JArray.Parse(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("bip39_JP.json"), Encoding.UTF32));

            foreach (JObject unitTest in test.OfType <JObject>())
            {
                byte[] entropy     = Encoders.Hex.DecodeData(unitTest["entropy"].ToString());
                string mnemonicStr = unitTest["mnemonic"].ToString();
                string seed        = unitTest["seed"].ToString();
                string passphrase  = unitTest["passphrase"].ToString();
                var    mnemonic    = new Mnemonic(mnemonicStr, Wordlist.Japanese);
                Assert.True(mnemonic.IsValidChecksum);
                Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed(passphrase)));
                string bip32       = unitTest["bip32_xprv"].ToString();
                string bip32Actual = mnemonic.DeriveExtKey(passphrase).ToString(KnownNetworks.Main);
                Assert.Equal(bip32, bip32Actual.ToString());
                mnemonic = new Mnemonic(Wordlist.Japanese, entropy);
                Assert.True(mnemonic.IsValidChecksum);
                bip32Actual = mnemonic.DeriveExtKey(passphrase).ToString(KnownNetworks.Main);
                Assert.Equal(bip32, bip32Actual.ToString());
            }
        }
Example #29
0
        public void EngTest()
        {
            JObject test = JObject.Parse(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("bip39_vectors.json")));

            foreach (JProperty language in test.Properties())
            {
                Wordlist lang = GetList(language.Name);
                foreach (JArray langTest in ((JArray)language.Value).OfType <JArray>().Take(2))
                {
                    byte[] entropy     = Encoders.Hex.DecodeData(langTest[0].ToString());
                    string mnemonicStr = langTest[1].ToString();
                    string seed        = langTest[2].ToString();
                    var    mnemonic    = new Mnemonic(mnemonicStr, lang);
                    Assert.True(mnemonic.IsValidChecksum);
                    Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed("TREZOR")));

                    mnemonic = new Mnemonic(lang, entropy);
                    Assert.True(mnemonic.IsValidChecksum);
                    Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed("TREZOR")));
                }
            }
        }
Example #30
0
        public static void HdWallet1()
        {
            ExtKey d = new ExtKey();


            string Path      = "m/44'/60'/0'/0/x";
            string words     = "";
            var    mneumonic = new Mnemonic(words);
            var    seed      = mneumonic.DeriveSeed("123456@a").ToHex();
            var    masterKey = new ExtKey(seed);
            var    d1        = new Account(masterKey.PrivateKey.ToBytes()).Address;

            for (int i = 0; i < 10; i++)
            {
                var    keyPath    = new NBitcoin.KeyPath(Path.Replace("x", i.ToString()));
                ExtKey extKey     = masterKey.Derive(keyPath);
                byte[] privateKey = extKey.PrivateKey.ToBytes();
                var    account    = new Account(privateKey);

                Console.WriteLine("Account index : " + i + " - Address : " + account.Address + " - Private key : " + account.PrivateKey);
            }
        }