4
4
using Nethereum . Web3 ;
5
5
using UnityEngine ;
6
6
using System ;
7
- using WalletConnectSharp . Core . Models ;
8
7
using WalletConnectSharp . Unity ;
9
8
using WalletConnectSharp . NEthereum ;
10
9
using Nethereum . Siwe . Core ;
11
- using Nethereum . Siwe ;
12
10
using System . Collections . Generic ;
11
+ using Nethereum . Web3 . Accounts ;
13
12
14
13
//using WalletConnectSharp.NEthereum;
15
14
@@ -20,43 +19,82 @@ namespace Thirdweb
20
19
/// </summary>
21
20
public class Wallet : Routable
22
21
{
23
- public Wallet ( )
24
- : base ( $ "sdk{ subSeparator } wallet") { }
22
+ public Wallet ( ) : base ( $ "sdk{ subSeparator } wallet") { }
25
23
26
24
/// <summary>
27
25
/// Connect a user's wallet via a given wallet provider
28
26
/// </summary>
29
27
/// <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 , string password = null , WCSessionData wcSessionData = null )
28
+ public async Task < string > Connect ( WalletConnection ? walletConnection = null )
31
29
{
32
30
if ( Utils . IsWebGLBuild ( ) )
33
31
{
34
32
var connection = walletConnection ?? new WalletConnection ( ) { provider = WalletProvider . Injected , } ;
35
- ;
36
33
return await Bridge . Connect ( connection ) ;
37
34
}
38
35
else
39
36
{
40
- ThirdwebSDK . NativeSession newNativeSession = new ThirdwebSDK . NativeSession ( ) ;
41
- if ( wcSessionData != null )
37
+ ThirdwebSDK . NativeSession oldSession = ThirdwebManager . Instance . SDK . nativeSession ;
38
+
39
+ if ( walletConnection == null )
42
40
{
43
- newNativeSession . lastRPC = ThirdwebManager . Instance . SDK . nativeSession . lastRPC ;
44
- newNativeSession . lastChainId = ThirdwebManager . Instance . SDK . nativeSession . lastChainId ;
45
- newNativeSession . account = null ;
46
- newNativeSession . web3 = WalletConnect . Instance . Session . BuildWeb3 ( new Uri ( newNativeSession . lastRPC ) ) . AsWalletAccount ( true ) ;
47
- newNativeSession . siweSession = new SiweMessageService ( ) ;
48
- ThirdwebManager . Instance . SDK . nativeSession = newNativeSession ;
49
- return WalletConnect . Instance . Session . Accounts [ 0 ] ;
41
+ Account noPassAcc = Utils . UnlockOrGenerateAccount ( oldSession . lastChainId , null , null ) ;
42
+ ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
43
+ oldSession . lastChainId ,
44
+ oldSession . lastRPC ,
45
+ noPassAcc ,
46
+ new Web3 ( noPassAcc , oldSession . lastRPC ) ,
47
+ oldSession . options ,
48
+ oldSession . siweSession
49
+ ) ;
50
+ return noPassAcc . Address ;
50
51
}
51
52
else
52
53
{
53
- newNativeSession . lastRPC = ThirdwebManager . Instance . SDK . nativeSession . lastRPC ;
54
- newNativeSession . lastChainId = ThirdwebManager . Instance . SDK . nativeSession . lastChainId ;
55
- newNativeSession . account = Utils . UnlockOrGenerateAccount ( newNativeSession . lastChainId , password , null ) ; // TODO: Allow custom private keys/passwords
56
- newNativeSession . web3 = new Web3 ( newNativeSession . account , newNativeSession . lastRPC ) ;
57
- newNativeSession . siweSession = new SiweMessageService ( ) ;
58
- ThirdwebManager . Instance . SDK . nativeSession = newNativeSession ;
59
- return ThirdwebManager . Instance . SDK . nativeSession . account . Address ;
54
+ if ( walletConnection ? . provider ? . ToString ( ) == "walletConnect" )
55
+ {
56
+ await WalletConnect . Instance . EnableWalletConnect ( ) ;
57
+
58
+ ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
59
+ oldSession . lastChainId ,
60
+ oldSession . lastRPC ,
61
+ null ,
62
+ WalletConnect . Instance . Session . BuildWeb3 ( new Uri ( oldSession . lastRPC ) ) . AsWalletAccount ( true ) ,
63
+ oldSession . options ,
64
+ oldSession . siweSession
65
+ ) ;
66
+ return Nethereum . Util . AddressUtil . Current . ConvertToChecksumAddress ( WalletConnect . Instance . Session . Accounts [ 0 ] ) ;
67
+ }
68
+ else if ( walletConnection ? . password != null )
69
+ {
70
+ Account acc = Utils . UnlockOrGenerateAccount ( oldSession . lastChainId , walletConnection ? . password , null ) ;
71
+ ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
72
+ oldSession . lastChainId ,
73
+ oldSession . lastRPC ,
74
+ acc ,
75
+ new Web3 ( acc , oldSession . lastRPC ) ,
76
+ oldSession . options ,
77
+ oldSession . siweSession
78
+ ) ;
79
+ return acc . Address ;
80
+ }
81
+ else if ( walletConnection ? . privateKey != null )
82
+ {
83
+ Account acc = Utils . UnlockOrGenerateAccount ( oldSession . lastChainId , null , walletConnection ? . privateKey ) ;
84
+ ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
85
+ oldSession . lastChainId ,
86
+ oldSession . lastRPC ,
87
+ acc ,
88
+ new Web3 ( acc , oldSession . lastRPC ) ,
89
+ oldSession . options ,
90
+ oldSession . siweSession
91
+ ) ;
92
+ return acc . Address ;
93
+ }
94
+ else
95
+ {
96
+ throw new UnityException ( "This wallet connection method is not supported on this platform!" ) ;
97
+ }
60
98
}
61
99
}
62
100
}
@@ -72,17 +110,21 @@ public async Task Disconnect()
72
110
}
73
111
else
74
112
{
113
+ ThirdwebSDK . NativeSession oldSession = ThirdwebManager . Instance . SDK . nativeSession ;
114
+
75
115
if ( Utils . ActiveWalletConnectSession ( ) )
76
116
{
77
117
WalletConnect . Instance . DisableWalletConnect ( ) ;
78
118
}
79
- ThirdwebSDK . NativeSession newNativeSession = new ThirdwebSDK . NativeSession ( ) ;
80
- newNativeSession . lastRPC = ThirdwebManager . Instance . SDK . nativeSession . lastRPC ;
81
- newNativeSession . lastChainId = ThirdwebManager . Instance . SDK . nativeSession . lastChainId ;
82
- newNativeSession . account = null ;
83
- newNativeSession . web3 = new Web3 ( newNativeSession . lastRPC ) ; // fallback
84
- newNativeSession . siweSession = new SiweMessageService ( ) ;
85
- ThirdwebManager . Instance . SDK . nativeSession = newNativeSession ;
119
+
120
+ ThirdwebManager . Instance . SDK . nativeSession = new ThirdwebSDK . NativeSession (
121
+ oldSession . lastChainId ,
122
+ oldSession . lastRPC ,
123
+ null ,
124
+ new Web3 ( oldSession . lastRPC ) ,
125
+ oldSession . options ,
126
+ oldSession . siweSession
127
+ ) ;
86
128
}
87
129
}
88
130
@@ -146,7 +188,7 @@ public async Task<string> Verify(LoginPayload payload)
146
188
{
147
189
if ( Utils . IsWebGLBuild ( ) )
148
190
{
149
- throw new UnityException ( "This functionality is not available on your current platform." ) ;
191
+ return await Bridge . InvokeRoute < string > ( $ "auth { subSeparator } verify" , Utils . ToJsonStringArray ( payload ) ) ;
150
192
}
151
193
else
152
194
{
@@ -414,6 +456,8 @@ public struct WalletConnection
414
456
{
415
457
public WalletProvider provider ;
416
458
public int chainId ;
459
+ public string password ;
460
+ public string privateKey ;
417
461
}
418
462
419
463
public class WalletProvider
@@ -445,6 +489,10 @@ public static WalletProvider MagicAuth
445
489
{
446
490
get { return new WalletProvider ( "magicAuth" ) ; }
447
491
}
492
+ public static WalletProvider DeviceWallet
493
+ {
494
+ get { return new WalletProvider ( "deviceWallet" ) ; }
495
+ }
448
496
449
497
public override string ToString ( )
450
498
{
0 commit comments