public static ExtendedKey Parse(string serialized) { byte[] data = Base58Encoding.DecodeWithCheckSum(serialized); Thrower.Condition <AddressException>(data.Length != 78, "invalid extended key"); using (var stream = new MemoryStream(data)) { using (var binReader = new BinaryReader(stream)) { var ext = binReader.ReadBytes(4); Thrower.Condition <AddressException>(!(ext.SequenceEqual(Xprv) || ext.SequenceEqual(Xpub)), "invalid magic number for an extended key"); var isPrivate = ext.SequenceEqual(Xprv); var depth = binReader.ReadByte(); var fingerprint = binReader.ReadBytes(4); var index = BitHelper.ToUInt32(binReader.ReadBytes(4), false); var chainCode = binReader.ReadBytes(32); var rawKey = binReader.ReadBytes(33); BitcoinKey key; if (isPrivate) { key = new BitcoinPrivateKey(rawKey.Skip(1).ToArray()); } else { key = new BitcoinPublicKey(rawKey); } return(new ExtendedKey(key, chainCode, depth, fingerprint, index)); } } }
/// <summary> /// The standard of creating an extended key from a given seed. /// </summary> public static ExtendedKey Create(byte[] hashKey, byte[] seed) { var computedHash = CryptoUtil.ComputeHmac512(hashKey, seed); var key = new BitcoinPrivateKey(computedHash.Take(32).ToArray()); var chain = computedHash.Skip(32).ToArray(); return(new ExtendedKey(key, chain, 0, BitHelper.GetBytes(0), 0)); }