Skip to content

Commit 6af055d

Browse files
authored
Merge pull request #16 from citizenwallet/feat/extend-get-2fa-address-with-options
getTwoFAAddress override options
2 parents 0223cd5 + bf2b116 commit 6af055d

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

src/session/index.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
Interface,
99
JsonRpcProvider,
1010
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';
1616

1717
const sessionManagerInterface = new Interface(sessionManagerModuleJson.abi);
1818
const twoFAFactoryInterface = new Interface(twoFAFactoryJson.abi);
@@ -61,7 +61,7 @@ export const generateSessionRequestHash = ({
6161
// Use ABI encoding to match the Dart implementation
6262
const abiCoder = new AbiCoder();
6363
const packedData = abiCoder.encode(
64-
["address", "address", "bytes32", "uint48"],
64+
['address', 'address', 'bytes32', 'uint48'],
6565
[sessionProvider, sessionOwner, salt, BigInt(expiry)]
6666
);
6767

@@ -87,7 +87,7 @@ export const generateSessionHash = ({
8787
// Use ABI encoding to match the Dart implementation
8888
const abiCoder = new AbiCoder();
8989
const packedData = abiCoder.encode(
90-
["bytes32", "uint256"],
90+
['bytes32', 'uint256'],
9191
[sessionRequestHash, BigInt(challenge)]
9292
);
9393

@@ -204,7 +204,7 @@ export const requestSession = async ({
204204
const challengeExpiry = Math.floor(Date.now() / 1000) + 120;
205205

206206
const data = getBytes(
207-
sessionManagerInterface.encodeFunctionData("request", [
207+
sessionManagerInterface.encodeFunctionData('request', [
208208
sessionSalt,
209209
sessionRequestHash,
210210
signedSessionRequestHash,
@@ -274,20 +274,20 @@ export const verifyIncomingSessionRequest = async ({
274274
sessionRequestHash
275275
);
276276
if (result.length < 5) {
277-
throw new Error("Session request not found");
277+
throw new Error('Session request not found');
278278
}
279279

280280
// check the expiry
281281
const expiry = Number(result[0]);
282282
const now = Math.floor(Date.now() / 1000);
283283
if (expiry < now) {
284-
throw new Error("Session request expired");
284+
throw new Error('Session request expired');
285285
}
286286

287287
// check the challenge expiry
288288
const challengeExpiry = Number(result[1]);
289289
if (challengeExpiry < now) {
290-
throw new Error("Challenge expired");
290+
throw new Error('Challenge expired');
291291
}
292292

293293
// Extract the stored signedSessionHash from the result
@@ -301,7 +301,7 @@ export const verifyIncomingSessionRequest = async ({
301301
// Compare the stored signedSessionHash with the provided one
302302
return storedSignedSessionHash === calculatedSignedSessionHash;
303303
} catch (error) {
304-
console.error("Error verifying incoming session request:", error);
304+
console.error('Error verifying incoming session request:', error);
305305
return false;
306306
}
307307
};
@@ -336,7 +336,7 @@ export const confirmSession = async ({
336336
const bundler = new BundlerService(community);
337337

338338
const data = getBytes(
339-
sessionManagerInterface.encodeFunctionData("confirm", [
339+
sessionManagerInterface.encodeFunctionData('confirm', [
340340
sessionRequestHash,
341341
sessionHash,
342342
signedSessionHash,
@@ -392,7 +392,7 @@ export const isSessionExpired = async ({
392392
const result = await contract.isExpired(account, owner);
393393
return result;
394394
} catch (error) {
395-
console.error("Error checking if session is expired:", error);
395+
console.error('Error checking if session is expired:', error);
396396
return true;
397397
}
398398
};
@@ -415,19 +415,30 @@ export const getTwoFAAddress = async ({
415415
community: CommunityConfig;
416416
source: string;
417417
type: string;
418-
options?: { accountFactoryAddress?: string };
418+
options?: {
419+
accountFactoryAddress?: string;
420+
sessionFactoryAddress?: string;
421+
sessionProviderAddress?: string;
422+
rpcUrl?: string;
423+
};
419424
}): 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;
424436

425437
const salt = generateSessionSalt({ source, type });
426438
const saltBigInt = BigInt(salt);
427439

428-
const rpcProvider = new JsonRpcProvider(
429-
community.getRPCUrl(accountFactoryAddress)
430-
);
440+
const resolvedRpcUrl = rpcUrl ?? community.getRPCUrl(accountFactoryAddress);
441+
const rpcProvider = new JsonRpcProvider(resolvedRpcUrl);
431442

432443
const contract = new Contract(
433444
factoryAddress,
@@ -436,14 +447,14 @@ export const getTwoFAAddress = async ({
436447
);
437448

438449
try {
439-
const result = await contract.getFunction("getAddress")(
450+
const result = await contract.getFunction('getAddress')(
440451
providerAddress,
441452
saltBigInt
442453
);
443454

444455
return result;
445456
} catch (error) {
446-
console.error("Error getting twoFA address:", error);
457+
console.error('Error getting twoFA address:', error);
447458
return null;
448459
}
449460
};
@@ -471,15 +482,15 @@ export const revokeSession = async ({
471482
const bundler = new BundlerService(community);
472483

473484
const data = getBytes(
474-
sessionManagerInterface.encodeFunctionData("revoke", [signer.address])
485+
sessionManagerInterface.encodeFunctionData('revoke', [signer.address])
475486
);
476487

477488
try {
478489
const tx = await bundler.call(signer, sessionModuleAddress, account, data);
479490

480491
return tx;
481492
} catch (error) {
482-
console.error("Error revoking session:", error);
493+
console.error('Error revoking session:', error);
483494
return null;
484495
}
485496
};

0 commit comments

Comments
 (0)