@@ -23,11 +23,10 @@ public class WalletConnect : IWalletConnect
23
23
public static IWalletConnect Instance { get ; } = LazyInstance . Value ;
24
24
25
25
public static SynchronizationContext UnitySyncContext { get ; private set ; }
26
-
26
+
27
27
public ISignClient SignClient { get ; private set ; }
28
28
29
29
public Linker Linker { get ; private set ; }
30
-
31
30
32
31
public SessionStruct ActiveSession => SignClient . AddressProvider . DefaultSession ;
33
32
@@ -40,7 +39,7 @@ public class WalletConnect : IWalletConnect
40
39
[ Obsolete ( "Use SessionConnected or SessionUpdated instead" ) ]
41
40
public event EventHandler < SessionStruct > ActiveSessionChanged ;
42
41
public event EventHandler < SessionStruct > SessionConnected ;
43
- public event EventHandler < SessionStruct > SessionUpdated ;
42
+ public event EventHandler < SessionStruct > SessionUpdated ;
44
43
public event EventHandler SessionDisconnected ;
45
44
46
45
private SessionStruct _activeSession ;
@@ -57,37 +56,40 @@ public async Task<IWalletConnect> InitializeAsync()
57
56
Debug . LogError ( "[WalletConnectUnity] Already initialized" ) ;
58
57
return this ;
59
58
}
60
-
59
+
61
60
var currentSyncContext = SynchronizationContext . Current ;
62
61
if ( currentSyncContext . GetType ( ) . FullName != "UnityEngine.UnitySynchronizationContext" )
63
62
throw new Exception (
64
- $ "[WalletConnectUnity] SynchronizationContext is not of type UnityEngine.UnitySynchronizationContext. Current type is <i>{ currentSyncContext . GetType ( ) . FullName } </i>. Make sure to initialize WalletConnect from the main thread.") ;
63
+ $ "[WalletConnectUnity] SynchronizationContext is not of type UnityEngine.UnitySynchronizationContext. Current type is <i>{ currentSyncContext . GetType ( ) . FullName } </i>. Make sure to initialize WalletConnect from the main thread."
64
+ ) ;
65
65
UnitySyncContext = currentSyncContext ;
66
-
66
+
67
67
var projectConfig = ProjectConfiguration . Load ( ) ;
68
68
69
- Assert . IsNotNull ( projectConfig ,
70
- $ "Project configuration not found. Expected to find it at <i>{ ProjectConfiguration . ConfigPath } </i>") ;
71
- Assert . IsFalse ( string . IsNullOrWhiteSpace ( projectConfig . Id ) ,
72
- $ "Project ID is not set in the project configuration asset ( <i> { ProjectConfiguration . ConfigPath } </i> )." ) ;
73
- Assert . IsFalse ( projectConfig . Metadata == null || string . IsNullOrWhiteSpace ( projectConfig . Metadata . Name ) ,
74
- $ "Project name is not set in the project configuration asset ( <i> { ProjectConfiguration . ConfigPath } </i> )." ) ;
69
+ Assert . IsNotNull ( projectConfig , $ "Project configuration not found. Expected to find it at <i> { ProjectConfiguration . ConfigPath } </i>" ) ;
70
+ Assert . IsFalse ( string . IsNullOrWhiteSpace ( projectConfig . Id ) , $ "Project ID is not set in the project configuration asset ( <i>{ ProjectConfiguration . ConfigPath } </i> ). ") ;
71
+ Assert . IsFalse (
72
+ projectConfig . Metadata == null || string . IsNullOrWhiteSpace ( projectConfig . Metadata . Name ) ,
73
+ $ "Project name is not set in the project configuration asset ( <i> { ProjectConfiguration . ConfigPath } </i> )."
74
+ ) ;
75
75
76
76
if ( projectConfig . LoggingEnabled )
77
77
WCLogger . Logger = new Logger ( ) ;
78
78
79
79
var storage = await BuildStorage ( ) ;
80
80
81
- SignClient = await WalletConnectSignClient . Init ( new SignClientOptions
82
- {
83
- Metadata = projectConfig . Metadata ,
84
- Name = projectConfig . Metadata . Name ,
85
- ProjectId = projectConfig . Id ,
86
- Storage = storage ,
87
- RelayUrlBuilder = new UnityRelayUrlBuilder ( ) ,
88
- ConnectionBuilder = new NativeWebSocketConnectionBuilder ( )
89
- } ) ;
90
-
81
+ SignClient = await WalletConnectSignClient . Init (
82
+ new SignClientOptions
83
+ {
84
+ Metadata = projectConfig . Metadata ,
85
+ Name = projectConfig . Metadata . Name ,
86
+ ProjectId = projectConfig . Id ,
87
+ Storage = storage ,
88
+ RelayUrlBuilder = new UnityRelayUrlBuilder ( ) ,
89
+ ConnectionBuilder = new NativeWebSocketConnectionBuilder ( )
90
+ }
91
+ ) ;
92
+
91
93
SignClient . SessionConnected += OnSessionConnected ;
92
94
SignClient . SessionUpdated += OnSessionUpdated ;
93
95
SignClient . SessionDeleted += OnSessionDeleted ;
@@ -114,11 +116,11 @@ public async Task<bool> TryResumeSessionAsync()
114
116
115
117
if ( string . IsNullOrWhiteSpace ( session . Topic ) )
116
118
return false ;
117
-
119
+
118
120
SignClient . AddressProvider . DefaultSession = session ;
119
121
120
122
await SignClient . Extend ( session . Topic ) ;
121
-
123
+
122
124
return true ;
123
125
}
124
126
@@ -148,30 +150,39 @@ public Task DisconnectAsync()
148
150
149
151
private void OnSessionConnected ( object sender , SessionStruct session )
150
152
{
151
- UnitySyncContext . Post ( _ =>
152
- {
153
- SessionConnected ? . Invoke ( this , session ) ;
154
- ActiveSessionChanged ? . Invoke ( this , session ) ;
155
- } , null ) ;
153
+ UnitySyncContext . Post (
154
+ _ =>
155
+ {
156
+ SessionConnected ? . Invoke ( this , session ) ;
157
+ ActiveSessionChanged ? . Invoke ( this , session ) ;
158
+ } ,
159
+ null
160
+ ) ;
156
161
}
157
162
158
163
private void OnSessionUpdated ( object sender , SessionEvent sessionEvent )
159
164
{
160
165
var sessionStruct = SignClient . Session . Values . First ( s => s . Topic == sessionEvent . Topic ) ;
161
- UnitySyncContext . Post ( _ =>
162
- {
163
- SessionUpdated ? . Invoke ( this , sessionStruct ) ;
164
- ActiveSessionChanged ? . Invoke ( this , sessionStruct ) ;
165
- } , null ) ;
166
+ UnitySyncContext . Post (
167
+ _ =>
168
+ {
169
+ SessionUpdated ? . Invoke ( this , sessionStruct ) ;
170
+ ActiveSessionChanged ? . Invoke ( this , sessionStruct ) ;
171
+ } ,
172
+ null
173
+ ) ;
166
174
}
167
175
168
176
private void OnSessionDeleted ( object sender , SessionEvent _ )
169
177
{
170
- UnitySyncContext . Post ( _ =>
171
- {
172
- SessionDisconnected ? . Invoke ( this , EventArgs . Empty ) ;
173
- ActiveSessionChanged ? . Invoke ( this , default ) ;
174
- } , null ) ;
178
+ UnitySyncContext . Post (
179
+ _ =>
180
+ {
181
+ SessionDisconnected ? . Invoke ( this , EventArgs . Empty ) ;
182
+ ActiveSessionChanged ? . Invoke ( this , default ) ;
183
+ } ,
184
+ null
185
+ ) ;
175
186
}
176
187
177
188
private static async Task < IKeyValueStorage > BuildStorage ( )
@@ -186,13 +197,13 @@ private static async Task<IKeyValueStorage> BuildStorage()
186
197
await playerPrefsStorage . Init ( ) ;
187
198
188
199
return playerPrefsStorage ;
189
- #endif
200
+ #else
190
201
191
202
var path = $ "{ Application . persistentDataPath } /WalletConnect/storage.json";
192
203
WCLogger . Log ( $ "[WalletConnectUnity] Using storage path <i>{ path } </i>") ;
193
-
204
+
194
205
var storage = new FileSystemStorage ( path ) ;
195
-
206
+
196
207
try
197
208
{
198
209
await storage . Init ( ) ;
@@ -205,6 +216,7 @@ private static async Task<IKeyValueStorage> BuildStorage()
205
216
}
206
217
207
218
return storage ;
219
+ #endif
208
220
}
209
221
210
222
private void ThrowIfNoActiveSession ( )
@@ -221,7 +233,8 @@ public void Dispose()
221
233
222
234
protected virtual void Dispose ( bool disposing )
223
235
{
224
- if ( disposed ) return ;
236
+ if ( disposed )
237
+ return ;
225
238
226
239
if ( disposing )
227
240
{
@@ -234,4 +247,4 @@ protected virtual void Dispose(bool disposing)
234
247
disposed = true ;
235
248
}
236
249
}
237
- }
250
+ }
0 commit comments