forked from getAlby/lightning-browser-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Dec 22, 2023
1 parent
608ec8b
commit 404cf04
Showing
29 changed files
with
418 additions
and
64 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,33 @@ | ||
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(k as T); | ||
|
||
if (this.keys.length > this.maxSize * 2) { | ||
this.keys.splice(-this.maxSize); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
import { USER_REJECTED_ERROR } from "~/common/constants"; | ||
import utils from "~/common/lib/utils"; | ||
import { getHostFromSender } from "~/common/utils/helpers"; | ||
import { | ||
addPermissionFor, | ||
hasPermissionFor, | ||
} from "~/extension/background-script/permissions"; | ||
import state from "~/extension/background-script/state"; | ||
import { MessageNip44DecryptGet, PermissionMethodNostr, Sender } from "~/types"; | ||
|
||
const nip44DecryptOrPrompt = async ( | ||
message: MessageNip44DecryptGet, | ||
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/confirmEncryptOrDecrypt", | ||
args: { | ||
encryptOrDecrypt: { | ||
action: "decrypt", | ||
peer: message.args.peer, | ||
payload: message.args.payload, | ||
}, | ||
}, | ||
}); | ||
|
||
// 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; |
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
import { USER_REJECTED_ERROR } from "~/common/constants"; | ||
import utils from "~/common/lib/utils"; | ||
import { getHostFromSender } from "~/common/utils/helpers"; | ||
import { | ||
addPermissionFor, | ||
hasPermissionFor, | ||
} from "~/extension/background-script/permissions"; | ||
import state from "~/extension/background-script/state"; | ||
import { MessageNip44EncryptGet, PermissionMethodNostr, Sender } from "~/types"; | ||
|
||
const nip44EncryptOrPrompt = async ( | ||
message: MessageNip44EncryptGet, | ||
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/confirmEncryptOrDecrypt", | ||
args: { | ||
encryptOrDecrypt: { | ||
action: "encrypt", | ||
peer: message.args.peer, | ||
message: message.args.plaintext, | ||
}, | ||
}, | ||
}); | ||
|
||
// 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
Oops, something went wrong.