Skip to content

Commit a320614

Browse files
committed
Refactor wallet disconnect and improve Reown connection flow
Introduces a DisconnectWallet method in ThirdwebManagerBase to centralize wallet disconnection logic and updates the editor to use this method. Refactors ReownWallet connection logic to separate session resumption and interactive connection with improved timeout handling and cleanup, enhancing reliability and maintainability.
1 parent e706eb9 commit a320614

File tree

3 files changed

+103
-44
lines changed

3 files changed

+103
-44
lines changed

Assets/Thirdweb/Editor/ThirdwebManagerEditor.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,11 @@ protected virtual void DrawDebugTab()
133133

134134
if (this.target is ThirdwebManagerBase manager)
135135
{
136-
var wallet = manager.ActiveWallet;
137-
if (wallet != null)
138-
{
139-
EditorApplication.delayCall += async () =>
140-
{
141-
try
142-
{
143-
await wallet.Disconnect();
144-
manager.ActiveWallet = null;
145-
Debug.Log("Active wallet disconnected.");
146-
}
147-
catch (Exception ex)
148-
{
149-
Debug.LogError($"Failed to disconnect active wallet: {ex.Message}");
150-
}
151-
};
152-
}
153-
else
136+
EditorApplication.delayCall += async () =>
154137
{
155-
Debug.LogWarning("No active wallet to disconnect.");
156-
}
138+
await manager.DisconnectWallet();
139+
Debug.Log("Active wallet disconnected.");
140+
};
157141
}
158142
else
159143
{

Assets/Thirdweb/Runtime/Unity/ThirdwebManagerBase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,22 @@ public virtual async Task<IThirdwebWallet> ConnectWallet(WalletOptions walletOpt
562562
}
563563
}
564564

565+
public virtual async Task DisconnectWallet()
566+
{
567+
if (this.ActiveWallet != null)
568+
{
569+
try
570+
{
571+
await this.ActiveWallet.Disconnect();
572+
}
573+
finally
574+
{
575+
this.ActiveWallet = null;
576+
}
577+
}
578+
PlayerPrefs.DeleteKey(THIRDWEB_AUTO_CONNECT_OPTIONS_KEY);
579+
}
580+
565581
public virtual async Task<SmartWallet> UpgradeToSmartWallet(IThirdwebWallet personalWallet, BigInteger chainId, SmartWalletOptions smartWalletOptions)
566582
{
567583
if (personalWallet.AccountType == ThirdwebAccountType.SmartAccount)

Assets/Thirdweb/Runtime/Unity/Wallets/Reown/ReownWallet.cs

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Nethereum.ABI.EIP712;
99
using Reown.AppKit.Unity;
10+
using AccountConnectedEventArgs = Reown.AppKit.Unity.Connector.AccountConnectedEventArgs;
1011

1112
namespace Thirdweb.Unity
1213
{
@@ -70,40 +71,22 @@ string[] excludedWalletIds
7071

7172
ThirdwebDebug.Log("Reown AppKit initialized.");
7273

73-
var connected = false;
74-
75-
try
76-
{
77-
connected = await AppKit.ConnectorController.TryResumeSessionAsync();
78-
}
79-
catch (Exception e)
80-
{
81-
ThirdwebDebug.LogWarning($"Failed to resume Reown session: {e.Message}");
82-
connected = false;
83-
}
74+
var connectionTimeout = TimeSpan.FromSeconds(120);
75+
var connected = await TryResumeExistingSessionAsync();
8476

8577
if (connected)
8678
{
8779
ThirdwebDebug.Log("Resumed previous Reown session.");
8880
}
8981
else
9082
{
91-
ThirdwebDebug.Log("Opening Reown modal... Timeout in 120 seconds.");
92-
AppKit.AccountConnected += (_, e) => connected = true;
93-
AppKit.OpenModal();
94-
95-
var timeoutMs = 120000; // 120 seconds
96-
var intervalMs = 250; // 0.25 second
97-
while (!connected && timeoutMs > 0)
98-
{
99-
await ThirdwebTask.Delay(intervalMs);
100-
timeoutMs -= intervalMs;
101-
}
83+
ThirdwebDebug.Log($"Awaiting Reown connection (timeout {connectionTimeout.TotalSeconds} seconds)...");
84+
connected = await WaitForInteractiveConnectionAsync(connectionTimeout);
10285
}
10386

10487
if (!connected)
10588
{
106-
throw new Exception("Reown connection timed out.");
89+
throw new TimeoutException($"Reown connection timed out after {connectionTimeout.TotalSeconds} seconds.");
10790
}
10891

10992
ThirdwebDebug.Log("Reown wallet connected.");
@@ -262,7 +245,83 @@ public async Task SwitchNetwork(BigInteger chainId)
262245
ThirdwebDebug.Log($"Switched Reown to chain ID {chainId}.");
263246
}
264247

265-
internal static Chain ToWcChain(ThirdwebClient client, BigInteger chainId)
248+
private static async Task<bool> TryResumeExistingSessionAsync()
249+
{
250+
try
251+
{
252+
return await AppKit.ConnectorController.TryResumeSessionAsync();
253+
}
254+
catch (Exception e)
255+
{
256+
ThirdwebDebug.LogWarning($"Failed to resume Reown session: {e.Message}");
257+
try
258+
{
259+
await AppKit.ConnectorController.DisconnectAsync();
260+
}
261+
catch (Exception disconnectException)
262+
{
263+
ThirdwebDebug.LogWarning($"Failed to clean up Reown session after resume error: {disconnectException.Message}");
264+
}
265+
266+
await ThirdwebTask.Delay(1000);
267+
return false;
268+
}
269+
}
270+
271+
private static async Task<bool> WaitForInteractiveConnectionAsync(TimeSpan timeout)
272+
{
273+
if (AppKit.ConnectorController.IsAccountConnected)
274+
{
275+
return true;
276+
}
277+
278+
var connectionEstablished = false;
279+
var connectedTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
280+
281+
void OnAccountConnected(object sender, AccountConnectedEventArgs args)
282+
{
283+
connectionEstablished = true;
284+
if (!connectedTcs.Task.IsCompleted)
285+
{
286+
connectedTcs.SetResult(true);
287+
}
288+
}
289+
290+
AppKit.AccountConnected += OnAccountConnected;
291+
292+
try
293+
{
294+
await ThirdwebTask.Delay(250);
295+
296+
if (!AppKit.ConnectorController.IsAccountConnected)
297+
{
298+
AppKit.OpenModal();
299+
}
300+
301+
var timeoutMilliseconds = (int)Math.Max(0, timeout.TotalMilliseconds);
302+
var timeoutTask = ThirdwebTask.Delay(timeoutMilliseconds);
303+
var completedTask = await Task.WhenAny(connectedTcs.Task, timeoutTask);
304+
305+
if (completedTask == connectedTcs.Task)
306+
{
307+
return await connectedTcs.Task;
308+
}
309+
310+
ThirdwebDebug.LogWarning($"Reown connection timed out after {timeout.TotalSeconds} seconds.");
311+
await AppKit.ConnectorController.DisconnectAsync();
312+
return false;
313+
}
314+
finally
315+
{
316+
AppKit.AccountConnected -= OnAccountConnected;
317+
if (!connectionEstablished)
318+
{
319+
AppKit.CloseModal();
320+
}
321+
}
322+
}
323+
324+
private static Chain ToWcChain(ThirdwebClient client, BigInteger chainId)
266325
{
267326
var wcChain = ChainConstants.Chains.All.FirstOrDefault(c => c.ChainReference == chainId.ToString());
268327

0 commit comments

Comments
 (0)