Skip to content

Commit

Permalink
Merge pull request #2753 from getAlby/persist-enable
Browse files Browse the repository at this point in the history
feat: add isEnabled provider method, which returns provider was ever enabled
  • Loading branch information
rolznz committed Oct 6, 2023
2 parents 20d5b7d + e0e692f commit a1a6b4c
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/extension/background-script/actions/alby/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import enable from "./enable";
export { enable };
import isEnabled from "./isEnabled";
export { enable, isEnabled };
30 changes: 30 additions & 0 deletions src/extension/background-script/actions/alby/isEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getHostFromSender } from "~/common/utils/helpers";
import db from "~/extension/background-script/db";
import type { MessageAllowanceEnable, Sender } from "~/types";

import state from "../../state";

const isEnabled = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

const enabledFor = new Set(allowance?.enabledFor);

if (isUnlocked && allowance && allowance.enabled && enabledFor.has("alby")) {
return {
data: { isEnabled: true },
};
} else {
return {
data: { isEnabled: false },
};
}
};

export default isEnabled;
2 changes: 2 additions & 0 deletions src/extension/background-script/actions/liquid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import enable from "./enable";
import fetchAssetRegistry from "./fetchAssetRegistry";
import getAddressOrPrompt from "./getAddressOrPrompt";
import getPsetPreview from "./getPsetPreview";
import isEnabled from "./isEnabled";
import signPset from "./signPset";
import signPsetWithPrompt from "./signPsetWithPrompt";

Expand All @@ -10,6 +11,7 @@ export {
fetchAssetRegistry,
getAddressOrPrompt,
getPsetPreview,
isEnabled,
signPset,
signPsetWithPrompt,
};
35 changes: 35 additions & 0 deletions src/extension/background-script/actions/liquid/isEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getHostFromSender } from "~/common/utils/helpers";
import db from "~/extension/background-script/db";
import type { MessageAllowanceEnable, Sender } from "~/types";

import state from "../../state";

const isEnabled = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

const enabledFor = new Set(allowance?.enabledFor);

if (
isUnlocked &&
allowance &&
allowance.enabled &&
enabledFor.has("liquid")
) {
return {
data: { isEnabled: true },
};
} else {
return {
data: { isEnabled: false },
};
}
};

export default isEnabled;
2 changes: 2 additions & 0 deletions src/extension/background-script/actions/nostr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import generatePrivateKey from "./generatePrivateKey";
import getPrivateKey from "./getPrivateKey";
import getPublicKeyOrPrompt from "./getPublicKeyOrPrompt";
import getRelays from "./getRelays";
import isEnabled from "./isEnabled";
import removePrivateKey from "./removePrivateKey";
import setPrivateKey from "./setPrivateKey";
import signEventOrPrompt from "./signEventOrPrompt";
Expand All @@ -21,6 +22,7 @@ export {
getPublicKey,
getPublicKeyOrPrompt,
getRelays,
isEnabled,
removePrivateKey,
setPrivateKey,
signEventOrPrompt,
Expand Down
30 changes: 30 additions & 0 deletions src/extension/background-script/actions/nostr/isEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getHostFromSender } from "~/common/utils/helpers";
import db from "~/extension/background-script/db";
import type { MessageAllowanceEnable, Sender } from "~/types";

import state from "../../state";

const isEnabled = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

const enabledFor = new Set(allowance?.enabledFor);

if (isUnlocked && allowance && allowance.enabled && enabledFor.has("nostr")) {
return {
data: { isEnabled: true },
};
} else {
return {
data: { isEnabled: false },
};
}
};

export default isEnabled;
3 changes: 2 additions & 1 deletion src/extension/background-script/actions/webbtc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import enable from "./enable";
import getAddress from "./getAddress";
import getAddressOrPrompt from "./getAddressOrPrompt";
import getInfo from "./getInfo";
import isEnabled from "./isEnabled";

export { enable, getAddress, getAddressOrPrompt, getInfo };
export { enable, getAddress, getAddressOrPrompt, getInfo, isEnabled };
35 changes: 35 additions & 0 deletions src/extension/background-script/actions/webbtc/isEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getHostFromSender } from "~/common/utils/helpers";
import db from "~/extension/background-script/db";
import type { MessageAllowanceEnable, Sender } from "~/types";

import state from "../../state";

const isEnabled = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

const enabledFor = new Set(allowance?.enabledFor);

if (
isUnlocked &&
allowance &&
allowance.enabled &&
enabledFor.has("webbtc")
) {
return {
data: { isEnabled: true },
};
} else {
return {
data: { isEnabled: false },
};
}
};

export default isEnabled;
2 changes: 2 additions & 0 deletions src/extension/background-script/actions/webln/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import enable from "./enable";
import getBalanceOrPrompt from "./getBalanceOrPrompt";
import isEnabled from "./isEnabled";
import keysendOrPrompt from "./keysendOrPrompt";
import lnurl from "./lnurl";
import makeInvoiceOrPrompt from "./makeInvoiceOrPrompt";
Expand All @@ -9,6 +10,7 @@ import signMessageOrPrompt from "./signMessageOrPrompt";
export {
enable,
getBalanceOrPrompt,
isEnabled,
keysendOrPrompt,
lnurl,
makeInvoiceOrPrompt,
Expand Down
30 changes: 30 additions & 0 deletions src/extension/background-script/actions/webln/isEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getHostFromSender } from "~/common/utils/helpers";
import db from "~/extension/background-script/db";
import type { MessageAllowanceEnable, Sender } from "~/types";

import state from "../../state";

const isEnabled = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

const enabledFor = new Set(allowance?.enabledFor);

