nips/07.md

59 lines
2.0 KiB
Markdown
Raw Normal View History

2022-05-06 19:54:45 -04:00
NIP-07
======
`window.nostr` capability for web browsers
------------------------------------------
2023-11-15 19:42:51 -05:00
`draft` `optional`
2022-05-06 19:54:45 -04:00
The `window.nostr` object may be made available by web browsers or extensions and websites or web-apps may make use of it after checking its availability.
That object must define the following methods:
```
async window.nostr.getPublicKey(): string // returns a public key as hex
2023-10-20 16:56:29 -04:00
async window.nostr.signEvent(event: { created_at: number, kind: number, tags: string[][], content: string }): Event // takes an event object, adds `id`, `pubkey` and `sig` and returns it
```
Aside from these two basic above, the following functions can also be implemented optionally:
```
async window.nostr.getRelays(): { [url: string]: {read: boolean, write: boolean} } // returns a basic map of relay urls to relay policies
async window.nostr.nip04.encrypt(pubkey, plaintext): string // returns ciphertext and iv as specified in nip-04 (deprecated)
async window.nostr.nip04.decrypt(pubkey, ciphertext): string // takes ciphertext and iv as specified in nip-04 (deprecated)
async window.nostr.nip44.encrypt(pubkey, plaintext): string // returns ciphertext as specified in nip-44
async window.nostr.nip44.decrypt(pubkey, ciphertext): string // takes ciphertext as specified in nip-44
2022-05-06 19:54:45 -04:00
```
2024-04-14 06:05:49 -04:00
## Command Queue
In order to avoid race conditions the object SHOULD have a `cmd` property.
This object is defined as follows
```ts
window.nostr.cmd: (() => void)[]
```
Browsers or extensions SHOULD invoke all callbacks queued once it has finished loading.
After that it should shadow the `push` method on `cmd`
to immediately invoke subsequent queues
This allows consumers to queue up calls to the NIP-07 providers even before
it is present.
### Examples
Queue a command
```js
window.nostr = window.nostr || { cmd: [] };
window.nostr.cmd.push(() => {
const pk = window.nostr.getPublicKey();
// do something with pk
});
```
2022-11-27 10:03:46 -05:00
### Implementation
2022-05-06 19:54:45 -04:00
See https://github.com/aljazceru/awesome-nostr#nip-07-browser-extensions.