44using Nethereum . Web3 ;
55using UnityEngine ;
66using System ;
7- using WalletConnectSharp . Core . Models ;
87using WalletConnectSharp . Unity ;
98using WalletConnectSharp . NEthereum ;
109using Nethereum . Siwe . Core ;
11- using Nethereum . Siwe ;
1210using System . Collections . Generic ;
11+ using Nethereum . Web3 . Accounts ;
1312
1413//using WalletConnectSharp.NEthereum;
1514
@@ -20,43 +19,82 @@ namespace Thirdweb
2019 /// </summary>
2120 public class Wallet : Routable
2221 {
23- public Wallet ( )
24- : base ( $ "sdk{ subSeparator } wallet") { }
22+ public Wallet ( ) : base ( $ "sdk{ subSeparator } wallet") { }
2523
2624 /// <summary>
2725 /// Connect a user's wallet via a given wallet provider
2826 /// </summary>
2927 /// <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 )
3129 {
3230 if ( Utils . IsWebGLBuild ( ) )
3331 {
3432 var connection = walletConnection ?? new WalletConnection ( ) { provider = WalletProvider . Injected , } ;
35- ;
3633 return await Bridge . Connect ( connection ) ;
3734 }
3835 else
3936 {
40- ThirdwebSDK . NativeSession newNativeSession = new ThirdwebSDK . NativeSession ( ) ;
41- if ( wcSessionData != null )
37+ ThirdwebSDK . NativeSession oldSession = ThirdwebManager . Instance . SDK . nativeSession ;
38+
39+ if ( walletConnection == null )
4240 {
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 ;
5051 }
5152 else
5253 {
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+ }
6098 }
6199 }
62100 }
@@ -72,17 +110,21 @@ public async Task Disconnect()
72110 }
73111 else
74112 {
113+ ThirdwebSDK . NativeSession oldSession = ThirdwebManager . Instance . SDK . nativeSession ;
114+
75115 if ( Utils . ActiveWalletConnectSession ( ) )
76116 {
77117 WalletConnect . Instance . DisableWalletConnect ( ) ;
78118 }
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+ ) ;
86128 }
87129 }
88130
@@ -146,7 +188,7 @@ public async Task<string> Verify(LoginPayload payload)
146188 {
147189 if ( Utils . IsWebGLBuild ( ) )
148190 {
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 ) ) ;
150192 }
151193 else
152194 {
@@ -414,6 +456,8 @@ public struct WalletConnection
414456 {
415457 public WalletProvider provider ;
416458 public int chainId ;
459+ public string password ;
460+ public string privateKey ;
417461 }
418462
419463 public class WalletProvider
@@ -445,6 +489,10 @@ public static WalletProvider MagicAuth
445489 {
446490 get { return new WalletProvider ( "magicAuth" ) ; }
447491 }
492+ public static WalletProvider DeviceWallet
493+ {
494+ get { return new WalletProvider ( "deviceWallet" ) ; }
495+ }
448496
449497 public override string ToString ( )
450498 {
0 commit comments