if (isUnlocked && allowance && allowance.enabled && enabledFor.has("webln")) {
return {
data: { isEnabled: true },
};
} else {
return {
data: { isEnabled: false },
};
}
};

export default isEnabled;
5 changes: 5 additions & 0 deletions src/extension/background-script/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,19 @@ const routes = {
// Public calls that are accessible from the inpage script (through the content script)
public: {
webbtc: {
isEnabled: webbtc.isEnabled,
enable: webbtc.enable,
getInfo: webbtc.getInfo,
getAddressOrPrompt: webbtc.getAddressOrPrompt,
},
alby: {
isEnabled: alby.isEnabled,
enable: alby.enable,
addAccount: accounts.promptAdd,
},
webln: {
enable: webln.enable,
isEnabled: webln.isEnabled,
getInfo: ln.getInfo,
sendPaymentOrPrompt: webln.sendPaymentOrPrompt,
keysendOrPrompt: webln.keysendOrPrompt,
Expand All @@ -104,11 +107,13 @@ const routes = {
request: ln.request,
},
liquid: {
isEnabled: liquid.isEnabled,
enable: liquid.enable,
getAddressOrPrompt: liquid.getAddressOrPrompt,
signPsetWithPrompt: liquid.signPsetWithPrompt,
},
nostr: {
isEnabled: nostr.isEnabled,
enable: nostr.enable,
getPublicKeyOrPrompt: nostr.getPublicKeyOrPrompt,
signEventOrPrompt: nostr.signEventOrPrompt,
Expand Down
7 changes: 5 additions & 2 deletions src/extension/content-script/alby.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import shouldInject from "./shouldInject";

// Alby calls that can be executed from the AlbyProvider.
// Update when new calls are added
const albyCalls = ["alby/enable", "alby/addAccount"];
const albyCalls = ["alby/enable", "alby/addAccount", "alby/isEnabled"];
// calls that can be executed when alby is not enabled for the current content page
const disabledCalls = ["alby/enable"];
const disabledCalls = ["alby/enable", "alby/isEnabled"];

let isEnabled = false; // store if alby is enabled for this content page
let isRejected = false; // store if the alby enable call failed. if so we do not prompt again
Expand Down Expand Up @@ -70,6 +70,9 @@ async function init() {
isRejected = true;
}
}
if (ev.data.action === "alby/isEnabled") {
isEnabled = response.data?.isEnabled;
}
postMessage(ev, response);
};
return browser.runtime
Expand Down
7 changes: 6 additions & 1 deletion src/extension/content-script/liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const liquidCalls = [
"liquid/getAddressOrPrompt",
"liquid/signPsetWithPrompt",
"liquid/enable",
"liquid/isEnabled",
];
// calls that can be executed when liquid is not enabled for the current content page
const disabledCalls = ["liquid/enable"];
const disabledCalls = ["liquid/enable", "liquid/isEnabled"];

let isEnabled = false; // store if liquid is enabled for this content page
let isRejected = false; // store if the liquid enable call failed. if so we do not prompt again
Expand Down Expand Up @@ -76,6 +77,10 @@ async function init() {
}
}

if (ev.data.action === `${SCOPE}/isEnabled`) {
isEnabled = response.data?.isEnabled;
}

postMessage(ev, response);
};

Expand Down
6 changes: 5 additions & 1 deletion src/extension/content-script/nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const nostrCalls = [
"nostr/on",
"nostr/off",
"nostr/emit",
"nostr/isEnabled",
];
// calls that can be executed when nostr is not enabled for the current content page
const disabledCalls = ["nostr/enable"];
const disabledCalls = ["nostr/enable", "nostr/isEnabled"];

let isEnabled = false; // store if nostr is enabled for this content page
let isRejected = false; // store if the nostr enable call failed. if so we do not prompt again
Expand Down Expand Up @@ -92,6 +93,9 @@ async function init() {
isRejected = true;
}
}
if (ev.data.action === "nostr/isEnabled") {
isEnabled = response.data?.isEnabled;
}

postMessage(ev, response);
};
Expand Down
7 changes: 6 additions & 1 deletion src/extension/content-script/webbtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const webbtcCalls = [
"webbtc/enable",
"webbtc/getInfo",
"webbtc/getAddressOrPrompt",
"webbtc/isEnabled",
];
// calls that can be executed when `window.webbtc` is not enabled for the current content page
const disabledCalls = ["webbtc/enable"];
const disabledCalls = ["webbtc/enable", "webbtc/isEnabled"];

let isEnabled = false; // store if webbtc is enabled for this content page
let isRejected = false; // store if the webbtc enable call failed. if so we do not prompt again
Expand Down Expand Up @@ -75,6 +76,10 @@ async function init() {
}
}

if (ev.data.action === `${SCOPE}/isEnabled`) {
isEnabled = response.data?.isEnabled;
}

postMessage(ev, response);
};

Expand Down
7 changes: 6 additions & 1 deletion src/extension/content-script/webln.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const weblnCalls = [
"webln/on",
"webln/emit",
"webln/off",
"webln/isEnabled",
];
// calls that can be executed when webln is not enabled for the current content page
const disabledCalls = ["webln/enable"];
const disabledCalls = ["webln/enable", "webln/isEnabled"];

let isEnabled = false; // store if webln is enabled for this content page
let isRejected = false; // store if the webln enable call failed. if so we do not prompt again
Expand Down Expand Up @@ -101,6 +102,10 @@ async function init() {
isRejected = true;
}
}

if (ev.data.action === "webln/isEnabled") {
isEnabled = response.data?.isEnabled;
}
postMessage(ev, response);
};
return browser.runtime
Expand Down
Loading

0 comments on commit a1a6b4c

Please sign in to comment.