From e272adcc3f32e31fe7668551453b8e34bc823c3e Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 22 Nov 2023 15:07:08 -0500 Subject: feature and internal api polish --- extension/src/entries/contentScript/nostr-shim.js | 123 ------------------ .../primary/components/PromptPopup.vue | 10 +- .../src/entries/contentScript/primary/main.js | 10 +- extension/src/entries/contentScript/util.ts | 138 +++++++++++++++++++++ extension/src/entries/nostr-provider.js | 6 +- .../src/entries/options/components/Identities.vue | 9 +- .../entries/options/components/SiteSettings.vue | 5 +- extension/src/entries/store/allowedOrigins.ts | 10 +- extension/src/entries/store/features.ts | 4 +- extension/src/entries/store/identity.ts | 4 +- extension/src/features/account-api.ts | 48 +++---- extension/src/features/auth-api.ts | 11 +- extension/src/features/identity-api.ts | 29 ++--- extension/src/features/index.ts | 3 +- extension/src/features/nip07allow-api.ts | 59 +++------ extension/src/features/nostr-api.ts | 6 +- extension/src/features/server-api/index.ts | 13 +- extension/src/features/settings.ts | 96 +++++--------- extension/src/features/tagfilter-api.ts | 50 ++++---- extension/src/features/types.ts | 70 +---------- extension/src/features/util.ts | 83 +++++++++++++ 21 files changed, 383 insertions(+), 404 deletions(-) delete mode 100644 extension/src/entries/contentScript/nostr-shim.js create mode 100644 extension/src/entries/contentScript/util.ts create mode 100644 extension/src/features/util.ts diff --git a/extension/src/entries/contentScript/nostr-shim.js b/extension/src/entries/contentScript/nostr-shim.js deleted file mode 100644 index 418b9c1..0000000 --- a/extension/src/entries/contentScript/nostr-shim.js +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright (C) 2023 Vaughn Nugent -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -import { runtime } from "webextension-polyfill" -import { isEqual, isNil, isEmpty } from 'lodash' -import { apiCall } from '@vnuge/vnlib.browser' -import { useScriptTag, watchOnce } from "@vueuse/core" -import { useStore } from '../store' -import { storeToRefs } from 'pinia' - -const _promptHandler = (() => { - let _handler = undefined; - return{ - invoke: (event) => _handler(event), - set: (handler) => _handler = handler - } -})() - -export const usePrompt = (callback) => _promptHandler.set(callback); - - -export const onLoad = async () =>{ - - const store = useStore() - const { nostr } = store.plugins - const { isTabAllowed, selectedKey } = storeToRefs(store) - - const injectHandler = () => { - - //Setup listener for the content script to process nostr messages - const ext = '@vnuge/nvault-extension' - - const scriptUrl = runtime.getURL('src/entries/nostr-provider.js') - - //setup script tag - useScriptTag(scriptUrl, undefined, { manual: false, defer: true }) - - //Only listen for messages if injection is enabled - window.addEventListener('message', async ({ source, data, origin }) => { - - const invokePrompt = async (cb) => { - //await propmt for user to allow the request - const allow = await _promptHandler.invoke({ ...data, origin }) - //send request to background - return response = allow ? await cb() : { error: 'User denied permission' } - } - - //Confirm the message format is correct - if (!isEqual(source, window) || isEmpty(data) || isNil(data.type)) { - return - } - //Confirm extension is for us - if (!isEqual(data.ext, ext)) { - return - } - - //clean any junk/methods with json parse/stringify - data = JSON.parse(JSON.stringify(data)) - - // pass on to background - var response; - await apiCall(async () => { - switch (data.type) { - case 'getPublicKey': - return invokePrompt(async () => selectedKey.value.PublicKey) - case 'signEvent': - return invokePrompt(async () => { - const event = data.payload.event - - //Set key id to selected key - event.KeyId = selectedKey.value.Id - event.pubkey = selectedKey.value.PublicKey; - - return await nostr.signEvent(event); - }) - //Check the public key against selected key - case 'getRelays': - return invokePrompt(async () => await nostr.getRelays()) - case 'nip04.encrypt': - return invokePrompt(async () => await nostr.nip04Encrypt({ - pubkey: data.payload.peer, - content: data.payload.plaintext, - //Set selected key id as our desired decryption key - KeyId: selectedKey.value.Id - })) - case 'nip04.decrypt': - return invokePrompt(async () => await nostr.nip04Decrypt({ - pubkey: data.payload.peer, - content: data.payload.ciphertext, - //Set selected key id as our desired decryption key - KeyId: selectedKey.value.Id - })) - default: - throw new Error('Unknown nostr message type') - } - }) - // return response message, must have the same id as the request - window.postMessage({ ext, id: data.id, response }, origin); - }); - } - - //Make sure the origin is allowed - if (store.isTabAllowed === false){ - //If not allowed yet, wait for the store to update - watchOnce(isTabAllowed, val => val ? injectHandler() : undefined); - } - else{ - injectHandler(); - } - -} \ No newline at end of file diff --git a/extension/src/entries/contentScript/primary/components/PromptPopup.vue b/extension/src/entries/contentScript/primary/components/PromptPopup.vue index b8b7cab..381f7b3 100644 --- a/extension/src/entries/contentScript/primary/components/PromptPopup.vue +++ b/extension/src/entries/contentScript/primary/components/PromptPopup.vue @@ -75,7 +75,7 @@