/// <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); }
/// <summary> /// Create a new wallet with mnemonic /// </summary> /// <param name="password"></param> /// <returns></returns> public bool Create(string password) { if (IsCreated) { Logger.Warn($"Wallet already created."); return(true); } Logger.Info($"Creating new wallet."); byte[] randomBytes = new byte[48]; _random.NextBytes(randomBytes); var memoryBytes = randomBytes.AsMemory(); var pswBytes = Encoding.UTF8.GetBytes(password); var salt = memoryBytes.Slice(0, 16).ToArray(); var seed = memoryBytes.Slice(16, 32).ToArray(); pswBytes = SHA256.Create().ComputeHash(pswBytes); var encryptedSeed = ManagedAes.EncryptStringToBytes_Aes(Utils.Bytes2HexString(seed, Utils.HexStringFormat.PURE), pswBytes, salt); _walletFile = new WalletFile(encryptedSeed, salt); Caching.Persist(_path, _walletFile); Chaos.NaCl.Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] privateKey, seed); Account = new Account(KeyType.ED25519, privateKey, publicKey); return(true); }