22using System . Threading . Tasks ;
33using Nethereum . Signer ;
44using Nethereum . Web3 ;
5- using Nethereum . Web3 . Accounts ;
65using UnityEngine ;
76using System ;
8- using WalletConnectSharp . Core ;
97using WalletConnectSharp . Core . Models ;
10- using WalletConnectSharp . Core . Models . Ethereum ;
118using WalletConnectSharp . Unity ;
12- using WalletConnectSharp . NEthereum . Account ;
139using WalletConnectSharp . NEthereum ;
10+ using Nethereum . Siwe . Core ;
11+ using Nethereum . Siwe ;
12+ using System . Collections . Generic ;
1413
1514//using WalletConnectSharp.NEthereum;
1615
@@ -45,6 +44,7 @@ public async Task<string> Connect(WalletConnection? walletConnection = null, str
4544 newNativeSession . lastChainId = ThirdwebManager . Instance . SDK . nativeSession . lastChainId ;
4645 newNativeSession . account = null ;
4746 newNativeSession . web3 = WalletConnect . Instance . Session . BuildWeb3 ( new Uri ( newNativeSession . lastRPC ) ) . AsWalletAccount ( true ) ;
47+ newNativeSession . siweSession = new SiweMessageService ( ) ;
4848 ThirdwebManager . Instance . SDK . nativeSession = newNativeSession ;
4949 return WalletConnect . Instance . Session . Accounts [ 0 ] ;
5050 }
@@ -54,6 +54,7 @@ public async Task<string> Connect(WalletConnection? walletConnection = null, str
5454 newNativeSession . lastChainId = ThirdwebManager . Instance . SDK . nativeSession . lastChainId ;
5555 newNativeSession . account = Utils . UnlockOrGenerateAccount ( newNativeSession . lastChainId , password , null ) ; // TODO: Allow custom private keys/passwords
5656 newNativeSession . web3 = new Web3 ( newNativeSession . account , newNativeSession . lastRPC ) ;
57+ newNativeSession . siweSession = new SiweMessageService ( ) ;
5758 ThirdwebManager . Instance . SDK . nativeSession = newNativeSession ;
5859 return ThirdwebManager . Instance . SDK . nativeSession . account . Address ;
5960 }
@@ -80,6 +81,7 @@ public async Task Disconnect()
8081 newNativeSession . lastChainId = ThirdwebManager . Instance . SDK . nativeSession . lastChainId ;
8182 newNativeSession . account = null ;
8283 newNativeSession . web3 = new Web3 ( newNativeSession . lastRPC ) ; // fallback
84+ newNativeSession . siweSession = new SiweMessageService ( ) ;
8385 ThirdwebManager . Instance . SDK . nativeSession = newNativeSession ;
8486 }
8587 }
@@ -96,7 +98,105 @@ public async Task<LoginPayload> Authenticate(string domain)
9698 }
9799 else
98100 {
99- throw new UnityException ( "This functionality is not yet available on your current platform." ) ;
101+ var siwe = ThirdwebManager . Instance . SDK . nativeSession . siweSession ;
102+ var siweMsg = new SiweMessage ( )
103+ {
104+ Resources = new List < string > ( ) ,
105+ Uri = $ "https://{ domain } ",
106+ Statement = "Please ensure that the domain above matches the URL of the current website." ,
107+ Address = await GetAddress ( ) ,
108+ Domain = domain ,
109+ ChainId = ( await GetChainId ( ) ) . ToString ( ) ,
110+ Version = "1" ,
111+ Nonce = null ,
112+ IssuedAt = null ,
113+ ExpirationTime = null ,
114+ NotBefore = null ,
115+ RequestId = null
116+ } ;
117+ siweMsg . SetIssuedAtNow ( ) ;
118+ siweMsg . SetExpirationTime ( DateTime . UtcNow . AddSeconds ( 60 * 5 ) ) ;
119+ siweMsg . SetNotBefore ( DateTime . UtcNow ) ;
120+ siweMsg = siwe . AssignNewNonce ( siweMsg ) ;
121+
122+ var finalMsg = SiweMessageStringBuilder . BuildMessage ( siweMsg ) ;
123+ var signature = await Sign ( finalMsg ) ;
124+ return new LoginPayload ( )
125+ {
126+ signature = signature ,
127+ payload = new LoginPayloadData ( )
128+ {
129+ domain = siweMsg . Domain ,
130+ address = siweMsg . Address ,
131+ statement = siweMsg . Statement ,
132+ uri = siweMsg . Uri ,
133+ version = siweMsg . Version ,
134+ chain_id = siweMsg . ChainId ,
135+ nonce = siweMsg . Nonce ,
136+ issued_at = siweMsg . IssuedAt ,
137+ expiration_time = siweMsg . ExpirationTime ,
138+ invalid_before = siweMsg . NotBefore ,
139+ resources = siweMsg . Resources ,
140+ }
141+ } ;
142+ }
143+ }
144+
145+ public async Task < string > Verify ( LoginPayload payload )
146+ {
147+ if ( Utils . IsWebGLBuild ( ) )
148+ {
149+ throw new UnityException ( "This functionality is not available on your current platform." ) ;
150+ }
151+ else
152+ {
153+ var siwe = ThirdwebManager . Instance . SDK . nativeSession . siweSession ;
154+ var siweMessage = new SiweMessage ( )
155+ {
156+ Domain = payload . payload . domain ,
157+ Address = payload . payload . address ,
158+ Statement = payload . payload . statement ,
159+ Uri = payload . payload . uri ,
160+ Version = payload . payload . version ,
161+ ChainId = payload . payload . chain_id ,
162+ Nonce = payload . payload . nonce ,
163+ IssuedAt = payload . payload . issued_at ,
164+ ExpirationTime = payload . payload . expiration_time ,
165+ NotBefore = payload . payload . invalid_before ,
166+ Resources = payload . payload . resources ,
167+ RequestId = null
168+ } ;
169+ var signature = payload . signature ;
170+ var validUser = await siwe . IsUserAddressRegistered ( siweMessage ) ;
171+ if ( validUser )
172+ {
173+ if ( await siwe . IsMessageSignatureValid ( siweMessage , signature ) )
174+ {
175+ if ( siwe . IsMessageTheSameAsSessionStored ( siweMessage ) )
176+ {
177+ if ( siwe . HasMessageDateStartedAndNotExpired ( siweMessage ) )
178+ {
179+ return siweMessage . Address ;
180+ }
181+ else
182+ {
183+ return "Expired" ;
184+ }
185+ }
186+ else
187+ {
188+ return "Invalid Session" ;
189+ }
190+ }
191+ else
192+ {
193+ return "Invalid Signature" ;
194+ }
195+ }
196+ else
197+ {
198+ return "Invalid User" ;
199+ }
100200 }
101201 }
102202
@@ -138,7 +238,7 @@ public async Task<string> GetAddress()
138238 {
139239 if ( Utils . ActiveWalletConnectSession ( ) )
140240 {
141- return WalletConnect . Instance . Session . Accounts [ 0 ] ;
241+ return Nethereum . Util . AddressUtil . Current . ConvertToChecksumAddress ( WalletConnect . Instance . Session . Accounts [ 0 ] ) ;
142242 }
143243 else if ( ThirdwebManager . Instance . SDK . nativeSession . account != null )
144244 {
0 commit comments