/// <summary> /// </summary> /// <param name="test"> /// </param> private static void RunTest(TestVector test) { var constHash = "Bitcoin seed".Select(Convert.ToByte).ToArray(); var headers = new CoinParameters { PublicKeyAddressVersion = 0, PrivateKeyVersion = 128 }; var seed = CryptoUtil.ConvertHex(test.strHexMaster); var key = ExtendedKey.Create(constHash, seed); var pubkey = key.GetPublicKey(); foreach (var derive in test.vDerive) { var skey = key.Serialize(); var spubkey = pubkey.Serialize(); Guard.Require(skey == derive.prv); Guard.Require(spubkey == derive.pub); var pkey = ExtendedKey.Parse(derive.prv); var ppubkey = ExtendedKey.Parse(derive.pub); var wif1 = pkey.GetKey(1).PrivateKey.ToWifKey(headers); var wif2 = key.GetKey(1).PrivateKey.ToWifKey(headers); Guard.Require(wif1 == wif2); var addr1 = ppubkey.GetKey(1).PublicKey.ToAddress(headers); var addr2 = pubkey.GetKey(1).ToAddress(headers); Guard.Require(addr1.ToString() == addr2.ToString()); key = key.GetChild(derive.nChild); if ((derive.nChild & 0x80000000) == 0) { var pubkeyn = pubkey.GetChild(derive.nChild); pubkey = key.GetPublicKey(); Guard.Require(pubkey.Master.ToAddress(headers).ToString() == pubkeyn.Master.ToAddress(headers).ToString()); } else { pubkey = key.GetPublicKey(); } } }
public void TestPathBip44() { const string ExpectedPubKey = "xpub6EncrYPyfQEpEmcMsHEnAXFQY5JkfMcyEWjExM8ppTtJ2y2TR7FfTjEVYxru1Ry9cYdsSkiPjZhv94KyJE8JT8bQuFKHpGzNV4cMscWeRLT"; const string ExpectedPrvKey = "xprvA1oGT2s5q2gX2HXtmFhmoPJfz3UGFtu7sHoe9xjDG8MKAAhJsZwQuvv1hgSHsCV1CDzL6pq9cFPoWgh7pV4TNKwdzCKBvm6MX5YqaoPZWDu"; const string Mnemonic = "thrive empower soon push mountain jeans chimney top jelly sorry beef hard napkin mule matrix today draft high vacuum exercise blind kitchen inflict abstract"; ////var key = CryptoService.CreateNew32ByteKeyPushEntropy(); ////var bip39 = new Bip39(Convert.FromBase64String(key.ConvertToString())); var bip39 = new Bip39(Mnemonic); var usersMasterSeed = bip39.SeedBytes; var prvkey = ExtendedKey.Create(ExtendedKey.Bitcoinseed, usersMasterSeed); var keyPath = ExtendedKeyPathBip44.CreateBip44(0).AddChild(0); var derrived = keyPath.Items.Aggregate(prvkey, (current, item) => current.GetChild(item)); var prv = derrived.Serialize(); var pub = derrived.GetPublicKey().Serialize(); Assert.AreEqual(ExpectedPubKey, pub); Assert.AreEqual(ExpectedPrvKey, prv); }