public void TestHDKeyGenerationNist() { var path = new HDPath("m/44/1729/0/0/1"); var key = new HDKey(ECKind.NistP256); var derived = key.Derive(path); var pubDerived = key.HDPubKey.Derive(path); Assert.Equal(derived.Address, pubDerived.Address); }
public void Kukai() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\kukai.json")) { var hdKey = HDKey.FromMnemonic(Mnemonic.Parse((string)sample.mnemonic)) .Derive((string)sample.path); Assert.Equal(sample.address, hdKey.Address); } }
public void Kukai() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\kukai.json")) { var password = string.IsNullOrEmpty(sample.password) ? "" : sample.password; var hdKey = HDKey.FromMnemonic(Mnemonic.Parse((string)sample.mnemonic), password) .Derive((string)sample.path); Assert.Equal(sample.address, hdKey.Address); } }
public void Atomex() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\atomex.json")) { var hdKey = HDKey.FromMnemonic(Mnemonic.Parse((string)sample.mnemonic)) .Derive((string)sample.path); Assert.Equal(sample.privateKey, hdKey.Key.GetBase58()); Assert.Equal(sample.address, hdKey.Address); } }
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 (KeyPath KeyPath, Key Key) GetNextKey() { if (HDKey is null || NextKeyPath is null) { throw new InvalidOperationException("Invalid keyset"); } var k = HDKey.Derive(NextKeyPath).PrivateKey; var path = NextKeyPath; NextKeyPath = NextKeyPath.Increment() !; return(path, k); }
public void TestHdKeyGenerationSecp() { var path = new HDPath("m/44/1729/0/0/0"); var key = new HDKey(ECKind.Secp256k1); var anotherKey = new HDKey(ECKind.Secp256k1); Assert.NotEqual(key.Address, anotherKey.Address); var derived = key.Derive(path); var pubDerived = key.HDPubKey.Derive(path); Assert.Equal(derived.Address, pubDerived.Address); }
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; } }
public Key GetKey(RootedKeyPath keyPath) { if (SingleKey is Key) { if (SingleKey.PubKey.GetHDFingerPrint() != keyPath.MasterFingerprint) { throw new InvalidOperationException("The fingerprint of the keyset, does not match the mnemonic"); } if (keyPath.KeyPath.Indexes.Length != 0) { throw new InvalidOperationException("Indices in the keypath are not valid for this keyset"); } return(SingleKey); } if (HDKey is null) { throw new InvalidOperationException("The keyset does not have a mnemonic set"); } if (HDKey.GetPublicKey().GetHDFingerPrint() != keyPath.MasterFingerprint) { throw new InvalidOperationException("The fingerprint of the keyset, does not match the mnemonic"); } return(HDKey.Derive(keyPath.KeyPath).PrivateKey); }