Skip to content

Commit

Permalink
fix: refactor dapp security context (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomicvladan committed Feb 23, 2022
1 parent 748a2e0 commit f0ee084
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
48 changes: 30 additions & 18 deletions src/background/dapp-session.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,9 @@ abstract class DappSecurityContext {

/** STORAGE FUNCTIONS */

public async setStorageItem(keyName: string, keyValue: unknown): Promise<void> {
const key = this.enrichStorageKey(keyName)

await new Promise(resolve => this.storage.set({ [key]: keyValue }, () => resolve(true)))
}

public getStorageItem(keyName: string): Promise<unknown> {
const key = this.enrichStorageKey(keyName)

return new Promise(resolve =>
this.storage.get([key], result => {
resolve(result[key])
}),
)
}
public abstract setStorageItem(keyName: string, keyValue: unknown): Promise<void>

private enrichStorageKey(keyName: string): string {
return `${this.storePrefix}-${keyName}`
}
public abstract getStorageItem(keyName: string): Promise<unknown>
}

class TabDappSecurityContext extends DappSecurityContext {
Expand Down Expand Up @@ -79,6 +63,26 @@ class TabDappSecurityContext extends DappSecurityContext {
this.isValidOriginContentRoot(originContentRoot)
)
}

public async setStorageItem(keyName: string, keyValue: unknown): Promise<void> {
const key = this.enrichStorageKey(keyName)

await new Promise(resolve => this.storage.set({ [key]: keyValue }, () => resolve(true)))
}

public getStorageItem(keyName: string): Promise<unknown> {
const key = this.enrichStorageKey(keyName)

return new Promise(resolve =>
this.storage.get([key], result => {
resolve(result[key])
}),
)
}

private enrichStorageKey(keyName: string): string {
return `${this.storePrefix}-${keyName}`
}
}

class ExtensionDappSecurityContext extends DappSecurityContext {
Expand All @@ -89,6 +93,14 @@ class ExtensionDappSecurityContext extends DappSecurityContext {
public isValid(sender: chrome.runtime.MessageSender): boolean {
return isSenderExtension(sender)
}

public setStorageItem(keyName: string, keyValue: unknown): Promise<void> {
throw new Error("ExtensionDappSecurityContext doesn't support extension storage functions")
}

public getStorageItem(keyName: string): Promise<unknown> {
throw new Error("ExtensionDappSecurityContext doesn't support extension storage functions")
}
}

export class DappSessionManager {
Expand Down
2 changes: 1 addition & 1 deletion src/background/feeder/e2e-session.feeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Request<A extends Action, P> {
action: A
sessionId: string
parameters: P
eventId?: string
eventId: string
}

interface Response {
Expand Down

0 comments on commit f0ee084

Please sign in to comment.