Beispiel #1
0
        public HdKeyStorage Unlock(SecureString password)
        {
            try
            {
                using var scopedSeed = new ScopedBytes(Aes.Decrypt(
                                                           encryptedBytes: Hex.FromString(EncryptedSeed),
                                                           password: password,
                                                           keySize: AesKeySize,
                                                           saltSize: AesSaltSize,
                                                           iterations: AesRfc2898Iterations));

                Seed = new SecureBytes(scopedSeed);

                foreach (var singleKey in NonHdKeys)
                {
                    singleKey.Unlock(password);
                }
            }
            catch (Exception e)
            {
                Log.Error(e, "Unlock error");
            }

            return(this);
        }
Beispiel #2
0
        public static HdKeyStorage LoadFromFile(string pathToFile, SecureString password)
        {
            if (!File.Exists(pathToFile))
            {
                throw new FileNotFoundException($"File {pathToFile} not found.");
            }

            if (new FileInfo(pathToFile).Length > MaxFileSizeInBytes)
            {
                throw new Exception("File is too large");
            }

            HdKeyStorage result;

            try
            {
                using var stream = new FileStream(pathToFile, FileMode.Open);
                var network = stream.ReadByte();

                var encryptedBytes = stream.ReadBytes((int)stream.Length - 1);

                var decryptedBytes = Aes.Decrypt(
                    encryptedBytes: encryptedBytes,
                    password: password,
                    keySize: AesKeySize,
                    saltSize: AesSaltSize,
                    iterations: AesRfc2898Iterations);

                var json = Encoding.UTF8.GetString(decryptedBytes);

                result = JsonConvert.DeserializeObject <HdKeyStorage>(json);

                if (result.Network != (Network)network)
                {
                    throw new Exception("Wallet type does not match the type specified during creation");
                }

                if (result.Version != CurrentVersion)
                {
                    throw new NotSupportedException($"Version {result.Version} does not match {CurrentVersion}");
                }
            }
            catch (Exception e)
            {
                Log.Error(e, "HdKeyStorage data loading error");
                throw;
            }

            return(result);
        }
Beispiel #3
0
        public void Unlock(SecureString password)
        {
            try
            {
                using var scopedSeed = new ScopedBytes(Aes.Decrypt(
                                                           encryptedBytes: Hex.FromString(EncryptedSeed),
                                                           password: password,
                                                           keySize: AesKeySize,
                                                           saltSize: AesSaltSize,
                                                           iterations: AesRfc2898Iterations));

                Seed = new SecureBytes(scopedSeed);
            }
            catch (Exception e)
            {
                Log.Error(e, "Unlock error");
            }
        }
Beispiel #4
0
        public void Encrypt(SecureString password)
        {
            try
            {
                using var scopedSeed = Seed.ToUnsecuredBytes();

                EncryptedSeed = Aes.Encrypt(
                    plainBytes: scopedSeed,
                    password: password,
                    keySize: AesKeySize,
                    saltSize: AesSaltSize,
                    iterations: AesRfc2898Iterations)
                                .ToHexString();
            }
            catch (Exception e)
            {
                Log.Error(e, "Encrypt error");
            }
        }
Beispiel #5
0
        public void SaveToFile(string pathToFile, SecureString password)
        {
            try
            {
                using var stream = new FileStream(pathToFile, FileMode.Create);
                var serialized      = JsonConvert.SerializeObject(this, Formatting.Indented);
                var serializedBytes = Encoding.UTF8.GetBytes(serialized);

                var encryptedBytes = Aes.Encrypt(
                    plainBytes: serializedBytes,
                    password: password,
                    keySize: AesKeySize,
                    saltSize: AesSaltSize,
                    iterations: AesRfc2898Iterations);

                stream.WriteByte((byte)Network);
                stream.Write(encryptedBytes, 0, encryptedBytes.Length);
            }
            catch (Exception e)
            {
                Log.Error(e, "HdKeyStorage save to file error");
            }
        }