@@ -3,14 +3,25 @@ import { Signer } from 'ethers';
33import LitProvider from '../src/lit-protocol-cipher-provider' ;
44import { disconnectWeb3 , LitNodeClientNodeJs } from '@lit-protocol/lit-node-client' ;
55import { HttpDataAccess , NodeConnectionConfig } from '@requestnetwork/request-client.js' ;
6- import { generateAuthSig } from '@lit-protocol/auth-helpers' ;
6+ import {
7+ generateAuthSig ,
8+ createSiweMessage ,
9+ LitAccessControlConditionResource ,
10+ } from '@lit-protocol/auth-helpers' ;
711import { EncryptionTypes } from '@requestnetwork/types' ;
8- import { createSiweMessageWithRecaps } from '@lit-protocol/auth-helpers' ;
912
1013// Mock dependencies
1114jest . mock ( '@lit-protocol/lit-node-client' ) ;
1215jest . mock ( '@requestnetwork/request-client.js' ) ;
13- jest . mock ( '@lit-protocol/auth-helpers' ) ;
16+ jest . mock ( '@lit-protocol/auth-helpers' , ( ) => ( {
17+ generateAuthSig : jest . fn ( ) ,
18+ createSiweMessage : jest . fn ( ) ,
19+ LitAccessControlConditionResource : jest . fn ( ) . mockImplementation ( ( path ) => ( {
20+ path,
21+ toString : ( ) => path ,
22+ toWildcardPath : ( ) => path ,
23+ } ) ) ,
24+ } ) ) ;
1425jest . mock ( '@lit-protocol/encryption' ) ;
1526
1627describe ( 'LitProvider' , ( ) => {
@@ -147,34 +158,151 @@ describe('LitProvider', () => {
147158 } ) ;
148159
149160 describe ( 'getSessionSignatures' , ( ) => {
150- it ( 'should get session signatures successfully' , async ( ) => {
161+ beforeEach ( ( ) => {
162+ ( LitAccessControlConditionResource as unknown as jest . Mock ) . mockClear ( ) ;
163+ ( createSiweMessage as jest . Mock ) . mockClear ( ) ;
164+
165+ // Mock getSessionSigs to call the authNeededCallback
166+ ( mockLitClient . getSessionSigs as jest . Mock ) . mockImplementation ( async ( args : unknown ) => {
167+ const { authNeededCallback } = args as {
168+ authNeededCallback : ( params : {
169+ uri : string ;
170+ expiration : string ;
171+ resourceAbilityRequests : unknown [ ] ;
172+ } ) => Promise < unknown > ;
173+ } ;
174+ if ( authNeededCallback ) {
175+ await authNeededCallback ( {
176+ uri : 'test://uri' ,
177+ expiration : '2024-12-31T23:59:59.999Z' ,
178+ resourceAbilityRequests : [ ] ,
179+ } ) ;
180+ }
181+ return { 'mock-session' : 'mock-sig' } ;
182+ } ) ;
183+ } ) ;
184+
185+ it ( 'should get session signatures successfully with default params' , async ( ) => {
151186 const mockAuthSig = {
152187 sig : 'mock-auth-sig' ,
153188 address : mockWalletAddress ,
154189 derivedVia : 'mock' ,
155190 signedMessage : 'mock' ,
156191 } ;
192+ const mockDomain = 'localhost' ;
193+ const mockStatement = 'Sign in with Ethereum' ;
194+
157195 ( generateAuthSig as jest . Mock ) . mockReturnValue ( Promise . resolve ( mockAuthSig ) ) ;
158- ( createSiweMessageWithRecaps as jest . Mock ) . mockReturnValue (
159- Promise . resolve ( 'mock-siwe-message' ) ,
196+ ( createSiweMessage as jest . Mock ) . mockReturnValue ( Promise . resolve ( 'mock-siwe-message' ) ) ;
197+
198+ await litProvider . getSessionSignatures (
199+ mockSigner ,
200+ mockWalletAddress ,
201+ mockDomain ,
202+ mockStatement ,
160203 ) ;
161204
205+ expect ( mockLitClient . connect ) . toHaveBeenCalled ( ) ;
206+ expect ( mockLitClient . getLatestBlockhash ) . toHaveBeenCalled ( ) ;
207+ expect ( LitAccessControlConditionResource ) . toHaveBeenCalledWith ( '*' ) ;
208+ expect ( createSiweMessage ) . toHaveBeenCalledWith ( {
209+ domain : mockDomain ,
210+ statement : mockStatement ,
211+ uri : 'test://uri' ,
212+ expiration : '2024-12-31T23:59:59.999Z' ,
213+ resources : [ ] ,
214+ walletAddress : mockWalletAddress ,
215+ nonce : 'mock-blockhash' ,
216+ litNodeClient : mockLitClient ,
217+ } ) ;
218+ expect ( mockLitClient . getSessionSigs ) . toHaveBeenCalled ( ) ;
219+ } ) ;
220+
221+ it ( 'should get session signatures successfully with undefined domain and statement' , async ( ) => {
222+ const mockAuthSig = {
223+ sig : 'mock-auth-sig' ,
224+ address : mockWalletAddress ,
225+ derivedVia : 'mock' ,
226+ signedMessage : 'mock' ,
227+ } ;
228+
229+ ( generateAuthSig as jest . Mock ) . mockReturnValue ( Promise . resolve ( mockAuthSig ) ) ;
230+ ( createSiweMessage as jest . Mock ) . mockReturnValue ( Promise . resolve ( 'mock-siwe-message' ) ) ;
231+
162232 await litProvider . getSessionSignatures ( mockSigner , mockWalletAddress ) ;
163233
164234 expect ( mockLitClient . connect ) . toHaveBeenCalled ( ) ;
165235 expect ( mockLitClient . getLatestBlockhash ) . toHaveBeenCalled ( ) ;
236+ expect ( createSiweMessage ) . toHaveBeenCalledWith ( {
237+ domain : undefined ,
238+ statement : undefined ,
239+ uri : 'test://uri' ,
240+ expiration : '2024-12-31T23:59:59.999Z' ,
241+ resources : [ ] ,
242+ walletAddress : mockWalletAddress ,
243+ nonce : 'mock-blockhash' ,
244+ litNodeClient : mockLitClient ,
245+ } ) ;
246+ expect ( mockLitClient . getSessionSigs ) . toHaveBeenCalled ( ) ;
247+ } ) ;
248+
249+ it ( 'should get session signatures successfully with all custom params' , async ( ) => {
250+ const mockAuthSig = {
251+ sig : 'mock-auth-sig' ,
252+ address : mockWalletAddress ,
253+ derivedVia : 'mock' ,
254+ signedMessage : 'mock' ,
255+ } ;
256+ const mockDomain = 'custom.domain' ;
257+ const mockStatement = 'Custom statement for signing' ;
258+
259+ ( generateAuthSig as jest . Mock ) . mockReturnValue ( Promise . resolve ( mockAuthSig ) ) ;
260+ ( createSiweMessage as jest . Mock ) . mockReturnValue ( Promise . resolve ( 'mock-siwe-message' ) ) ;
261+
262+ await litProvider . getSessionSignatures (
263+ mockSigner ,
264+ mockWalletAddress ,
265+ mockDomain ,
266+ mockStatement ,
267+ ) ;
268+
269+ expect ( mockLitClient . connect ) . toHaveBeenCalled ( ) ;
270+ expect ( mockLitClient . getLatestBlockhash ) . toHaveBeenCalled ( ) ;
271+ expect ( createSiweMessage ) . toHaveBeenCalledWith ( {
272+ domain : mockDomain ,
273+ statement : mockStatement ,
274+ uri : expect . any ( String ) ,
275+ expiration : expect . any ( String ) ,
276+ resources : expect . any ( Array ) ,
277+ walletAddress : mockWalletAddress ,
278+ nonce : 'mock-blockhash' ,
279+ litNodeClient : mockLitClient ,
280+ } ) ;
166281 expect ( mockLitClient . getSessionSigs ) . toHaveBeenCalled ( ) ;
167282 } ) ;
168283
169284 it ( 'should not get new signatures if they already exist' , async ( ) => {
285+ const mockDomain = 'localhost' ;
286+ const mockStatement = 'Sign in with Ethereum' ;
287+
170288 // Set session signatures
171- await litProvider . getSessionSignatures ( mockSigner , mockWalletAddress ) ;
289+ await litProvider . getSessionSignatures (
290+ mockSigner ,
291+ mockWalletAddress ,
292+ mockDomain ,
293+ mockStatement ,
294+ ) ;
172295
173296 // Reset mocks
174297 jest . clearAllMocks ( ) ;
175298
176299 // Call again, should not call Lit SDK methods
177- await litProvider . getSessionSignatures ( mockSigner , mockWalletAddress ) ;
300+ await litProvider . getSessionSignatures (
301+ mockSigner ,
302+ mockWalletAddress ,
303+ mockDomain ,
304+ mockStatement ,
305+ ) ;
178306
179307 expect ( mockLitClient . connect ) . not . toHaveBeenCalled ( ) ;
180308 expect ( mockLitClient . getLatestBlockhash ) . not . toHaveBeenCalled ( ) ;
0 commit comments