-
Notifications
You must be signed in to change notification settings - Fork 210
NIP44 Encryption Support #3075
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
NIP44 Encryption Support #3075
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f24eade
feat: add NIP 44 encryption support
f3bb66d
Use separate encrypt/decrypt dialogs for nip44
9318232
fix: make functionality work
pavanjoshi914 51538c3
Switch back to simple interface for nip44
19182ba
fix: encryptOrPrompt args
rolznz fde0634
chore: run prettier
rolznz 6959be9
chore: update yarn.lock
rolznz 0d65641
Merge pull request #2 from getAlby/fix/coracle-nip44
staab f5893ab
Rename pk to peerPubkey
d9702fa
feat: extract nip44
pavanjoshi914 0f276fb
Merge branch 'master' into extract-nip44
pavanjoshi914 6afc39a
fix: translations
pavanjoshi914 63c583b
refactor: nip04 encrypt decrypt functions
pavanjoshi914 a898d59
Merge branch 'master' into extract-nip44
pavanjoshi914 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
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,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); | ||
| } | ||
| } | ||
| } |
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
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
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
68 changes: 68 additions & 0 deletions
68
src/extension/background-script/actions/nostr/nip44DecryptOrPrompt.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,68 @@ | ||
| 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.ciphertext | ||
| ); | ||
|
|
||
| return { data: response }; | ||
| } else { | ||
| const promptResponse = await utils.openPrompt<{ | ||
| confirm: boolean; | ||
| rememberPermission: boolean; | ||
| }>({ | ||
| ...message, | ||
| action: "public/nostr/confirmDecrypt", | ||
| }); | ||
|
|
||
| // 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.ciphertext | ||
| ); | ||
|
|
||
| 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; |
72 changes: 72 additions & 0 deletions
72
src/extension/background-script/actions/nostr/nip44EncryptOrPrompt.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,72 @@ | ||
| import { USER_REJECTED_ERROR } from "~/common/constants"; | ||
| import nostr from "~/common/lib/nostr"; | ||
| 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 | ||
| ); | ||
| return { data: response }; | ||
| } else { | ||
| const promptResponse = await utils.openPrompt<{ | ||
| confirm: boolean; | ||
| rememberPermission: boolean; | ||
| }>({ | ||
| ...message, | ||
| action: "public/nostr/confirmEncrypt", | ||
| args: { | ||
| encrypt: { | ||
| recipientNpub: nostr.hexToNip19(message.args.peer, "npub"), | ||
| 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 | ||
| ); | ||
|
|
||
| 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 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
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.
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.