fiatjaf / nos2x

nostr signer extension
293 stars 64 forks source link

add ping handler #63

Open Vap0r1ze opened 3 months ago

Vap0r1ze commented 3 months ago

Adds a noop ping command that an external extension could call to detect if nos2x is installed.

Vap0r1ze commented 3 months ago

@fiatjaf I probably should explain why exactly I think this is needed:

The only way an extension can directly check if an extension is installed is by using the overly-permissive management permission & API.

Right now the only way (that I can think of) to indirectly detect the presence of this extension, is to abuse the if (NO_PERMISSIONS_REQUIRED[type]) check by sending type with a property name that has a truthy value on the default Object prototype (eg: "_proto_", "constructor", "", etc).

This is very hacky and may break if NO_PERMISSIONS_REQUIRED becomes something else like a null-prototype object (eg: export const NO_PERMISSIONS_REQUIRED = Object.create(null), import * as NO_PERMISSIONS_REQUIRED from "./constants"). This PR provides an intended way to achieve this without the worry of breaking in the future.

fiatjaf commented 3 months ago

What about checking if window.nostr exists in the browser?

fiatjaf commented 3 months ago

I think we can merge this, but if this is the way it should be added to NIP-07 first so other extensions can implement it.

Vap0r1ze commented 3 months ago

What about checking if window.nostr exists in the browser?

This wouldn't work for background scripts unless they create a content script on a non-extension page first to check for it. An extension I'm working on does this because the websocket connections live in the background script.

I think we can merge this, but if this is the way it should be added to NIP-07 first so other extensions can implement it.

Given that this is the ideal way, are you saying that I actually should make a PR for nostr-protocol/nips#1370?

One thing to note (I mentioned this in the linked issue): if a background script wants to utilize another extension for NIP-07 functionality using the Messaging API, they will need to have their own hardcoded list of extension IDs to do ping checks with. An extension could probably give the user the ability to input an extension ID to get around this limitation.