|
7 | 7 | using System.Threading.Tasks; |
8 | 8 | using Nethereum.ABI.EIP712; |
9 | 9 | using Reown.AppKit.Unity; |
| 10 | +using AccountConnectedEventArgs = Reown.AppKit.Unity.Connector.AccountConnectedEventArgs; |
10 | 11 |
|
11 | 12 | namespace Thirdweb.Unity |
12 | 13 | { |
@@ -70,40 +71,22 @@ string[] excludedWalletIds |
70 | 71 |
|
71 | 72 | ThirdwebDebug.Log("Reown AppKit initialized."); |
72 | 73 |
|
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(); |
84 | 76 |
|
85 | 77 | if (connected) |
86 | 78 | { |
87 | 79 | ThirdwebDebug.Log("Resumed previous Reown session."); |
88 | 80 | } |
89 | 81 | else |
90 | 82 | { |
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); |
102 | 85 | } |
103 | 86 |
|
104 | 87 | if (!connected) |
105 | 88 | { |
106 | | - throw new Exception("Reown connection timed out."); |
| 89 | + throw new TimeoutException($"Reown connection timed out after {connectionTimeout.TotalSeconds} seconds."); |
107 | 90 | } |
108 | 91 |
|
109 | 92 | ThirdwebDebug.Log("Reown wallet connected."); |
@@ -262,7 +245,83 @@ public async Task SwitchNetwork(BigInteger chainId) |
262 | 245 | ThirdwebDebug.Log($"Switched Reown to chain ID {chainId}."); |
263 | 246 | } |
264 | 247 |
|
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) |
266 | 325 | { |
267 | 326 | var wcChain = ChainConstants.Chains.All.FirstOrDefault(c => c.ChainReference == chainId.ToString()); |
268 | 327 |
|
|
0 commit comments