-
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 11, 2023
1 parent
dffd6e5
commit 0f97ae0
Showing
25 changed files
with
281 additions
and
23 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
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 { MessageDecryptGet, PermissionMethodNostr, Sender } from "~/types"; | ||
|
||
import { addPermissionFor, hasPermissionFor } from "./helpers"; | ||
|
||
const nip44DecryptOrPrompt = async ( | ||
message: MessageDecryptGet, | ||
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/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.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; |
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
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 { MessageEncryptGet, PermissionMethodNostr, Sender } from "~/types"; | ||
|
||
import { addPermissionFor, hasPermissionFor } from "./helpers"; | ||
|
||
const nip44EncryptOrPrompt = async ( | ||
message: MessageEncryptGet, | ||
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/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 | ||
); | ||
|
||
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
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.