aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/contentScript/nostr-shim.js
blob: 26b17a94dac7c5b584d2a24a3e9ab80ae07ac2eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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 <https://www.gnu.org/licenses/>.


import { runtime } from "webextension-polyfill";
import { isEqual, isNil, isEmpty } from 'lodash' 
import { sendMessage } from 'webext-bridge/content-script'
import { apiCall } from '@vnuge/vnlib.browser'
import { useManagment } from './../../bg-api/content-script'

const { getSiteConfig } = useManagment()
const nip07Enabled = () => getSiteConfig().then(p => p.autoInject);

//Setup listener for the content script to process nostr messages

const ext = '@vnuge/nvault-extension'

let _promptHandler = () => {}

export const usePrompt = (callback) => {
    //Register the callback
    _promptHandler = async (event) => {
        return new Promise((resolve, reject) => {
            callback(event).then(resolve).catch(reject)
        })
    }
    return {}
}

//Only inject the script if the site has autoInject enabled
nip07Enabled().then(enabled => {
    console.log('Nip07 enabled:', enabled)
    if (enabled) {
        // inject the script that will provide window.nostr
        let script = document.createElement('script');
        script.setAttribute('async', 'false');
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('src', runtime.getURL('src/entries/nostr-provider.js'));
        document.head.appendChild(script);

        //Only listen for messages if injection is enabled
        window.addEventListener('message', async ({ source, data, origin }) => {
            //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
            }

            // pass on to background
            var response;
            await apiCall(async () => {
                switch (data.type) {
                    case 'getPublicKey':
                    case 'signEvent':
                    //Check the public key against selected key
                    case 'getRelays':
                    case 'nip04.encrypt':
                    case 'nip04.decrypt':
                        //await propmt for user to allow the request
                        const allow = await _promptHandler({ ...data, origin })
                        //send request to background
                        response = allow ? await sendMessage(data.type, { ...data.payload, origin }) : { error: 'User denied permission' }
                        break;
                    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);
        });
    }
})