aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/contentScript/util.ts
blob: aecb7b2375674d4c68792771301fe6da002d5b84 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2024 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 { isEqual, isNil, isEmpty } from 'lodash'
import { apiCall } from '@vnuge/vnlib.browser'
import { Store, storeToRefs } from 'pinia'
import { useScriptTag, watchOnce } from "@vueuse/core"
import { useStore } from '../store'
import { PrStatus } from '../../features'

const registerWindowHandler = (store: Store, extName: string) => {

    const { selectedKey } = storeToRefs(store)
    const { nostr, permission } = store.plugins;

    const onAsyncCall = async ({ source, data, origin } : MessageEvent<any>) => {

        //clean any junk/methods with json parse/stringify
        data = JSON.parse(JSON.stringify(data))

        const requestPermission = async (cb: (...args: any) => Promise<any>) => {
            //await propmt for user to allow the request
            const allow = await permission.requestAndWaitResult({ ...data, requestType: data.type, origin })
            //send request to background
            return allow == PrStatus.Approved ? 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, extName)) {
            return
        }

        switch (data.type) {
            case 'getPublicKey':
                return requestPermission(async () => selectedKey.value?.PublicKey);
            case 'signEvent':
                return requestPermission(async () => {
                    const event = data.payload.event
                    return await nostr.signEvent({
                        ...event,
                        KeyId: selectedKey.value!.Id,
                        pubkey: selectedKey.value!.PublicKey
                    });
                })
            //Check the public key against selected key
            case 'getRelays':
                return requestPermission(async () => await nostr.getRelays())
            case 'nip04.encrypt':
                return requestPermission(async () => {
                    return await nostr.nip04Encrypt({
                        pubkey: data.payload.peer,
                        content: data.payload.plaintext,
                        //Set selected key id as our desired encryption key
                        KeyId: selectedKey.value!.Id
                    })
                })
            case 'nip04.decrypt':
                return requestPermission(async () => {
                    const result = await apiCall(async () => {
                        return await nostr.nip04Decrypt({
                            pubkey: data.payload.peer,
                            content: data.payload.ciphertext,
                            //Set selected key id as our desired decryption key
                            KeyId: selectedKey.value!.Id
                        })
                    })
                    return isNil(result) ? 'Error: Failed to decrypt message' : result
                })
        }
    }

    //Only listen for messages if injection is enabled
    window.addEventListener('message', async (args) => {

        //Wrap in apicall to display errors
        await apiCall(async () => {
            let response; 

            try{
                response = await onAsyncCall(args)
            }
            catch(e: any){
                const error = JSON.stringify(e)
                response = { error }
                //rethrow for logging
                throw e;
            }
            finally{
                // always return response message, must have the same id as the request
                window.postMessage({ ext: extName, id: args.data.id, response }, origin);
            }
        })
    });
}

export const onLoad = (extName: string, scriptUrl: string) => {
   
    const store = useStore()
    const { isTabAllowed } = storeToRefs(store)

    const injectHandler = () => {
        //inject the nostr provider script into the page
        useScriptTag(scriptUrl, undefined, { manual: false, defer: true })
        //Regsiter listener for messages from the injected script
        registerWindowHandler(store, extName)
    }

    //Make sure the origin is allowed
    if (isTabAllowed.value === false) {
        //If not allowed yet, wait for the store to update
        watchOnce(isTabAllowed, val => val ? injectHandler() : undefined);
    }
    else {
        injectHandler();
    }
}