@@ -8,11 +8,11 @@ import {
8
8
Interface ,
9
9
JsonRpcProvider ,
10
10
Contract ,
11
- } from " ethers" ;
12
- import { CommunityConfig } from " ../config" ;
13
- import { BundlerService } from " ../bundler" ;
14
- import sessionManagerModuleJson from " ../abi/SessionManagerModule.json" ;
15
- import twoFAFactoryJson from " ../abi/TwoFAFactory.json" ;
11
+ } from ' ethers' ;
12
+ import { CommunityConfig } from ' ../config' ;
13
+ import { BundlerService } from ' ../bundler' ;
14
+ import sessionManagerModuleJson from ' ../abi/SessionManagerModule.json' ;
15
+ import twoFAFactoryJson from ' ../abi/TwoFAFactory.json' ;
16
16
17
17
const sessionManagerInterface = new Interface ( sessionManagerModuleJson . abi ) ;
18
18
const twoFAFactoryInterface = new Interface ( twoFAFactoryJson . abi ) ;
@@ -61,7 +61,7 @@ export const generateSessionRequestHash = ({
61
61
// Use ABI encoding to match the Dart implementation
62
62
const abiCoder = new AbiCoder ( ) ;
63
63
const packedData = abiCoder . encode (
64
- [ " address" , " address" , " bytes32" , " uint48" ] ,
64
+ [ ' address' , ' address' , ' bytes32' , ' uint48' ] ,
65
65
[ sessionProvider , sessionOwner , salt , BigInt ( expiry ) ]
66
66
) ;
67
67
@@ -87,7 +87,7 @@ export const generateSessionHash = ({
87
87
// Use ABI encoding to match the Dart implementation
88
88
const abiCoder = new AbiCoder ( ) ;
89
89
const packedData = abiCoder . encode (
90
- [ " bytes32" , " uint256" ] ,
90
+ [ ' bytes32' , ' uint256' ] ,
91
91
[ sessionRequestHash , BigInt ( challenge ) ]
92
92
) ;
93
93
@@ -204,7 +204,7 @@ export const requestSession = async ({
204
204
const challengeExpiry = Math . floor ( Date . now ( ) / 1000 ) + 120 ;
205
205
206
206
const data = getBytes (
207
- sessionManagerInterface . encodeFunctionData ( " request" , [
207
+ sessionManagerInterface . encodeFunctionData ( ' request' , [
208
208
sessionSalt ,
209
209
sessionRequestHash ,
210
210
signedSessionRequestHash ,
@@ -274,20 +274,20 @@ export const verifyIncomingSessionRequest = async ({
274
274
sessionRequestHash
275
275
) ;
276
276
if ( result . length < 5 ) {
277
- throw new Error ( " Session request not found" ) ;
277
+ throw new Error ( ' Session request not found' ) ;
278
278
}
279
279
280
280
// check the expiry
281
281
const expiry = Number ( result [ 0 ] ) ;
282
282
const now = Math . floor ( Date . now ( ) / 1000 ) ;
283
283
if ( expiry < now ) {
284
- throw new Error ( " Session request expired" ) ;
284
+ throw new Error ( ' Session request expired' ) ;
285
285
}
286
286
287
287
// check the challenge expiry
288
288
const challengeExpiry = Number ( result [ 1 ] ) ;
289
289
if ( challengeExpiry < now ) {
290
- throw new Error ( " Challenge expired" ) ;
290
+ throw new Error ( ' Challenge expired' ) ;
291
291
}
292
292
293
293
// Extract the stored signedSessionHash from the result
@@ -301,7 +301,7 @@ export const verifyIncomingSessionRequest = async ({
301
301
// Compare the stored signedSessionHash with the provided one
302
302
return storedSignedSessionHash === calculatedSignedSessionHash ;
303
303
} catch ( error ) {
304
- console . error ( " Error verifying incoming session request:" , error ) ;
304
+ console . error ( ' Error verifying incoming session request:' , error ) ;
305
305
return false ;
306
306
}
307
307
} ;
@@ -336,7 +336,7 @@ export const confirmSession = async ({
336
336
const bundler = new BundlerService ( community ) ;
337
337
338
338
const data = getBytes (
339
- sessionManagerInterface . encodeFunctionData ( " confirm" , [
339
+ sessionManagerInterface . encodeFunctionData ( ' confirm' , [
340
340
sessionRequestHash ,
341
341
sessionHash ,
342
342
signedSessionHash ,
@@ -392,7 +392,7 @@ export const isSessionExpired = async ({
392
392
const result = await contract . isExpired ( account , owner ) ;
393
393
return result ;
394
394
} catch ( error ) {
395
- console . error ( " Error checking if session is expired:" , error ) ;
395
+ console . error ( ' Error checking if session is expired:' , error ) ;
396
396
return true ;
397
397
}
398
398
} ;
@@ -415,19 +415,30 @@ export const getTwoFAAddress = async ({
415
415
community : CommunityConfig ;
416
416
source : string ;
417
417
type : string ;
418
- options ?: { accountFactoryAddress ?: string } ;
418
+ options ?: {
419
+ accountFactoryAddress ?: string ;
420
+ sessionFactoryAddress ?: string ;
421
+ sessionProviderAddress ?: string ;
422
+ rpcUrl ?: string ;
423
+ } ;
419
424
} ) : Promise < string | null > => {
420
- const { accountFactoryAddress } = options ?? { } ;
421
-
422
- const factoryAddress = community . primarySessionConfig . factory_address ;
423
- const providerAddress = community . primarySessionConfig . provider_address ;
425
+ const {
426
+ accountFactoryAddress,
427
+ sessionFactoryAddress,
428
+ sessionProviderAddress,
429
+ rpcUrl,
430
+ } = options ?? { } ;
431
+
432
+ const factoryAddress =
433
+ sessionFactoryAddress ?? community . primarySessionConfig . factory_address ;
434
+ const providerAddress =
435
+ sessionProviderAddress ?? community . primarySessionConfig . provider_address ;
424
436
425
437
const salt = generateSessionSalt ( { source, type } ) ;
426
438
const saltBigInt = BigInt ( salt ) ;
427
439
428
- const rpcProvider = new JsonRpcProvider (
429
- community . getRPCUrl ( accountFactoryAddress )
430
- ) ;
440
+ const resolvedRpcUrl = rpcUrl ?? community . getRPCUrl ( accountFactoryAddress ) ;
441
+ const rpcProvider = new JsonRpcProvider ( resolvedRpcUrl ) ;
431
442
432
443
const contract = new Contract (
433
444
factoryAddress ,
@@ -436,14 +447,14 @@ export const getTwoFAAddress = async ({
436
447
) ;
437
448
438
449
try {
439
- const result = await contract . getFunction ( " getAddress" ) (
450
+ const result = await contract . getFunction ( ' getAddress' ) (
440
451
providerAddress ,
441
452
saltBigInt
442
453
) ;
443
454
444
455
return result ;
445
456
} catch ( error ) {
446
- console . error ( " Error getting twoFA address:" , error ) ;
457
+ console . error ( ' Error getting twoFA address:' , error ) ;
447
458
return null ;
448
459
}
449
460
} ;
@@ -471,15 +482,15 @@ export const revokeSession = async ({
471
482
const bundler = new BundlerService ( community ) ;
472
483
473
484
const data = getBytes (
474
- sessionManagerInterface . encodeFunctionData ( " revoke" , [ signer . address ] )
485
+ sessionManagerInterface . encodeFunctionData ( ' revoke' , [ signer . address ] )
475
486
) ;
476
487
477
488
try {
478
489
const tx = await bundler . call ( signer , sessionModuleAddress , account , data ) ;
479
490
480
491
return tx ;
481
492
} catch ( error ) {
482
- console . error ( " Error revoking session:" , error ) ;
493
+ console . error ( ' Error revoking session:' , error ) ;
483
494
return null ;
484
495
}
485
496
} ;
0 commit comments