Пример #1
0
        public async Task SetPasswordAsync(string password)
        {
            Mnemonic seedWords = null;
            await Task.Run(() =>
            {
                // Here we are not letting anything that will be autocorrected later.
                // Generate wallet with password exactly as entered for compatibility.
                PasswordHelper.Guard(password);

                string walletFilePath = Path.Combine(_walletManager.WalletDirectories.WalletsDir, $"{_config.Network}.json");
                KeyManager.CreateNew(out seedWords, password, walletFilePath);
            });

            await _storage.SetSeedWords(password, seedWords.ToString());

            // this should not be a config
            _uiConfig.HasSeed = true;
            _uiConfig.ToFile();
        }
        public async Task LoadWallet()
        {
            SeedWords = Guard.Correct(SeedWords);
            Password  = Guard.Correct(Password); // Do not let whitespaces to the beginning and to the end.

            string walletFilePath = Path.Combine(_walletManager.WalletDirectories.WalletsDir, $"{_config.Network}.json");

            Mnemonic mnemonic = null;
            await Task.Run(() =>
            {
                KeyPath.TryParse(ACCOUNT_KEY_PATH, out KeyPath keyPath);

                mnemonic = new Mnemonic(SeedWords);
                var km   = KeyManager.Recover(mnemonic, Password, filePath: null, keyPath, MIN_GAP_LIMIT);
                km.SetNetwork(_config.Network);
                km.SetFilePath(walletFilePath);
                _walletManager.AddWallet(km);
            });

            await _storage.SetSeedWords(Password, mnemonic.ToString());

            _uiConfig.HasSeed = true;
            _uiConfig.ToFile();
        }
Пример #3
0
        public async Task InitSeedWords(string password)
        {
            string     wordString = null;
            KeyManager keyManager = null;

            try
            {
                // ensure correct pw
                PasswordHelper.Guard(password);
                string walletFilePath = Path.Combine(_walletManager.WalletDirectories.WalletsDir, $"{_config.Network}.json");
                await Task.Run(() =>
                {
                    keyManager = KeyManager.FromFile(walletFilePath);
                    keyManager.GetMasterExtKey(password ?? "");
                });

                await Task.Run(async() =>
                {
                    // this next line doesn't really run async :/
                    wordString = await _storage.GetSeedWords(password);
                    if (wordString is null)
                    {
                        throw new KeyNotFoundException();
                    }
                });
            }
            catch (SecurityException e)
            {
                // toss bad password to UI
                throw e;
            }
            // KeyNotFoundException || ArgumentException
            catch
            {
                // try migrate from the legacy system
                var seedWords = await _hsm.GetAsync(LegacyWordsLoc);

                if (string.IsNullOrEmpty(seedWords))
                {
                    // check if corrupted and show message
                    throw new InvalidOperationException("Try again if you cancelled the biometric authentication. Otherwise, there are no seed words saved. Please back up using \"Export Wallet File\"");
                }

                // check if words match the wallet file
                KeyManager legacyKM = null;
                await Task.Run(() =>
                {
                    KeyPath.TryParse(ACCOUNT_KEY_PATH, out KeyPath keyPath);

                    var mnemonic = new Mnemonic(seedWords);
                    legacyKM     = KeyManager.Recover(mnemonic, password, filePath: null, keyPath, MIN_GAP_LIMIT);
                });

                if (legacyKM.EncryptedSecret != keyManager.EncryptedSecret)
                {
                    throw new InvalidOperationException("Corrupt seed words. Please back up using \"Export Wallet File\" instead.");
                }

                await _storage.SetSeedWords(password, seedWords);

                wordString = seedWords;
            }
            finally
            {
                SeedWords = wordString?.Split(' ').ToList();
            }
        }