@@ -47,6 +47,8 @@ public class WalletConnect : BindableMonoBehavior
47
47
48
48
[ BindComponent ]
49
49
private NativeWebSocketTransport _transport ;
50
+ private bool initialized ;
51
+ private bool eventsSetup ;
50
52
51
53
public static WalletConnect Instance ;
52
54
@@ -63,6 +65,10 @@ protected override void Awake()
63
65
return ;
64
66
}
65
67
68
+ ClearSession ( ) ;
69
+ if ( Session != null )
70
+ Session = null ;
71
+
66
72
base . Awake ( ) ;
67
73
}
68
74
@@ -103,90 +109,85 @@ public void SetMode(bool active)
103
109
104
110
public async Task < WCSessionData > Connect ( int chainId )
105
111
{
106
- while ( true )
112
+ SavedSession savedSession = null ;
113
+ if ( PlayerPrefs . HasKey ( SessionKey ) )
107
114
{
108
- try
109
- {
110
- SavedSession savedSession = null ;
111
- if ( PlayerPrefs . HasKey ( SessionKey ) )
112
- {
113
- var json = PlayerPrefs . GetString ( SessionKey ) ;
114
- savedSession = JsonConvert . DeserializeObject < SavedSession > ( json ) ;
115
- }
115
+ var json = PlayerPrefs . GetString ( SessionKey ) ;
116
+ savedSession = JsonConvert . DeserializeObject < SavedSession > ( json ) ;
117
+ }
116
118
117
- if ( Session != null )
119
+ if ( Session != null )
120
+ {
121
+ if ( savedSession != null )
122
+ {
123
+ if ( Session . KeyData != savedSession . Key )
118
124
{
119
- if ( savedSession != null )
120
- {
121
- if ( Session . KeyData != savedSession . Key )
122
- {
123
- if ( Session . SessionConnected )
124
- await Session . Disconnect ( ) ;
125
- else if ( Session . TransportConnected )
126
- await Session . Transport . Close ( ) ;
127
- }
128
- else if ( ! Session . Connected && ! Session . Connecting )
129
- {
130
- StartCoroutine ( SetupDefaultWallet ( ) ) ;
131
- SetupEvents ( ) ;
132
- return await CompleteConnect ( ) ;
133
- }
134
- else
135
- {
136
- Debug . LogWarning ( "Already Connected" ) ;
137
- return null ;
138
- }
139
- }
140
- else if ( Session . SessionConnected )
141
- {
125
+ if ( Session . SessionConnected )
142
126
await Session . Disconnect ( ) ;
143
- }
144
127
else if ( Session . TransportConnected )
145
- {
146
128
await Session . Transport . Close ( ) ;
147
- }
148
- // else if (Session.Connecting)
149
- // {
150
- // Debug.LogWarning("Session connecting...");
151
- // return null;
152
- // }
153
129
}
154
-
155
- if ( savedSession != null )
130
+ else if ( ! Session . Connected && ! Session . Connecting )
156
131
{
157
- Session = new WalletConnectUnitySession ( savedSession , this , _transport ) ;
132
+ StartCoroutine ( SetupDefaultWallet ( ) ) ;
133
+ SetupEvents ( ) ;
134
+ return await CompleteConnect ( ) ;
158
135
}
159
136
else
160
137
{
161
- Session = new WalletConnectUnitySession (
162
- new ClientMeta ( )
163
- {
164
- Name = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appName ,
165
- Description = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appDescription ,
166
- URL = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appUrl ,
167
- Icons = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appIcons ,
168
- } ,
169
- this ,
170
- null ,
171
- _transport ,
172
- null ,
173
- chainId
174
- ) ;
138
+ Debug . LogWarning ( "Already Connected" ) ;
139
+ return null ;
175
140
}
176
-
177
- StartCoroutine ( SetupDefaultWallet ( ) ) ;
178
- SetupEvents ( ) ;
179
- return await CompleteConnect ( ) ;
180
141
}
181
- catch ( System . Exception e )
142
+ else if ( Session . SessionConnected )
143
+ {
144
+ await Session . Disconnect ( ) ;
145
+ }
146
+ else if ( Session . TransportConnected )
182
147
{
183
- Debug . LogWarning ( "WalletConnect.Connect Error, Regeneratinge | " + e . Message ) ;
148
+ await Session . Transport . Close ( ) ;
184
149
}
150
+ // else if (Session.Connecting)
151
+ // {
152
+ // Debug.LogWarning("Session connecting...");
153
+ // return null;
154
+ // }
185
155
}
156
+
157
+ if ( savedSession != null )
158
+ {
159
+ Session = new WalletConnectUnitySession ( savedSession , this , _transport ) ;
160
+ }
161
+ else
162
+ {
163
+ Session = new WalletConnectUnitySession (
164
+ new ClientMeta ( )
165
+ {
166
+ Name = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appName ,
167
+ Description = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appDescription ,
168
+ URL = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appUrl ,
169
+ Icons = ThirdwebManager . Instance . SDK . nativeSession . options . wallet ? . appIcons ,
170
+ } ,
171
+ this ,
172
+ null ,
173
+ _transport ,
174
+ null ,
175
+ chainId
176
+ ) ;
177
+ }
178
+
179
+ StartCoroutine ( SetupDefaultWallet ( ) ) ;
180
+ SetupEvents ( ) ;
181
+ return await CompleteConnect ( ) ;
186
182
}
187
183
188
184
private void SetupEvents ( )
189
185
{
186
+ if ( eventsSetup )
187
+ return ;
188
+
189
+ eventsSetup = true ;
190
+
190
191
Session . OnSessionConnect += ( sender , session ) =>
191
192
{
192
193
Debug . LogWarning ( "[WalletConnect] Session Connected" ) ;
@@ -212,8 +213,7 @@ private async Task<WCSessionData> CompleteConnect()
212
213
{
213
214
try
214
215
{
215
- var session = await Session . SourceConnectSession ( ) ;
216
- return session ;
216
+ return await Session . SourceConnectSession ( ) ;
217
217
}
218
218
catch ( IOException e )
219
219
{
@@ -309,18 +309,14 @@ private IEnumerator DownloadImagesFor(string id, string[] sizes = null)
309
309
}
310
310
}
311
311
312
- private void OnDestroy ( )
313
- {
314
- SaveSession ( ) ;
315
- }
316
-
317
- private void OnApplicationQuit ( )
318
- {
319
- SaveSession ( ) ;
320
- }
321
-
322
312
private async void OnApplicationPause ( bool pauseStatus )
323
313
{
314
+ if ( ! initialized )
315
+ {
316
+ initialized = true ;
317
+ return ;
318
+ }
319
+
324
320
if ( pauseStatus )
325
321
SaveSession ( ) ;
326
322
else if ( PlayerPrefs . HasKey ( SessionKey ) )
0 commit comments