Getting started
1. Prerequisites (the developer does these once, by hand)
Nothing, by default: call NostrBridge.init() without a clientId and users sign in with Google/Apple/X through the hosted signer's shared sign-in (Web3Auth's panel in the widget, and a small window on the signer's origin for the account's own screens; NostrBridge.login() opens the sign-in sheet), or with an extension or NIP-46 bunker. The console notes (info) that the page uses shared sign-in.
Shared sign-in, what to tell the developer:
- It works on any site, with no Web3Auth account and no registration. A user gets the same npub on every site that uses it.
- It is free while donations cover the shared project's Web3Auth bill, and may later be limited per site, become paid, or be turned off.
- Moving a live app from shared sign-in to its own
clientId(or back) gives its OAuth users new, empty npubs. If the app will need its own keys or predictable costs, start with its ownclientId. - Safari and every iPhone/iPad browser keep the widget's stored data only until the browser quits: users sign in again and get the same identities. In these browsers only, the bridge remembers the provider they used (
localStoragenostr-bridge:provider: google, apple, facebook or twitter; cleared on log out) and the widget offers "Continue with Google" as one tap, with "⋯" for the other ways. - Whoever runs the hosted signer can obtain shared-mode users' keys: everyone who can change the code served on the signer's origin (repository write access, CI and its secrets, the hosting provider, the build's dependencies). The shared project's Web3Auth allowlist lists only the signer's origin, so sites can't. If a site is added to it, the signer pauses new shared sign-ins ("Sign-in paused"; sites already signed in keep working).
- The operator can block a single site from it (warning
SHARED_BLOCKED); that site keeps extension and bunker sign-in. - A user who runs their own copy of the signer can pick "Use my own signer" under the sign-in panel (#72). The bridge then frames that signer on this site instead of
bunkerOrigin, without the page'sclientIdandglobalLogin(the user's own project signs them in), and logsnostr-bridge: using the signer this user chose for this site. Kept per site inlocalStorage(nostr-bridge:signer) until the user logs out there; a bar the bridge draws above the widget (#nostr-signer-own, while signed out) names that signer and offers "Use site's"; not onnostrOnlypages; a signer that doesn't answer within 15 s is forgotten and the page reloads. The page needs no code for it. - Users back up the whole account (master key and backup file) in the sign-in window, and restore only at
<signer>/restore/, a page they open themselves. Apps can't start either; don't build flows around them. - Decrypts, replacing the profile, follows or relays, deletions, Evolustr app keys and key export are confirmed in a popup on the signer's origin (the widget shows "Review this request" first), so no page can cover the prompt. Expect two clicks for those.
- The page using the signer must be a top-level window: inside another site's frame the signer refuses with
NESTED_FRAME(all modes).
Nostr-only sign-in (extension or NIP-46 bunker, no Google/Apple/X): NostrBridge.init({ nostrOnly: true }).
Behaviour change: before shared sign-in, init() without a clientId meant Nostr-only sign-in, and pages that load the hosted bridge get the new default by themselves. When editing an existing page that calls init() without a clientId and was meant for extension and bunker users only, add nostrOnly: true.
For Google/Apple/X sign-in with keys of the app's own Web3Auth project:
- Create a Web3Auth project (Sapphire Mainnet) at https://web3auth.io and copy its client ID.
- In the Web3Auth dashboard, under Project Settings → Domains → Allowlist URLs, add
https://signer.shardsigner.com(the signer iframe runs there) and the app's exact origin (e.g.https://myapp.com). - Open the portal at https://saintego.github.io/nostr-shard-signer/portal/, connect a Nostr key, and register the client ID together with the app's origin. The portal only accepts origins that are in the Web3Auth allowlist. To add origins later, use the portal's Update Domains tab.
The signer offers social login on an origin that is in the NIP-33 registry (step 3) or in the Web3Auth allowlist (step 2). If only one of them lists it, it still works and the console shows a NOT_IN_REGISTRY or NOT_ALLOWLISTED warning. The registry keeps working when Web3Auth's allowlist can't be read.
The portal refuses localhost, IP addresses and http origins. For local testing, add the localhost origin to the Web3Auth allowlist instead.
2. Complete example
<!doctype html>
<html>
<head>
<script src="https://signer.shardsigner.com/nostr-bridge.js"></script>
</head>
<body>
<p id="status">Loading…</p>
<button id="login" hidden>Sign in</button>
<button id="logout" hidden>Sign out</button>
<button id="publish" disabled>Publish note</button>
<script type="module">
import { SimplePool } from "https://cdn.jsdelivr.net/npm/nostr-tools@2/+esm";
const RELAYS = ["wss://relay.damus.io", "wss://nos.lol"];
const statusEl = document.getElementById("status");
const publishBtn = document.getElementById("publish");
const loginBtn = document.getElementById("login");
const logoutBtn = document.getElementById("logout");
// Called with the current state once known (restored sessions included),
// then on every sign-in / sign-out.
NostrBridge.onAuthChange(({ loggedIn, pubkey }) => {
statusEl.textContent = loggedIn ? "Signed in as " + pubkey : "Not signed in.";
publishBtn.disabled = !loggedIn;
loginBtn.hidden = loggedIn;
logoutBtn.hidden = !loggedIn;
});
window.addEventListener("message", (e) => {
if (e.origin === "" && e.data?.type === "SIGNER_ERROR")
statusEl.textContent = e.data.message + " " + e.data.hint;
});
loginBtn.addEventListener("click", () => NostrBridge.login());
logoutBtn.addEventListener("click", () => NostrBridge.logout());
await NostrBridge.init({ clientId: "YOUR_WEB3AUTH_CLIENT_ID" });
publishBtn.addEventListener("click", async () => {
const signed = await window.nostr.signEvent({
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: "Hello Nostr!",
});
const pool = new SimplePool();
await Promise.any(pool.publish(RELAYS, signed));
pool.close(RELAYS);
});
</script>
</body>
</html>In a framework (React, Vue, Svelte…)
- Either install the npm package and load the bridge from code (below), or put the
<script src=…nostr-bridge.js>tag in the HTML shell (index.html,app.html, or the root layout's head) and callNostrBridge.init(). Never import or bundlenostr-bridge.jsitself. - Call
NostrBridge.init()client-side only (e.g. a top-level effect oronMount). It is idempotent: repeated calls (StrictMode, remounts) return the first call's promise, so no module-level guard is needed. With SSR, never call it on the server. - Keep auth state in app state, updated from
NostrBridge.onAuthChange(return its unsubscribe function from the effect's cleanup). - TypeScript without npm: copy
nostr-bridge.d.tsinto the project to typeNostrBridge; its interfaces can be imported (import type { NostrBridgeConfig, NostrBridgeAuthState } from "./nostr-bridge"). It deliberately doesn't typewindow.nostr, so it coexists with other NIP-07 typings; addnostr-bridge-window.d.tstoo if nothing else declareswindow.nostr.
React:
useEffect(() => {
NostrBridge.init({ clientId: "YOUR_WEB3AUTH_CLIENT_ID" });
return NostrBridge.onAuthChange(setAuth); // { loggedIn, pubkey }
}, []);
// <button onClick={() => NostrBridge.login()}>Sign in</button>With the npm package (npm install nostr-shard-signer), no script tag is needed. loadNostrBridge(config) loads nostr-bridge.js from the signer (<bunkerOrigin>/nostr-bridge.js, so bridge and signer always match), calls init(config), and resolves with NostrBridge. Like init() it is idempotent, and it rejects on the server, so call it client-side only:
// Optional, at the top of the file: types window.nostr.
/// <reference types="nostr-shard-signer/window" />
import { loadNostrBridge } from "nostr-shard-signer";
useEffect(() => {
let off = () => {};
loadNostrBridge({ clientId: "YOUR_WEB3AUTH_CLIENT_ID" }).then((bridge) => {
off = bridge.onAuthChange(setAuth);
});
return () => off();
}, []);Without an init call
The script tag can carry the config; the bridge then calls init() itself:
<script src="https://signer.shardsigner.com/nostr-bridge.js"
data-client-id="YOUR_WEB3AUTH_CLIENT_ID" data-layout="floating"></script>Attributes map to the init() options: data-client-id, data-nostr-only, data-bunker-origin, data-layout, data-button-size, data-mount-selector, data-locale, data-theme, data-sign-in-ui, data-force-iframe and data-powered-by ("true"/"false"). Any of them starts init, so data-layout="floating" alone gives the defaults (shared sign-in), and data-nostr-only="true" alone gives Nostr-only sign-in. An explicit init() afterwards returns the same promise; NostrBridge.ready resolves when it is done.