5
5
using System . Linq ;
6
6
using System ;
7
7
using System . IO ;
8
+ using Newtonsoft . Json ;
8
9
9
10
namespace Thirdweb . Unity
10
11
{
12
+ [ Serializable ]
11
13
public enum WalletProvider
12
14
{
13
15
PrivateKeyWallet ,
@@ -17,6 +19,7 @@ public enum WalletProvider
17
19
EcosystemWallet
18
20
}
19
21
22
+ [ Serializable ]
20
23
public class InAppWalletOptions : EcosystemWalletOptions
21
24
{
22
25
public InAppWalletOptions (
@@ -39,16 +42,34 @@ public InAppWalletOptions(
39
42
) { }
40
43
}
41
44
45
+ [ Serializable ]
42
46
public class EcosystemWalletOptions
43
47
{
48
+ [ JsonProperty ( "ecosystemId" ) ]
44
49
public string EcosystemId ;
50
+
51
+ [ JsonProperty ( "ecosystemPartnerId" ) ]
45
52
public string EcosystemPartnerId ;
53
+
54
+ [ JsonProperty ( "email" ) ]
46
55
public string Email ;
56
+
57
+ [ JsonProperty ( "phoneNumber" ) ]
47
58
public string PhoneNumber ;
59
+
60
+ [ JsonProperty ( "authProvider" ) ]
48
61
public AuthProvider AuthProvider ;
62
+
63
+ [ JsonProperty ( "jwtOrPayload" ) ]
49
64
public string JwtOrPayload ;
65
+
66
+ [ JsonProperty ( "storageDirectoryPath" ) ]
50
67
public string StorageDirectoryPath ;
68
+
69
+ [ JsonProperty ( "siweSigner" ) ]
51
70
public IThirdwebWallet SiweSigner ;
71
+
72
+ [ JsonProperty ( "legacyEncryptionKey" ) ]
52
73
public string LegacyEncryptionKey ;
53
74
54
75
public EcosystemWalletOptions (
@@ -75,14 +96,28 @@ public EcosystemWalletOptions(
75
96
}
76
97
}
77
98
99
+ [ Serializable ]
78
100
public class SmartWalletOptions
79
101
{
102
+ [ JsonProperty ( "sponsorGas" ) ]
80
103
public bool SponsorGas ;
104
+
105
+ [ JsonProperty ( "factoryAddress" ) ]
81
106
public string FactoryAddress ;
107
+
108
+ [ JsonProperty ( "accountAddressOverride" ) ]
82
109
public string AccountAddressOverride ;
110
+
111
+ [ JsonProperty ( "entryPoint" ) ]
83
112
public string EntryPoint ;
113
+
114
+ [ JsonProperty ( "bundlerUrl" ) ]
84
115
public string BundlerUrl ;
116
+
117
+ [ JsonProperty ( "paymasterUrl" ) ]
85
118
public string PaymasterUrl ;
119
+
120
+ [ JsonProperty ( "tokenPaymaster" ) ]
86
121
public TokenPaymaster TokenPaymaster ;
87
122
88
123
public SmartWalletOptions (
@@ -105,12 +140,22 @@ public SmartWalletOptions(
105
140
}
106
141
}
107
142
143
+ [ Serializable ]
108
144
public class WalletOptions
109
145
{
146
+ [ JsonProperty ( "provider" ) ]
110
147
public WalletProvider Provider ;
148
+
149
+ [ JsonProperty ( "chainId" ) ]
111
150
public BigInteger ChainId ;
151
+
152
+ [ JsonProperty ( "inAppWalletOptions" ) ]
112
153
public InAppWalletOptions InAppWalletOptions ;
154
+
155
+ [ JsonProperty ( "ecosystemWalletOptions" , NullValueHandling = NullValueHandling . Ignore ) ]
113
156
public EcosystemWalletOptions EcosystemWalletOptions ;
157
+
158
+ [ JsonProperty ( "smartWalletOptions" , NullValueHandling = NullValueHandling . Ignore ) ]
114
159
public SmartWalletOptions SmartWalletOptions ;
115
160
116
161
public WalletOptions (
@@ -154,6 +199,9 @@ public class ThirdwebManager : MonoBehaviour
154
199
[ field: SerializeField ]
155
200
private bool OptOutUsageAnalytics { get ; set ; } = false ;
156
201
202
+ [ field: SerializeField ]
203
+ private bool AutoConnectLastWallet { get ; set ; } = false ;
204
+
157
205
[ field: SerializeField ]
158
206
private ulong [ ] SupportedChains { get ; set ; } = new ulong [ ] { 421614 } ;
159
207
@@ -170,11 +218,13 @@ public class ThirdwebManager : MonoBehaviour
170
218
171
219
public IThirdwebWallet ActiveWallet { get ; private set ; }
172
220
221
+ public bool Initialized { get ; private set ; }
222
+
173
223
public static ThirdwebManager Instance { get ; private set ; }
174
224
175
225
public static readonly string THIRDWEB_UNITY_SDK_VERSION = "5.14.0" ;
176
226
177
- private bool _initialized ;
227
+ private const string THIRDWEB_AUTO_CONNECT_OPTIONS_KEY = "ThirdwebAutoConnectOptions" ;
178
228
179
229
private Dictionary < string , IThirdwebWallet > _walletMapping ;
180
230
@@ -199,7 +249,7 @@ private void Awake()
199
249
}
200
250
}
201
251
202
- public void Initialize ( )
252
+ public async void Initialize ( )
203
253
{
204
254
if ( string . IsNullOrEmpty ( ClientId ) )
205
255
{
@@ -229,12 +279,26 @@ public void Initialize()
229
279
230
280
_walletMapping = new Dictionary < string , IThirdwebWallet > ( ) ;
231
281
232
- _initialized = true ;
282
+ if ( AutoConnectLastWallet && GetAutoConnectOptions ( out var lastWalletOptions ) )
283
+ {
284
+ ThirdwebDebug . Log ( "Auto-connecting to last wallet." ) ;
285
+ try
286
+ {
287
+ _ = await ConnectWallet ( lastWalletOptions ) ;
288
+ ThirdwebDebug . Log ( "Auto-connected to last wallet." ) ;
289
+ }
290
+ catch ( Exception e )
291
+ {
292
+ ThirdwebDebug . LogError ( "Failed to auto-connect to last wallet: " + e . Message ) ;
293
+ }
294
+ }
295
+
296
+ Initialized = true ;
233
297
}
234
298
235
299
public async Task < ThirdwebContract > GetContract ( string address , BigInteger chainId , string abi = null )
236
300
{
237
- if ( ! _initialized )
301
+ if ( ! Initialized )
238
302
{
239
303
throw new InvalidOperationException ( "ThirdwebManager is not initialized." ) ;
240
304
}
@@ -279,11 +343,6 @@ public void RemoveWallet(string address)
279
343
280
344
public async Task < IThirdwebWallet > ConnectWallet ( WalletOptions walletOptions )
281
345
{
282
- if ( ! _initialized )
283
- {
284
- throw new InvalidOperationException ( "ThirdwebManager is not initialized." ) ;
285
- }
286
-
287
346
if ( walletOptions == null )
288
347
{
289
348
throw new ArgumentNullException ( nameof ( walletOptions ) ) ;
@@ -420,7 +479,6 @@ public async Task<IThirdwebWallet> ConnectWallet(WalletOptions walletOptions)
420
479
}
421
480
422
481
var address = await wallet . GetAddress ( ) ;
423
- ThirdwebDebug . Log ( $ "Wallet address: { address } ") ;
424
482
425
483
var isSmartWallet = walletOptions . SmartWalletOptions != null ;
426
484
@@ -429,6 +487,8 @@ public async Task<IThirdwebWallet> ConnectWallet(WalletOptions walletOptions)
429
487
TrackUsage ( "connectWallet" , "connect" , isSmartWallet ? "smartWallet" : walletOptions . Provider . ToString ( ) [ ..1 ] . ToLower ( ) + walletOptions . Provider . ToString ( ) [ 1 ..] , address ) ;
430
488
}
431
489
490
+ SetAutoConnectOptions ( walletOptions ) ;
491
+
432
492
if ( isSmartWallet )
433
493
{
434
494
ThirdwebDebug . Log ( "Upgrading to SmartWallet." ) ;
@@ -444,11 +504,6 @@ public async Task<IThirdwebWallet> ConnectWallet(WalletOptions walletOptions)
444
504
445
505
public async Task < SmartWallet > UpgradeToSmartWallet ( IThirdwebWallet personalWallet , BigInteger chainId , SmartWalletOptions smartWalletOptions )
446
506
{
447
- if ( ! _initialized )
448
- {
449
- throw new InvalidOperationException ( "ThirdwebManager is not initialized." ) ;
450
- }
451
-
452
507
if ( personalWallet . AccountType == ThirdwebAccountType . SmartAccount )
453
508
{
454
509
ThirdwebDebug . LogWarning ( "Wallet is already a SmartWallet." ) ;
@@ -480,6 +535,12 @@ public async Task<SmartWallet> UpgradeToSmartWallet(IThirdwebWallet personalWall
480
535
await AddWallet ( wallet ) ;
481
536
SetActiveWallet ( wallet ) ;
482
537
538
+ if ( AutoConnectLastWallet && GetAutoConnectOptions ( out var lastWalletOptions ) )
539
+ {
540
+ lastWalletOptions . SmartWalletOptions = smartWalletOptions ;
541
+ SetAutoConnectOptions ( lastWalletOptions ) ;
542
+ }
543
+
483
544
return wallet ;
484
545
}
485
546
@@ -498,6 +559,44 @@ public async Task<List<LinkedAccount>> LinkAccount(IThirdwebWallet mainWallet, I
498
559
) ;
499
560
}
500
561
562
+ private bool GetAutoConnectOptions ( out WalletOptions lastWalletOptions )
563
+ {
564
+ var connectOptionsStr = PlayerPrefs . GetString ( THIRDWEB_AUTO_CONNECT_OPTIONS_KEY , null ) ;
565
+ if ( ! string . IsNullOrEmpty ( connectOptionsStr ) )
566
+ {
567
+ try
568
+ {
569
+ lastWalletOptions = JsonConvert . DeserializeObject < WalletOptions > ( connectOptionsStr ) ;
570
+ return true ;
571
+ }
572
+ catch
573
+ {
574
+ ThirdwebDebug . LogWarning ( "Failed to load last wallet options." ) ;
575
+ PlayerPrefs . DeleteKey ( THIRDWEB_AUTO_CONNECT_OPTIONS_KEY ) ;
576
+ lastWalletOptions = null ;
577
+ return false ;
578
+ }
579
+ }
580
+ lastWalletOptions = null ;
581
+ return false ;
582
+ }
583
+
584
+ private void SetAutoConnectOptions ( WalletOptions walletOptions )
585
+ {
586
+ if ( AutoConnectLastWallet && walletOptions . Provider != WalletProvider . WalletConnectWallet )
587
+ {
588
+ try
589
+ {
590
+ PlayerPrefs . SetString ( THIRDWEB_AUTO_CONNECT_OPTIONS_KEY , JsonConvert . SerializeObject ( walletOptions ) ) ;
591
+ }
592
+ catch
593
+ {
594
+ ThirdwebDebug . LogWarning ( "Failed to save last wallet options." ) ;
595
+ PlayerPrefs . DeleteKey ( THIRDWEB_AUTO_CONNECT_OPTIONS_KEY ) ;
596
+ }
597
+ }
598
+ }
599
+
501
600
private async void TrackUsage ( string source , string action , string walletType , string walletAddress )
502
601
{
503
602
if ( string . IsNullOrEmpty ( source ) || string . IsNullOrEmpty ( action ) || string . IsNullOrEmpty ( walletType ) || string . IsNullOrEmpty ( walletAddress ) )
0 commit comments