Skip to content

Commit fadb271

Browse files
authored
Fix main thread encryption/decryption freeze (#180)
1 parent 493f6ae commit fadb271

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public static void DeleteLocalAccount()
316316
File.Delete(GetAccountPath());
317317
}
318318

319-
public static Account UnlockOrGenerateLocalAccount(BigInteger chainId, string password = null, string privateKey = null)
319+
public static async Task<Account> UnlockOrGenerateLocalAccount(BigInteger chainId, string password = null, string privateKey = null)
320320
{
321321
password = string.IsNullOrEmpty(password) ? GetDeviceIdentifier() : password;
322322

@@ -334,7 +334,7 @@ public static Account UnlockOrGenerateLocalAccount(BigInteger chainId, string pa
334334
try
335335
{
336336
var encryptedJson = File.ReadAllText(path);
337-
var key = keyStoreService.DecryptKeyStoreFromJson(password, encryptedJson);
337+
var key = await Task.Run(() => keyStoreService.DecryptKeyStoreFromJson(password, encryptedJson));
338338
return new Account(key, chainId);
339339
}
340340
catch (System.Exception)
@@ -350,13 +350,13 @@ public static Account UnlockOrGenerateLocalAccount(BigInteger chainId, string pa
350350
rng.GetBytes(seed);
351351
}
352352
var ecKey = Nethereum.Signer.EthECKey.GenerateKey(seed);
353-
File.WriteAllText(path, EncryptAndGenerateKeyStore(ecKey, password));
353+
File.WriteAllText(path, await EncryptAndGenerateKeyStore(ecKey, password));
354354
return new Account(ecKey, chainId);
355355
}
356356
}
357357
}
358358

359-
public static string EncryptAndGenerateKeyStore(EthECKey ecKey, string password)
359+
public static async Task<string> EncryptAndGenerateKeyStore(EthECKey ecKey, string password)
360360
{
361361
var keyStoreService = new Nethereum.KeyStore.KeyStoreScryptService();
362362
var scryptParams = new Nethereum.KeyStore.Model.ScryptParams
@@ -366,7 +366,7 @@ public static string EncryptAndGenerateKeyStore(EthECKey ecKey, string password)
366366
R = 1,
367367
P = 8
368368
};
369-
var keyStore = keyStoreService.EncryptAndGenerateKeyStore(password, ecKey.GetPrivateKeyAsBytes(), ecKey.GetPublicAddress(), scryptParams);
369+
var keyStore = await Task.Run(() => keyStoreService.EncryptAndGenerateKeyStore(password, ecKey.GetPrivateKeyAsBytes(), ecKey.GetPublicAddress(), scryptParams));
370370
return keyStoreService.SerializeKeyStoreToJson(keyStore);
371371
}
372372

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public async Task<string> Export(string password)
9191
else
9292
{
9393
var localAccount = ThirdwebManager.Instance.SDK.Session.ActiveWallet.GetLocalAccount() ?? throw new Exception("No local account found");
94-
return Utils.EncryptAndGenerateKeyStore(new EthECKey(localAccount.PrivateKey), password);
94+
return await Utils.EncryptAndGenerateKeyStore(new EthECKey(localAccount.PrivateKey), password);
9595
}
9696
}
9797

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebLocalWallet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public ThirdwebLocalWallet()
2020
_signerProvider = WalletProvider.LocalWallet;
2121
}
2222

23-
public Task<string> Connect(WalletConnection walletConnection, string rpc)
23+
public async Task<string> Connect(WalletConnection walletConnection, string rpc)
2424
{
25-
_account = Utils.UnlockOrGenerateLocalAccount(walletConnection.chainId, walletConnection.password, null);
25+
_account = await Utils.UnlockOrGenerateLocalAccount(walletConnection.chainId, walletConnection.password, null);
2626
_web3 = new Web3(_account, rpc);
27-
return Task.FromResult(_account.Address);
27+
return _account.Address;
2828
}
2929

3030
public Task Disconnect(bool endSession = true)

0 commit comments

Comments
 (0)