From 0c7a9fd8656c2048fdda310e367996f68d38cc0e Mon Sep 17 00:00:00 2001 From: escapedcat Date: Mon, 28 Nov 2022 16:03:42 +0100 Subject: [PATCH] refactor(nostr): use request approach, femove unused code #1727 --- src/app/screens/Nostr/ConfirmSignMessage.tsx | 13 +--- src/common/lib/api.ts | 15 ---- .../actions/nostr/signEventOrPrompt.ts | 26 ++++++- .../actions/permissions/__tests__/add.test.ts | 71 ------------------- .../actions/permissions/add.ts | 31 -------- .../actions/permissions/index.ts | 3 +- src/extension/background-script/router.ts | 1 - 7 files changed, 25 insertions(+), 135 deletions(-) delete mode 100644 src/extension/background-script/actions/permissions/__tests__/add.test.ts delete mode 100644 src/extension/background-script/actions/permissions/add.ts diff --git a/src/app/screens/Nostr/ConfirmSignMessage.tsx b/src/app/screens/Nostr/ConfirmSignMessage.tsx index 2e4d6330c7..616115c5ef 100644 --- a/src/app/screens/Nostr/ConfirmSignMessage.tsx +++ b/src/app/screens/Nostr/ConfirmSignMessage.tsx @@ -11,11 +11,9 @@ import { toast } from "react-toastify"; import ScreenHeader from "~/app/components/ScreenHeader"; import { useNavigationState } from "~/app/hooks/useNavigationState"; import { USER_REJECTED_ERROR } from "~/common/constants"; -import api from "~/common/lib/api"; import msg from "~/common/lib/msg"; import { Event } from "~/extension/ln/nostr/types"; import type { OriginData } from "~/types"; -import { PermissionMethod } from "~/types"; function ConfirmSignMessage() { const navState = useNavigationState(); @@ -35,18 +33,9 @@ function ConfirmSignMessage() { async function confirm() { try { setLoading(true); - - if (rememberPermission) { - await api.addPermission({ - host: origin.host, - method: PermissionMethod["NOSTR_SIGNMESSAGE"], - enabled: true, - blocked: false, - }); - } - msg.reply({ confirm: true, + rememberPermission, }); setSuccessMessage(tCommon("success")); } catch (e) { diff --git a/src/common/lib/api.ts b/src/common/lib/api.ts index 7473fd3fee..04d292adcc 100644 --- a/src/common/lib/api.ts +++ b/src/common/lib/api.ts @@ -16,7 +16,6 @@ import type { MessageSettingsSet, LnurlAuthResponse, Invoice, - MessagePermissionAdd, } from "~/types"; import { @@ -123,19 +122,6 @@ export const lnurlAuth = ( export const getCurrencyRate = async () => utils.call<{ rate: number }>("getCurrencyRate"); -export const addPermission = async ({ - host, - method, - enabled, - blocked, -}: MessagePermissionAdd["args"]) => - utils.call("addPermission", { - host, - method, - enabled, - blocked, - }); - export default { getAccountInfo, getAccounts, @@ -158,5 +144,4 @@ export default { getInvoices, lnurlAuth, getCurrencyRate, - addPermission, }; diff --git a/src/extension/background-script/actions/nostr/signEventOrPrompt.ts b/src/extension/background-script/actions/nostr/signEventOrPrompt.ts index 67b83db86f..443ab9f796 100644 --- a/src/extension/background-script/actions/nostr/signEventOrPrompt.ts +++ b/src/extension/background-script/actions/nostr/signEventOrPrompt.ts @@ -19,6 +19,15 @@ const signEventOrPrompt = async (message: MessageSignEvent) => { }; } + const allowance = await db.allowances + .where("host") + .equalsIgnoreCase(message.origin.host) + .first(); + + if (!allowance?.id) { + return { error: "Could not find an allowance for this host" }; + } + const hasPermission = await db.permissions .where("host") .equalsIgnoreCase(message.origin.host) @@ -31,15 +40,26 @@ const signEventOrPrompt = async (message: MessageSignEvent) => { try { if (!hasPermission) { - const response = await utils.openPrompt<{ + const promptResponse = await utils.openPrompt<{ confirm: boolean; + rememberPermission: boolean; }>({ ...message, action: "public/nostr/confirmSignMessage", }); - if (!response.data.confirm) { - throw new Error("User rejected"); + // add permission to db only if user decided to always allow this request + if (promptResponse.data.rememberPermission) { + const permissionIsAdded = await db.permissions.add({ + createdAt: Date.now().toString(), + allowanceId: allowance.id, + host: message.origin.host, + method: PermissionMethod["NOSTR_SIGNMESSAGE"], + enabled: true, + blocked: true, + }); + + !!permissionIsAdded && (await db.saveToStorage()); } } diff --git a/src/extension/background-script/actions/permissions/__tests__/add.test.ts b/src/extension/background-script/actions/permissions/__tests__/add.test.ts deleted file mode 100644 index 415a818181..0000000000 --- a/src/extension/background-script/actions/permissions/__tests__/add.test.ts +++ /dev/null @@ -1,71 +0,0 @@ -import db from "~/extension/background-script/db"; -import type { DbAllowance, MessagePermissionAdd } from "~/types"; -import { PermissionMethod } from "~/types"; - -import addPermission from "../add"; - -Date.now = jest.fn(() => 1487076708000); - -const stackerNews = { - host: "stacker.news", - imageURL: "https://stacker.news/apple-touch-icon.png", - name: "Stacker News", -}; - -const mockAllowances: DbAllowance[] = [ - { - ...stackerNews, - enabled: true, - id: 3, - lastPaymentAt: 1667291232423, - lnurlAuth: true, - remainingBudget: 500, - totalBudget: 500, - createdAt: "1667291216372", - tag: "", - }, -]; - -beforeEach(async () => { - // fill the DB first - await db.allowances.bulkAdd(mockAllowances); -}); - -afterEach(() => { - jest.clearAllMocks(); -}); - -describe("add permission", () => { - test("saves permissions", async () => { - const message: MessagePermissionAdd = { - application: "LBE", - prompt: true, - action: "addPermission", - origin: { - internal: true, - }, - args: { - host: stackerNews.host, - method: PermissionMethod["WEBLN_SIGNMESSAGE"], - enabled: true, - blocked: false, - }, - }; - - await addPermission(message); - - const dbPermissions = await db.permissions.toArray(); - - expect(dbPermissions).toStrictEqual([ - { - id: 1, - allowanceId: 3, - createdAt: "1487076708000", - host: "stacker.news", - method: PermissionMethod["WEBLN_SIGNMESSAGE"], - blocked: false, - enabled: true, - }, - ]); - }); -}); diff --git a/src/extension/background-script/actions/permissions/add.ts b/src/extension/background-script/actions/permissions/add.ts deleted file mode 100644 index 9103307b89..0000000000 --- a/src/extension/background-script/actions/permissions/add.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { MessagePermissionAdd } from "~/types"; - -import db from "../../db"; - -const addPermission = async (message: MessagePermissionAdd) => { - const { host, method, enabled, blocked } = message.args; - - const matchingAllowance = await db.allowances - .where("host") - .equalsIgnoreCase(host) - .first(); - - if (!matchingAllowance?.id) { - return { error: "No Allowance set for this host" }; - } - - const added = await db.permissions.add({ - createdAt: Date.now().toString(), - allowanceId: matchingAllowance.id, - host, - method, - enabled, - blocked, - }); - - await db.saveToStorage(); - - return { data: added }; -}; - -export default addPermission; diff --git a/src/extension/background-script/actions/permissions/index.ts b/src/extension/background-script/actions/permissions/index.ts index 885aeadec4..ff6d02b25a 100644 --- a/src/extension/background-script/actions/permissions/index.ts +++ b/src/extension/background-script/actions/permissions/index.ts @@ -1,4 +1,3 @@ -import add from "./add"; import deletePermission from "./delete"; -export { add, deletePermission }; +export { deletePermission }; diff --git a/src/extension/background-script/router.ts b/src/extension/background-script/router.ts index 5273a64d3f..fc6aae0127 100644 --- a/src/extension/background-script/router.ts +++ b/src/extension/background-script/router.ts @@ -51,7 +51,6 @@ const routes = { lnurl: lnurl, lnurlAuth: auth, getCurrencyRate: cache.getCurrencyRate, - addPermission: permissions.add, deletePermission: permissions.deletePermission, nostr: { generatePrivateKey: nostr.generatePrivateKey,