fix: improve portal api

This commit is contained in:
Derrick Hammer 2023-06-23 18:48:43 -04:00
parent 2890e908d5
commit ca43e88300
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 42 additions and 25 deletions

View File

@ -10,7 +10,7 @@ export const DEFAULT_PORTAL_LIST: Portal[] = [
{ id: "lumeweb", url: "https://web3portal.com", name: "web3portal.com" }, { id: "lumeweb", url: "https://web3portal.com", name: "web3portal.com" },
]; ];
const ACTIVE_PORTALS = new Set<Client>(); let ACTIVE_PORTALS = new Set<Client>();
type PortalSessionsStore = { [id: string]: string }; type PortalSessionsStore = { [id: string]: string };
@ -19,31 +19,8 @@ export function maybeInitDefaultPortals(): ErrTuple {
return [null, "activePortalMasterKey not set"]; return [null, "activePortalMasterKey not set"];
} }
let portalSessionsData = window.localStorage.getItem("portals");
let portalSessions: PortalSessionsStore = {};
if (portalSessions) {
portalSessions = JSON.parse(
portalSessionsData as string,
) as PortalSessionsStore;
}
for (const portal of DEFAULT_PORTAL_LIST) { for (const portal of DEFAULT_PORTAL_LIST) {
let jwt: string | null = null; initPortal(portal);
if (portalSessions) {
if (portal.id in portalSessions) {
jwt = portalSessions[portal.id];
}
}
const client = new Client({
email: generatePortalEmail(portal),
portalUrl: portal.url,
privateKey: generatePortalKeyPair(portal).privateKey,
jwt: jwt as string,
});
ACTIVE_PORTALS.add(client);
} }
return [null, null]; return [null, null];
@ -76,3 +53,43 @@ export function generatePortalKeyPair(portal: Portal): KeyPair {
export function getActivePortals(): Set<Client> { export function getActivePortals(): Set<Client> {
return ACTIVE_PORTALS; return ACTIVE_PORTALS;
} }
export function addActivePortal(portal: Client) {
ACTIVE_PORTALS.add(portal);
}
export function initPortal(portal: Portal) {
const sessions = getPortalSessions();
let jwt: string | null = null;
if (sessions) {
if (portal.id in sessions) {
jwt = sessions[portal.id];
}
}
const client = new Client({
email: generatePortalEmail(portal),
portalUrl: portal.url,
privateKey: generatePortalKeyPair(portal).privateKey,
jwt: jwt as string,
});
addActivePortal(client);
}
export function getPortalSessions() {
let portalSessionsData = window.localStorage.getItem("portals");
let portalSessions: PortalSessionsStore = {};
if (portalSessions) {
portalSessions = JSON.parse(
portalSessionsData as string,
) as PortalSessionsStore;
return portalSessions;
}
return undefined;
}
export function setActivePortals(portals: Client[]) {
ACTIVE_PORTALS = new Set<Client>(portals);
}