feat: NIP-44 (v2) encrypt/decrypt for window.nostr#21
Closed
jjohare wants to merge 1 commit into
Closed
Conversation
Add window.nostr.nip44.{encrypt,decrypt} to the injected NIP-07 provider so
apps that use NIP-17 / NIP-59 gift-wrapped direct messages (e.g. the DreamLab
nostr-rust-forum) can encrypt/decrypt DMs through the Podkey extension.
Implementation mirrors the existing signing/nip04 message path exactly:
page provider (nostr-provider.js) posts a podkey-request CustomEvent ->
content script (injected.js) relays via chrome.runtime.sendMessage ->
background service worker (background.js) routes NIP44_ENCRYPT/NIP44_DECRYPT/
NIP44_GET_CONVERSATION_KEY to handlers that resolve the keypair the same way
GET_PUBLIC_KEY/SIGN_EVENT do. The raw private key never leaves the background;
only the base64 payload / plaintext / conversation key crosses to the page.
NIP-44 v2 (src/nip44.js) uses the vetted @noble stack the extension already
depends on for nip04-era crypto:
- @noble/secp256k1 : ECDH shared secret (x-coordinate)
- @noble/hashes : hkdf (extract/expand), hmac, sha256
- @noble/ciphers : chacha20 (added dependency)
conversation key = hkdf_extract(salt="nip44-v2", ikm=ECDH.x);
per-message keys = hkdf_expand(conv_key, info=nonce, 76) ->
chacha_key|chacha_nonce|hmac_key; payload = base64(0x02|nonce|ct|mac),
mac = hmac_sha256(hmac_key, nonce|ct). Padding and unpadding per spec.
Tests (test/nip44.test.js, node --test) cover symmetric conversation keys,
round-trips (unicode/long/random-nonce), background-path helpers, HMAC tamper
detection, and the official NIP-44 spec vectors (deterministic conversation
key + encrypt/decrypt). All 34 tests pass; npm run build bundles cleanly.
Co-Authored-By: jjohare <github@thedreamlab.uk>
|
Claude Code seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Collaborator
Author
|
Folded into #22 — a single CI-green, adversarially-reviewed PR that consolidates this branch with the other security fixes plus NIP-44 and residual gap-fixes (exact-host trust, NIP-98 nonce, signEvent self-verify, honest provider surface, auto-sign default OFF). Suggest reviewing/merging #22; this can be closed in its favour. |
jjohare
added a commit
that referenced
this pull request
Jun 15, 2026
Consolidated hardening of the Podkey key-holding signer: brings seven in-flight security/feature branches plus residual gap-fills, a polished CSP-clean UI, a CI build/test/lint gate, and a 133-test suite into one reviewable PR. The fix set was independently re-derived from a full audit of the extension and every item below was verified fixed *in the final tree* by an adversarial review pass (not just claimed). **Supersedes** (folded in here; safe to close in favour of this PR): #12, #14, #17, #18, #19, #20, #21. ## Security posture — before → after | Area | Before | After | |---|---|---| | Consent | Auto-approve everything (`showPermissionPrompt` hard-returned `true`) | **Explicit per-origin consent** popup that blocks on the user's decision; closing or 60s timeout denies | | Key at rest | `storage.local` (persisted plaintext) | **`storage.session` only** (in-memory, cleared on browser close); raw key never crosses to the page | | Crypto | A fake `crypto-browser.js` in the tree (`pubkey = sha256(privkey)`) | Deleted; **real `@noble` crypto** only, with `signEvent` schnorr self-verify before return | | NIP-98 | Signed-event cache → byte-identical id → replayable | **Fresh per-token 16-byte nonce**, redirect-aware `u` tag, page-side body-hash binding | | Solid-host trust | Substring match (`inrupt.net.evil.com` accepted) | **Exact-host match**, case-insensitive; lookalikes rejected | | Interception | Double fetch/XHR interception + double 401-retry | **Single interception path**, one retry | | Message channel | Page could inject arbitrary fields / origin / `privateKey` | Content script **whitelists 4 message types, forwards only safe fields, enforces the real origin** | | Provider surface | Advertised `nip04`/`getRelays` that throw / return `{}` | **Honest**: `getPublicKey`, `signEvent`, `nip44.{encrypt,decrypt}` only | | UI | Inline scripts + `innerHTML` XSS sinks | **CSP `script-src 'self'`**; all DOM via `textContent`/`createElement` | | Auto-sign | Defaulted **ON** (first-run silent trust + NIP-98 mint) | Defaults **OFF** — silent trusted-origin Solid signing and NIP-98 auto-trust are strictly opt-in | ## Notes for review - **Consent timeout:** `approve.js` actively sends `approved:false` + closes at the 60s mark (one-shot `decisionSent` guard) so the visible countdown is truthful; the background `chrome.windows` 60s timer remains a backstop. If you'd prefer the popup stay passive and let the background own the timeout, that is the single behavioural line to reconsider — everything else on the UI is restyling. - The page-context `nip98-interceptor.js` still duplicates a couple of `auth-header-utils.js` helpers because page context can't ESM-import the module; intentional, tracked separately. ## Verification - `npm run build` ✓ · `npm test` → **133 pass / 0 fail** ✓ · `npm run lint` → **0 errors** (now `no-unused-vars: error`) ✓ - New `.github/workflows/ci.yml` runs build → test → lint on every PR and push to `main`. 🤖 Generated by Claude Code
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds NIP-44 (v2) encryption to the injected NIP-07 provider:
window.nostr.nip44.encrypt(peerPubkeyHex, plaintext)→ base64 payloadwindow.nostr.nip44.decrypt(peerPubkeyHex, base64payload)→ plaintextWhy (DreamLab forum DM use case)
The DreamLab nostr-rust-forum
needs
window.nostr.nip44.{encrypt,decrypt}to decrypt NIP-44 gift-wrapped(NIP-59) direct messages for users who sign in with Podkey. Podkey currently
injects
getPublicKey,signEvent, andnip04, but notnip44, so theforum shows "Your Nostr extension doesn't support NIP-44 encryption". Adding
nip44makes DMs (and any NIP-17 / NIP-44 feature) work through the extension.How — mirrors the existing message path exactly
NIP-07 keeps the private key in the background service worker, never the page.
The new code reuses the same page → content-script → background round-trip
the provider already uses for signing:
src/nostr-provider.js(page):nip44.encrypt/decryptpost apodkey-requestCustomEvent (NIP44_ENCRYPT/NIP44_DECRYPT) — identicalshape to the existing
nip04calls.src/injected.js(content script): unchanged — already relays{ id, type, ...data }generically to the background viachrome.runtime.sendMessage.src/background.js(service worker): newNIP44_ENCRYPT/NIP44_DECRYPT/NIP44_GET_CONVERSATION_KEYcases route to handlers thatresolve the keypair through a
resolveKeypairForEncryption()helper whichgates on origin trust exactly like
GET_PUBLIC_KEY/SIGN_EVENTdo.The raw private key never crosses to the page — only the base64
payload / plaintext / conversation-key hex is returned.
NIP-44 v2 implementation
src/nip44.jsimplements the current spec (version0x02) using the vetted@noblestack the extension already depends on:@noble/secp256k1— ECDH shared secret (x-coordinate)@noble/hashes—hkdf(extract/expand),hmac,sha256@noble/ciphers—chacha20(new dependency, same@noblefamily)Tests
test/nip44.test.js(runs under the existingnode --testrunner) covers:background-path helpers, HMAC tamper detection, unknown-version rejection
deterministic encrypt/decrypt with a fixed nonce)
npm test→ all green (incl. the pre-existing suites).npm run buildbundlesthe service worker cleanly with the new module +
chacha20inlined.Notes for reviewers
nip04is left untouched (still a stub onmain); this PR is NIP-44 only,which is what NIP-17/NIP-59 DMs require.
src/background.bundle.jsis git-ignored (build artifact) and not committed.🤖 Generated by Claude Code