Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: manifest v3 support #1051 #1124

Merged
merged 62 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
58d6307
refactor!: manifest v3 support #1051
escapedcat Jul 8, 2022
7f61c9a
refactor!: fix manifest v3 errors #1051
escapedcat Jul 8, 2022
992efc5
test: update ext-id finding #1051
escapedcat Jul 11, 2022
a4c5d69
fix(axios): use fetach-adapter within service-worker #1124
escapedcat Jul 12, 2022
902713c
chore: update opera version #1051
escapedcat Sep 1, 2022
3a21e4e
revert: remove outdated function wich was added earlier in this PR #1051
escapedcat Sep 9, 2022
8321efb
fix: use further axios adapters #1051
escapedcat Sep 9, 2022
a9a61a1
fix(notifications): update imageUrl for manifest v3 #1051
escapedcat Sep 9, 2022
acd58b1
fix(manfifest): add mssing bundle to ressources for manifest v3 #1051
escapedcat Sep 9, 2022
afe92ff
fix(seticon): use action for manifest v3 #1051
escapedcat Sep 9, 2022
29ebf1e
chore: remove rebase issue #1051
escapedcat Sep 9, 2022
c9f359f
chore: trigger actions
escapedcat Oct 10, 2022
50f7925
chore: trigger actions
escapedcat Oct 10, 2022
f84b183
chore: merge master
escapedcat Oct 11, 2022
8818410
fix: use further axios adapters #1051
escapedcat Oct 11, 2022
f247a23
fix: use storage sessions for pw #1051
escapedcat Nov 8, 2022
117a7dc
chore: messy storage.session progress #1051
escapedcat Nov 11, 2022
d1d04d2
chore: move addListeners to teeh top #1051
escapedcat Nov 11, 2022
b90d101
chore: revert settings handling and cleanup #1051
escapedcat Nov 11, 2022
231e5a3
feat: support pw-change and further cleanup #1051
escapedcat Nov 11, 2022
6e4cc0c
feat: merge master and adjust nostr, revert axios to v0 #1051
escapedcat Nov 11, 2022
6eb2ed0
chore: update unit-tests
escapedcat Nov 14, 2022
c5f3262
refactor: centralize chrome.storage.session usage #1051
escapedcat Nov 14, 2022
05fa220
refactor: use polyfill but ignore type #1051
escapedcat Nov 15, 2022
54edc6d
feat: add mv3 check #1051
escapedcat Nov 15, 2022
077200c
feat: support seticon for v2 and v3 #1051
escapedcat Nov 16, 2022
62106e8
test: update test for v3 #1051
escapedcat Nov 16, 2022
54d3370
fix: patch jest-webextension-mock getManifest support #1051
escapedcat Nov 16, 2022
5d0481d
fix: getCurrency test-setup #1051
escapedcat Nov 16, 2022
56b901c
fix: check for null and undefined password #1051
escapedcat Nov 17, 2022
253ecce
chore: add linting for tests #1051
escapedcat Nov 17, 2022
f490aa8
fix: patch lnmessage for comando #1051
escapedcat Nov 17, 2022
e17cb3f
feat: create correct manifest for chrome v3 and v2 for FF and opera #…
escapedcat Nov 17, 2022
91f62bf
refactor: update to lnmessage v0.14 #1051
escapedcat Nov 18, 2022
925016a
Merge branch 'master' into refactor/manifest-v3-support
bumi Nov 20, 2022
f363647
fix: nostr #1051
escapedcat Nov 21, 2022
657760b
fix: manifest warning #1051
escapedcat Nov 22, 2022
461acdd
chore: merge master #1051
escapedcat Nov 22, 2022
9433185
chore: update jest-webextension-mock getManifest support #1051
escapedcat Nov 26, 2022
81ee21e
chore: merge master #1051
escapedcat Dec 1, 2022
5383e5c
chore: merge master #1051
escapedcat Dec 2, 2022
affbc4f
chore: merge master #1051
escapedcat Dec 5, 2022
5064114
Merge branch 'master' into refactor/manifest-v3-support
escapedcat Dec 8, 2022
bc7b862
feat: show prefixed error messages #1051
escapedcat Dec 8, 2022
b9d97f3
fix: nostr getPublicKey #1051
escapedcat Dec 8, 2022
bf8a9d1
Merge branch 'master' into refactor/manifest-v3-support
escapedcat Dec 13, 2022
955581f
fix(notifications): update imageUrl for manifest v3 #1051
escapedcat Dec 13, 2022
f624f63
Merge branch 'master' into refactor/manifest-v3-support
bumi Jan 9, 2023
1acacba
Merge branch 'master' into HEAD
im-adithya Feb 2, 2023
7acf4ae
chore: merge master #1051
escapedcat Feb 7, 2023
dd91efc
test: fix nostr tests #1051
escapedcat Feb 7, 2023
18fa751
test: skip auth test till axios-fetch is fixed #1051
escapedcat Feb 7, 2023
20fe751
Merge branch 'master' into refactor/manifest-v3-support
escapedcat Feb 13, 2023
26a9c5f
fix(lock): close tabs/popup for chrome and firefox #1051
escapedcat Feb 13, 2023
b009251
fix: types after merge master #1051
escapedcat Mar 2, 2023
43a4091
fix: use further axios adapters #1051
escapedcat Mar 2, 2023
dd85f9a
chore: merge master #1051
escapedcat Mar 20, 2023
05802a9
fix(commando): revert lnmessage to v0.0.19 #1051
escapedcat Mar 20, 2023
3ea8086
fix(commando): use lnmessage 0.3.0 beta #1051
escapedcat Mar 22, 2023
b695b58
fix: window error within SW #1051
escapedcat Mar 22, 2023
573268b
chore: remove unused dep #1051
escapedcat Mar 22, 2023
9b53a29
fix: use further axios adapters #1051
escapedcat Mar 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"jest": true
},
"rules": {
"@typescript-eslint/ban-ts-comment": [
"error",
{
"ts-ignore": "allow-with-description"
}
],
"@typescript-eslint/no-unused-vars": ["warn", { "args": "none" }], // No warnings for unused function arguments, which might be used in the future.
"no-console": ["error", { "allow": ["info", "warn", "error"] }],
"no-constant-binary-expression": "error",
Expand Down
7 changes: 7 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Object.defineProperty(window, "matchMedia", {
beforeAll(() => {
// Enable the mocking in tests.
server.listen();

//https://github.com/mozilla/webextension-polyfill/issues/329
global.chrome.storage.session = {
set: jest.fn(),
get: jest.fn(),
};
});

afterEach(() => {
Expand All @@ -44,4 +50,5 @@ afterEach(() => {
afterAll(() => {
// Clean up once the tests are done.
server.close();
jest.restoreAllMocks();
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"lint:js:fix": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
"tsc:compile": "tsc --noEmit",
"format": "prettier --check '**/*.(md|json)' 'src/**/*.(js|ts|jsx|tsx)'",
"format:fix": "prettier --loglevel silent --write '**/*.(md|json)' 'src/**/*.(js|ts|jsx|tsx)'",
"format:fix": "prettier --loglevel silent --write '**/*.(md|json)' '(src|tests)/**/*.(js|ts|jsx|tsx)'",
"test:unit": "jest",
"test:e2e": "del-cli ./puppeteer-user-data-dir && npx playwright test",
"test": "yarn test:unit && yarn test:e2e",
Expand All @@ -41,7 +41,9 @@
"@noble/secp256k1": "^1.7.1",
"@tailwindcss/forms": "^0.5.3",
"@tailwindcss/line-clamp": "^0.4.2",
"axios": "^1.2.2",
"@vespaiach/axios-fetch-adapter": "^0.3.0",
"avvvatars-react": "^0.4.2",
"axios": "^0.27.2",
"bech32": "^2.0.0",
"bolt11": "^1.4.0",
"crypto-js": "^4.1.1",
Expand Down
4 changes: 4 additions & 0 deletions src/app/context/AccountContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@ export function AccountProvider({ children }: { children: React.ReactNode }) {
})
.catch((e) => {
toast.error(`An unexpected error occurred (${e.message})`);
console.error(
`AccountContext: An unexpected error occurred (${e.message})`
escapedcat marked this conversation as resolved.
Show resolved Hide resolved
);
})
.finally(() => {
setLoading(false);
});

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
7 changes: 4 additions & 3 deletions src/app/context/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ export const SettingsProvider = ({
useEffect(() => {
api
.getSettings()
.then((response) => {
setSettings(response);
.then((settings) => {
setSettings(settings);
})
.catch((e) => {
toast.error(
toast.error(`An unexpected error occurred (${e.message})`);
console.error(
`SettingsProvider: An unexpected error occurred (${e.message})`
);
})
Expand Down
7 changes: 6 additions & 1 deletion src/common/lib/lnurl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetchAdapter from "@vespaiach/axios-fetch-adapter";
import axios from "axios";
import lightningPayReq from "bolt11";
import Hex from "crypto-js/enc-hex";
Expand Down Expand Up @@ -89,8 +90,12 @@ const lnurl = {
} else {
try {
const { data }: { data: LNURLDetails | LNURLError } = await axios.get(
url.toString()
url.toString(),
{
adapter: fetchAdapter,
}
);

const lnurlDetails = data;

if (isLNURLDetailsError(lnurlDetails)) {
Expand Down
4 changes: 4 additions & 0 deletions src/common/utils/mv3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import browser from "webextension-polyfill";

export const isManifestV3 =
browser.runtime.getManifest().manifest_version === 3;
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jest.mock("~/common/lib/crypto", () => {
});

const defaultMockState = {
password: "123456",
saveToStorage: jest.fn,
password: () => "123456",
accounts: {},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jest.mock("~/common/lib/crypto", () => {
});

const mockState = {
password: "123456",
password: jest.fn,
saveToStorage: jest.fn,
accounts: {
"888": {
Expand All @@ -45,6 +45,10 @@ describe("export account", () => {
});

test("export existing lndhub account", async () => {
(chrome.storage.session.get as jest.Mock).mockResolvedValue({
password: 123456,
});

const message: MessageAccountDecryptedDetails = {
application: "LBE",
args: {
Expand All @@ -69,6 +73,10 @@ describe("export account", () => {
});

test("export non-existing account should error", async () => {
(chrome.storage.session.get as jest.Mock).mockResolvedValue({
password: 123456,
});

const message: MessageAccountDecryptedDetails = {
application: "LBE",
args: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jest.mock("~/common/lib/crypto", () => {
});

const mockState = {
password: "123456",
saveToStorage: jest.fn,
accounts: {
"888": {
Expand All @@ -38,6 +37,10 @@ describe("edit account", () => {
});

test("edit existing account", async () => {
(chrome.storage.session.get as jest.Mock).mockResolvedValue({
password: 123456,
});

const message: MessageAccountEdit = {
application: "LBE",
args: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import unlock from "../unlock";

jest.mock("~/extension/background-script/state");

const passwordMock = jest.fn;

const mockState = {
password: "123456",
password: passwordMock,
currentAccountId: "1e1e8ea6-493e-480b-9855-303d37506e97",
getAccount: () => ({
config:
Expand All @@ -33,9 +35,7 @@ describe("edit account", () => {
};

state.getState = jest.fn().mockReturnValue(mockState);
state.setState = () => jest.fn;

const spy = jest.spyOn(state, "setState");
const spy = jest.spyOn(mockState, "password");

expect(await unlock(message)).toStrictEqual({
data: {
Expand All @@ -44,9 +44,7 @@ describe("edit account", () => {
},
});

expect(spy).toHaveBeenNthCalledWith(1, {
password: "1",
});
expect(spy).toHaveBeenNthCalledWith(1, "1");

expect(spy).toHaveBeenCalledTimes(1);
});
Expand Down
6 changes: 2 additions & 4 deletions src/extension/background-script/actions/accounts/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ const add = async (message: MessageAccountAdd) => {

// TODO: add validations
// TODO: make sure a password is set

const password = state.getState().password;
const currentAccountId = state.getState().currentAccountId;

const password = await state.getState().password();
if (!password) return { error: "Password is missing" };

const currentAccountId = state.getState().currentAccountId;
const accountId = uuidv4();
newAccount.config = encryptData(newAccount.config, password);
tmpAccounts[accountId] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { MessageAccountDecryptedDetails } from "~/types";

const decryptedDetails = async (message: MessageAccountDecryptedDetails) => {
const accounts = state.getState().accounts;
const password = state.getState().password as string;
const password = await state.getState().password();
if (!password) return { error: "Password is missing" };
const accountId = message.args.id;

if (accountId in accounts) {
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background-script/actions/accounts/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const unlock = async (message: MessageAccountUnlock) => {
}

// if everything is fine we keep the password in memory
state.setState({ password });
await state.getState().password(password);
// load the connector to make sure it is initialized for the future calls
// with this we prevent potentially multiple action calls trying to initialize the connector in parallel
// we have to be careful here: if the unlock fails (e.g. because of an error in getConnector() the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const enable = async (
message: MessageAllowanceEnable,
sender: Runtime.MessageSender
) => {
const isUnlocked = state.getState().isUnlocked();
const isUnlocked = await state.getState().isUnlocked();
const host = message.origin.host || message.args.host;
const allowance = await db.allowances
.where("host")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const mockState = {
settings: { exchange: "coindesk", currency: CURRENCIES["USD"] },
};

// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.mock("@vespaiach/axios-fetch-adapter", () => {});

state.getState = jest.fn().mockReturnValue(mockState);

jest.useFakeTimers().setSystemTime(new Date(1577836800000)); // Wed Jan 01 2020 08:00:00
Expand Down
20 changes: 14 additions & 6 deletions src/extension/background-script/actions/cache/getCurrencyRate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetchAdapter from "@vespaiach/axios-fetch-adapter";
import axios from "axios";
import dayjs from "dayjs";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
Expand Down Expand Up @@ -35,22 +36,31 @@ const getFiatBtcRate = async (currency: CURRENCIES): Promise<number> => {

if (exchange === "yadio") {
response = await axios.get(
`https://api.yadio.io/exrates/${currency.toLowerCase()}`
`https://api.yadio.io/exrates/${currency.toLowerCase()}`,
{
adapter: fetchAdapter,
}
);
const data = await response?.data;
return data.BTC / numSatsInBtc;
}

if (exchange === "coindesk") {
response = await axios.get(
`https://api.coindesk.com/v1/bpi/currentprice/${currency.toLowerCase()}.json`
`https://api.coindesk.com/v1/bpi/currentprice/${currency.toLowerCase()}.json`,
{
adapter: fetchAdapter,
}
);
const data = await response?.data;
return data.bpi[currency].rate_float / numSatsInBtc;
}

response = await axios.get(
`https://getalby.com/api/rates/${currency.toLowerCase()}.json`
`https://getalby.com/api/rates/${currency.toLowerCase()}.json`,
{
adapter: fetchAdapter,
}
);
const data = await response?.data;

Expand Down Expand Up @@ -85,9 +95,7 @@ export const getCurrencyRateWithCache = async (currency: CURRENCIES) => {
};

const getCurrencyRate = async (message: MessageCurrencyRateGet) => {
const settings = state.getState().settings;
const { currency } = settings;

const { currency } = state.getState().settings;
const rate = await getCurrencyRateWithCache(currency);

return {
Expand Down
3 changes: 2 additions & 1 deletion src/extension/background-script/actions/ln/makeInvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ const makeInvoice = async (message, sender) => {

if (message.args.amount) {
amount = parseInt(message.args.amount);

const connector = await state.getState().getConnector();

try {
const response = await connector.makeInvoice({
amount,
memo,
});

return response;
} catch (e) {
return { error: e.message };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const lnurlDetails: LNURLDetails = {
url: "https://lnurl.fiatjaf.com/lnurl-login",
};

describe("auth", () => {
// skip till this is solved:
// https://github.com/axios/axios/pull/5146
// test works if we do not use:
// https://github.com/getAlby/lightning-browser-extension/blob/refactor/manifest-v3-support/src/extension/background-script/actions/lnurl/auth.ts#L93
describe.skip("auth", () => {
test("returns success response", async () => {
state.getState = jest.fn().mockReturnValue(mockState);

Expand Down
6 changes: 5 additions & 1 deletion src/extension/background-script/actions/lnurl/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetchAdapter from "@vespaiach/axios-fetch-adapter";
import axios from "axios";
import Hex from "crypto-js/enc-hex";
import hmacSHA256 from "crypto-js/hmac-sha256";
Expand Down Expand Up @@ -87,7 +88,10 @@ export async function authFunction({

try {
const authResponse = await axios.get<AuthResponseObject>(
loginURL.toString()
loginURL.toString(),
{
adapter: fetchAdapter,
}
);

// if the service returned with a HTTP 200 we still check if the response data is OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function authOrPrompt(

// we have the check the unlock status manually. The account can still be locked
// If it is locked we must show a prompt to unlock
const isUnlocked = state.getState().isUnlocked();
const isUnlocked = await state.getState().isUnlocked();

// check if there is a publisher and lnurlAuth is enabled,
// otherwise we we prompt the user
Expand Down
16 changes: 8 additions & 8 deletions src/extension/background-script/actions/nostr/decryptOrPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const decryptOrPrompt = async (message: MessageDecryptGet) => {
);

if (hasPermission) {
const response = state
.getState()
.getNostr()
.decrypt(message.args.peer, message.args.ciphertext);
const response = (await state.getState().getNostr()).decrypt(
message.args.peer,
message.args.ciphertext
);

return { data: response };
} else {
Expand All @@ -44,10 +44,10 @@ const decryptOrPrompt = async (message: MessageDecryptGet) => {
);
}
if (promptResponse.data.confirm) {
const response = state
.getState()
.getNostr()
.decrypt(message.args.peer, message.args.ciphertext);
const response = (await state.getState().getNostr()).decrypt(
message.args.peer,
message.args.ciphertext
);

return { data: response };
} else {
Expand Down
Loading