Skip to content

Commit

Permalink
refactor(nostr): use request approach, remove unused code #1727
Browse files Browse the repository at this point in the history
  • Loading branch information
escapedcat committed Nov 29, 2022
1 parent 4188426 commit b68713a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 135 deletions.
13 changes: 1 addition & 12 deletions src/app/screens/Nostr/ConfirmSignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
15 changes: 0 additions & 15 deletions src/common/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
MessageSettingsSet,
LnurlAuthResponse,
Invoice,
MessagePermissionAdd,
} from "~/types";

import {
Expand Down Expand Up @@ -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<MessagePermissionAdd["args"]>("addPermission", {
host,
method,
enabled,
blocked,
});

export default {
getAccountInfo,
getAccounts,
Expand All @@ -158,5 +144,4 @@ export default {
getInvoices,
lnurlAuth,
getCurrencyRate,
addPermission,
};
26 changes: 23 additions & 3 deletions src/extension/background-script/actions/nostr/signEventOrPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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());
}
}

Expand Down

This file was deleted.

31 changes: 0 additions & 31 deletions src/extension/background-script/actions/permissions/add.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/extension/background-script/actions/permissions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import add from "./add";
import deletePermission from "./delete";

export { add, deletePermission };
export { deletePermission };
1 change: 0 additions & 1 deletion src/extension/background-script/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const routes = {
lnurl: lnurl,
lnurlAuth: auth,
getCurrencyRate: cache.getCurrencyRate,
addPermission: permissions.add,
deletePermission: permissions.deletePermission,
nostr: {
generatePrivateKey: nostr.generatePrivateKey,
Expand Down

0 comments on commit b68713a

Please sign in to comment.