private static PrivateKeys GetPrivateKeysFromSeed() { while (true) { ConsoleMessage.Write(ConsoleColor.Yellow, "Mnemonic seed (25 words): "); string input = Console.ReadLine(); switch (Mnemonics.MnemonicToPrivateKey(input)) { case ILeft <Error> error: { ConsoleMessage.WriteLine(ConsoleColor.Red, error.Value.errorMessage); ConsoleMessage.WriteLine("Try again.\n"); continue; } case IRight <PrivateKey> key: { var privateSpendKey = key.Value; var privateViewKey = KeyOps.GenerateDeterministicKeys(privateSpendKey) .privateKey; return(new PrivateKeys(privateSpendKey, privateViewKey)); } } } }
NewWallet(string filename, string password, string mnemonicSeed) { /* Derive the mnemonic into a private spend key if possible */ return(Mnemonics.MnemonicToPrivateKey(mnemonicSeed) .Fmap(privateSpendKey => { /* Derive the private view key from the private spend key */ var privateKeys = new PrivateKeys( privateSpendKey, KeyOps.GenerateDeterministicKeys(privateSpendKey).privateKey ); /* Try and create the new wallet from the private keys */ return NewWallet(filename, password, privateKeys); })); }
public void TestMnemonicBothWays() { for (int i = 0; i < 1000; i++) { /* Generate a random private key */ PrivateKey p = KeyOps.GenerateKeys().privateKey; /* Convert to mnemonic */ string m = Mnemonics.PrivateKeyToMnemonic(p); /* Convert back to a private key */ Mnemonics.MnemonicToPrivateKey(m).Do( err => Assert.Fail($"Failed to parse mnemonic seed: {err}"), /* Should be the same as the original private key */ key => Assert.AreEqual <PrivateKey>(p, key) ); } }
public void TestMnemonicToPrivateKey() { string m1 = "optical rumble bamboo worry auctions width essential request oxygen acoustic wounded gawk ginger ornament sixteen pawnshop pairing soapy rift alley enraged orbit axle binocular bamboo"; Mnemonics.MnemonicToPrivateKey(m1).Do( err => Assert.Fail($"Failed to parse mnemonic seed: {err}"), /* The corresponding private key to this mnemonic */ key => Assert.AreEqual <PrivateKey>(key, new PrivateKey("f41d515ac6e84de566f3a9eb559f3e58db6d8b28906857288f9f9f3809846006")) ); string m2 = "psychic frown wetsuit orders rover rays ruffled initiate adhesive acumen among lectures drunk itches tanks highway evolved amended asked thorn nanny juggled vaults velvet adhesive"; Mnemonics.MnemonicToPrivateKey(m2).Do( err => Assert.Fail($"Failed to parse mnemonic seed: {err}"), /* A completely unrelated private key */ key => Assert.AreNotEqual <PrivateKey>(key, new PrivateKey("17a27c50a43b99505d7934c1f05e748deee52bb813d7b1d0654ac980d1a93304")) ); /* Mnemonic with invalid checksum */ string m3 = "double hatchet solved bifocals dozen ulcers sickness sneeze unrest deftly molten oven deity spud upgrade shipped vogue razor gopher sailor drowning epoxy nephew oust spud"; Mnemonics.MnemonicToPrivateKey(m3).Do( err => { /* Expected */ }, key => Assert.Fail($"Mnemonic has invalid checksum but was still successfully parsed!") ); /* Not 25 words */ string m4 = "lol, i'm not valid"; Mnemonics.MnemonicToPrivateKey(m4).Do( err => { /* Expected */ }, key => Assert.Fail($"Mnemonic is invalid length but was still successfully parsed!") ); /* Final word is not in mnemonic dictionary */ string m5 = "daily utopia pistons null giddy pirate return espionage fossil rustled biweekly fictional sedan jubilee asked ugly mystery paper awning titans point luxury eccentric ecstatic word_not_in_dictionary"; Mnemonics.MnemonicToPrivateKey(m5).Do( err => { /* Expected */ }, key => Assert.Fail($"Mnemonic has invalid word but was still successfully parsed!") ); }