-
Notifications
You must be signed in to change notification settings - Fork 6
Fix/eid onboarding #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix/eid onboarding #415
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8250c89
fix: onboarding
sosweetham 2a77735
fix: key service
sosweetham fb13329
fix: make scan page less cucked
sosweetham 0cbab5b
fix: signer event listener style
sosweetham 4d24b72
fix: format && check
sosweetham 2479b56
fix: remove hardcoded platform url
sosweetham d7207a9
chore: fix format
coodos 81674b4
fix: remove unnecessary platform url
sosweetham 3165649
fix: format and check
sosweetham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
infrastructure/eid-wallet/src/lib/global/controllers/key.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { KeyManagerFactory } from "$lib/crypto"; | ||
| import type { KeyManager } from "$lib/crypto"; | ||
| import type { Store } from "@tauri-apps/plugin-store"; | ||
|
|
||
| export type KeyServiceContext = | ||
| | "onboarding" | ||
| | "signing" | ||
| | "verification" | ||
| | "pre-verification"; | ||
|
|
||
| type PersistedContext = { | ||
| keyId: string; | ||
| context: KeyServiceContext; | ||
| managerType: "hardware" | "software"; | ||
| lastUsed: string; | ||
| }; | ||
|
|
||
| const CONTEXTS_KEY = "keyService.contexts"; | ||
| const READY_KEY = "keyService.ready"; | ||
|
|
||
| export class KeyService { | ||
| #store: Store; | ||
| #managerCache = new Map<string, KeyManager>(); | ||
| #contexts = new Map<string, PersistedContext>(); | ||
| #ready = false; | ||
|
|
||
| constructor(store: Store) { | ||
| this.#store = store; | ||
| } | ||
|
|
||
| async initialize(): Promise<void> { | ||
| const storedContexts = | ||
| await this.#store.get<Record<string, PersistedContext>>( | ||
| CONTEXTS_KEY, | ||
| ); | ||
| if (storedContexts) { | ||
| for (const [key, value] of Object.entries(storedContexts)) { | ||
| this.#contexts.set(key, value); | ||
| } | ||
| } | ||
| this.#ready = (await this.#store.get<boolean>(READY_KEY)) ?? false; | ||
| } | ||
|
|
||
| get isReady(): boolean { | ||
| return this.#ready; | ||
| } | ||
|
|
||
| async setReady(value: boolean): Promise<void> { | ||
| this.#ready = value; | ||
| await this.#store.set(READY_KEY, value); | ||
| } | ||
|
|
||
| async reset(): Promise<void> { | ||
| this.#managerCache.clear(); | ||
| this.#contexts.clear(); | ||
| await this.#store.delete(CONTEXTS_KEY); | ||
| await this.#store.delete(READY_KEY); | ||
| this.#ready = false; | ||
| } | ||
|
|
||
| async getManager( | ||
| keyId: string, | ||
| context: KeyServiceContext, | ||
| ): Promise<KeyManager> { | ||
| const cacheKey = this.#getCacheKey(keyId, context); | ||
| if (this.#managerCache.has(cacheKey)) { | ||
| const cachedManager = this.#managerCache.get(cacheKey); | ||
| if (cachedManager) { | ||
| await this.#touchContext(cacheKey, cachedManager); | ||
| return cachedManager; | ||
| } | ||
| this.#managerCache.delete(cacheKey); | ||
| } | ||
|
|
||
| const manager = await KeyManagerFactory.getKeyManagerForContext( | ||
| keyId, | ||
| context, | ||
| ); | ||
| this.#managerCache.set(cacheKey, manager); | ||
| await this.#persistContext(cacheKey, manager, keyId, context); | ||
| return manager; | ||
| } | ||
|
|
||
| async ensureKey( | ||
| keyId: string, | ||
| context: KeyServiceContext, | ||
| ): Promise<{ manager: KeyManager; created: boolean }> { | ||
| const manager = await this.getManager(keyId, context); | ||
| const exists = await manager.exists(keyId); | ||
| let created = false; | ||
| if (!exists) { | ||
| await manager.generate(keyId); | ||
| await this.#touchContext( | ||
| this.#getCacheKey(keyId, context), | ||
| manager, | ||
| ); | ||
| created = true; | ||
| } | ||
| return { manager, created }; | ||
| } | ||
|
|
||
| async getPublicKey( | ||
| keyId: string, | ||
| context: KeyServiceContext, | ||
| ): Promise<string | undefined> { | ||
| const manager = await this.getManager(keyId, context); | ||
| const publicKey = await manager.getPublicKey(keyId); | ||
| await this.#touchContext(this.#getCacheKey(keyId, context), manager); | ||
| return publicKey; | ||
| } | ||
|
|
||
| async signPayload( | ||
| keyId: string, | ||
| context: KeyServiceContext, | ||
| payload: string, | ||
| ): Promise<string> { | ||
| const manager = await this.getManager(keyId, context); | ||
| const signature = await manager.signPayload(keyId, payload); | ||
| await this.#touchContext(this.#getCacheKey(keyId, context), manager); | ||
| return signature; | ||
| } | ||
|
|
||
| async verifySignature( | ||
| keyId: string, | ||
| context: KeyServiceContext, | ||
| payload: string, | ||
| signature: string, | ||
| ): Promise<boolean> { | ||
| const manager = await this.getManager(keyId, context); | ||
| const result = await manager.verifySignature(keyId, payload, signature); | ||
| await this.#touchContext(this.#getCacheKey(keyId, context), manager); | ||
| return result; | ||
| } | ||
|
|
||
| async isHardwareAvailable(): Promise<boolean> { | ||
| return KeyManagerFactory.isHardwareAvailable(); | ||
| } | ||
|
|
||
| #getCacheKey(keyId: string, context: KeyServiceContext): string { | ||
| return `${context}:${keyId}`; | ||
| } | ||
|
|
||
| async #persistContext( | ||
| cacheKey: string, | ||
| manager: KeyManager, | ||
| keyId: string, | ||
| context: KeyServiceContext, | ||
| ): Promise<void> { | ||
| const entry: PersistedContext = { | ||
| keyId, | ||
| context, | ||
| managerType: manager.getType(), | ||
| lastUsed: new Date().toISOString(), | ||
| }; | ||
| this.#contexts.set(cacheKey, entry); | ||
| await this.#store.set(CONTEXTS_KEY, Object.fromEntries(this.#contexts)); | ||
| } | ||
|
|
||
| async #touchContext(cacheKey: string, manager?: KeyManager): Promise<void> { | ||
| const current = this.#contexts.get(cacheKey); | ||
| if (!current && manager) { | ||
| const { keyId, context } = this.#parseCacheKey(cacheKey); | ||
| await this.#persistContext(cacheKey, manager, keyId, context); | ||
| return; | ||
| } | ||
| if (!current) { | ||
| return; | ||
| } | ||
| current.lastUsed = new Date().toISOString(); | ||
| this.#contexts.set(cacheKey, current); | ||
| await this.#store.set(CONTEXTS_KEY, Object.fromEntries(this.#contexts)); | ||
| } | ||
|
|
||
| #parseCacheKey(cacheKey: string): { | ||
| context: KeyServiceContext; | ||
| keyId: string; | ||
| } { | ||
| const [context, ...rest] = cacheKey.split(":"); | ||
| return { | ||
| context: context as KeyServiceContext, | ||
| keyId: rest.join(":"), | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export { GlobalState } from "./state"; | ||
| export { runtime } from "./runtime.svelte"; | ||
| export { KeyService } from "./controllers/key"; | ||
| export type { KeyServiceContext } from "./controllers/key"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.