Esempio n. 1
0
        public void TestHDPathParse()
        {
            Assert.Equal("m", HDPath.Parse("").ToString());
            Assert.Equal("m", HDPath.Parse("m").ToString());
            Assert.Equal("m", HDPath.Parse("/").ToString());
            Assert.Equal("m", HDPath.Parse("m/").ToString());
            Assert.Equal("m/1", HDPath.Parse("1").ToString());
            Assert.Equal("m/1", HDPath.Parse("/1").ToString());
            Assert.Equal("m/1", HDPath.Parse("m/1").ToString());
            Assert.Equal("m/1/2'/3'", HDPath.Parse("m/1/2'/3h").ToString());

            Assert.Throws <ArgumentNullException>(() => HDPath.Parse(null));
            Assert.Throws <FormatException>(() => HDPath.Parse("m/1//2"));
            Assert.Throws <FormatException>(() => HDPath.Parse("m/m"));
            Assert.Throws <FormatException>(() => HDPath.Parse("m/-1"));
            Assert.Throws <FormatException>(() => HDPath.Parse($"m/{0x80000000u}"));
        }
Esempio n. 2
0
        static void TestPublicKeyDerivation(string path, string seed, ECKind kind = ECKind.Secp256k1)
        {
            var masterKey    = HDKey.FromSeed(Hex.Parse(seed), kind);
            var masterPubKey = masterKey.HDPubKey;

            foreach (var uind in HDPath.Parse(path))
            {
                var childKey    = masterKey.Derive((int)(uind & 0x7FFFFFFF), (uind & 0x80000000) != 0);
                var childPubKey = childKey.HDPubKey;

                if ((uind & 0x80000000) == 0)
                {
                    var derivedPubKey = masterPubKey.Derive((int)uind);
                    Assert.Equal(derivedPubKey.PubKey.GetBase58(), childPubKey.PubKey.GetBase58());
                    Assert.Equal(Hex.Convert(derivedPubKey.ChainCode), Hex.Convert(childPubKey.ChainCode));
                }

                masterKey    = childKey;
                masterPubKey = childPubKey;
            }
        }