Skip to content

Commit

Permalink
fix: user controllerId instead of agentId
Browse files Browse the repository at this point in the history
  • Loading branch information
HunnySajid committed Mar 29, 2024
1 parent 819e0b6 commit f1947a2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
2 changes: 0 additions & 2 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { browserStorageService } from "@pages/background/services/browser-storage";
import {
WEB_APP_PERMS,
configService,
Expand Down Expand Up @@ -209,7 +208,6 @@ chrome.runtime.onMessage.addListener(function (
message.subtype === "disconnect-agent"
) {
await signifyService.disconnect();
await userService.removePasscode();
sendResponse({ data: { isConnected: false } });
}

Expand Down
6 changes: 4 additions & 2 deletions src/pages/background/resource/signin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ const getSigninsObject = async (): Promise<IObjectSignins> => {

export const getSignins = async (): Promise<ISignin[]> => {
const signinsObj = await getSigninsObject();
return signinsObj[signifyService.getAgentID()] ?? [];
const controllerId = await signifyService.getControllerID();
return signinsObj[controllerId] ?? [];
};

export const updateSignins = async (signins: ISignin[]) => {
const signinsObj = await getSigninsObject();
signinsObj[signifyService.getAgentID()] = signins;
const controllerId = await signifyService.getControllerID();
signinsObj[controllerId] = signins;
await browserStorageService.setValue("signins", signinsObj);
};

Expand Down
14 changes: 10 additions & 4 deletions src/pages/background/services/signify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Signify = () => {
} catch (error) {
console.log("Timer expired, client and passcode zeroed out");
_client = null;
await userService.removeControllerId();
await userService.removePasscode();
}

Expand Down Expand Up @@ -47,6 +48,8 @@ const Signify = () => {
_client = new SignifyClient(agentUrl, passcode, Tier.low, bootUrl);
await _client.boot();
await _client.connect();
const state = await getState();
await userService.setControllerId(state?.controller?.state?.i);
setTimeoutAlarm();
} catch (error) {
console.error(error);
Expand All @@ -60,6 +63,8 @@ const Signify = () => {
await ready();
_client = new SignifyClient(agentUrl, passcode, Tier.low);
await _client.connect();
const state = await getState();
await userService.setControllerId(state?.controller?.state?.i);
setTimeoutAlarm();
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -117,6 +122,7 @@ const Signify = () => {

const disconnect = async () => {
_client = null;
await userService.removeControllerId();
await userService.removePasscode();
};

Expand Down Expand Up @@ -150,9 +156,9 @@ const Signify = () => {
return signed_headers;
};

const getAgentID = (): string => {
validateClient();
return _client?.agent?.pre!;
const getControllerID = async (): Promise<string> => {
const controllerId = await userService.getControllerId();
return controllerId;
};

const createAID = async (name: string) => {
Expand All @@ -171,7 +177,7 @@ const Signify = () => {
createAID,
generatePasscode,
bootAndConnect,
getAgentID,
getControllerID,
};
};

Expand Down
69 changes: 46 additions & 23 deletions src/pages/background/services/user.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
import { browserStorageService } from "@pages/background/services/browser-storage"
import { browserStorageService } from "@pages/background/services/browser-storage";

const USER_ENUMS = {
PASSCODE: "user-passcode"
}
PASSCODE: "user-passcode",
CONTROLLER_ID: "controller-id",
};

const User = () => {
const getPasscode = async () : Promise<string> => {
return await browserStorageService.getValue(USER_ENUMS.PASSCODE) as string;
}

const removePasscode = async () => {
await browserStorageService.removeKey(USER_ENUMS.PASSCODE);
}

const setPasscode = async (passcode: string) => {
await browserStorageService.setValue(USER_ENUMS.PASSCODE, passcode)
}

return {
removePasscode,
getPasscode,
setPasscode
}
}

export const userService = User()
const getPasscode = async (): Promise<string> => {
return (await browserStorageService.getValue(
USER_ENUMS.PASSCODE
)) as string;
};

const removePasscode = async () => {
await browserStorageService.removeKey(USER_ENUMS.PASSCODE);
};

const setPasscode = async (passcode: string) => {
await browserStorageService.setValue(USER_ENUMS.PASSCODE, passcode);
};

const getControllerId = async (): Promise<string> => {
return (await browserStorageService.getValue(
USER_ENUMS.CONTROLLER_ID
)) as string;
};

const removeControllerId = async () => {
await browserStorageService.removeKey(USER_ENUMS.CONTROLLER_ID);
};

const setControllerId = async (controllerId: string) => {
await browserStorageService.setValue(
USER_ENUMS.CONTROLLER_ID,
controllerId
);
};

return {
removePasscode,
getPasscode,
setPasscode,
getControllerId,
setControllerId,
removeControllerId,
};
};

export const userService = User();

0 comments on commit f1947a2

Please sign in to comment.