Skip to content

Commit

Permalink
feat(nostr): apply enable to support unlock #1727
Browse files Browse the repository at this point in the history
  • Loading branch information
escapedcat committed Nov 29, 2022
1 parent b4f5110 commit 1b48c60
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 30 deletions.
11 changes: 10 additions & 1 deletion src/app/screens/Unlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ function Unlock() {
function unlock() {
auth
.unlock(password, () => {
navigate(from, { replace: true });
console.log("FROM", from);

if (from !== "/") {
console.log("FROM JA");
navigate(from, { replace: true });
} else {
// unlock was triggered on it's own there's no from (i.e. nostr action)
console.log("FROM NEIn");
window.close();
}
})
.catch((e) => {
setError(e.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("enable allowance", () => {
const message: MessageAllowanceEnable = {
application: "LBE",
prompt: true,
action: "enableAllowance",
action: "public/webln/enable",
origin: {
location: "test",
domain: "",
Expand Down Expand Up @@ -95,7 +95,7 @@ describe("enable allowance", () => {
const message: MessageAllowanceEnable = {
application: "LBE",
prompt: true,
action: "enableAllowance",
action: "public/webln/enable",
origin: {
location: "test",
domain: "",
Expand Down
16 changes: 14 additions & 2 deletions src/extension/background-script/actions/allowances/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,40 @@ import { setIcon, ExtensionIcon } from "../setup/setIcon";

const enable = async (
message: MessageAllowanceEnable,
sender: Runtime.MessageSender
sender?: Runtime.MessageSender
) => {
console.log("INSIDE enbale <---- message: ", message);
const isUnlocked = state.getState().isUnlocked();
const host = message.origin.host || message.args.host;
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();
console.log("enable -> isUnlocked: ", isUnlocked);
console.log("enable -> allowance: ", allowance);

if (isUnlocked && allowance && allowance.enabled) {
console.log("enable -> no prompt needed");

return {
data: { enabled: true },
};
} else {
console.log("enable -> prompt needed");

try {
console.log("enable -> prompt needed - try");
const response = await utils.openPrompt<{
enabled: boolean;
remember: boolean;
}>(message);
if (response.data.enabled && sender.tab) {

if (response.data.enabled && sender?.tab) {
await setIcon(ExtensionIcon.Active, sender.tab.id as number); // highlight the icon when enabled
}

console.log("enable -> prompt needed - response: ", response);

// if the response should be saved/remembered we update the allowance for the domain
// as this returns a promise we must wait until it resolves
if (response.data.enabled && response.data.remember) {
Expand Down
66 changes: 42 additions & 24 deletions src/extension/background-script/actions/nostr/signEventOrPrompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import utils from "~/common/lib/utils";
import enable from "~/extension/background-script/actions/allowances/enable";
import db from "~/extension/background-script/db";
import { DbPermission, MessageSignEvent, DbAllowance } from "~/types";
import {
MessageAllowanceEnable,
DbPermission,
MessageSignEvent,
} from "~/types";

import state from "../../state";
import { validateEvent } from "./helpers";
Expand All @@ -20,6 +25,17 @@ const signEventOrPrompt = async (message: MessageSignEvent) => {

console.log("message.args.event", message.args.event);

const enableMessage: MessageAllowanceEnable = {
origin: message.origin, // TODO: make sure we pass the image as well
args: {
host: message.origin.host,
},
action: "public/webln/enable",
};

const foo = await enable(enableMessage);
console.log("after enbale <----", foo);

const dbPermissions = await db.permissions
.where("host")
.equalsIgnoreCase(message.origin.host)
Expand All @@ -44,33 +60,35 @@ const signEventOrPrompt = async (message: MessageSignEvent) => {
throw new Error("User rejected");
}

const matchingAllowance = await db.allowances
.where("host")
.equalsIgnoreCase(message.origin.host)
.first();
let allowanceId = matchingAllowance?.id;

if (!allowanceId) {
console.log("mno matching allowance");

const dbAllowance: DbAllowance = {
createdAt: Date.now().toString(),
enabled: true,
host: message.origin.host,
imageURL: "", // need to get the image
lastPaymentAt: 0,
lnurlAuth: false,
name: "",
remainingBudget: 0,
tag: "",
totalBudget: 0,
};
allowanceId = await db.allowances.add(dbAllowance);
}
// if (!allowanceId) {
// console.log("mno matching allowance");

// const dbAllowance: DbAllowance = {
// createdAt: Date.now().toString(),
// enabled: true,
// host: message.origin.host,
// imageURL: "", // need to get the image
// lastPaymentAt: 0,
// lnurlAuth: false,
// name: "",
// remainingBudget: 0,
// tag: "",
// totalBudget: 0,
// };
// allowanceId = await db.allowances.add(dbAllowance);
// }

console.log("matching allowance!");

if (response.data.rememberPermission) {
const matchingAllowance = await db.allowances
.where("host")
.equalsIgnoreCase(message.origin.host)
.first();
const allowanceId = matchingAllowance?.id;

if (!allowanceId) return; // type-guard only

console.log("rememberme?");

const permission: DbPermission = {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export interface MessageAllowanceEnable extends MessageDefault {
args: {
host: Allowance["host"];
};
action: "enableAllowance";
action: "public/webln/enable";
}

export interface MessageAllowanceDelete extends MessageDefault {
Expand Down

0 comments on commit 1b48c60

Please sign in to comment.