-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jonathan Staab
committed
Aug 16, 2023
1 parent
dffd6e5
commit 9584cc6
Showing
27 changed files
with
375 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,29 @@ | ||
export class LRUCache<T, U> { | ||
map = new Map<T, U>(); | ||
keys: T[] = []; | ||
|
||
constructor(readonly maxSize: number) {} | ||
|
||
has(k: T) { | ||
return this.map.has(k); | ||
} | ||
|
||
get(k: T) { | ||
const v = this.map.get(k); | ||
|
||
if (v !== undefined) { | ||
this.keys.push(this.keys.shift() as T); | ||
} | ||
|
||
return v; | ||
} | ||
|
||
set(k: T, v: U) { | ||
this.map.set(k, v); | ||
this.keys.push(k); | ||
|
||
if (this.map.size > this.maxSize) { | ||
this.map.delete(this.keys.shift() as T); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/extension/background-script/actions/nostr/nip44DecryptOrPrompt.ts
This file contains 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,70 @@ | ||
import { USER_REJECTED_ERROR } from "~/common/constants"; | ||
import utils from "~/common/lib/utils"; | ||
import { getHostFromSender } from "~/common/utils/helpers"; | ||
import state from "~/extension/background-script/state"; | ||
import i18n from "~/i18n/i18nConfig"; | ||
import { Nip44MessageDecryptGet, PermissionMethodNostr, Sender } from "~/types"; | ||
|
||
import { addPermissionFor, hasPermissionFor } from "./helpers"; | ||
|
||
const nip44DecryptOrPrompt = async ( | ||
message: Nip44MessageDecryptGet, | ||
sender: Sender | ||
) => { | ||
const host = getHostFromSender(sender); | ||
if (!host) return; | ||
|
||
try { | ||
const hasPermission = await hasPermissionFor( | ||
PermissionMethodNostr["NOSTR_NIP44DECRYPT"], | ||
host | ||
); | ||
|
||
if (hasPermission) { | ||
const nostr = await state.getState().getNostr(); | ||
const response = await nostr.nip44Decrypt( | ||
message.args.peer, | ||
message.args.payload | ||
); | ||
|
||
return { data: response }; | ||
} else { | ||
const promptResponse = await utils.openPrompt<{ | ||
confirm: boolean; | ||
rememberPermission: boolean; | ||
}>({ | ||
...message, | ||
action: "public/nostr/confirm", | ||
args: { | ||
description: i18n.t("permissions:nostr.nip44decrypt"), | ||
}, | ||
}); | ||
|
||
// add permission to db only if user decided to always allow this request | ||
if (promptResponse.data.rememberPermission) { | ||
await addPermissionFor( | ||
PermissionMethodNostr["NOSTR_NIP44DECRYPT"], | ||
host | ||
); | ||
} | ||
if (promptResponse.data.confirm) { | ||
const nostr = await state.getState().getNostr(); | ||
const response = await nostr.nip44Decrypt( | ||
message.args.peer, | ||
message.args.payload | ||
); | ||
|
||
return { data: response }; | ||
} else { | ||
return { error: USER_REJECTED_ERROR }; | ||
} | ||
} | ||
} catch (e) { | ||
console.error("decrypt failed", e); | ||
if (e instanceof Error) { | ||
return { error: e.message }; | ||
} | ||
} | ||
}; | ||
|
||
export default nip44DecryptOrPrompt; |
70 changes: 70 additions & 0 deletions
70
src/extension/background-script/actions/nostr/nip44EncryptOrPrompt.ts
This file contains 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,70 @@ | ||
import { USER_REJECTED_ERROR } from "~/common/constants"; | ||
import utils from "~/common/lib/utils"; | ||
import { getHostFromSender } from "~/common/utils/helpers"; | ||
import state from "~/extension/background-script/state"; | ||
import i18n from "~/i18n/i18nConfig"; | ||
import { Nip44MessageEncryptGet, PermissionMethodNostr, Sender } from "~/types"; | ||
|
||
import { addPermissionFor, hasPermissionFor } from "./helpers"; | ||
|
||
const nip44EncryptOrPrompt = async ( | ||
message: Nip44MessageEncryptGet, | ||
sender: Sender | ||
) => { | ||
const host = getHostFromSender(sender); | ||
if (!host) return; | ||
|
||
try { | ||
const hasPermission = await hasPermissionFor( | ||
PermissionMethodNostr["NOSTR_NIP44ENCRYPT"], | ||
host | ||
); | ||
|
||
if (hasPermission) { | ||
const response = (await state.getState().getNostr()).nip44Encrypt( | ||
message.args.peer, | ||
message.args.plaintext, | ||
message.args.v | ||
); | ||
|
||
return { data: response }; | ||
} else { | ||
const promptResponse = await utils.openPrompt<{ | ||
confirm: boolean; | ||
rememberPermission: boolean; | ||
}>({ | ||
...message, | ||
action: "public/nostr/confirm", | ||
args: { | ||
description: i18n.t("permissions:nostr.nip44encrypt"), | ||
}, | ||
}); | ||
|
||
// add permission to db only if user decided to always allow this request | ||
if (promptResponse.data.rememberPermission) { | ||
await addPermissionFor( | ||
PermissionMethodNostr["NOSTR_NIP44ENCRYPT"], | ||
host | ||
); | ||
} | ||
if (promptResponse.data.confirm) { | ||
const response = (await state.getState().getNostr()).nip44Encrypt( | ||
message.args.peer, | ||
message.args.plaintext, | ||
message.args.v | ||
); | ||
|
||
return { data: response }; | ||
} else { | ||
return { error: USER_REJECTED_ERROR }; | ||
} | ||
} | ||
} catch (e) { | ||
console.error("encrypt failed", e); | ||
if (e instanceof Error) { | ||
return { error: e.message }; | ||
} | ||
} | ||
}; | ||
|
||
export default nip44EncryptOrPrompt; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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.