@@ -47,6 +47,8 @@ public class WalletConnect : BindableMonoBehavior
4747
4848 [ BindComponent ]
4949 private NativeWebSocketTransport _transport ;
50+ private bool initialized ;
51+ private bool eventsSetup ;
5052
5153 public static WalletConnect Instance ;
5254
@@ -63,6 +65,10 @@ protected override void Awake()
6365 return ;
6466 }
6567
68+ ClearSession ( ) ;
69+ if ( Session != null )
70+ Session = null ;
71+
6672 base . Awake ( ) ;
6773 }
6874
@@ -103,90 +109,85 @@ public void SetMode(bool active)
103109
104110 public async Task < WCSessionData > Connect ( int chainId )
105111 {
106- while ( true )
112+ SavedSession savedSession = null ;
113+ if ( PlayerPrefs . HasKey ( SessionKey ) )
107114 {
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+ }
116118
117- if ( Session != null )
119+ if ( Session != null )
120+ {
121+ if ( savedSession != null )
122+ {
123+ if ( Session . KeyData != savedSession . Key )
118124 {
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 )
142126 await Session . Disconnect ( ) ;
143- }
144127 else if ( Session . TransportConnected )
145- {
146128 await Session . Transport . Close ( ) ;
147- }
148- // else if (Session.Connecting)
149- // {
150- // Debug.LogWarning("Session connecting...");
151- // return null;
152- // }
153129 }
154-
155- if ( savedSession != null )
130+ else if ( ! Session . Connected && ! Session . Connecting )
156131 {
157- Session = new WalletConnectUnitySession ( savedSession , this , _transport ) ;
132+ StartCoroutine ( SetupDefaultWallet ( ) ) ;
133+ SetupEvents ( ) ;
134+ return await CompleteConnect ( ) ;
158135 }
159136 else
160137 {
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 ;
175140 }
176-
177- StartCoroutine ( SetupDefaultWallet ( ) ) ;
178- SetupEvents ( ) ;
179- return await CompleteConnect ( ) ;
180141 }
181- catch ( System . Exception e )
142+ else if ( Session . SessionConnected )
143+ {
144+ await Session . Disconnect ( ) ;
145+ }
146+ else if ( Session . TransportConnected )
182147 {
183- Debug . LogWarning ( "WalletConnect.Connect Error, Regeneratinge | " + e . Message ) ;
148+ await Session . Transport . Close ( ) ;
184149 }
150+ // else if (Session.Connecting)
151+ // {
152+ // Debug.LogWarning("Session connecting...");
153+ // return null;
154+ // }
185155 }
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 ( ) ;
186182 }
187183
188184 private void SetupEvents ( )
189185 {
186+ if ( eventsSetup )
187+ return ;
188+
189+ eventsSetup = true ;
190+
190191 Session . OnSessionConnect += ( sender , session ) =>
191192 {
192193 Debug . LogWarning ( "[WalletConnect] Session Connected" ) ;
@@ -212,8 +213,7 @@ private async Task<WCSessionData> CompleteConnect()
212213 {
213214 try
214215 {
215- var session = await Session . SourceConnectSession ( ) ;
216- return session ;
216+ return await Session . SourceConnectSession ( ) ;
217217 }
218218 catch ( IOException e )
219219 {
@@ -309,18 +309,14 @@ private IEnumerator DownloadImagesFor(string id, string[] sizes = null)
309309 }
310310 }
311311
312- private void OnDestroy ( )
313- {
314- SaveSession ( ) ;
315- }
316-
317- private void OnApplicationQuit ( )
318- {
319- SaveSession ( ) ;
320- }
321-
322312 private async void OnApplicationPause ( bool pauseStatus )
323313 {
314+ if ( ! initialized )
315+ {
316+ initialized = true ;
317+ return ;
318+ }
319+
324320 if ( pauseStatus )
325321 SaveSession ( ) ;
326322 else if ( PlayerPrefs . HasKey ( SessionKey ) )
0 commit comments