/// <summary> /// Unlocks the asynchronous. /// </summary> /// <param name="password">The password.</param> /// <param name="noCheck">if set to <c>true</c> [no check].</param> /// <returns></returns> /// <exception cref="Exception">Public key check failed!</exception> public async Task <bool> UnlockAsync(string password, bool noCheck = false) { if (IsUnlocked || !IsCreated) { Logger.Warn("Wallet is already unlocked or doesn't exist."); return(IsUnlocked && IsCreated); } Logger.Info("Unlock new wallet."); try { var pswBytes = Encoding.UTF8.GetBytes(password); pswBytes = SHA256.Create().ComputeHash(pswBytes); var seed = ManagedAes.DecryptStringFromBytes_Aes(_walletFile.EncryptedSeed, pswBytes, _walletFile.Salt); byte[] publicKey = null; byte[] privateKey = null; switch (_walletFile.KeyType) { case KeyType.Ed25519: Ed25519.KeyPairFromSeed(out publicKey, out privateKey, Utils.HexToByteArray(seed)); break; case KeyType.Sr25519: var miniSecret = new MiniSecret(Utils.HexToByteArray(seed), ExpandMode.Ed25519); var getPair = miniSecret.GetPair(); privateKey = getPair.Secret.ToBytes(); publicKey = getPair.Public.Key; break; } if (noCheck || !publicKey.SequenceEqual(_walletFile.PublicKey)) { throw new Exception("Public key check failed!"); } Account = Account.Build(_walletFile.KeyType, privateKey, publicKey); } catch (Exception exception) { Logger.Warn($"Couldn't unlock the wallet with this password. {exception}"); return(false); } if (IsOnline) { _subscriptionAccountInfo = await SubscribeAccountInfoAsync(); } return(true); }
/// <summary> /// Unlock a locked wallet. /// </summary> /// <param name="password"></param> /// <returns></returns> public bool Unlock(string password) { if (IsUnlocked || !IsCreated) { Logger.Warn($"Wallet is already unlocked or doesn't exist."); return(IsUnlocked && IsCreated); } Logger.Info($"Unlock new wallet."); var pswBytes = Encoding.UTF8.GetBytes(password); pswBytes = SHA256.Create().ComputeHash(pswBytes); var seed = ManagedAes.DecryptStringFromBytes_Aes(_walletFile.encryptedSeed, pswBytes, _walletFile.salt); Chaos.NaCl.Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] privateKey, Utils.HexToByteArray(seed)); Account = new Account(KeyType.ED25519, privateKey, publicKey); return(true); }