From 2f199e1cdc6ceae72e33c309a3c4ac8ca2c263a8 Mon Sep 17 00:00:00 2001 From: artur Date: Thu, 31 Oct 2024 14:56:45 +0100 Subject: [PATCH] Add code examples, minor fixes --- 146.md | 101 +++++++++++++++++++++++++++++---- 512.md | 173 --------------------------------------------------------- 2 files changed, 91 insertions(+), 183 deletions(-) delete mode 100644 512.md diff --git a/146.md b/146.md index 1f83576..a98e703 100644 --- a/146.md +++ b/146.md @@ -12,7 +12,7 @@ Due to privacy-related restrictions, local storage of iframes is [partitioned](h Advantage: minimal latency, no relay involved, no reliance on unreliable webpush, fully cross-platform. User private key is as safe in iframe's local storage as in top-level storage due to browsers' cross-origin restrictions. -Disadvantage: potentially more frequent "confirmations", higher surface for web-based hacks. +Disadvantage: potentially more frequent "confirmations", bigger surface for web-based hacks. ## Terms @@ -26,15 +26,15 @@ Disadvantage: potentially more frequent "confirmations", higher surface for web- ## Overview -When nip46 connection is established, client embeds invisible `worker iframe` and exchanges nip46 messages with it using `postMessage`. +When nip46 connection is established, `client` embeds invisible `worker iframe` and exchanges nip46 messages with it using `postMessage`. -To overcome partitioned storage, when client initiates nip46 connection and receives `auth_url`, it shows the `starter iframe` with "Continue" button. After user clicks, starter iframe launches auth_url popup to access top-level signer. When user confirms, top-level signer sends the user private key back to starter iframe using postMessage. User private key can then be used by `worker iframe`. +To overcome partitioned storage, when client initiates nip46 connection and receives `auth_url`, it shows the `starter iframe` with "Continue" button. After user clicks, starter iframe launches auth_url popup to access `top-level signer`. When user confirms, top-level signer sends the user private key back to starter iframe using postMessage. User private key can then be used by worker iframe. -To overcome ephemeral partitioned storage ([Brave](https://brave.com/privacy-updates/7-ephemeral-storage/), Webkit), when worker iframe receives a request but has no target keys, it pauses the call and sends an error code. Client shows visible `rebinder iframe` with "Continue" button. When user clicks, rebinder iframe launches top-level signer in a popup, which checks if client pubkey has access to the user pubkey and if so sends user private key back to rebinder iframe, without user having to confirm anything. +To overcome _ephemeral_ partitioned storage ([Brave](https://brave.com/privacy-updates/7-ephemeral-storage/), Webkit), when worker iframe receives a request but has no target keys, it pauses the call and sends an error code. Client shows `rebinder iframe` with "Continue" button. When user clicks, rebinder iframe launches top-level signer in a popup, which checks if client pubkey has access to the user pubkey and if so sends user private key back to rebinder iframe, without user having to confirm anything. ## Worker iframe -Signer may signal support for this NIP by adding `nip46.iframe_url` field in their nostr.json file. If client detects a signer that supports iframes, after the connection has already been established, the client MAY create an invisible `worker` iframe with `src` set to `nip46.iframe_url`. +Signer may signal support for this NIP by adding `nip46.iframe_url` field in their `/.well-known/nostr.json?name=_` file. If client detects a signer that supports iframes, after the connection has already been established, the client MAY create an invisible `worker iframe` with `src` set to `nip46.iframe_url`. When worker iframe has started and is ready to process requests it must notify the client by calling `window.parent.postMessage("workerReady", "*")`. In turn, the client must subscribe to worker's messages with `window.addEventListener("message", handler)` before creating an iframe, and must wait for the message event with `data` set to `workerReady` and `origin` set to origin of `nip46.iframe_url` before sending requests. @@ -44,26 +44,107 @@ Worker iframe processes the request and sends back the reply event by using `req ## Starter iframe -Due to local storage partitioning, an iframe must import the needed key from the top-level storage of the signer. To do that, iframe must obtain a reference to a top-level signer and exchange messages. This fits well with the need to open top-level signer popup with `auth_url` of the nip-46 connection flow. However, this time the popup opener must be the signer iframe, and it must follow a [user gesture](https://developer.mozilla.org/en-US/docs/Web/API/UserActivation) in iframe. +Due to local storage partitioning, an iframe must import the needed key from the top-level storage of the signer. To do that, `iframe signer` must obtain a reference to a `top-level signer` and exchange messages. This fits well with the need to open top-level signer popup with `auth_url` of the nip-46 connection flow. However, this time the popup opener must be the `signer iframe`, and it must follow a [user gesture](https://developer.mozilla.org/en-US/docs/Web/API/UserActivation) in iframe. To achieve that, a `starter iframe` MAY be embedded by the client when `auth_url` is received while the client is connecting. Starter iframe MUST be served when `nip46.iframe_url` has `?auth_url=` parameter, and MUST display a button suggesting the users to `Continue`. Recommended button dimentions SHOULD be up to `180px` width and `80px` height. When user clicks on `Continue` button in starter iframe, it MUST open the popup with `auth_url` and MUST accept the user's private key if user confirms in the popup. Details are implementation-defined, see recommendations below. -When starter iframe has finished importing the user private key from the popup, it MUST notify the app by calling `window.parent.postMessage("starterDone", "*")`. When client receives the `starterDone` from iframe_url's origin it MAY destroy the starter iframe and MAY create the worker iframe to start processing requests. +When starter iframe has finished importing the user private key from the popup, it MUST notify the app by calling `window.parent.postMessage("starterDone", "*")`. When client receives the `starterDone` from iframe_url's origin it MAY destroy the starter iframe and MAY create the `worker iframe` to start processing requests. ## Rebinder iframe -When worker iframe receives a nip46 request targeting user pubkey that it doesn't have (local storage deleted) it MAY pause the call and then MUST reply with a string `errorNoKey:` to notify the client. +When `worker iframe` receives a nip46 request targeting user pubkey that it doesn't have (local storage deleted) it MAY pause the call and then MUST reply with a string `errorNoKey:` to notify the `client`. -If client receives `errorNoKey:` reply matching one of pending requests, it MAY display a `rebinder iframe`. Rebinder iframe MUST be served when `nip46.iframe_url` has `&rebind=&pubkey=` parameters, and MUST display a button similar to starter iframe suggesting the users to `Continue`. +If client receives `errorNoKey:` reply matching one of pending requests, it MAY display a `rebinder iframe`. Rebinder iframe MUST be served when `nip46.iframe_url` has `&rebind=&pubkey=` parameters, and MUST display a button similar to starter iframe suggesting the users to `Continue`. -When user clicks on `Continue` button in rebinder iframe, it MUST open a top-level signer popup. The popup MUST check that provided `local_pubkey` has permissions to access `remote_user_pubkey` and then MUST pass the user private key to rebinder iframe. Details are implementation-defined, see appendix for recommendations. +When user clicks on `Continue` button in rebinder iframe, it MUST open a `top-level signer` popup. The popup MUST check that provided `client_pubkey` has permissions to access `remote_signer_pubkey` and then MUST pass the user private key to rebinder iframe. Details are implementation-defined, see appendix for recommendations. After rebinder iframe has finished importing the user private key, it MUST notify the client by calling `window.parent.postMessage("rebinderDone", "*")`. When client receives the `rebinderDone` from iframe_url's origin it MAY destroy the rebinder iframe. When worker iframe that has paused the request notices that user private key was imported by rebinder into the local store, it MUST restart the paused call and deliver the reply to the client. +## Client Pseudocode + +### Worker iframe usage + +``` +const iframeOrigin = new URL(iframeUrl).origin; + +// helper +const getReply = async () => { + return new Promise(ok => { + const handler = (e) => { + if (e.origin !== iframeOrigin) return; + window.removeEventListener("message", handler) + ok(e.data); + } + window.addEventListener("message", handler) + }) +}; + +// create invisible iframe +const iframe = createIframe(iframeUrl, 'style="display: none"') + +// wait for workerReady message +while (await getReply() !== "workerReady"); + +// send nip46 request event +const nip46Req = await createReq("sign_event", {...}); +iframe.contentWindow.postMessage(nip46Req, iframeOrigin); + +// get reply +const nip46Reply = await getReply(); + +// process reply +... +``` + +### Starter iframe usage + +``` +const authUrl = await connectGetAuthUrl(...); + +// display starter iframe +const iframe = createIframe(`${iframeUrl}?auth_url=${authUrl}`, 'style="width: 180px; height: 80px') + +// wait till it's done +while (await getReply() !== "starterDone"); + +// can delete starter +deleteIframe(iframe); + +// create worker to process requests + +... +``` + +### Rebinder iframe usage + +``` +// worker iframe returns error +let workerReply = await getReply() + +if (workerReply.startsWith("errorNoKey:")) { + + // display starter iframe + const iframe = createIframe(`${iframeUrl}?rebind=${clientPubkey}&pubkey=${remoteSignerPubkey}`, 'style="width: 180px; height: 80px') + + // wait till it's done + while (await getReply() !== "rebinderDone"); + + // can delete rebinder + deleteIframe(iframe); + + // re-fetch reply + workerReply = await getReply(); +} + +// process worker reply + +... +``` + ## Recommendations diff --git a/512.md b/512.md deleted file mode 100644 index bcb0208..0000000 --- a/512.md +++ /dev/null @@ -1,173 +0,0 @@ -Nostr Sites -=========== - -This NIP describes a way to render a selection of Nostr events on a website. - -A web page that is part of the Nostr Site must include `nostr:site` meta tag: -``` - -``` - -The `site` event, referenced by `naddr` from `nostr:site` meta tag, is a parameterized replaceable event of kind `30512`. The `content` field MAY contain a rich description of the site in markdown syntax (NIP-23), all tags are optional (except `d`). - -Tags: -``` -{ - "tags":[ - // absolute url of the nostr site root, may include /path/, must not include query string, must end with / - ["r", "https://site.com/"], - ["name", ""], - ["title", ""], - ["summary", ""], - ["image", ""], - - // contributors, if omitted - event's author is the single site contributor - ["p", ""], - - // filters for fetching published events - - // - single-letter tag - ["include", "", ""], - // list of included event kinds - ["kind", "30023"], - // override contributors' outbox relays to fetch content - ["relay", ""], - - // event id and package hash of the extensions (themes, plugin) - ["x", "", "", "", ""], - - // renderer engine, preferably reverse-domain notation, should match theme engine and plugin engine, i.e. `pro.npub.v1` - ["z", ""], - - // meta tags for website rendering, seo, social, navigation - ["icon", ""], - ["logo", "
"], - ["color", "<#hex - accent color, PWA theme_color>"], - ["lang", ""], - ["meta_title", ""], - ["meta_description", ""], - ["og_title", ""], - ["og_description", ""], - ["og_image", ""], - ["twitter_image", ""], - ["twitter_title", ""], - ["twitter_description", ""], - - // primary navigation, one tag per link - ["nav", "", "