@@ -26,73 +26,61 @@ public Wallet()
26
26
/// <summary>
27
27
/// Connect a user's wallet via a given wallet provider
28
28
/// </summary>
29
- /// <param name="walletConnection">The wallet provider and chainId to connect to . Defaults to the injected browser extension .</param>
30
- public async Task < string > Connect ( WalletConnection ? walletConnection = null )
29
+ /// <param name="walletConnection">The wallet provider and optional parameters . Defaults to local wallet .</param>
30
+ public async Task < string > Connect ( WalletConnection walletConnection = null )
31
31
{
32
+ walletConnection ??= new WalletConnection ( WalletProvider . LocalWallet , 1 ) ;
33
+
32
34
if ( Utils . IsWebGLBuild ( ) )
33
35
{
34
- var connection = walletConnection ?? new WalletConnection ( ) { provider = WalletProvider . Injected , } ;
35
- return await Bridge . Connect ( connection ) ;
36
+ return await Bridge . Connect ( walletConnection ) ;
36
37
}
37
38
else
38
39
{
39
40
ThirdwebSDK . NativeSession oldSession = ThirdwebManager . Instance . SDK . nativeSession ;
40
41
41
- if ( walletConnection == null )
42
+ if ( walletConnection . provider == WalletProvider . WalletConnectV1 )
42
43
{
43
- Account noPassAcc = Utils . UnlockOrGenerateAccount ( oldSession . lastChainId , null , null ) ;
44
+ await WalletConnect . Instance . EnableWalletConnect ( ) ;
45
+
44
46
ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
45
47
oldSession . lastChainId ,
46
48
oldSession . lastRPC ,
47
- noPassAcc ,
48
- new Web3 ( noPassAcc , oldSession . lastRPC ) ,
49
+ null ,
50
+ WalletConnect . Instance . Session . BuildWeb3 ( new Uri ( oldSession . lastRPC ) ) . AsWalletAccount ( true ) ,
49
51
oldSession . options ,
50
52
oldSession . siweSession
51
53
) ;
52
- return noPassAcc . Address ;
53
- }
54
- else
55
- {
56
- if ( walletConnection ? . provider ? . ToString ( ) == "walletConnectV1" )
57
- {
58
- await WalletConnect . Instance . EnableWalletConnect ( ) ;
59
54
60
- ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
61
- oldSession . lastChainId ,
62
- oldSession . lastRPC ,
63
- null ,
64
- WalletConnect . Instance . Session . BuildWeb3 ( new Uri ( oldSession . lastRPC ) ) . AsWalletAccount ( true ) ,
65
- oldSession . options ,
66
- oldSession . siweSession
67
- ) ;
55
+ // Switch to chain
56
+ try
57
+ {
58
+ await WalletConnect . Instance . WalletSwitchEthChain ( new EthChain ( ) { chainId = ThirdwebManager . Instance . SDK . currentChainData . chainId } ) ;
59
+ }
60
+ catch ( System . Exception e )
61
+ {
62
+ Debug . LogWarning ( "Switching chain error, attempting to add chain: " + e . Message ) ;
68
63
69
- // Switch to chain
64
+ // Add chain
70
65
try
71
66
{
67
+ await WalletConnect . Instance . WalletAddEthChain ( ThirdwebManager . Instance . SDK . currentChainData ) ;
72
68
await WalletConnect . Instance . WalletSwitchEthChain ( new EthChain ( ) { chainId = ThirdwebManager . Instance . SDK . currentChainData . chainId } ) ;
73
69
}
74
- catch ( System . Exception e )
70
+ catch ( System . Exception f )
75
71
{
76
- Debug . LogWarning ( "Switching chain error, attempting to add chain: " + e . Message ) ;
77
-
78
- // Add chain
79
- try
80
- {
81
- await WalletConnect . Instance . WalletAddEthChain ( ThirdwebManager . Instance . SDK . currentChainData ) ;
82
- await WalletConnect . Instance . WalletSwitchEthChain ( new EthChain ( ) { chainId = ThirdwebManager . Instance . SDK . currentChainData . chainId } ) ;
83
- }
84
- catch ( System . Exception f )
85
- {
86
- Debug . LogWarning ( "Adding chain error: " + f . Message ) ;
87
- return Nethereum . Util . AddressUtil . Current . ConvertToChecksumAddress ( WalletConnect . Instance . Session . Accounts [ 0 ] ) ;
88
- }
72
+ Debug . LogWarning ( "Adding chain error: " + f . Message ) ;
73
+ return Nethereum . Util . AddressUtil . Current . ConvertToChecksumAddress ( WalletConnect . Instance . Session . Accounts [ 0 ] ) ;
89
74
}
90
-
91
- return Nethereum . Util . AddressUtil . Current . ConvertToChecksumAddress ( WalletConnect . Instance . Session . Accounts [ 0 ] ) ;
92
75
}
93
- else if ( walletConnection ? . password != null )
76
+
77
+ return Nethereum . Util . AddressUtil . Current . ConvertToChecksumAddress ( WalletConnect . Instance . Session . Accounts [ 0 ] ) ;
78
+ }
79
+ else if ( walletConnection . provider == WalletProvider . LocalWallet )
80
+ {
81
+ if ( walletConnection . privateKey != null )
94
82
{
95
- Account acc = Utils . UnlockOrGenerateAccount ( oldSession . lastChainId , walletConnection ? . password , null ) ;
83
+ Account acc = Utils . UnlockOrGenerateLocalAccount ( oldSession . lastChainId , null , walletConnection . privateKey ) ;
96
84
ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
97
85
oldSession . lastChainId ,
98
86
oldSession . lastRPC ,
@@ -103,9 +91,9 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
103
91
) ;
104
92
return acc . Address ;
105
93
}
106
- else if ( walletConnection ? . privateKey != null )
94
+ else if ( walletConnection . password != null )
107
95
{
108
- Account acc = Utils . UnlockOrGenerateAccount ( oldSession . lastChainId , null , walletConnection ? . privateKey ) ;
96
+ Account acc = Utils . UnlockOrGenerateLocalAccount ( oldSession . lastChainId , walletConnection . password , null ) ;
109
97
ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
110
98
oldSession . lastChainId ,
111
99
oldSession . lastRPC ,
@@ -118,9 +106,22 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
118
106
}
119
107
else
120
108
{
121
- throw new UnityException ( "This wallet connection method is not supported on this platform!" ) ;
109
+ Account noPassAcc = Utils . UnlockOrGenerateLocalAccount ( oldSession . lastChainId , null , null ) ;
110
+ ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
111
+ oldSession . lastChainId ,
112
+ oldSession . lastRPC ,
113
+ noPassAcc ,
114
+ new Web3 ( noPassAcc , oldSession . lastRPC ) ,
115
+ oldSession . options ,
116
+ oldSession . siweSession
117
+ ) ;
118
+ return noPassAcc . Address ;
122
119
}
123
120
}
121
+ else
122
+ {
123
+ throw new UnityException ( "This wallet connection method is not supported on this platform!" ) ;
124
+ }
124
125
}
125
126
}
126
127
@@ -478,51 +479,29 @@ public async Task FundWallet(FundWalletOptions options)
478
479
}
479
480
}
480
481
481
- public struct WalletConnection
482
+ public class WalletConnection
482
483
{
483
484
public WalletProvider provider ;
484
485
public int chainId ;
485
486
public string password ;
486
487
public string privateKey ;
487
- }
488
488
489
- public class WalletProvider
490
- {
491
- private WalletProvider ( string value )
489
+ public WalletConnection ( WalletProvider provider = WalletProvider . LocalWallet , int chainId = 1 , string password = null , string privateKey = null )
492
490
{
493
- Value = value ;
494
- }
495
-
496
- public static string Value { get ; private set ; }
497
-
498
- public static WalletProvider MetaMask
499
- {
500
- get { return new WalletProvider ( "metamask" ) ; }
501
- }
502
- public static WalletProvider CoinbaseWallet
503
- {
504
- get { return new WalletProvider ( "coinbaseWallet" ) ; }
505
- }
506
- public static WalletProvider WalletConnect
507
- {
508
- get { return new WalletProvider ( "walletConnectV1" ) ; }
509
- }
510
- public static WalletProvider Injected
511
- {
512
- get { return new WalletProvider ( "injected" ) ; }
513
- }
514
- public static WalletProvider MagicAuth
515
- {
516
- get { return new WalletProvider ( "magicAuth" ) ; }
517
- }
518
- public static WalletProvider DeviceWallet
519
- {
520
- get { return new WalletProvider ( "localWallet" ) ; }
491
+ this . provider = provider ;
492
+ this . chainId = chainId ;
493
+ this . password = password ;
494
+ this . privateKey = privateKey ;
521
495
}
496
+ }
522
497
523
- public override string ToString ( )
524
- {
525
- return Value ;
526
- }
498
+ public enum WalletProvider
499
+ {
500
+ MetaMask ,
501
+ CoinbaseWallet ,
502
+ WalletConnectV1 ,
503
+ Injected ,
504
+ MagicAuth ,
505
+ LocalWallet ,
527
506
}
528
507
}
0 commit comments