From 77a11721bf47ea53383756fda4d27247a2a46b2b Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Wed, 6 Nov 2024 20:34:36 +0700 Subject: [PATCH 1/2] Migration - BouncyCastle Fallback --- .../EmbeddedWallet/EmbeddedWallet.Misc.cs | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs b/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs index ac23073..1446dde 100644 --- a/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs +++ b/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs @@ -1,5 +1,10 @@ using System.Security.Cryptography; using Nethereum.Web3.Accounts; +using System; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Paddings; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Parameters; namespace Thirdweb.EWS; @@ -58,6 +63,7 @@ public async Task SignOutAsync() var privateKeyBytes = utf8WithoutBom.GetBytes(privateKey); byte[] encryptedPrivateKeyBytes; + try { using var aes = Aes.Create(); @@ -71,9 +77,32 @@ public async Task SignOutAsync() using var encryptor = aes.CreateEncryptor(); encryptedPrivateKeyBytes = encryptor.TransformFinalBlock(privateKeyBytes, 0, privateKeyBytes.Length); } - catch (Exception ex) + // Fallback to BouncyCastle + catch (Exception) { - throw new InvalidOperationException("Encryption failed.", ex); + try + { + var key = Convert.FromBase64String(plainTextBase64); + + var engine = new AesEngine(); + var blockCipher = new Org.BouncyCastle.Crypto.Modes.CbcBlockCipher(engine); + var cipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding()); + + var keyParam = new KeyParameter(key); + var keyParamWithIV = new ParametersWithIV(keyParam, iv); + + cipher.Init(true, keyParamWithIV); + + encryptedPrivateKeyBytes = new byte[cipher.GetOutputSize(privateKeyBytes.Length)]; + var length = cipher.ProcessBytes(privateKeyBytes, 0, privateKeyBytes.Length, encryptedPrivateKeyBytes, 0); + length += cipher.DoFinal(encryptedPrivateKeyBytes, length); + + Array.Resize(ref encryptedPrivateKeyBytes, length); + } + catch (Exception ex) + { + throw new InvalidOperationException("Migration failed", ex); + } } var encryptedData = new byte[iv.Length + encryptedPrivateKeyBytes.Length]; From c6a99a7f174b3206410b84ee2c5fbeb44858db74 Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Wed, 6 Nov 2024 20:34:47 +0700 Subject: [PATCH 2/2] Update EmbeddedWallet.Misc.cs --- .../InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs b/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs index 1446dde..02917ba 100644 --- a/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs +++ b/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs @@ -1,7 +1,5 @@ using System.Security.Cryptography; using Nethereum.Web3.Accounts; -using System; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Paddings; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters;