2023-06-23 07:28:39 +00:00
|
|
|
import { ErrTuple, KeyPair, Portal } from "#types.js";
|
|
|
|
import { Client } from "@lumeweb/libportal";
|
|
|
|
import { deriveChildKey } from "#keys.js";
|
|
|
|
import { ed25519 } from "@noble/curves/ed25519";
|
2023-06-23 12:55:20 +00:00
|
|
|
import { bytesToHex } from "@noble/hashes/utils";
|
2023-06-24 17:35:26 +00:00
|
|
|
import COMMUNITY_PORTAL_LIST from "@lumeweb/community-portals";
|
2023-06-23 07:28:39 +00:00
|
|
|
|
|
|
|
let activePortalMasterKey;
|
|
|
|
|
2023-06-24 17:35:26 +00:00
|
|
|
export const DEFAULT_PORTAL_LIST = COMMUNITY_PORTAL_LIST;
|
2023-06-23 07:28:39 +00:00
|
|
|
|
2023-06-23 22:48:43 +00:00
|
|
|
let ACTIVE_PORTALS = new Set<Client>();
|
2023-06-23 07:28:39 +00:00
|
|
|
|
|
|
|
type PortalSessionsStore = { [id: string]: string };
|
|
|
|
|
|
|
|
export function maybeInitDefaultPortals(): ErrTuple {
|
|
|
|
if (!activePortalMasterKey) {
|
|
|
|
return [null, "activePortalMasterKey not set"];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const portal of DEFAULT_PORTAL_LIST) {
|
2023-06-23 23:30:48 +00:00
|
|
|
addActivePortal(initPortal(portal));
|
2023-06-23 07:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [null, null];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setActivePortalMasterKey(key: Uint8Array) {
|
|
|
|
activePortalMasterKey = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function generatePortalEmail(portal: Portal) {
|
|
|
|
const keyPair = generatePortalKeyPair(portal);
|
|
|
|
|
|
|
|
const userId = bytesToHex(keyPair.publicKey.slice(0, 12));
|
|
|
|
|
|
|
|
return `${userId}@example.com`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function generatePortalKeyPair(portal: Portal): KeyPair {
|
|
|
|
const privateKey = deriveChildKey(
|
|
|
|
activePortalMasterKey,
|
|
|
|
`portal-account:${portal.id}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
privateKey,
|
|
|
|
publicKey: ed25519.getPublicKey(privateKey),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-23 23:14:29 +00:00
|
|
|
export function getActivePortals(): Client[] {
|
|
|
|
return [...ACTIVE_PORTALS];
|
2023-06-23 07:28:39 +00:00
|
|
|
}
|
2023-06-23 22:48:43 +00:00
|
|
|
|
|
|
|
export function addActivePortal(portal: Client) {
|
|
|
|
ACTIVE_PORTALS.add(portal);
|
|
|
|
}
|
|
|
|
|
2023-06-23 23:30:48 +00:00
|
|
|
export function initPortal(portal: Portal): Client {
|
2023-06-23 22:48:43 +00:00
|
|
|
const sessions = getPortalSessions();
|
|
|
|
let jwt: string | null = null;
|
|
|
|
if (sessions) {
|
|
|
|
if (portal.id in sessions) {
|
|
|
|
jwt = sessions[portal.id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 23:30:48 +00:00
|
|
|
return new Client({
|
2023-06-23 22:48:43 +00:00
|
|
|
email: generatePortalEmail(portal),
|
|
|
|
portalUrl: portal.url,
|
|
|
|
privateKey: generatePortalKeyPair(portal).privateKey,
|
|
|
|
jwt: jwt as string,
|
|
|
|
});
|
|
|
|
}
|
2023-06-23 23:14:29 +00:00
|
|
|
|
2023-06-23 22:48:43 +00:00
|
|
|
export function getPortalSessions() {
|
2023-06-26 06:07:03 +00:00
|
|
|
if (typeof globalThis.localStorage === "undefined") {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
let portalSessionsData = globalThis.localStorage.getItem("portals");
|
2023-06-23 22:48:43 +00:00
|
|
|
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);
|
|
|
|
}
|