public void TestEd25519() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\ed25519.json")) { var hdKey = HDKey.FromSeed(Hex.Parse((string)sample.seed)) .Derive((string)sample.path); Assert.Equal(sample.privateKey, hdKey.Key.GetHex()); Assert.Equal(sample.chainCode, Hex.Convert(hdKey.ChainCode)); Assert.Equal(sample.pubKey, hdKey.PubKey.GetHex()); } }
public void TestNistp256() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\nistp256.json")) { var hdKey = HDKey.FromSeed(Hex.Parse((string)sample.seed), ECKind.NistP256) .Derive((string)sample.path); Assert.Equal(sample.privateKey, hdKey.Key.GetHex()); Assert.Equal(sample.chainCode, Hex.Convert(hdKey.ChainCode)); Assert.Equal(sample.pubKey, hdKey.PubKey.GetHex()); TestPublicKeyDerivation(sample.path, sample.seed, ECKind.NistP256); } }
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; } }