Skip to content

Commit

Permalink
Feat/discover public encryption key (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammed-Mamoun98 authored Oct 28, 2024
1 parent 4622896 commit dbf6da7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
12 changes: 12 additions & 0 deletions apps/idos-enclave/src/lib/enclave.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ export class Enclave {
.filter(({ content }) => negate(() => matchCriteria(content, privateFieldFilters.omit)));
}

async discoverUserEncryptionKey() {
const { password } = await this.#openDialog("discoverPublicKey");
const humanId = crypto.randomUUID();
const secretKey = await idOSKeyDerivation({ password, salt: humanId });

const keyPair = nacl.box.keyPair.fromSecretKey(secretKey);

const encryptionPublicKey = Base64Codec.encode(keyPair.publicKey);
return { encryptionPublicKey, humanId };
}

async backupPasswordOrSecret() {
this.backupButton.style.display = "block";
this.backupButton.disabled = false;
Expand Down Expand Up @@ -366,6 +377,7 @@ export class Enclave {
filterCredentialsByCountries: () => [credentials, countries],
filterCredentials: () => [credentials, privateFieldFilters],
backupPasswordOrSecret: () => [],
discoverUserEncryptionKey: () => [],
}[requestName];

if (!paramBuilder) throw new Error(`Unexpected request from parent: ${requestName}`);
Expand Down
13 changes: 10 additions & 3 deletions apps/idos-enclave/src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export interface Configuration {
theme?: "light" | "dark";
}

type AllowedIntent = "passkey" | "password" | "confirm" | "auth" | "backupPasswordOrSecret";
type AllowedIntent =
| "passkey"
| "password"
| "confirm"
| "auth"
| "backupPasswordOrSecret"
| "discoverPublicKey";

export interface EventData {
intent: AllowedIntent;
Expand All @@ -40,6 +46,7 @@ const allowedIntents: AllowedIntent[] = [
"confirm",
"auth",
"backupPasswordOrSecret",
"discoverPublicKey",
];

function Layout({ onHeaderClick, children }: PropsWithChildren<{ onHeaderClick?: () => void }>) {
Expand All @@ -55,7 +62,7 @@ function Layout({ onHeaderClick, children }: PropsWithChildren<{ onHeaderClick?:

export function App({ store, enclave }: AppProps) {
const [method, setMethod] = useState<Method | null>(null);
const [mode, setMode] = useState<Mode>("existing");
const [mode, setMode] = useState<Mode>(store.get("human-id") ? "existing" : "new");
const [theme, setTheme] = useState<Theme | null>(localStorage.getItem("theme") as Theme | null);
const [confirm, setConfirm] = useState<boolean>(false);
const responsePort = useRef<MessagePort | null>(null);
Expand Down Expand Up @@ -232,7 +239,7 @@ export function App({ store, enclave }: AppProps) {
<Layout onHeaderClick={resetMethod}>
<div class="flex flex-col gap-4">
<ChooseMethod setMethod={setMethod} mode={mode} />
{method !== "new" ? (
{mode !== "new" ? (
<button
type="button"
onClick={() => {
Expand Down
15 changes: 14 additions & 1 deletion packages/idos-sdk-js/src/lib/enclave-providers/iframe-enclave.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { idOSCredential } from "@idos-network/idos-sdk-types";
import type { BackupPasswordInfo } from "../types";
import type { EnclaveOptions, EnclaveProvider, StoredData } from "./types";
import type {
DiscoverEncryptionKeyResponse,
EnclaveOptions,
EnclaveProvider,
StoredData,
} from "./types";

export class IframeEnclave implements EnclaveProvider {
options: Omit<EnclaveOptions, "container" | "url">;
Expand Down Expand Up @@ -213,4 +218,12 @@ export class IframeEnclave implements EnclaveProvider {
console.error(error);
}
}

async discoverUserEncryptionKey(): Promise<DiscoverEncryptionKeyResponse> {
const response = await this.#requestToEnclave({
discoverUserEncryptionKey: {}, // we can pass humanId here
});
this.#hideEnclave();
return response as DiscoverEncryptionKeyResponse;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { idOSCredential } from "@idos-network/idos-sdk-types";
import type { EnclaveProvider, StoredData } from "./types";
import type { DiscoverEncryptionKeyResponse, EnclaveProvider, StoredData } from "./types";

export class MetaMaskSnapEnclave implements EnclaveProvider {
// biome-ignore lint/suspicious/noExplicitAny: Types will be added later
Expand All @@ -19,6 +19,10 @@ export class MetaMaskSnapEnclave implements EnclaveProvider {
throw new Error("Method not implemented.");
}

async discoverUserEncryptionKey(): Promise<DiscoverEncryptionKeyResponse> {
throw new Error("Method not implemented.");
}

filterCredentialsByCountries(
credentials: Record<string, string>[],
countries: string[],
Expand Down
7 changes: 6 additions & 1 deletion packages/idos-sdk-js/src/lib/enclave-providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export interface StoredData {
signerPublicKey?: string;
}

export interface DiscoverEncryptionKeyResponse {
humanId: string;
encryptionPublicKey: string;
}

export interface EnclaveOptions {
container: string;
theme?: "light" | "dark";
Expand All @@ -31,7 +36,7 @@ export interface EnclaveProvider {
updateStore(key: string, value: unknown): Promise<void>;
encrypt(message: Uint8Array, receiverPublicKey?: Uint8Array): Promise<Uint8Array>;
decrypt(message: Uint8Array, senderPublicKey?: Uint8Array): Promise<Uint8Array>;

discoverUserEncryptionKey(): Promise<DiscoverEncryptionKeyResponse>;
filterCredentialsByCountries(
credentials: Record<string, string>[],
countries: string[],
Expand Down
4 changes: 4 additions & 0 deletions packages/idos-sdk-js/src/lib/enclave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ export class Enclave {

return this.provider.backupPasswordOrSecret(callbackFn);
}

async discoverUserEncryptionKey() {
return this.provider.discoverUserEncryptionKey();
}
}
4 changes: 4 additions & 0 deletions packages/idos-sdk-js/src/lib/idos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,8 @@ export class idOS {
await this.updateAttributesIfNeeded(filteredUserAttributes, litSavableAttributes);
});
}

async discoverEncryptionKey() {
return this.enclave.discoverUserEncryptionKey();
}
}

0 comments on commit dbf6da7

Please sign in to comment.