Skip to content

Commit

Permalink
refactor(permissions): use permission enum #1727
Browse files Browse the repository at this point in the history
  • Loading branch information
escapedcat committed Nov 28, 2022
1 parent 0ae37e1 commit 757b296
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/app/screens/Nostr/ConfirmSignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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();
Expand All @@ -38,7 +39,7 @@ function ConfirmSignMessage() {
if (rememberPermission) {
await api.addPermission({
host: origin.host,
method: "signMessage",
method: PermissionMethod["NOSTR_SIGNMESSAGE"],
enabled: true,
blocked: false,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import db from "~/extension/background-script/db";
import type { DbAllowance, MessageAllowanceDelete } from "~/types";
import { PermissionMethod } from "~/types";

import deleteAllowance from "../delete";

Expand Down Expand Up @@ -38,7 +39,7 @@ const mockPermissions = [
allowanceId: 1,
createdAt: "1667291216372",
host: "pro.kollider.xyz",
method: "listChannels",
method: PermissionMethod["WEBLN_LISTCHANNELS"],
blocked: false,
enabled: true,
},
Expand All @@ -47,7 +48,7 @@ const mockPermissions = [
allowanceId: 2,
createdAt: "1667291216372",
host: "lnmarkets.com",
method: "getinfo",
method: PermissionMethod["WEBLN_GETINFO"],
blocked: false,
enabled: true,
},
Expand All @@ -56,7 +57,7 @@ const mockPermissions = [
allowanceId: 2,
createdAt: "1667291216372",
host: "lnmarkets.com",
method: "some-method",
method: PermissionMethod["WEBLN_SIGNMESSAGE"],
blocked: false,
enabled: true,
},
Expand Down
4 changes: 2 additions & 2 deletions src/extension/background-script/actions/ln/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import utils from "~/common/lib/utils";
import { MessageGenericRequest } from "~/types";
import { MessageGenericRequest, PermissionMethod } from "~/types";

import db from "../../db";
import state from "../../state";
Expand Down Expand Up @@ -36,7 +36,7 @@ const request = async (
}

// prefix method with webln to prevent potential naming conflicts (e.g. with nostr calls that also use the permissions)
const weblnMethod = `${WEBLN_PREFIX}${method}`;
const weblnMethod = `${WEBLN_PREFIX}${method}` as PermissionMethod;

const permission = await db.permissions
.where("host")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import utils from "~/common/lib/utils";
import db from "~/extension/background-script/db";
import { MessageSignEvent } from "~/types";
import { PermissionMethod } from "~/types";

import state from "../../state";
import { validateEvent } from "./helpers";
Expand All @@ -22,7 +23,9 @@ const signEventOrPrompt = async (message: MessageSignEvent) => {
.where("host")
.equalsIgnoreCase(message.origin.host)
.and(
(permission) => permission.enabled && permission.method === "signMessage"
(permission) =>
permission.enabled &&
permission.method === PermissionMethod["NOSTR_SIGNMESSAGE"]
)
.first();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import db from "~/extension/background-script/db";
import type { DbAllowance, MessagePermissionAdd } from "~/types";
import { PermissionMethod } from "~/types";

import addPermission from "../add";

Expand Down Expand Up @@ -45,7 +46,7 @@ describe("add permission", () => {
},
args: {
host: stackerNews.host,
method: "signMessage",
method: PermissionMethod["WEBLN_SIGNMESSAGE"],
enabled: true,
blocked: false,
},
Expand All @@ -61,7 +62,7 @@ describe("add permission", () => {
allowanceId: 3,
createdAt: "1487076708000",
host: "stacker.news",
method: "signMessage",
method: PermissionMethod["WEBLN_SIGNMESSAGE"],
blocked: false,
enabled: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import db from "~/extension/background-script/db";
import type { MessagePermissionDelete } from "~/types";
import { PermissionMethod } from "~/types";

import deletePermission from "../delete";

Expand All @@ -15,7 +16,7 @@ const mockPermissions = [
allowanceId: 3,
createdAt: "1667291216372",
host: stackerNews.host,
method: "the-request-method-1",
method: PermissionMethod["WEBLN_LISTCHANNELS"],
blocked: false,
enabled: true,
},
Expand All @@ -24,7 +25,7 @@ const mockPermissions = [
allowanceId: 3,
createdAt: "1667291216372",
host: stackerNews.host,
method: "the-request-method-2",
method: PermissionMethod["WEBLN_GETINFO"],
blocked: false,
enabled: true,
},
Expand All @@ -50,7 +51,7 @@ describe("delete permission", () => {
},
args: {
host: stackerNews.host,
method: "the-request-method-2",
method: PermissionMethod["WEBLN_GETINFO"],
},
};

Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,19 @@ export interface Payment extends Omit<DbPayment, "id"> {
id: number;
}

export enum PermissionMethod {
NOSTR_SIGNMESSAGE = "nostr/signMessage",
WEBLN_LISTCHANNELS = "webln/listchannels",
WEBLN_GETINFO = "webln/getinfo",
WEBLN_SIGNMESSAGE = "webln/signmessage",
}

export interface DbPermission {
id?: number;
createdAt: string;
allowanceId: number;
host: string;
method: "signMessage";
method: PermissionMethod;
enabled: boolean;
blocked: boolean;
}
Expand Down

0 comments on commit 757b296

Please sign in to comment.