@formstr/signer 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -133,6 +133,8 @@ const myPlugin: AndroidSignerPlugin = {
133
133
 
134
134
  The interface signatures intentionally mirror `nostr-signer-capacitor-plugin`'s exported `NostrSignerPlugin` so the real wrapper is directly assignable. If you write a custom plugin, the package's test suite includes a compile-time conformance guard (`tests/helpers/mockAndroidPlugin.ts`) you can model your own check on — wire it up in your CI and you'll catch any drift the moment the upstream wrapper changes shape.
135
135
 
136
+ **Identifier shape.** The `npub` field returned by `getPublicKey` is permissive: the package accepts either a bech32 `npub1…` string (the NIP-55 spec shape) or a 32-byte hex pubkey (what current Amber builds actually return). Whichever you hand back, the package normalizes internally — `StoredAccount.npub` is always bech32 and `StoredAccount.pubkey` is always lowercase hex. Anything else surfaces as a debuggable error including a preview of what was received.
137
+
136
138
  ## NIP-46 app identity (required for nostrconnect)
137
139
 
138
140
  The nostrconnect URI you generate must include a `name` (and ideally `url`/`image`) so remote signer apps can show the user *which app* is asking to pair. Without it:
package/dist/index.cjs CHANGED
@@ -388,36 +388,45 @@ function describeIdentifier(value) {
388
388
  const suffix = value.length > 12 ? "\u2026" : "";
389
389
  return `"${prefix}${suffix}" (length=${value.length})`;
390
390
  }
391
+ var HEX_PUBKEY_RE = /^[0-9a-f]{64}$/i;
391
392
  async function loginWithAndroidSigner(plugin, packageName) {
392
393
  if (packageName) {
393
394
  await plugin.setPackageName(packageName);
394
395
  }
395
- const { npub, package: pluginPackage } = await plugin.getPublicKey(packageName);
396
+ const { npub: rawIdentifier, package: pluginPackage } = await plugin.getPublicKey(packageName);
396
397
  const resolvedPackage = pluginPackage || packageName;
397
398
  if (!resolvedPackage) {
398
399
  throw new Error(
399
400
  "@formstr/signer: android signer did not return a package name and none was supplied"
400
401
  );
401
402
  }
403
+ const { pubkey, npub } = normalizeAndroidIdentifier(rawIdentifier);
404
+ return {
405
+ signer: new AndroidSigner(plugin, resolvedPackage, npub, pubkey),
406
+ pubkey,
407
+ npub,
408
+ packageName: resolvedPackage
409
+ };
410
+ }
411
+ function normalizeAndroidIdentifier(rawIdentifier) {
412
+ if (typeof rawIdentifier === "string" && HEX_PUBKEY_RE.test(rawIdentifier)) {
413
+ const pubkey = rawIdentifier.toLowerCase();
414
+ return { pubkey, npub: import_nostr_tools4.nip19.npubEncode(pubkey) };
415
+ }
402
416
  let decoded;
403
417
  try {
404
- decoded = import_nostr_tools4.nip19.decode(npub);
418
+ decoded = import_nostr_tools4.nip19.decode(rawIdentifier);
405
419
  } catch (e) {
406
420
  throw new Error(
407
- `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(npub)}): ${e.message}`
421
+ `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(rawIdentifier)}): ${e.message}`
408
422
  );
409
423
  }
410
424
  if (decoded.type !== "npub") {
411
425
  throw new Error(
412
- `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(npub)})`
426
+ `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(rawIdentifier)})`
413
427
  );
414
428
  }
415
- return {
416
- signer: new AndroidSigner(plugin, resolvedPackage, npub, decoded.data),
417
- pubkey: decoded.data,
418
- npub,
419
- packageName: resolvedPackage
420
- };
429
+ return { pubkey: decoded.data, npub: rawIdentifier };
421
430
  }
422
431
 
423
432
  // src/core/signer.ts
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/core/signer.ts","../src/core/storage.ts","../src/core/localSigner.ts","../src/nip49.ts","../src/nip07.ts","../src/nip46.ts","../src/nip55.ts"],"sourcesContent":["export { Signer, createSigner } from './core/signer.js';\nexport { LocalSigner } from './core/localSigner.js';\nexport { localStorageAdapter } from './core/storage.js';\nexport type { StorageAdapter } from './core/storage.js';\nexport type {\n SignerConfig,\n ActiveSigner,\n StoredAccount,\n LoginMethod,\n SignerEvent,\n NostrConnectOptions,\n BunkerLoginOptions,\n RelayMismatchInfo,\n RelayMismatchHandler,\n UnlockOptions,\n} from './core/types.js';\nexport {\n encryptSecretKey,\n decryptNcryptsec,\n generateAccount,\n type GeneratedAccount,\n} from './nip49.js';\nexport { ExtensionSigner, getWindowNostr, type WindowNostr } from './nip07.js';\nexport {\n AndroidSigner,\n loginWithAndroidSigner,\n type AndroidSignerPlugin,\n type AndroidSignerAppInfo,\n type AndroidLoginOptions,\n type AndroidLoginResult,\n} from './nip55.js';\nexport {\n BunkerSigner,\n connectWithBunkerUri,\n initiateNostrConnect,\n bytesToHex,\n hexToBytes,\n type BunkerPointer,\n type BunkerLoginOptions as BunkerConnectOptions,\n type BunkerConnectResult,\n type NostrConnectInitOptions,\n type NostrConnectInitiation,\n} from './nip46.js';\n","import { getPublicKey, nip19 } from 'nostr-tools';\nimport { BunkerSigner as ToolsBunkerSigner } from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type {\n ActiveSigner,\n BunkerLoginOptions,\n NostrConnectOptions,\n SignerConfig,\n SignerEvent,\n StoredAccount,\n UnlockOptions,\n} from './types.js';\nimport { localStorageAdapter, type StorageAdapter } from './storage.js';\nimport { LocalSigner } from './localSigner.js';\nimport { decryptNcryptsec, generateAccount } from '../nip49.js';\nimport { ExtensionSigner } from '../nip07.js';\nimport {\n BunkerSigner,\n bytesToHex,\n connectWithBunkerUri,\n hexToBytes,\n initiateNostrConnect,\n} from '../nip46.js';\nimport {\n AndroidSigner,\n loginWithAndroidSigner as connectWithAndroidSigner,\n type AndroidLoginOptions,\n type AndroidSignerAppInfo,\n type AndroidSignerPlugin,\n} from '../nip55.js';\n\nconst ACCOUNTS_KEY = 'accounts';\nconst ACTIVE_KEY = 'active-pubkey';\n\n/**\n * Multi-account Nostr signer with persistence.\n *\n * **Hydration.** The constructor reads previously-saved accounts from\n * the configured storage adapter. Every hydrated account starts\n * **locked**: present in `listAccounts()` and (if it was the active one\n * before) reachable via `getActiveAccount()`, but `getActiveSigner()`\n * returns `null` until the user re-authenticates. The matching\n * `loginWith*` method unlocks the active account.\n *\n * **Locked vs unlocked.** Use `getActiveAccount()` to render UI (\"logged\n * in as @alice\") and `getActiveSigner()` to decide whether the user can\n * actually sign. The pattern is \"show the account always, gate signing\n * on the signer.\"\n *\n * **Events.** Subscribe via `onChange()` to re-render when an account\n * is added, switched, or removed. See {@link SignerEvent}.\n */\nexport class Signer {\n readonly #storage: StorageAdapter;\n readonly #defaultAndroidPlugin: AndroidSignerPlugin | undefined;\n readonly #appMetadata: { name?: string; url?: string; image?: string };\n #accounts: StoredAccount[] = [];\n #activePubkey: string | null = null;\n #activeSigner: ActiveSigner | null = null;\n #listeners = new Set<(event: SignerEvent) => void>();\n\n constructor(config: SignerConfig = {}) {\n this.#storage = config.storage ?? localStorageAdapter(config.storageKeyPrefix);\n this.#defaultAndroidPlugin = config.androidSignerPlugin;\n this.#appMetadata = {\n name: config.appName,\n url: config.appUrl,\n image: config.appImage,\n };\n this.#hydrate();\n }\n\n #hydrate(): void {\n try {\n const raw = this.#storage.get(ACCOUNTS_KEY);\n if (raw) {\n const parsed = JSON.parse(raw);\n if (Array.isArray(parsed)) this.#accounts = parsed as StoredAccount[];\n }\n this.#activePubkey = this.#storage.get(ACTIVE_KEY);\n } catch {\n this.#accounts = [];\n this.#activePubkey = null;\n }\n }\n\n #persistAccounts(): void {\n this.#storage.set(ACCOUNTS_KEY, JSON.stringify(this.#accounts));\n }\n\n #persistActive(): void {\n if (this.#activePubkey) this.#storage.set(ACTIVE_KEY, this.#activePubkey);\n else this.#storage.remove(ACTIVE_KEY);\n }\n\n #upsertAccount(account: StoredAccount): void {\n const idx = this.#accounts.findIndex(a => a.pubkey === account.pubkey);\n if (idx >= 0) this.#accounts[idx] = account;\n else this.#accounts.push(account);\n this.#persistAccounts();\n }\n\n #setActive(account: StoredAccount, signer: ActiveSigner): void {\n const wasDifferent = this.#activePubkey !== null && this.#activePubkey !== account.pubkey;\n this.#activePubkey = account.pubkey;\n this.#activeSigner = signer;\n this.#persistActive();\n this.#emit({ type: wasDifferent ? 'switch' : 'login', account });\n }\n\n #emit(event: SignerEvent): void {\n for (const cb of this.#listeners) {\n try {\n cb(event);\n } catch {\n // listener errors are swallowed so one bad listener can't break others\n }\n }\n }\n\n /**\n * Generate a brand-new nsec, encrypt it with `passphrase` (NIP-49),\n * persist the resulting `ncryptsec` account, and activate it. Returns\n * the new account's `npub` and `ncryptsec` — the caller must surface\n * the `ncryptsec` to the user **immediately** since it is the only way\n * back into the account on a fresh device.\n *\n * @throws if `passphrase` is empty.\n */\n async createAccount(passphrase: string): Promise<{ npub: string; ncryptsec: string }> {\n if (!passphrase) throw new Error('createAccount: passphrase required');\n const { secretKey, pubkey, npub, ncryptsec } = generateAccount(passphrase);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return { npub, ncryptsec };\n }\n\n /**\n * Decrypt an ncryptsec with the user's passphrase, persist the account\n * (overwriting any previous entry for the same pubkey), and activate it.\n *\n * @throws if either argument is empty, or if the passphrase doesn't\n * decrypt the ncryptsec.\n */\n async loginWithNcryptsec(ncryptsec: string, passphrase: string): Promise<StoredAccount> {\n if (!ncryptsec) throw new Error('loginWithNcryptsec: ncryptsec required');\n if (!passphrase) throw new Error('loginWithNcryptsec: passphrase required');\n const secretKey = decryptNcryptsec(ncryptsec, passphrase);\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return account;\n }\n\n /**\n * Connect via the NIP-07 browser extension exposed at `window.nostr`.\n * The extension prompts the user for permission on first use.\n *\n * @throws if no extension is installed or the user denies the request.\n */\n async loginWithExtension(): Promise<StoredAccount> {\n const extension = new ExtensionSigner();\n const pubkey = await extension.getPublicKey();\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'extension' };\n this.#upsertAccount(account);\n this.#setActive(account, extension);\n return account;\n }\n\n /**\n * Connect to a NIP-46 remote signer via a `bunker://` URI. Relays are\n * read from the URI itself — no hardcoded fallbacks. Pass a `pool`\n * to reuse an existing relay connection; pass `clientSecretKey` to\n * resume a previous session (the hex from `StoredAccount.nip46`).\n *\n * @throws if the URI is malformed, no relay is reachable, or the\n * remote signer rejects pairing within the implementation's timeout.\n */\n async loginWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n ): Promise<StoredAccount> {\n const result = await connectWithBunkerUri(uri, options);\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Initiate a NIP-46 `nostrconnect://` pairing. Generates a client\n * keypair, publishes a connect request to the supplied `relays`, and\n * waits for a remote signer to pair. Call `options.onUri(uri)` to\n * render the URI as a QR code; the returned promise resolves once\n * pairing completes. Cancel by aborting `options.signal`.\n *\n * @throws if `relays` is empty, the user aborts, the pairing times\n * out, or no signer responds.\n */\n async loginWithNostrConnect(options: NostrConnectOptions): Promise<StoredAccount> {\n if (options.relays.length === 0) {\n throw new Error('loginWithNostrConnect: at least one relay required');\n }\n // Merge per-call metadata over SignerConfig defaults (appName/url/image).\n // `name` is required — Amber (and likely other NIP-55 signer apps) gate\n // the consent UI on having a recognizable client identity in the URI.\n // Without one they will receive the request but never surface\n // approve/deny buttons, leaving the pairing silently stuck.\n const metadata = {\n name: options.metadata?.name ?? this.#appMetadata.name,\n url: options.metadata?.url ?? this.#appMetadata.url,\n image: options.metadata?.image ?? this.#appMetadata.image,\n };\n if (!metadata.name) {\n throw new Error(\n '@formstr/signer: loginWithNostrConnect requires an app name. Set `appName` in createSigner() or pass `metadata.name` to loginWithNostrConnect().',\n );\n }\n const init = initiateNostrConnect({\n relays: options.relays,\n metadata,\n perms: options.perms,\n pool: options.pool,\n onAuth: options.onAuth,\n signal: options.signal,\n timeoutMs: options.timeoutMs,\n onRelayMismatch: options.onRelayMismatch,\n });\n options.onUri(init.uri);\n const result = await init.complete;\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri: init.uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Enumerate NIP-55 signer apps installed on the device, via the\n * configured Android plugin (or `plugin` if supplied). Useful for\n * rendering a \"pick your signer\" list — the built-in UI does this\n * automatically when the Android tab is selected.\n *\n * Only meaningful inside a Capacitor Android shell. On web/iOS the\n * configured plugin is typically absent and this throws.\n *\n * @throws if no plugin is configured and none is passed in.\n */\n async listAndroidSignerApps(\n plugin?: AndroidSignerPlugin,\n ): Promise<AndroidSignerAppInfo[]> {\n const p = plugin ?? this.#defaultAndroidPlugin;\n if (!p) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to listAndroidSignerApps)',\n );\n }\n const { apps } = await p.getInstalledSignerApps();\n return apps;\n }\n\n /**\n * Sign in via a NIP-55 Android external signer (Amber, etc). If\n * `options.packageName` is given, that specific signer app is invoked;\n * otherwise the plugin picks a default (typically the only installed\n * signer, or an OS chooser). Pass `options.plugin` to override the\n * configured default for this call.\n *\n * @throws if no plugin is configured, the signer app cannot be\n * resolved to a package name, or the user denies the request.\n */\n async loginWithAndroidSigner(options: AndroidLoginOptions = {}): Promise<StoredAccount> {\n const plugin = options.plugin ?? this.#defaultAndroidPlugin;\n if (!plugin) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to loginWithAndroidSigner)',\n );\n }\n const result = await connectWithAndroidSigner(plugin, options.packageName);\n const account: StoredAccount = {\n npub: result.npub,\n pubkey: result.pubkey,\n method: 'android',\n androidPackageName: result.packageName,\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /** Snapshot of every persisted account, in insertion order. */\n listAccounts(): StoredAccount[] {\n return [...this.#accounts];\n }\n\n /**\n * The currently selected account, or `null` if none. Present even when\n * the account is locked (no active signer yet). Use this to render\n * \"logged in as @alice\" — pair with {@link getActiveSigner} to decide\n * whether signing is actually available.\n */\n getActiveAccount(): StoredAccount | null {\n if (!this.#activePubkey) return null;\n return this.#accounts.find(a => a.pubkey === this.#activePubkey) ?? null;\n }\n\n /**\n * The unlocked signer for the active account, or `null` if locked.\n * After a fresh page load this is `null` for every account type\n * (passphrase / extension grant / signer-app handshake all need to\n * be redone). Calling the matching `loginWith*` method unlocks it.\n */\n getActiveSigner(): ActiveSigner | null {\n return this.#activeSigner;\n }\n\n /**\n * Silently unlock the active account from persisted state — no user\n * prompt, no fresh pairing. The package already keeps everything it\n * needs to reconstruct the runtime signer on disk; this method is the\n * way to actually use that on cold start instead of re-running each\n * method's first-time login flow.\n *\n * Behavior by method:\n *\n * - `extension`: constructs an {@link ExtensionSigner}, which just\n * proxies to `window.nostr`. No setup roundtrip — individual\n * operations may still prompt depending on the extension's own\n * permission state, but unlock itself does not.\n *\n * - `nip46`: reuses the stored `clientSecretKey` to construct a\n * {@link BunkerSigner} against the stored bunker pubkey + relays\n * via `BunkerSigner.fromBunker`. Deliberately skips the `connect`\n * request — the remote signer (Amber etc.) approved this client\n * pubkey on first pairing and re-sending `connect` is what surfaces\n * a fresh approval prompt every cold start. Requires `options.pool`\n * so the BunkerSigner has somewhere to listen for responses.\n * The cached user pubkey is fed into the wrapper so a follow-up\n * `getPublicKey()` is a memory read, not a relay request.\n *\n * - `android`: constructs an {@link AndroidSigner} directly from the\n * stored `androidPackageName` + `pubkey` + `npub`. Skips the\n * `getPublicKey` content-provider roundtrip that\n * {@link loginWithAndroidSigner} performs and that — on Amber —\n * surfaces as a permission prompt every cold start.\n *\n * - `ncryptsec`: returns `null`. There is no silent path — the user's\n * passphrase isn't (and shouldn't be) persisted. The caller must\n * drive the passphrase prompt and call {@link loginWithNcryptsec}.\n *\n * Returns `null` (without emitting any event or mutating state) when\n * there is no active account, when the account is missing fields\n * required to unlock, when `nip46` is the method but no `pool` was\n * supplied, or when `android` is the method but no plugin is\n * configured. On success emits the same `login` / `switch` event the\n * corresponding `loginWith*` would.\n */\n async unlock(options: UnlockOptions = {}): Promise<ActiveSigner | null> {\n const account = this.getActiveAccount();\n if (!account) return null;\n\n switch (account.method) {\n case 'extension': {\n const signer = new ExtensionSigner();\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'nip46': {\n if (!account.nip46) return null;\n if (!options.pool) return null;\n const { remoteSignerPubkey, relays, clientSecretKey } = account.nip46;\n if (!remoteSignerPubkey || !relays.length || !clientSecretKey) {\n return null;\n }\n const tools = ToolsBunkerSigner.fromBunker(\n hexToBytes(clientSecretKey),\n { pubkey: remoteSignerPubkey, relays, secret: null },\n { pool: options.pool },\n );\n // No tools.connect() — the bunker already approved this client\n // on first pairing; re-sending `connect` is what triggers the\n // cold-start approval prompt we are trying to avoid.\n const signer = new BunkerSigner(tools, account.pubkey);\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'android': {\n if (!account.androidPackageName) return null;\n if (!this.#defaultAndroidPlugin) return null;\n const signer = new AndroidSigner(\n this.#defaultAndroidPlugin,\n account.androidPackageName,\n account.npub,\n account.pubkey,\n );\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'ncryptsec':\n // No silent path — passphrase is not (and must not be) persisted.\n return null;\n }\n }\n\n /**\n * Make `pubkey` the active account. Clears the in-memory signer —\n * the new account starts **locked** even if it was previously\n * unlocked in this session.\n *\n * @throws if `pubkey` does not match any persisted account.\n */\n async switchAccount(pubkey: string): Promise<void> {\n const account = this.#accounts.find(a => a.pubkey === pubkey);\n if (!account) throw new Error(`switchAccount: no account for pubkey ${pubkey}`);\n this.#activePubkey = pubkey;\n this.#activeSigner = null;\n this.#persistActive();\n this.#emit({ type: 'switch', account });\n }\n\n /**\n * Remove an account from storage. `pubkey` defaults to the active\n * account. If the active account is removed, the in-memory signer is\n * cleared. No-op if there is nothing to remove.\n */\n async logout(pubkey?: string): Promise<void> {\n const target = pubkey ?? this.#activePubkey;\n if (!target) return;\n this.#accounts = this.#accounts.filter(a => a.pubkey !== target);\n this.#persistAccounts();\n if (this.#activePubkey === target) {\n this.#activePubkey = null;\n this.#activeSigner = null;\n this.#persistActive();\n }\n this.#emit({ type: 'logout', pubkey: target });\n }\n\n /**\n * Subscribe to account-state changes. Returns an unsubscribe function.\n * Listener errors are swallowed so one bad listener can't break others.\n * See {@link SignerEvent} for the variants.\n */\n onChange(cb: (event: SignerEvent) => void): () => void {\n this.#listeners.add(cb);\n return () => {\n this.#listeners.delete(cb);\n };\n }\n}\n\n/** Convenience wrapper around `new Signer(config)`. */\nexport function createSigner(config: SignerConfig = {}): Signer {\n return new Signer(config);\n}\n","export interface StorageAdapter {\n get(key: string): string | null;\n set(key: string, value: string): void;\n remove(key: string): void;\n}\n\nconst DEFAULT_PREFIX = '@formstr/signer:';\n\nexport function localStorageAdapter(prefix: string = DEFAULT_PREFIX): StorageAdapter {\n const ls = (): Storage | null => {\n try {\n return typeof globalThis !== 'undefined' && globalThis.localStorage\n ? globalThis.localStorage\n : null;\n } catch {\n return null;\n }\n };\n return {\n get(key) {\n try {\n return ls()?.getItem(prefix + key) ?? null;\n } catch {\n return null;\n }\n },\n set(key, value) {\n try {\n ls()?.setItem(prefix + key, value);\n } catch {\n // swallow quota / privacy-mode errors\n }\n },\n remove(key) {\n try {\n ls()?.removeItem(prefix + key);\n } catch {\n // swallow\n }\n },\n };\n}\n","import {\n finalizeEvent,\n getPublicKey,\n nip04,\n nip44,\n type Event as NostrEvent,\n type EventTemplate,\n} from 'nostr-tools';\nimport type { ActiveSigner } from './types.js';\n\n/**\n * ActiveSigner backed by a raw secret key held in memory.\n * The secret key never leaves this object — there is no getter for it.\n */\nexport class LocalSigner implements ActiveSigner {\n readonly #secretKey: Uint8Array;\n\n constructor(secretKey: Uint8Array) {\n this.#secretKey = secretKey;\n }\n\n async getPublicKey(): Promise<string> {\n return getPublicKey(this.#secretKey);\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return finalizeEvent(event, this.#secretKey);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return nip04.encrypt(this.#secretKey, peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return nip04.decrypt(this.#secretKey, peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.encrypt(plaintext, key);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.decrypt(ciphertext, key);\n }\n}\n","import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';\nimport { encrypt as nip49Encrypt, decrypt as nip49Decrypt } from 'nostr-tools/nip49';\n\nexport function encryptSecretKey(secretKey: Uint8Array, passphrase: string): string {\n return nip49Encrypt(secretKey, passphrase);\n}\n\nexport function decryptNcryptsec(ncryptsec: string, passphrase: string): Uint8Array {\n return nip49Decrypt(ncryptsec, passphrase);\n}\n\nexport interface GeneratedAccount {\n secretKey: Uint8Array;\n pubkey: string;\n npub: string;\n ncryptsec: string;\n}\n\nexport function generateAccount(passphrase: string): GeneratedAccount {\n const secretKey = generateSecretKey();\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const ncryptsec = nip49Encrypt(secretKey, passphrase);\n return { secretKey, pubkey, npub, ncryptsec };\n}\n","import type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\nexport interface WindowNostr {\n getPublicKey(): Promise<string>;\n signEvent(event: EventTemplate): Promise<NostrEvent>;\n getRelays?(): Promise<Record<string, { read: boolean; write: boolean }>>;\n nip04?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n nip44?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n}\n\nexport function getWindowNostr(): WindowNostr {\n const nostr = (globalThis as { nostr?: WindowNostr }).nostr;\n if (!nostr) {\n throw new Error(\n '@formstr/signer: NIP-07 extension not found (globalThis.nostr is undefined)',\n );\n }\n return nostr;\n}\n\nexport class ExtensionSigner implements ActiveSigner {\n async getPublicKey(): Promise<string> {\n return getWindowNostr().getPublicKey();\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return getWindowNostr().signEvent(event);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.encrypt(peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.decrypt(peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.encrypt(peerPubkey, plaintext);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.decrypt(peerPubkey, ciphertext);\n }\n}\n","import { generateSecretKey, getPublicKey } from 'nostr-tools';\nimport {\n BunkerSigner as ToolsBunkerSigner,\n createNostrConnectURI,\n parseBunkerInput,\n type BunkerPointer,\n} from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner, RelayMismatchHandler } from './core/types.js';\n\nexport type { BunkerPointer };\n\n/**\n * Thin wrapper around nostr-tools' BunkerSigner that exposes only the\n * ActiveSigner surface. We keep this layer so callers depend on a stable\n * interface even if we ever swap the underlying implementation.\n *\n * Optionally accepts a `cachedUserPubkey`. When supplied, {@link getPublicKey}\n * returns it without a bunker roundtrip. The user's signer pubkey is fixed\n * for a given paired account, so caching it after the initial `connect` —\n * or feeding it back in from persisted storage on unlock — avoids both a\n * network hop and a potential approval prompt on every cold start. Without\n * a cached value we fall back to asking the bunker, matching the prior\n * behavior.\n */\nexport class BunkerSigner implements ActiveSigner {\n readonly #delegate: ToolsBunkerSigner;\n readonly #cachedUserPubkey: string | null;\n\n constructor(delegate: ToolsBunkerSigner, cachedUserPubkey?: string) {\n this.#delegate = delegate;\n this.#cachedUserPubkey = cachedUserPubkey ?? null;\n }\n\n getPublicKey(): Promise<string> {\n if (this.#cachedUserPubkey !== null) {\n return Promise.resolve(this.#cachedUserPubkey);\n }\n return this.#delegate.getPublicKey();\n }\n signEvent(event: EventTemplate): Promise<NostrEvent> {\n return this.#delegate.signEvent(event);\n }\n nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip04Encrypt(peerPubkey, plaintext);\n }\n nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip04Decrypt(peerPubkey, ciphertext);\n }\n nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip44Encrypt(peerPubkey, plaintext);\n }\n nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip44Decrypt(peerPubkey, ciphertext);\n }\n async close(): Promise<void> {\n return this.#delegate.close();\n }\n}\n\nexport interface BunkerLoginOptions {\n /** Custom pool, e.g. for tests. Defaults to a new SimplePool inside nostr-tools. */\n pool?: AbstractSimplePool;\n /** Called when the remote signer needs the user to visit an auth URL. */\n onAuth?: (url: string) => void;\n /** Optional client session keypair (hex bytes). Auto-generated if omitted. */\n clientSecretKey?: Uint8Array;\n /** Notified when the bunker's preferred relays differ from the URI's. */\n onRelayMismatch?: RelayMismatchHandler;\n /**\n * NIP-46 permissions to request as the 3rd `connect` param\n * (e.g. `['sign_event:1', 'nip44_encrypt']`). When omitted, the\n * connect request carries no perms — bunker UIs may then skip the\n * approval prompt entirely, leaving the user with nothing to tap.\n */\n perms?: string[];\n}\n\nasync function fetchBunkerRelays(tools: ToolsBunkerSigner): Promise<string[] | null> {\n try {\n const resp = await tools.sendRequest('get_relays', []);\n const parsed = JSON.parse(resp) as unknown;\n if (Array.isArray(parsed)) {\n return parsed.filter((r): r is string => typeof r === 'string');\n }\n if (typeof parsed === 'object' && parsed !== null) {\n return Object.keys(parsed as Record<string, unknown>);\n }\n return null;\n } catch {\n return null;\n }\n}\n\nfunction relayListsMatch(a: string[], b: string[]): boolean {\n if (a.length !== b.length) return false;\n const sa = [...a].sort();\n const sb = [...b].sort();\n for (let i = 0; i < sa.length; i++) if (sa[i] !== sb[i]) return false;\n return true;\n}\n\nasync function resolveRelayChoice(\n tools: ToolsBunkerSigner,\n userRelays: string[],\n onRelayMismatch: RelayMismatchHandler | undefined,\n): Promise<string[]> {\n if (!onRelayMismatch) return userRelays;\n const bunkerRelays = await fetchBunkerRelays(tools);\n if (!bunkerRelays || relayListsMatch(userRelays, bunkerRelays)) return userRelays;\n const accept = await onRelayMismatch({ userRelays, bunkerRelays });\n return accept ? bunkerRelays : userRelays;\n}\n\nexport interface BunkerConnectResult {\n signer: BunkerSigner;\n pubkey: string;\n pointer: BunkerPointer;\n clientSecretKey: Uint8Array;\n}\n\n/**\n * Connect to a remote signer via a bunker:// URI (or a NIP-05 identifier).\n * Relays come from the URI — there is no fallback default list.\n */\nexport async function connectWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n): Promise<BunkerConnectResult> {\n const pointer = await parseBunkerInput(uri);\n if (!pointer) {\n throw new Error('@formstr/signer: invalid bunker URI');\n }\n if (!pointer.relays?.length) {\n throw new Error('@formstr/signer: bunker URI must include at least one relay');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const tools = ToolsBunkerSigner.fromBunker(clientSecretKey, pointer, {\n pool: options.pool,\n onauth: options.onAuth,\n });\n // nostr-tools' BunkerSigner.connect() hardcodes only [pubkey, secret],\n // dropping the optional 3rd `perms` arg defined by NIP-46. Without it\n // bunker UIs (Amber, etc.) have no permissions to authorize and may\n // skip the approval prompt entirely. We send the request directly.\n await tools.sendRequest('connect', [\n pointer.pubkey,\n pointer.secret ?? '',\n (options.perms ?? []).join(','),\n ]);\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n pointer.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...pointer, relays: resolvedRelays },\n clientSecretKey,\n };\n}\n\nexport interface NostrConnectInitOptions {\n /** User-supplied relays. The whole point of the strict-relay rule. */\n relays: string[];\n metadata?: { name?: string; url?: string; image?: string };\n /** Permissions to request (NIP-46 perms list, e.g. [\"sign_event:1\",\"nip44_encrypt\"]). */\n perms?: string[];\n pool?: AbstractSimplePool;\n onAuth?: (url: string) => void;\n /** Override the auto-generated client session keypair. */\n clientSecretKey?: Uint8Array;\n /** Override the auto-generated URI secret. */\n secret?: string;\n /** Abort the pairing wait. */\n signal?: AbortSignal;\n /** Max wait for pairing in ms (default 5 minutes). */\n timeoutMs?: number;\n /** Notified when the bunker's preferred relays differ from the user's. */\n onRelayMismatch?: RelayMismatchHandler;\n}\n\nexport interface NostrConnectInitiation {\n uri: string;\n clientPubkey: string;\n complete: Promise<BunkerConnectResult>;\n}\n\n/**\n * Generate a nostrconnect:// URI and wait for the remote signer to pair.\n * The caller displays the URI (typically as a QR code), and the returned\n * `complete` promise resolves once the signer connects back.\n */\nexport function initiateNostrConnect(options: NostrConnectInitOptions): NostrConnectInitiation {\n if (options.relays.length === 0) {\n throw new Error('@formstr/signer: at least one relay is required for nostrconnect');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const clientPubkey = getPublicKey(clientSecretKey);\n const secret = options.secret ?? Math.random().toString(36).slice(2);\n const uri = createNostrConnectURI({\n clientPubkey,\n relays: options.relays,\n secret,\n perms: options.perms,\n name: options.metadata?.name,\n url: options.metadata?.url,\n image: options.metadata?.image,\n });\n const maxWaitOrAbort: number | AbortSignal =\n options.signal ?? options.timeoutMs ?? 300_000;\n // skipSwitchRelays:true — keep the caller-supplied relays authoritative,\n // never silently swap to whatever the bunker prefers.\n const complete = ToolsBunkerSigner.fromURI(\n clientSecretKey,\n uri,\n { pool: options.pool, onauth: options.onAuth, skipSwitchRelays: true },\n maxWaitOrAbort,\n ).then(async (tools) => {\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n options.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...tools.bp, relays: resolvedRelays },\n clientSecretKey,\n };\n });\n return { uri, clientPubkey, complete };\n}\n\nconst hexAlphabet = '0123456789abcdef';\n\nexport function bytesToHex(bytes: Uint8Array): string {\n let s = '';\n for (const b of bytes) s += hexAlphabet[b >> 4] + hexAlphabet[b & 0xf];\n return s;\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n if (hex.length % 2 !== 0) throw new Error('hexToBytes: odd-length hex string');\n const out = new Uint8Array(hex.length / 2);\n for (let i = 0; i < out.length; i++) {\n out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n }\n return out;\n}\n","import { getEventHash, nip19, type Event as NostrEvent, type EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\n/**\n * Subset of `nostr-signer-capacitor-plugin`'s exported `NostrSignerPlugin`\n * that we depend on. Signatures intentionally mirror that library\n * (positional args, per-call `packageName`) so the real plugin is\n * structurally assignable here — and any mock written against this\n * interface is a faithful stand-in. The conformance is enforced by a\n * compile-time guard in `tests/helpers/mockAndroidPlugin.ts`.\n */\nexport interface AndroidSignerAppInfo {\n name: string;\n packageName: string;\n iconUrl?: string;\n}\n\nexport interface AndroidSignerPlugin {\n setPackageName(packageName: string): Promise<void>;\n getInstalledSignerApps(): Promise<{ apps: AndroidSignerAppInfo[] }>;\n getPublicKey(\n packageName?: string,\n permissions?: string,\n ): Promise<{ npub: string; package: string }>;\n signEvent(\n packageName: string,\n eventJson: string,\n id: string,\n npub: string,\n ): Promise<{ signature: string; id: string; event: string }>;\n nip04Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip04Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n}\n\nexport interface AndroidLoginOptions {\n /** The Android package name of the external signer app (e.g. com.greenart7c3.nostrsigner). */\n packageName?: string;\n /** Override the plugin for this call. Falls back to SignerConfig.androidSignerPlugin. */\n plugin?: AndroidSignerPlugin;\n}\n\nexport class AndroidSigner implements ActiveSigner {\n readonly #plugin: AndroidSignerPlugin;\n readonly #packageName: string;\n readonly #npub: string;\n readonly #pubkey: string;\n\n constructor(\n plugin: AndroidSignerPlugin,\n packageName: string,\n npub: string,\n pubkey: string,\n ) {\n this.#plugin = plugin;\n this.#packageName = packageName;\n this.#npub = npub;\n this.#pubkey = pubkey;\n }\n\n async getPublicKey(): Promise<string> {\n return this.#pubkey;\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n const unsigned = { ...event, pubkey: this.#pubkey };\n const eventId = getEventHash(unsigned);\n const result = await this.#plugin.signEvent(\n this.#packageName,\n JSON.stringify(unsigned),\n eventId,\n this.#npub,\n );\n return JSON.parse(result.event) as NostrEvent;\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n}\n\nexport interface AndroidLoginResult {\n signer: AndroidSigner;\n pubkey: string;\n npub: string;\n packageName: string;\n}\n\n/**\n * Render a debuggable summary of what the Android signer plugin returned\n * where an npub was expected. Includes type and length, plus a truncated\n * prefix that preserves the bech32 HRP (so callers can tell `nsec1…` /\n * `nprofile1…` / a raw hex pubkey apart) without leaking the full secret\n * material that an erroneous `nsec` response would carry.\n */\nfunction describeIdentifier(value: unknown): string {\n if (value === null) return 'null';\n if (value === undefined) return 'undefined';\n if (typeof value !== 'string') {\n return `<${typeof value}>`;\n }\n if (value.length === 0) return 'empty string';\n const prefix = value.slice(0, 12);\n const suffix = value.length > 12 ? '…' : '';\n return `\"${prefix}${suffix}\" (length=${value.length})`;\n}\n\nexport async function loginWithAndroidSigner(\n plugin: AndroidSignerPlugin,\n packageName?: string,\n): Promise<AndroidLoginResult> {\n if (packageName) {\n await plugin.setPackageName(packageName);\n }\n const { npub, package: pluginPackage } = await plugin.getPublicKey(packageName);\n const resolvedPackage = pluginPackage || packageName;\n if (!resolvedPackage) {\n throw new Error(\n '@formstr/signer: android signer did not return a package name and none was supplied',\n );\n }\n // Wrap nip19.decode so a bech32 failure (\"Data must be at least 6\n // characters long\", \"Invalid checksum\", ...) surfaces what the plugin\n // actually returned. Without this, callers see an opaque bech32 crash\n // and can't tell whether Amber sent back an empty string, a hex\n // pubkey, an nsec, or something else entirely.\n let decoded: ReturnType<typeof nip19.decode>;\n try {\n decoded = nip19.decode(npub);\n } catch (e) {\n // nostr-tools' nip19 decoder always throws Error instances on bech32\n // failures (\"Data must be at least 6 characters long\", \"Invalid\n // checksum\", \"Unknown prefix\", ...). Pass the message straight through.\n throw new Error(\n `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(npub)}): ${(e as Error).message}`,\n );\n }\n if (decoded.type !== 'npub') {\n throw new Error(\n `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(npub)})`,\n );\n }\n return {\n signer: new AndroidSigner(plugin, resolvedPackage, npub, decoded.data),\n pubkey: decoded.data,\n npub,\n packageName: resolvedPackage,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,sBAAoC;AACpC,IAAAC,gBAAkD;;;ACKlD,IAAM,iBAAiB;AAEhB,SAAS,oBAAoB,SAAiB,gBAAgC;AACnF,QAAM,KAAK,MAAsB;AAC/B,QAAI;AACF,aAAO,OAAO,eAAe,eAAe,WAAW,eACnD,WAAW,eACX;AA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AAe;AACpB,SAAK,MAAM,EAAE,MAAM,UAAU,QAAQ,CAAC;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OAAO,QAAgC;AAC3C,UAAM,SAAS,UAAU,KAAK;AAC9B,QAAI,CAAC,OAAQ;AACb,SAAK,YAAY,KAAK,UAAU,OAAO,OAAK,EAAE,WAAW,MAAM;AAC/D,SAAK,iBAAiB;AACtB,QAAI,KAAK,kBAAkB,QAAQ;AACjC,WAAK,gBAAgB;AACrB,WAAK,gBAAgB;AACrB,WAAK,eAAe;AAAA,IACtB;AACA,SAAK,MAAM,EAAE,MAAM,UAAU,QAAQ,OAAO,CAAC;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,SAAS,IAA8C;AACrD,SAAK,WAAW,IAAI,EAAE;AACtB,WAAO,MAAM;AACX,WAAK,WAAW,OAAO,EAAE;AAAA,IAC3B;AAAA,EACF;AACF;AAGO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,OAAO,MAAM;AAC1B;","names":["import_nostr_tools","import_nip46","import_nostr_tools","nip49Encrypt","nip49Decrypt","import_nostr_tools","ToolsBunkerSigner","import_nostr_tools","ToolsBunkerSigner"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/core/signer.ts","../src/core/storage.ts","../src/core/localSigner.ts","../src/nip49.ts","../src/nip07.ts","../src/nip46.ts","../src/nip55.ts"],"sourcesContent":["export { Signer, createSigner } from './core/signer.js';\nexport { LocalSigner } from './core/localSigner.js';\nexport { localStorageAdapter } from './core/storage.js';\nexport type { StorageAdapter } from './core/storage.js';\nexport type {\n SignerConfig,\n ActiveSigner,\n StoredAccount,\n LoginMethod,\n SignerEvent,\n NostrConnectOptions,\n BunkerLoginOptions,\n RelayMismatchInfo,\n RelayMismatchHandler,\n UnlockOptions,\n} from './core/types.js';\nexport {\n encryptSecretKey,\n decryptNcryptsec,\n generateAccount,\n type GeneratedAccount,\n} from './nip49.js';\nexport { ExtensionSigner, getWindowNostr, type WindowNostr } from './nip07.js';\nexport {\n AndroidSigner,\n loginWithAndroidSigner,\n type AndroidSignerPlugin,\n type AndroidSignerAppInfo,\n type AndroidLoginOptions,\n type AndroidLoginResult,\n} from './nip55.js';\nexport {\n BunkerSigner,\n connectWithBunkerUri,\n initiateNostrConnect,\n bytesToHex,\n hexToBytes,\n type BunkerPointer,\n type BunkerLoginOptions as BunkerConnectOptions,\n type BunkerConnectResult,\n type NostrConnectInitOptions,\n type NostrConnectInitiation,\n} from './nip46.js';\n","import { getPublicKey, nip19 } from 'nostr-tools';\nimport { BunkerSigner as ToolsBunkerSigner } from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type {\n ActiveSigner,\n BunkerLoginOptions,\n NostrConnectOptions,\n SignerConfig,\n SignerEvent,\n StoredAccount,\n UnlockOptions,\n} from './types.js';\nimport { localStorageAdapter, type StorageAdapter } from './storage.js';\nimport { LocalSigner } from './localSigner.js';\nimport { decryptNcryptsec, generateAccount } from '../nip49.js';\nimport { ExtensionSigner } from '../nip07.js';\nimport {\n BunkerSigner,\n bytesToHex,\n connectWithBunkerUri,\n hexToBytes,\n initiateNostrConnect,\n} from '../nip46.js';\nimport {\n AndroidSigner,\n loginWithAndroidSigner as connectWithAndroidSigner,\n type AndroidLoginOptions,\n type AndroidSignerAppInfo,\n type AndroidSignerPlugin,\n} from '../nip55.js';\n\nconst ACCOUNTS_KEY = 'accounts';\nconst ACTIVE_KEY = 'active-pubkey';\n\n/**\n * Multi-account Nostr signer with persistence.\n *\n * **Hydration.** The constructor reads previously-saved accounts from\n * the configured storage adapter. Every hydrated account starts\n * **locked**: present in `listAccounts()` and (if it was the active one\n * before) reachable via `getActiveAccount()`, but `getActiveSigner()`\n * returns `null` until the user re-authenticates. The matching\n * `loginWith*` method unlocks the active account.\n *\n * **Locked vs unlocked.** Use `getActiveAccount()` to render UI (\"logged\n * in as @alice\") and `getActiveSigner()` to decide whether the user can\n * actually sign. The pattern is \"show the account always, gate signing\n * on the signer.\"\n *\n * **Events.** Subscribe via `onChange()` to re-render when an account\n * is added, switched, or removed. See {@link SignerEvent}.\n */\nexport class Signer {\n readonly #storage: StorageAdapter;\n readonly #defaultAndroidPlugin: AndroidSignerPlugin | undefined;\n readonly #appMetadata: { name?: string; url?: string; image?: string };\n #accounts: StoredAccount[] = [];\n #activePubkey: string | null = null;\n #activeSigner: ActiveSigner | null = null;\n #listeners = new Set<(event: SignerEvent) => void>();\n\n constructor(config: SignerConfig = {}) {\n this.#storage = config.storage ?? localStorageAdapter(config.storageKeyPrefix);\n this.#defaultAndroidPlugin = config.androidSignerPlugin;\n this.#appMetadata = {\n name: config.appName,\n url: config.appUrl,\n image: config.appImage,\n };\n this.#hydrate();\n }\n\n #hydrate(): void {\n try {\n const raw = this.#storage.get(ACCOUNTS_KEY);\n if (raw) {\n const parsed = JSON.parse(raw);\n if (Array.isArray(parsed)) this.#accounts = parsed as StoredAccount[];\n }\n this.#activePubkey = this.#storage.get(ACTIVE_KEY);\n } catch {\n this.#accounts = [];\n this.#activePubkey = null;\n }\n }\n\n #persistAccounts(): void {\n this.#storage.set(ACCOUNTS_KEY, JSON.stringify(this.#accounts));\n }\n\n #persistActive(): void {\n if (this.#activePubkey) this.#storage.set(ACTIVE_KEY, this.#activePubkey);\n else this.#storage.remove(ACTIVE_KEY);\n }\n\n #upsertAccount(account: StoredAccount): void {\n const idx = this.#accounts.findIndex(a => a.pubkey === account.pubkey);\n if (idx >= 0) this.#accounts[idx] = account;\n else this.#accounts.push(account);\n this.#persistAccounts();\n }\n\n #setActive(account: StoredAccount, signer: ActiveSigner): void {\n const wasDifferent = this.#activePubkey !== null && this.#activePubkey !== account.pubkey;\n this.#activePubkey = account.pubkey;\n this.#activeSigner = signer;\n this.#persistActive();\n this.#emit({ type: wasDifferent ? 'switch' : 'login', account });\n }\n\n #emit(event: SignerEvent): void {\n for (const cb of this.#listeners) {\n try {\n cb(event);\n } catch {\n // listener errors are swallowed so one bad listener can't break others\n }\n }\n }\n\n /**\n * Generate a brand-new nsec, encrypt it with `passphrase` (NIP-49),\n * persist the resulting `ncryptsec` account, and activate it. Returns\n * the new account's `npub` and `ncryptsec` — the caller must surface\n * the `ncryptsec` to the user **immediately** since it is the only way\n * back into the account on a fresh device.\n *\n * @throws if `passphrase` is empty.\n */\n async createAccount(passphrase: string): Promise<{ npub: string; ncryptsec: string }> {\n if (!passphrase) throw new Error('createAccount: passphrase required');\n const { secretKey, pubkey, npub, ncryptsec } = generateAccount(passphrase);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return { npub, ncryptsec };\n }\n\n /**\n * Decrypt an ncryptsec with the user's passphrase, persist the account\n * (overwriting any previous entry for the same pubkey), and activate it.\n *\n * @throws if either argument is empty, or if the passphrase doesn't\n * decrypt the ncryptsec.\n */\n async loginWithNcryptsec(ncryptsec: string, passphrase: string): Promise<StoredAccount> {\n if (!ncryptsec) throw new Error('loginWithNcryptsec: ncryptsec required');\n if (!passphrase) throw new Error('loginWithNcryptsec: passphrase required');\n const secretKey = decryptNcryptsec(ncryptsec, passphrase);\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return account;\n }\n\n /**\n * Connect via the NIP-07 browser extension exposed at `window.nostr`.\n * The extension prompts the user for permission on first use.\n *\n * @throws if no extension is installed or the user denies the request.\n */\n async loginWithExtension(): Promise<StoredAccount> {\n const extension = new ExtensionSigner();\n const pubkey = await extension.getPublicKey();\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'extension' };\n this.#upsertAccount(account);\n this.#setActive(account, extension);\n return account;\n }\n\n /**\n * Connect to a NIP-46 remote signer via a `bunker://` URI. Relays are\n * read from the URI itself — no hardcoded fallbacks. Pass a `pool`\n * to reuse an existing relay connection; pass `clientSecretKey` to\n * resume a previous session (the hex from `StoredAccount.nip46`).\n *\n * @throws if the URI is malformed, no relay is reachable, or the\n * remote signer rejects pairing within the implementation's timeout.\n */\n async loginWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n ): Promise<StoredAccount> {\n const result = await connectWithBunkerUri(uri, options);\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Initiate a NIP-46 `nostrconnect://` pairing. Generates a client\n * keypair, publishes a connect request to the supplied `relays`, and\n * waits for a remote signer to pair. Call `options.onUri(uri)` to\n * render the URI as a QR code; the returned promise resolves once\n * pairing completes. Cancel by aborting `options.signal`.\n *\n * @throws if `relays` is empty, the user aborts, the pairing times\n * out, or no signer responds.\n */\n async loginWithNostrConnect(options: NostrConnectOptions): Promise<StoredAccount> {\n if (options.relays.length === 0) {\n throw new Error('loginWithNostrConnect: at least one relay required');\n }\n // Merge per-call metadata over SignerConfig defaults (appName/url/image).\n // `name` is required — Amber (and likely other NIP-55 signer apps) gate\n // the consent UI on having a recognizable client identity in the URI.\n // Without one they will receive the request but never surface\n // approve/deny buttons, leaving the pairing silently stuck.\n const metadata = {\n name: options.metadata?.name ?? this.#appMetadata.name,\n url: options.metadata?.url ?? this.#appMetadata.url,\n image: options.metadata?.image ?? this.#appMetadata.image,\n };\n if (!metadata.name) {\n throw new Error(\n '@formstr/signer: loginWithNostrConnect requires an app name. Set `appName` in createSigner() or pass `metadata.name` to loginWithNostrConnect().',\n );\n }\n const init = initiateNostrConnect({\n relays: options.relays,\n metadata,\n perms: options.perms,\n pool: options.pool,\n onAuth: options.onAuth,\n signal: options.signal,\n timeoutMs: options.timeoutMs,\n onRelayMismatch: options.onRelayMismatch,\n });\n options.onUri(init.uri);\n const result = await init.complete;\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri: init.uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Enumerate NIP-55 signer apps installed on the device, via the\n * configured Android plugin (or `plugin` if supplied). Useful for\n * rendering a \"pick your signer\" list — the built-in UI does this\n * automatically when the Android tab is selected.\n *\n * Only meaningful inside a Capacitor Android shell. On web/iOS the\n * configured plugin is typically absent and this throws.\n *\n * @throws if no plugin is configured and none is passed in.\n */\n async listAndroidSignerApps(\n plugin?: AndroidSignerPlugin,\n ): Promise<AndroidSignerAppInfo[]> {\n const p = plugin ?? this.#defaultAndroidPlugin;\n if (!p) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to listAndroidSignerApps)',\n );\n }\n const { apps } = await p.getInstalledSignerApps();\n return apps;\n }\n\n /**\n * Sign in via a NIP-55 Android external signer (Amber, etc). If\n * `options.packageName` is given, that specific signer app is invoked;\n * otherwise the plugin picks a default (typically the only installed\n * signer, or an OS chooser). Pass `options.plugin` to override the\n * configured default for this call.\n *\n * @throws if no plugin is configured, the signer app cannot be\n * resolved to a package name, or the user denies the request.\n */\n async loginWithAndroidSigner(options: AndroidLoginOptions = {}): Promise<StoredAccount> {\n const plugin = options.plugin ?? this.#defaultAndroidPlugin;\n if (!plugin) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to loginWithAndroidSigner)',\n );\n }\n const result = await connectWithAndroidSigner(plugin, options.packageName);\n const account: StoredAccount = {\n npub: result.npub,\n pubkey: result.pubkey,\n method: 'android',\n androidPackageName: result.packageName,\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /** Snapshot of every persisted account, in insertion order. */\n listAccounts(): StoredAccount[] {\n return [...this.#accounts];\n }\n\n /**\n * The currently selected account, or `null` if none. Present even when\n * the account is locked (no active signer yet). Use this to render\n * \"logged in as @alice\" — pair with {@link getActiveSigner} to decide\n * whether signing is actually available.\n */\n getActiveAccount(): StoredAccount | null {\n if (!this.#activePubkey) return null;\n return this.#accounts.find(a => a.pubkey === this.#activePubkey) ?? null;\n }\n\n /**\n * The unlocked signer for the active account, or `null` if locked.\n * After a fresh page load this is `null` for every account type\n * (passphrase / extension grant / signer-app handshake all need to\n * be redone). Calling the matching `loginWith*` method unlocks it.\n */\n getActiveSigner(): ActiveSigner | null {\n return this.#activeSigner;\n }\n\n /**\n * Silently unlock the active account from persisted state — no user\n * prompt, no fresh pairing. The package already keeps everything it\n * needs to reconstruct the runtime signer on disk; this method is the\n * way to actually use that on cold start instead of re-running each\n * method's first-time login flow.\n *\n * Behavior by method:\n *\n * - `extension`: constructs an {@link ExtensionSigner}, which just\n * proxies to `window.nostr`. No setup roundtrip — individual\n * operations may still prompt depending on the extension's own\n * permission state, but unlock itself does not.\n *\n * - `nip46`: reuses the stored `clientSecretKey` to construct a\n * {@link BunkerSigner} against the stored bunker pubkey + relays\n * via `BunkerSigner.fromBunker`. Deliberately skips the `connect`\n * request — the remote signer (Amber etc.) approved this client\n * pubkey on first pairing and re-sending `connect` is what surfaces\n * a fresh approval prompt every cold start. Requires `options.pool`\n * so the BunkerSigner has somewhere to listen for responses.\n * The cached user pubkey is fed into the wrapper so a follow-up\n * `getPublicKey()` is a memory read, not a relay request.\n *\n * - `android`: constructs an {@link AndroidSigner} directly from the\n * stored `androidPackageName` + `pubkey` + `npub`. Skips the\n * `getPublicKey` content-provider roundtrip that\n * {@link loginWithAndroidSigner} performs and that — on Amber —\n * surfaces as a permission prompt every cold start.\n *\n * - `ncryptsec`: returns `null`. There is no silent path — the user's\n * passphrase isn't (and shouldn't be) persisted. The caller must\n * drive the passphrase prompt and call {@link loginWithNcryptsec}.\n *\n * Returns `null` (without emitting any event or mutating state) when\n * there is no active account, when the account is missing fields\n * required to unlock, when `nip46` is the method but no `pool` was\n * supplied, or when `android` is the method but no plugin is\n * configured. On success emits the same `login` / `switch` event the\n * corresponding `loginWith*` would.\n */\n async unlock(options: UnlockOptions = {}): Promise<ActiveSigner | null> {\n const account = this.getActiveAccount();\n if (!account) return null;\n\n switch (account.method) {\n case 'extension': {\n const signer = new ExtensionSigner();\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'nip46': {\n if (!account.nip46) return null;\n if (!options.pool) return null;\n const { remoteSignerPubkey, relays, clientSecretKey } = account.nip46;\n if (!remoteSignerPubkey || !relays.length || !clientSecretKey) {\n return null;\n }\n const tools = ToolsBunkerSigner.fromBunker(\n hexToBytes(clientSecretKey),\n { pubkey: remoteSignerPubkey, relays, secret: null },\n { pool: options.pool },\n );\n // No tools.connect() — the bunker already approved this client\n // on first pairing; re-sending `connect` is what triggers the\n // cold-start approval prompt we are trying to avoid.\n const signer = new BunkerSigner(tools, account.pubkey);\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'android': {\n if (!account.androidPackageName) return null;\n if (!this.#defaultAndroidPlugin) return null;\n const signer = new AndroidSigner(\n this.#defaultAndroidPlugin,\n account.androidPackageName,\n account.npub,\n account.pubkey,\n );\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'ncryptsec':\n // No silent path — passphrase is not (and must not be) persisted.\n return null;\n }\n }\n\n /**\n * Make `pubkey` the active account. Clears the in-memory signer —\n * the new account starts **locked** even if it was previously\n * unlocked in this session.\n *\n * @throws if `pubkey` does not match any persisted account.\n */\n async switchAccount(pubkey: string): Promise<void> {\n const account = this.#accounts.find(a => a.pubkey === pubkey);\n if (!account) throw new Error(`switchAccount: no account for pubkey ${pubkey}`);\n this.#activePubkey = pubkey;\n this.#activeSigner = null;\n this.#persistActive();\n this.#emit({ type: 'switch', account });\n }\n\n /**\n * Remove an account from storage. `pubkey` defaults to the active\n * account. If the active account is removed, the in-memory signer is\n * cleared. No-op if there is nothing to remove.\n */\n async logout(pubkey?: string): Promise<void> {\n const target = pubkey ?? this.#activePubkey;\n if (!target) return;\n this.#accounts = this.#accounts.filter(a => a.pubkey !== target);\n this.#persistAccounts();\n if (this.#activePubkey === target) {\n this.#activePubkey = null;\n this.#activeSigner = null;\n this.#persistActive();\n }\n this.#emit({ type: 'logout', pubkey: target });\n }\n\n /**\n * Subscribe to account-state changes. Returns an unsubscribe function.\n * Listener errors are swallowed so one bad listener can't break others.\n * See {@link SignerEvent} for the variants.\n */\n onChange(cb: (event: SignerEvent) => void): () => void {\n this.#listeners.add(cb);\n return () => {\n this.#listeners.delete(cb);\n };\n }\n}\n\n/** Convenience wrapper around `new Signer(config)`. */\nexport function createSigner(config: SignerConfig = {}): Signer {\n return new Signer(config);\n}\n","export interface StorageAdapter {\n get(key: string): string | null;\n set(key: string, value: string): void;\n remove(key: string): void;\n}\n\nconst DEFAULT_PREFIX = '@formstr/signer:';\n\nexport function localStorageAdapter(prefix: string = DEFAULT_PREFIX): StorageAdapter {\n const ls = (): Storage | null => {\n try {\n return typeof globalThis !== 'undefined' && globalThis.localStorage\n ? globalThis.localStorage\n : null;\n } catch {\n return null;\n }\n };\n return {\n get(key) {\n try {\n return ls()?.getItem(prefix + key) ?? null;\n } catch {\n return null;\n }\n },\n set(key, value) {\n try {\n ls()?.setItem(prefix + key, value);\n } catch {\n // swallow quota / privacy-mode errors\n }\n },\n remove(key) {\n try {\n ls()?.removeItem(prefix + key);\n } catch {\n // swallow\n }\n },\n };\n}\n","import {\n finalizeEvent,\n getPublicKey,\n nip04,\n nip44,\n type Event as NostrEvent,\n type EventTemplate,\n} from 'nostr-tools';\nimport type { ActiveSigner } from './types.js';\n\n/**\n * ActiveSigner backed by a raw secret key held in memory.\n * The secret key never leaves this object — there is no getter for it.\n */\nexport class LocalSigner implements ActiveSigner {\n readonly #secretKey: Uint8Array;\n\n constructor(secretKey: Uint8Array) {\n this.#secretKey = secretKey;\n }\n\n async getPublicKey(): Promise<string> {\n return getPublicKey(this.#secretKey);\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return finalizeEvent(event, this.#secretKey);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return nip04.encrypt(this.#secretKey, peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return nip04.decrypt(this.#secretKey, peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.encrypt(plaintext, key);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.decrypt(ciphertext, key);\n }\n}\n","import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';\nimport { encrypt as nip49Encrypt, decrypt as nip49Decrypt } from 'nostr-tools/nip49';\n\nexport function encryptSecretKey(secretKey: Uint8Array, passphrase: string): string {\n return nip49Encrypt(secretKey, passphrase);\n}\n\nexport function decryptNcryptsec(ncryptsec: string, passphrase: string): Uint8Array {\n return nip49Decrypt(ncryptsec, passphrase);\n}\n\nexport interface GeneratedAccount {\n secretKey: Uint8Array;\n pubkey: string;\n npub: string;\n ncryptsec: string;\n}\n\nexport function generateAccount(passphrase: string): GeneratedAccount {\n const secretKey = generateSecretKey();\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const ncryptsec = nip49Encrypt(secretKey, passphrase);\n return { secretKey, pubkey, npub, ncryptsec };\n}\n","import type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\nexport interface WindowNostr {\n getPublicKey(): Promise<string>;\n signEvent(event: EventTemplate): Promise<NostrEvent>;\n getRelays?(): Promise<Record<string, { read: boolean; write: boolean }>>;\n nip04?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n nip44?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n}\n\nexport function getWindowNostr(): WindowNostr {\n const nostr = (globalThis as { nostr?: WindowNostr }).nostr;\n if (!nostr) {\n throw new Error(\n '@formstr/signer: NIP-07 extension not found (globalThis.nostr is undefined)',\n );\n }\n return nostr;\n}\n\nexport class ExtensionSigner implements ActiveSigner {\n async getPublicKey(): Promise<string> {\n return getWindowNostr().getPublicKey();\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return getWindowNostr().signEvent(event);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.encrypt(peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.decrypt(peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.encrypt(peerPubkey, plaintext);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.decrypt(peerPubkey, ciphertext);\n }\n}\n","import { generateSecretKey, getPublicKey } from 'nostr-tools';\nimport {\n BunkerSigner as ToolsBunkerSigner,\n createNostrConnectURI,\n parseBunkerInput,\n type BunkerPointer,\n} from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner, RelayMismatchHandler } from './core/types.js';\n\nexport type { BunkerPointer };\n\n/**\n * Thin wrapper around nostr-tools' BunkerSigner that exposes only the\n * ActiveSigner surface. We keep this layer so callers depend on a stable\n * interface even if we ever swap the underlying implementation.\n *\n * Optionally accepts a `cachedUserPubkey`. When supplied, {@link getPublicKey}\n * returns it without a bunker roundtrip. The user's signer pubkey is fixed\n * for a given paired account, so caching it after the initial `connect` —\n * or feeding it back in from persisted storage on unlock — avoids both a\n * network hop and a potential approval prompt on every cold start. Without\n * a cached value we fall back to asking the bunker, matching the prior\n * behavior.\n */\nexport class BunkerSigner implements ActiveSigner {\n readonly #delegate: ToolsBunkerSigner;\n readonly #cachedUserPubkey: string | null;\n\n constructor(delegate: ToolsBunkerSigner, cachedUserPubkey?: string) {\n this.#delegate = delegate;\n this.#cachedUserPubkey = cachedUserPubkey ?? null;\n }\n\n getPublicKey(): Promise<string> {\n if (this.#cachedUserPubkey !== null) {\n return Promise.resolve(this.#cachedUserPubkey);\n }\n return this.#delegate.getPublicKey();\n }\n signEvent(event: EventTemplate): Promise<NostrEvent> {\n return this.#delegate.signEvent(event);\n }\n nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip04Encrypt(peerPubkey, plaintext);\n }\n nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip04Decrypt(peerPubkey, ciphertext);\n }\n nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip44Encrypt(peerPubkey, plaintext);\n }\n nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip44Decrypt(peerPubkey, ciphertext);\n }\n async close(): Promise<void> {\n return this.#delegate.close();\n }\n}\n\nexport interface BunkerLoginOptions {\n /** Custom pool, e.g. for tests. Defaults to a new SimplePool inside nostr-tools. */\n pool?: AbstractSimplePool;\n /** Called when the remote signer needs the user to visit an auth URL. */\n onAuth?: (url: string) => void;\n /** Optional client session keypair (hex bytes). Auto-generated if omitted. */\n clientSecretKey?: Uint8Array;\n /** Notified when the bunker's preferred relays differ from the URI's. */\n onRelayMismatch?: RelayMismatchHandler;\n /**\n * NIP-46 permissions to request as the 3rd `connect` param\n * (e.g. `['sign_event:1', 'nip44_encrypt']`). When omitted, the\n * connect request carries no perms — bunker UIs may then skip the\n * approval prompt entirely, leaving the user with nothing to tap.\n */\n perms?: string[];\n}\n\nasync function fetchBunkerRelays(tools: ToolsBunkerSigner): Promise<string[] | null> {\n try {\n const resp = await tools.sendRequest('get_relays', []);\n const parsed = JSON.parse(resp) as unknown;\n if (Array.isArray(parsed)) {\n return parsed.filter((r): r is string => typeof r === 'string');\n }\n if (typeof parsed === 'object' && parsed !== null) {\n return Object.keys(parsed as Record<string, unknown>);\n }\n return null;\n } catch {\n return null;\n }\n}\n\nfunction relayListsMatch(a: string[], b: string[]): boolean {\n if (a.length !== b.length) return false;\n const sa = [...a].sort();\n const sb = [...b].sort();\n for (let i = 0; i < sa.length; i++) if (sa[i] !== sb[i]) return false;\n return true;\n}\n\nasync function resolveRelayChoice(\n tools: ToolsBunkerSigner,\n userRelays: string[],\n onRelayMismatch: RelayMismatchHandler | undefined,\n): Promise<string[]> {\n if (!onRelayMismatch) return userRelays;\n const bunkerRelays = await fetchBunkerRelays(tools);\n if (!bunkerRelays || relayListsMatch(userRelays, bunkerRelays)) return userRelays;\n const accept = await onRelayMismatch({ userRelays, bunkerRelays });\n return accept ? bunkerRelays : userRelays;\n}\n\nexport interface BunkerConnectResult {\n signer: BunkerSigner;\n pubkey: string;\n pointer: BunkerPointer;\n clientSecretKey: Uint8Array;\n}\n\n/**\n * Connect to a remote signer via a bunker:// URI (or a NIP-05 identifier).\n * Relays come from the URI — there is no fallback default list.\n */\nexport async function connectWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n): Promise<BunkerConnectResult> {\n const pointer = await parseBunkerInput(uri);\n if (!pointer) {\n throw new Error('@formstr/signer: invalid bunker URI');\n }\n if (!pointer.relays?.length) {\n throw new Error('@formstr/signer: bunker URI must include at least one relay');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const tools = ToolsBunkerSigner.fromBunker(clientSecretKey, pointer, {\n pool: options.pool,\n onauth: options.onAuth,\n });\n // nostr-tools' BunkerSigner.connect() hardcodes only [pubkey, secret],\n // dropping the optional 3rd `perms` arg defined by NIP-46. Without it\n // bunker UIs (Amber, etc.) have no permissions to authorize and may\n // skip the approval prompt entirely. We send the request directly.\n await tools.sendRequest('connect', [\n pointer.pubkey,\n pointer.secret ?? '',\n (options.perms ?? []).join(','),\n ]);\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n pointer.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...pointer, relays: resolvedRelays },\n clientSecretKey,\n };\n}\n\nexport interface NostrConnectInitOptions {\n /** User-supplied relays. The whole point of the strict-relay rule. */\n relays: string[];\n metadata?: { name?: string; url?: string; image?: string };\n /** Permissions to request (NIP-46 perms list, e.g. [\"sign_event:1\",\"nip44_encrypt\"]). */\n perms?: string[];\n pool?: AbstractSimplePool;\n onAuth?: (url: string) => void;\n /** Override the auto-generated client session keypair. */\n clientSecretKey?: Uint8Array;\n /** Override the auto-generated URI secret. */\n secret?: string;\n /** Abort the pairing wait. */\n signal?: AbortSignal;\n /** Max wait for pairing in ms (default 5 minutes). */\n timeoutMs?: number;\n /** Notified when the bunker's preferred relays differ from the user's. */\n onRelayMismatch?: RelayMismatchHandler;\n}\n\nexport interface NostrConnectInitiation {\n uri: string;\n clientPubkey: string;\n complete: Promise<BunkerConnectResult>;\n}\n\n/**\n * Generate a nostrconnect:// URI and wait for the remote signer to pair.\n * The caller displays the URI (typically as a QR code), and the returned\n * `complete` promise resolves once the signer connects back.\n */\nexport function initiateNostrConnect(options: NostrConnectInitOptions): NostrConnectInitiation {\n if (options.relays.length === 0) {\n throw new Error('@formstr/signer: at least one relay is required for nostrconnect');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const clientPubkey = getPublicKey(clientSecretKey);\n const secret = options.secret ?? Math.random().toString(36).slice(2);\n const uri = createNostrConnectURI({\n clientPubkey,\n relays: options.relays,\n secret,\n perms: options.perms,\n name: options.metadata?.name,\n url: options.metadata?.url,\n image: options.metadata?.image,\n });\n const maxWaitOrAbort: number | AbortSignal =\n options.signal ?? options.timeoutMs ?? 300_000;\n // skipSwitchRelays:true — keep the caller-supplied relays authoritative,\n // never silently swap to whatever the bunker prefers.\n const complete = ToolsBunkerSigner.fromURI(\n clientSecretKey,\n uri,\n { pool: options.pool, onauth: options.onAuth, skipSwitchRelays: true },\n maxWaitOrAbort,\n ).then(async (tools) => {\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n options.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...tools.bp, relays: resolvedRelays },\n clientSecretKey,\n };\n });\n return { uri, clientPubkey, complete };\n}\n\nconst hexAlphabet = '0123456789abcdef';\n\nexport function bytesToHex(bytes: Uint8Array): string {\n let s = '';\n for (const b of bytes) s += hexAlphabet[b >> 4] + hexAlphabet[b & 0xf];\n return s;\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n if (hex.length % 2 !== 0) throw new Error('hexToBytes: odd-length hex string');\n const out = new Uint8Array(hex.length / 2);\n for (let i = 0; i < out.length; i++) {\n out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n }\n return out;\n}\n","import { getEventHash, nip19, type Event as NostrEvent, type EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\n/**\n * Subset of `nostr-signer-capacitor-plugin`'s exported `NostrSignerPlugin`\n * that we depend on. Signatures intentionally mirror that library\n * (positional args, per-call `packageName`) so the real plugin is\n * structurally assignable here — and any mock written against this\n * interface is a faithful stand-in. The conformance is enforced by a\n * compile-time guard in `tests/helpers/mockAndroidPlugin.ts`.\n */\nexport interface AndroidSignerAppInfo {\n name: string;\n packageName: string;\n iconUrl?: string;\n}\n\nexport interface AndroidSignerPlugin {\n setPackageName(packageName: string): Promise<void>;\n getInstalledSignerApps(): Promise<{ apps: AndroidSignerAppInfo[] }>;\n getPublicKey(\n packageName?: string,\n permissions?: string,\n ): Promise<{ npub: string; package: string }>;\n signEvent(\n packageName: string,\n eventJson: string,\n id: string,\n npub: string,\n ): Promise<{ signature: string; id: string; event: string }>;\n nip04Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip04Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n}\n\nexport interface AndroidLoginOptions {\n /** The Android package name of the external signer app (e.g. com.greenart7c3.nostrsigner). */\n packageName?: string;\n /** Override the plugin for this call. Falls back to SignerConfig.androidSignerPlugin. */\n plugin?: AndroidSignerPlugin;\n}\n\nexport class AndroidSigner implements ActiveSigner {\n readonly #plugin: AndroidSignerPlugin;\n readonly #packageName: string;\n readonly #npub: string;\n readonly #pubkey: string;\n\n constructor(\n plugin: AndroidSignerPlugin,\n packageName: string,\n npub: string,\n pubkey: string,\n ) {\n this.#plugin = plugin;\n this.#packageName = packageName;\n this.#npub = npub;\n this.#pubkey = pubkey;\n }\n\n async getPublicKey(): Promise<string> {\n return this.#pubkey;\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n const unsigned = { ...event, pubkey: this.#pubkey };\n const eventId = getEventHash(unsigned);\n const result = await this.#plugin.signEvent(\n this.#packageName,\n JSON.stringify(unsigned),\n eventId,\n this.#npub,\n );\n return JSON.parse(result.event) as NostrEvent;\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n}\n\nexport interface AndroidLoginResult {\n signer: AndroidSigner;\n pubkey: string;\n npub: string;\n packageName: string;\n}\n\n/**\n * Render a debuggable summary of what the Android signer plugin returned\n * where an npub was expected. Includes type and length, plus a truncated\n * prefix that preserves the bech32 HRP (so callers can tell `nsec1…` /\n * `nprofile1…` / a raw hex pubkey apart) without leaking the full secret\n * material that an erroneous `nsec` response would carry.\n */\nfunction describeIdentifier(value: unknown): string {\n if (value === null) return 'null';\n if (value === undefined) return 'undefined';\n if (typeof value !== 'string') {\n return `<${typeof value}>`;\n }\n if (value.length === 0) return 'empty string';\n const prefix = value.slice(0, 12);\n const suffix = value.length > 12 ? '…' : '';\n return `\"${prefix}${suffix}\" (length=${value.length})`;\n}\n\n/**\n * Lowercase 32-byte hex pubkey shape. NIP-55 nominally returns an `npub`\n * in the `signature` extra, but recent Amber builds put the raw hex\n * pubkey there instead — we accept either form and normalize internally.\n */\nconst HEX_PUBKEY_RE = /^[0-9a-f]{64}$/i;\n\nexport async function loginWithAndroidSigner(\n plugin: AndroidSignerPlugin,\n packageName?: string,\n): Promise<AndroidLoginResult> {\n if (packageName) {\n await plugin.setPackageName(packageName);\n }\n const { npub: rawIdentifier, package: pluginPackage } =\n await plugin.getPublicKey(packageName);\n const resolvedPackage = pluginPackage || packageName;\n if (!resolvedPackage) {\n throw new Error(\n '@formstr/signer: android signer did not return a package name and none was supplied',\n );\n }\n const { pubkey, npub } = normalizeAndroidIdentifier(rawIdentifier);\n return {\n signer: new AndroidSigner(plugin, resolvedPackage, npub, pubkey),\n pubkey,\n npub,\n packageName: resolvedPackage,\n };\n}\n\n/**\n * Normalize whatever the Android signer plugin handed us in the `npub`\n * slot into a `{ pubkey, npub }` pair. Accepts either:\n * - a 32-byte lowercase hex pubkey (newer Amber builds return this),\n * - a bech32 `npub1…` (the original NIP-55 spec shape).\n * Throws a debuggable error for anything else, including the preview\n * of what was actually received so the caller can triage.\n */\nfunction normalizeAndroidIdentifier(\n rawIdentifier: unknown,\n): { pubkey: string; npub: string } {\n if (typeof rawIdentifier === 'string' && HEX_PUBKEY_RE.test(rawIdentifier)) {\n const pubkey = rawIdentifier.toLowerCase();\n return { pubkey, npub: nip19.npubEncode(pubkey) };\n }\n // Wrap nip19.decode so a bech32 failure (\"Data must be at least 6\n // characters long\", \"Invalid checksum\", ...) surfaces what the plugin\n // actually returned. Without this, callers see an opaque bech32 crash\n // and can't tell whether Amber sent back an empty string, an nsec, or\n // something else entirely.\n let decoded: ReturnType<typeof nip19.decode>;\n try {\n decoded = nip19.decode(rawIdentifier as string);\n } catch (e) {\n // nostr-tools' nip19 decoder always throws Error instances on bech32\n // failures (\"Data must be at least 6 characters long\", \"Invalid\n // checksum\", \"Unknown prefix\", ...). Pass the message straight through.\n throw new Error(\n `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(rawIdentifier)}): ${(e as Error).message}`,\n );\n }\n if (decoded.type !== 'npub') {\n throw new Error(\n `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(rawIdentifier)})`,\n );\n }\n return { pubkey: decoded.data, npub: rawIdentifier as string };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,sBAAoC;AACpC,IAAAC,gBAAkD;;;ACKlD,IAAM,iBAAiB;AAEhB,SAAS,oBAAoB,SAAiB,gBAAgC;AACnF,QAAM,KAAK,MAAsB;AAC/B,QAAI;AACF,aAAO,OAAO,eAAe,eAAe,WAAW,eACnD,WAAW,eACX;AAAA,IACN,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AAAA,IACL,IAAI,KAAK;AACP,UAAI;AACF,eAAO,GAAG,GAAG,QAAQ,SAAS,GAAG,KAAK;AAAA,MACxC,QAAQ;AACN,eAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA,IAAI,KAAK,OAAO;AACd,UAAI;AACF,WAAG,GAAG,QAAQ,SAAS,KAAK,KAAK;AAAA,MACnC,QAAQ;AAAA,MAER;AAAA,IACF;AAAA,IACA,OAAO,KAAK;AACV,UAAI;AACF,WAAG,GAAG,WAAW,SAAS,GAAG;AAAA,MAC/B,QAAQ;AAAA,MAER;AAAA,IACF;AAAA,EACF;AACF;;;ACzCA,yBAOO;AAOA,IAAM,cAAN,MAA0C;AAAA,EACtC;AAAA,EAET,YAAY,WAAuB;AACjC,SAAK,aAAa;AAAA,EACpB;AAAA,EAEA,MAAM,eAAgC;AACpC,eAAO,iCAAa,KAAK,UAAU;AAAA,EACrC;AAAA,EAEA,MAAM,UAAU,OAA2C;AACzD,eAAO,kCAAc,OAAO,KAAK,UAAU;AAAA,EAC7C;AAAA,EAEA,MAAM,aAAa,YAAoB,WAAoC;AACzE,WAAO,y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AA;AAAA;AAAA,EAOA,SAAS,IAA8C;AACrD,SAAK,WAAW,IAAI,EAAE;AACtB,WAAO,MAAM;AACX,WAAK,WAAW,OAAO,EAAE;AAAA,IAC3B;AAAA,EACF;AACF;AAGO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,OAAO,MAAM;AAC1B;","names":["import_nostr_tools","import_nip46","import_nostr_tools","nip49Encrypt","nip49Decrypt","import_nostr_tools","ToolsBunkerSigner","import_nostr_tools","ToolsBunkerSigner"]}
package/dist/index.js CHANGED
@@ -356,36 +356,45 @@ function describeIdentifier(value) {
356
356
  const suffix = value.length > 12 ? "\u2026" : "";
357
357
  return `"${prefix}${suffix}" (length=${value.length})`;
358
358
  }
359
+ var HEX_PUBKEY_RE = /^[0-9a-f]{64}$/i;
359
360
  async function loginWithAndroidSigner(plugin, packageName) {
360
361
  if (packageName) {
361
362
  await plugin.setPackageName(packageName);
362
363
  }
363
- const { npub, package: pluginPackage } = await plugin.getPublicKey(packageName);
364
+ const { npub: rawIdentifier, package: pluginPackage } = await plugin.getPublicKey(packageName);
364
365
  const resolvedPackage = pluginPackage || packageName;
365
366
  if (!resolvedPackage) {
366
367
  throw new Error(
367
368
  "@formstr/signer: android signer did not return a package name and none was supplied"
368
369
  );
369
370
  }
371
+ const { pubkey, npub } = normalizeAndroidIdentifier(rawIdentifier);
372
+ return {
373
+ signer: new AndroidSigner(plugin, resolvedPackage, npub, pubkey),
374
+ pubkey,
375
+ npub,
376
+ packageName: resolvedPackage
377
+ };
378
+ }
379
+ function normalizeAndroidIdentifier(rawIdentifier) {
380
+ if (typeof rawIdentifier === "string" && HEX_PUBKEY_RE.test(rawIdentifier)) {
381
+ const pubkey = rawIdentifier.toLowerCase();
382
+ return { pubkey, npub: nip192.npubEncode(pubkey) };
383
+ }
370
384
  let decoded;
371
385
  try {
372
- decoded = nip192.decode(npub);
386
+ decoded = nip192.decode(rawIdentifier);
373
387
  } catch (e) {
374
388
  throw new Error(
375
- `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(npub)}): ${e.message}`
389
+ `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(rawIdentifier)}): ${e.message}`
376
390
  );
377
391
  }
378
392
  if (decoded.type !== "npub") {
379
393
  throw new Error(
380
- `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(npub)})`
394
+ `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(rawIdentifier)})`
381
395
  );
382
396
  }
383
- return {
384
- signer: new AndroidSigner(plugin, resolvedPackage, npub, decoded.data),
385
- pubkey: decoded.data,
386
- npub,
387
- packageName: resolvedPackage
388
- };
397
+ return { pubkey: decoded.data, npub: rawIdentifier };
389
398
  }
390
399
 
391
400
  // src/core/signer.ts
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core/signer.ts","../src/core/storage.ts","../src/core/localSigner.ts","../src/nip49.ts","../src/nip07.ts","../src/nip46.ts","../src/nip55.ts"],"sourcesContent":["import { getPublicKey, nip19 } from 'nostr-tools';\nimport { BunkerSigner as ToolsBunkerSigner } from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type {\n ActiveSigner,\n BunkerLoginOptions,\n NostrConnectOptions,\n SignerConfig,\n SignerEvent,\n StoredAccount,\n UnlockOptions,\n} from './types.js';\nimport { localStorageAdapter, type StorageAdapter } from './storage.js';\nimport { LocalSigner } from './localSigner.js';\nimport { decryptNcryptsec, generateAccount } from '../nip49.js';\nimport { ExtensionSigner } from '../nip07.js';\nimport {\n BunkerSigner,\n bytesToHex,\n connectWithBunkerUri,\n hexToBytes,\n initiateNostrConnect,\n} from '../nip46.js';\nimport {\n AndroidSigner,\n loginWithAndroidSigner as connectWithAndroidSigner,\n type AndroidLoginOptions,\n type AndroidSignerAppInfo,\n type AndroidSignerPlugin,\n} from '../nip55.js';\n\nconst ACCOUNTS_KEY = 'accounts';\nconst ACTIVE_KEY = 'active-pubkey';\n\n/**\n * Multi-account Nostr signer with persistence.\n *\n * **Hydration.** The constructor reads previously-saved accounts from\n * the configured storage adapter. Every hydrated account starts\n * **locked**: present in `listAccounts()` and (if it was the active one\n * before) reachable via `getActiveAccount()`, but `getActiveSigner()`\n * returns `null` until the user re-authenticates. The matching\n * `loginWith*` method unlocks the active account.\n *\n * **Locked vs unlocked.** Use `getActiveAccount()` to render UI (\"logged\n * in as @alice\") and `getActiveSigner()` to decide whether the user can\n * actually sign. The pattern is \"show the account always, gate signing\n * on the signer.\"\n *\n * **Events.** Subscribe via `onChange()` to re-render when an account\n * is added, switched, or removed. See {@link SignerEvent}.\n */\nexport class Signer {\n readonly #storage: StorageAdapter;\n readonly #defaultAndroidPlugin: AndroidSignerPlugin | undefined;\n readonly #appMetadata: { name?: string; url?: string; image?: string };\n #accounts: StoredAccount[] = [];\n #activePubkey: string | null = null;\n #activeSigner: ActiveSigner | null = null;\n #listeners = new Set<(event: SignerEvent) => void>();\n\n constructor(config: SignerConfig = {}) {\n this.#storage = config.storage ?? localStorageAdapter(config.storageKeyPrefix);\n this.#defaultAndroidPlugin = config.androidSignerPlugin;\n this.#appMetadata = {\n name: config.appName,\n url: config.appUrl,\n image: config.appImage,\n };\n this.#hydrate();\n }\n\n #hydrate(): void {\n try {\n const raw = this.#storage.get(ACCOUNTS_KEY);\n if (raw) {\n const parsed = JSON.parse(raw);\n if (Array.isArray(parsed)) this.#accounts = parsed as StoredAccount[];\n }\n this.#activePubkey = this.#storage.get(ACTIVE_KEY);\n } catch {\n this.#accounts = [];\n this.#activePubkey = null;\n }\n }\n\n #persistAccounts(): void {\n this.#storage.set(ACCOUNTS_KEY, JSON.stringify(this.#accounts));\n }\n\n #persistActive(): void {\n if (this.#activePubkey) this.#storage.set(ACTIVE_KEY, this.#activePubkey);\n else this.#storage.remove(ACTIVE_KEY);\n }\n\n #upsertAccount(account: StoredAccount): void {\n const idx = this.#accounts.findIndex(a => a.pubkey === account.pubkey);\n if (idx >= 0) this.#accounts[idx] = account;\n else this.#accounts.push(account);\n this.#persistAccounts();\n }\n\n #setActive(account: StoredAccount, signer: ActiveSigner): void {\n const wasDifferent = this.#activePubkey !== null && this.#activePubkey !== account.pubkey;\n this.#activePubkey = account.pubkey;\n this.#activeSigner = signer;\n this.#persistActive();\n this.#emit({ type: wasDifferent ? 'switch' : 'login', account });\n }\n\n #emit(event: SignerEvent): void {\n for (const cb of this.#listeners) {\n try {\n cb(event);\n } catch {\n // listener errors are swallowed so one bad listener can't break others\n }\n }\n }\n\n /**\n * Generate a brand-new nsec, encrypt it with `passphrase` (NIP-49),\n * persist the resulting `ncryptsec` account, and activate it. Returns\n * the new account's `npub` and `ncryptsec` — the caller must surface\n * the `ncryptsec` to the user **immediately** since it is the only way\n * back into the account on a fresh device.\n *\n * @throws if `passphrase` is empty.\n */\n async createAccount(passphrase: string): Promise<{ npub: string; ncryptsec: string }> {\n if (!passphrase) throw new Error('createAccount: passphrase required');\n const { secretKey, pubkey, npub, ncryptsec } = generateAccount(passphrase);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return { npub, ncryptsec };\n }\n\n /**\n * Decrypt an ncryptsec with the user's passphrase, persist the account\n * (overwriting any previous entry for the same pubkey), and activate it.\n *\n * @throws if either argument is empty, or if the passphrase doesn't\n * decrypt the ncryptsec.\n */\n async loginWithNcryptsec(ncryptsec: string, passphrase: string): Promise<StoredAccount> {\n if (!ncryptsec) throw new Error('loginWithNcryptsec: ncryptsec required');\n if (!passphrase) throw new Error('loginWithNcryptsec: passphrase required');\n const secretKey = decryptNcryptsec(ncryptsec, passphrase);\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return account;\n }\n\n /**\n * Connect via the NIP-07 browser extension exposed at `window.nostr`.\n * The extension prompts the user for permission on first use.\n *\n * @throws if no extension is installed or the user denies the request.\n */\n async loginWithExtension(): Promise<StoredAccount> {\n const extension = new ExtensionSigner();\n const pubkey = await extension.getPublicKey();\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'extension' };\n this.#upsertAccount(account);\n this.#setActive(account, extension);\n return account;\n }\n\n /**\n * Connect to a NIP-46 remote signer via a `bunker://` URI. Relays are\n * read from the URI itself — no hardcoded fallbacks. Pass a `pool`\n * to reuse an existing relay connection; pass `clientSecretKey` to\n * resume a previous session (the hex from `StoredAccount.nip46`).\n *\n * @throws if the URI is malformed, no relay is reachable, or the\n * remote signer rejects pairing within the implementation's timeout.\n */\n async loginWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n ): Promise<StoredAccount> {\n const result = await connectWithBunkerUri(uri, options);\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Initiate a NIP-46 `nostrconnect://` pairing. Generates a client\n * keypair, publishes a connect request to the supplied `relays`, and\n * waits for a remote signer to pair. Call `options.onUri(uri)` to\n * render the URI as a QR code; the returned promise resolves once\n * pairing completes. Cancel by aborting `options.signal`.\n *\n * @throws if `relays` is empty, the user aborts, the pairing times\n * out, or no signer responds.\n */\n async loginWithNostrConnect(options: NostrConnectOptions): Promise<StoredAccount> {\n if (options.relays.length === 0) {\n throw new Error('loginWithNostrConnect: at least one relay required');\n }\n // Merge per-call metadata over SignerConfig defaults (appName/url/image).\n // `name` is required — Amber (and likely other NIP-55 signer apps) gate\n // the consent UI on having a recognizable client identity in the URI.\n // Without one they will receive the request but never surface\n // approve/deny buttons, leaving the pairing silently stuck.\n const metadata = {\n name: options.metadata?.name ?? this.#appMetadata.name,\n url: options.metadata?.url ?? this.#appMetadata.url,\n image: options.metadata?.image ?? this.#appMetadata.image,\n };\n if (!metadata.name) {\n throw new Error(\n '@formstr/signer: loginWithNostrConnect requires an app name. Set `appName` in createSigner() or pass `metadata.name` to loginWithNostrConnect().',\n );\n }\n const init = initiateNostrConnect({\n relays: options.relays,\n metadata,\n perms: options.perms,\n pool: options.pool,\n onAuth: options.onAuth,\n signal: options.signal,\n timeoutMs: options.timeoutMs,\n onRelayMismatch: options.onRelayMismatch,\n });\n options.onUri(init.uri);\n const result = await init.complete;\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri: init.uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Enumerate NIP-55 signer apps installed on the device, via the\n * configured Android plugin (or `plugin` if supplied). Useful for\n * rendering a \"pick your signer\" list — the built-in UI does this\n * automatically when the Android tab is selected.\n *\n * Only meaningful inside a Capacitor Android shell. On web/iOS the\n * configured plugin is typically absent and this throws.\n *\n * @throws if no plugin is configured and none is passed in.\n */\n async listAndroidSignerApps(\n plugin?: AndroidSignerPlugin,\n ): Promise<AndroidSignerAppInfo[]> {\n const p = plugin ?? this.#defaultAndroidPlugin;\n if (!p) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to listAndroidSignerApps)',\n );\n }\n const { apps } = await p.getInstalledSignerApps();\n return apps;\n }\n\n /**\n * Sign in via a NIP-55 Android external signer (Amber, etc). If\n * `options.packageName` is given, that specific signer app is invoked;\n * otherwise the plugin picks a default (typically the only installed\n * signer, or an OS chooser). Pass `options.plugin` to override the\n * configured default for this call.\n *\n * @throws if no plugin is configured, the signer app cannot be\n * resolved to a package name, or the user denies the request.\n */\n async loginWithAndroidSigner(options: AndroidLoginOptions = {}): Promise<StoredAccount> {\n const plugin = options.plugin ?? this.#defaultAndroidPlugin;\n if (!plugin) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to loginWithAndroidSigner)',\n );\n }\n const result = await connectWithAndroidSigner(plugin, options.packageName);\n const account: StoredAccount = {\n npub: result.npub,\n pubkey: result.pubkey,\n method: 'android',\n androidPackageName: result.packageName,\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /** Snapshot of every persisted account, in insertion order. */\n listAccounts(): StoredAccount[] {\n return [...this.#accounts];\n }\n\n /**\n * The currently selected account, or `null` if none. Present even when\n * the account is locked (no active signer yet). Use this to render\n * \"logged in as @alice\" — pair with {@link getActiveSigner} to decide\n * whether signing is actually available.\n */\n getActiveAccount(): StoredAccount | null {\n if (!this.#activePubkey) return null;\n return this.#accounts.find(a => a.pubkey === this.#activePubkey) ?? null;\n }\n\n /**\n * The unlocked signer for the active account, or `null` if locked.\n * After a fresh page load this is `null` for every account type\n * (passphrase / extension grant / signer-app handshake all need to\n * be redone). Calling the matching `loginWith*` method unlocks it.\n */\n getActiveSigner(): ActiveSigner | null {\n return this.#activeSigner;\n }\n\n /**\n * Silently unlock the active account from persisted state — no user\n * prompt, no fresh pairing. The package already keeps everything it\n * needs to reconstruct the runtime signer on disk; this method is the\n * way to actually use that on cold start instead of re-running each\n * method's first-time login flow.\n *\n * Behavior by method:\n *\n * - `extension`: constructs an {@link ExtensionSigner}, which just\n * proxies to `window.nostr`. No setup roundtrip — individual\n * operations may still prompt depending on the extension's own\n * permission state, but unlock itself does not.\n *\n * - `nip46`: reuses the stored `clientSecretKey` to construct a\n * {@link BunkerSigner} against the stored bunker pubkey + relays\n * via `BunkerSigner.fromBunker`. Deliberately skips the `connect`\n * request — the remote signer (Amber etc.) approved this client\n * pubkey on first pairing and re-sending `connect` is what surfaces\n * a fresh approval prompt every cold start. Requires `options.pool`\n * so the BunkerSigner has somewhere to listen for responses.\n * The cached user pubkey is fed into the wrapper so a follow-up\n * `getPublicKey()` is a memory read, not a relay request.\n *\n * - `android`: constructs an {@link AndroidSigner} directly from the\n * stored `androidPackageName` + `pubkey` + `npub`. Skips the\n * `getPublicKey` content-provider roundtrip that\n * {@link loginWithAndroidSigner} performs and that — on Amber —\n * surfaces as a permission prompt every cold start.\n *\n * - `ncryptsec`: returns `null`. There is no silent path — the user's\n * passphrase isn't (and shouldn't be) persisted. The caller must\n * drive the passphrase prompt and call {@link loginWithNcryptsec}.\n *\n * Returns `null` (without emitting any event or mutating state) when\n * there is no active account, when the account is missing fields\n * required to unlock, when `nip46` is the method but no `pool` was\n * supplied, or when `android` is the method but no plugin is\n * configured. On success emits the same `login` / `switch` event the\n * corresponding `loginWith*` would.\n */\n async unlock(options: UnlockOptions = {}): Promise<ActiveSigner | null> {\n const account = this.getActiveAccount();\n if (!account) return null;\n\n switch (account.method) {\n case 'extension': {\n const signer = new ExtensionSigner();\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'nip46': {\n if (!account.nip46) return null;\n if (!options.pool) return null;\n const { remoteSignerPubkey, relays, clientSecretKey } = account.nip46;\n if (!remoteSignerPubkey || !relays.length || !clientSecretKey) {\n return null;\n }\n const tools = ToolsBunkerSigner.fromBunker(\n hexToBytes(clientSecretKey),\n { pubkey: remoteSignerPubkey, relays, secret: null },\n { pool: options.pool },\n );\n // No tools.connect() — the bunker already approved this client\n // on first pairing; re-sending `connect` is what triggers the\n // cold-start approval prompt we are trying to avoid.\n const signer = new BunkerSigner(tools, account.pubkey);\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'android': {\n if (!account.androidPackageName) return null;\n if (!this.#defaultAndroidPlugin) return null;\n const signer = new AndroidSigner(\n this.#defaultAndroidPlugin,\n account.androidPackageName,\n account.npub,\n account.pubkey,\n );\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'ncryptsec':\n // No silent path — passphrase is not (and must not be) persisted.\n return null;\n }\n }\n\n /**\n * Make `pubkey` the active account. Clears the in-memory signer —\n * the new account starts **locked** even if it was previously\n * unlocked in this session.\n *\n * @throws if `pubkey` does not match any persisted account.\n */\n async switchAccount(pubkey: string): Promise<void> {\n const account = this.#accounts.find(a => a.pubkey === pubkey);\n if (!account) throw new Error(`switchAccount: no account for pubkey ${pubkey}`);\n this.#activePubkey = pubkey;\n this.#activeSigner = null;\n this.#persistActive();\n this.#emit({ type: 'switch', account });\n }\n\n /**\n * Remove an account from storage. `pubkey` defaults to the active\n * account. If the active account is removed, the in-memory signer is\n * cleared. No-op if there is nothing to remove.\n */\n async logout(pubkey?: string): Promise<void> {\n const target = pubkey ?? this.#activePubkey;\n if (!target) return;\n this.#accounts = this.#accounts.filter(a => a.pubkey !== target);\n this.#persistAccounts();\n if (this.#activePubkey === target) {\n this.#activePubkey = null;\n this.#activeSigner = null;\n this.#persistActive();\n }\n this.#emit({ type: 'logout', pubkey: target });\n }\n\n /**\n * Subscribe to account-state changes. Returns an unsubscribe function.\n * Listener errors are swallowed so one bad listener can't break others.\n * See {@link SignerEvent} for the variants.\n */\n onChange(cb: (event: SignerEvent) => void): () => void {\n this.#listeners.add(cb);\n return () => {\n this.#listeners.delete(cb);\n };\n }\n}\n\n/** Convenience wrapper around `new Signer(config)`. */\nexport function createSigner(config: SignerConfig = {}): Signer {\n return new Signer(config);\n}\n","export interface StorageAdapter {\n get(key: string): string | null;\n set(key: string, value: string): void;\n remove(key: string): void;\n}\n\nconst DEFAULT_PREFIX = '@formstr/signer:';\n\nexport function localStorageAdapter(prefix: string = DEFAULT_PREFIX): StorageAdapter {\n const ls = (): Storage | null => {\n try {\n return typeof globalThis !== 'undefined' && globalThis.localStorage\n ? globalThis.localStorage\n : null;\n } catch {\n return null;\n }\n };\n return {\n get(key) {\n try {\n return ls()?.getItem(prefix + key) ?? null;\n } catch {\n return null;\n }\n },\n set(key, value) {\n try {\n ls()?.setItem(prefix + key, value);\n } catch {\n // swallow quota / privacy-mode errors\n }\n },\n remove(key) {\n try {\n ls()?.removeItem(prefix + key);\n } catch {\n // swallow\n }\n },\n };\n}\n","import {\n finalizeEvent,\n getPublicKey,\n nip04,\n nip44,\n type Event as NostrEvent,\n type EventTemplate,\n} from 'nostr-tools';\nimport type { ActiveSigner } from './types.js';\n\n/**\n * ActiveSigner backed by a raw secret key held in memory.\n * The secret key never leaves this object — there is no getter for it.\n */\nexport class LocalSigner implements ActiveSigner {\n readonly #secretKey: Uint8Array;\n\n constructor(secretKey: Uint8Array) {\n this.#secretKey = secretKey;\n }\n\n async getPublicKey(): Promise<string> {\n return getPublicKey(this.#secretKey);\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return finalizeEvent(event, this.#secretKey);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return nip04.encrypt(this.#secretKey, peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return nip04.decrypt(this.#secretKey, peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.encrypt(plaintext, key);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.decrypt(ciphertext, key);\n }\n}\n","import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';\nimport { encrypt as nip49Encrypt, decrypt as nip49Decrypt } from 'nostr-tools/nip49';\n\nexport function encryptSecretKey(secretKey: Uint8Array, passphrase: string): string {\n return nip49Encrypt(secretKey, passphrase);\n}\n\nexport function decryptNcryptsec(ncryptsec: string, passphrase: string): Uint8Array {\n return nip49Decrypt(ncryptsec, passphrase);\n}\n\nexport interface GeneratedAccount {\n secretKey: Uint8Array;\n pubkey: string;\n npub: string;\n ncryptsec: string;\n}\n\nexport function generateAccount(passphrase: string): GeneratedAccount {\n const secretKey = generateSecretKey();\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const ncryptsec = nip49Encrypt(secretKey, passphrase);\n return { secretKey, pubkey, npub, ncryptsec };\n}\n","import type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\nexport interface WindowNostr {\n getPublicKey(): Promise<string>;\n signEvent(event: EventTemplate): Promise<NostrEvent>;\n getRelays?(): Promise<Record<string, { read: boolean; write: boolean }>>;\n nip04?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n nip44?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n}\n\nexport function getWindowNostr(): WindowNostr {\n const nostr = (globalThis as { nostr?: WindowNostr }).nostr;\n if (!nostr) {\n throw new Error(\n '@formstr/signer: NIP-07 extension not found (globalThis.nostr is undefined)',\n );\n }\n return nostr;\n}\n\nexport class ExtensionSigner implements ActiveSigner {\n async getPublicKey(): Promise<string> {\n return getWindowNostr().getPublicKey();\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return getWindowNostr().signEvent(event);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.encrypt(peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.decrypt(peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.encrypt(peerPubkey, plaintext);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.decrypt(peerPubkey, ciphertext);\n }\n}\n","import { generateSecretKey, getPublicKey } from 'nostr-tools';\nimport {\n BunkerSigner as ToolsBunkerSigner,\n createNostrConnectURI,\n parseBunkerInput,\n type BunkerPointer,\n} from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner, RelayMismatchHandler } from './core/types.js';\n\nexport type { BunkerPointer };\n\n/**\n * Thin wrapper around nostr-tools' BunkerSigner that exposes only the\n * ActiveSigner surface. We keep this layer so callers depend on a stable\n * interface even if we ever swap the underlying implementation.\n *\n * Optionally accepts a `cachedUserPubkey`. When supplied, {@link getPublicKey}\n * returns it without a bunker roundtrip. The user's signer pubkey is fixed\n * for a given paired account, so caching it after the initial `connect` —\n * or feeding it back in from persisted storage on unlock — avoids both a\n * network hop and a potential approval prompt on every cold start. Without\n * a cached value we fall back to asking the bunker, matching the prior\n * behavior.\n */\nexport class BunkerSigner implements ActiveSigner {\n readonly #delegate: ToolsBunkerSigner;\n readonly #cachedUserPubkey: string | null;\n\n constructor(delegate: ToolsBunkerSigner, cachedUserPubkey?: string) {\n this.#delegate = delegate;\n this.#cachedUserPubkey = cachedUserPubkey ?? null;\n }\n\n getPublicKey(): Promise<string> {\n if (this.#cachedUserPubkey !== null) {\n return Promise.resolve(this.#cachedUserPubkey);\n }\n return this.#delegate.getPublicKey();\n }\n signEvent(event: EventTemplate): Promise<NostrEvent> {\n return this.#delegate.signEvent(event);\n }\n nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip04Encrypt(peerPubkey, plaintext);\n }\n nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip04Decrypt(peerPubkey, ciphertext);\n }\n nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip44Encrypt(peerPubkey, plaintext);\n }\n nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip44Decrypt(peerPubkey, ciphertext);\n }\n async close(): Promise<void> {\n return this.#delegate.close();\n }\n}\n\nexport interface BunkerLoginOptions {\n /** Custom pool, e.g. for tests. Defaults to a new SimplePool inside nostr-tools. */\n pool?: AbstractSimplePool;\n /** Called when the remote signer needs the user to visit an auth URL. */\n onAuth?: (url: string) => void;\n /** Optional client session keypair (hex bytes). Auto-generated if omitted. */\n clientSecretKey?: Uint8Array;\n /** Notified when the bunker's preferred relays differ from the URI's. */\n onRelayMismatch?: RelayMismatchHandler;\n /**\n * NIP-46 permissions to request as the 3rd `connect` param\n * (e.g. `['sign_event:1', 'nip44_encrypt']`). When omitted, the\n * connect request carries no perms — bunker UIs may then skip the\n * approval prompt entirely, leaving the user with nothing to tap.\n */\n perms?: string[];\n}\n\nasync function fetchBunkerRelays(tools: ToolsBunkerSigner): Promise<string[] | null> {\n try {\n const resp = await tools.sendRequest('get_relays', []);\n const parsed = JSON.parse(resp) as unknown;\n if (Array.isArray(parsed)) {\n return parsed.filter((r): r is string => typeof r === 'string');\n }\n if (typeof parsed === 'object' && parsed !== null) {\n return Object.keys(parsed as Record<string, unknown>);\n }\n return null;\n } catch {\n return null;\n }\n}\n\nfunction relayListsMatch(a: string[], b: string[]): boolean {\n if (a.length !== b.length) return false;\n const sa = [...a].sort();\n const sb = [...b].sort();\n for (let i = 0; i < sa.length; i++) if (sa[i] !== sb[i]) return false;\n return true;\n}\n\nasync function resolveRelayChoice(\n tools: ToolsBunkerSigner,\n userRelays: string[],\n onRelayMismatch: RelayMismatchHandler | undefined,\n): Promise<string[]> {\n if (!onRelayMismatch) return userRelays;\n const bunkerRelays = await fetchBunkerRelays(tools);\n if (!bunkerRelays || relayListsMatch(userRelays, bunkerRelays)) return userRelays;\n const accept = await onRelayMismatch({ userRelays, bunkerRelays });\n return accept ? bunkerRelays : userRelays;\n}\n\nexport interface BunkerConnectResult {\n signer: BunkerSigner;\n pubkey: string;\n pointer: BunkerPointer;\n clientSecretKey: Uint8Array;\n}\n\n/**\n * Connect to a remote signer via a bunker:// URI (or a NIP-05 identifier).\n * Relays come from the URI — there is no fallback default list.\n */\nexport async function connectWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n): Promise<BunkerConnectResult> {\n const pointer = await parseBunkerInput(uri);\n if (!pointer) {\n throw new Error('@formstr/signer: invalid bunker URI');\n }\n if (!pointer.relays?.length) {\n throw new Error('@formstr/signer: bunker URI must include at least one relay');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const tools = ToolsBunkerSigner.fromBunker(clientSecretKey, pointer, {\n pool: options.pool,\n onauth: options.onAuth,\n });\n // nostr-tools' BunkerSigner.connect() hardcodes only [pubkey, secret],\n // dropping the optional 3rd `perms` arg defined by NIP-46. Without it\n // bunker UIs (Amber, etc.) have no permissions to authorize and may\n // skip the approval prompt entirely. We send the request directly.\n await tools.sendRequest('connect', [\n pointer.pubkey,\n pointer.secret ?? '',\n (options.perms ?? []).join(','),\n ]);\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n pointer.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...pointer, relays: resolvedRelays },\n clientSecretKey,\n };\n}\n\nexport interface NostrConnectInitOptions {\n /** User-supplied relays. The whole point of the strict-relay rule. */\n relays: string[];\n metadata?: { name?: string; url?: string; image?: string };\n /** Permissions to request (NIP-46 perms list, e.g. [\"sign_event:1\",\"nip44_encrypt\"]). */\n perms?: string[];\n pool?: AbstractSimplePool;\n onAuth?: (url: string) => void;\n /** Override the auto-generated client session keypair. */\n clientSecretKey?: Uint8Array;\n /** Override the auto-generated URI secret. */\n secret?: string;\n /** Abort the pairing wait. */\n signal?: AbortSignal;\n /** Max wait for pairing in ms (default 5 minutes). */\n timeoutMs?: number;\n /** Notified when the bunker's preferred relays differ from the user's. */\n onRelayMismatch?: RelayMismatchHandler;\n}\n\nexport interface NostrConnectInitiation {\n uri: string;\n clientPubkey: string;\n complete: Promise<BunkerConnectResult>;\n}\n\n/**\n * Generate a nostrconnect:// URI and wait for the remote signer to pair.\n * The caller displays the URI (typically as a QR code), and the returned\n * `complete` promise resolves once the signer connects back.\n */\nexport function initiateNostrConnect(options: NostrConnectInitOptions): NostrConnectInitiation {\n if (options.relays.length === 0) {\n throw new Error('@formstr/signer: at least one relay is required for nostrconnect');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const clientPubkey = getPublicKey(clientSecretKey);\n const secret = options.secret ?? Math.random().toString(36).slice(2);\n const uri = createNostrConnectURI({\n clientPubkey,\n relays: options.relays,\n secret,\n perms: options.perms,\n name: options.metadata?.name,\n url: options.metadata?.url,\n image: options.metadata?.image,\n });\n const maxWaitOrAbort: number | AbortSignal =\n options.signal ?? options.timeoutMs ?? 300_000;\n // skipSwitchRelays:true — keep the caller-supplied relays authoritative,\n // never silently swap to whatever the bunker prefers.\n const complete = ToolsBunkerSigner.fromURI(\n clientSecretKey,\n uri,\n { pool: options.pool, onauth: options.onAuth, skipSwitchRelays: true },\n maxWaitOrAbort,\n ).then(async (tools) => {\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n options.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...tools.bp, relays: resolvedRelays },\n clientSecretKey,\n };\n });\n return { uri, clientPubkey, complete };\n}\n\nconst hexAlphabet = '0123456789abcdef';\n\nexport function bytesToHex(bytes: Uint8Array): string {\n let s = '';\n for (const b of bytes) s += hexAlphabet[b >> 4] + hexAlphabet[b & 0xf];\n return s;\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n if (hex.length % 2 !== 0) throw new Error('hexToBytes: odd-length hex string');\n const out = new Uint8Array(hex.length / 2);\n for (let i = 0; i < out.length; i++) {\n out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n }\n return out;\n}\n","import { getEventHash, nip19, type Event as NostrEvent, type EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\n/**\n * Subset of `nostr-signer-capacitor-plugin`'s exported `NostrSignerPlugin`\n * that we depend on. Signatures intentionally mirror that library\n * (positional args, per-call `packageName`) so the real plugin is\n * structurally assignable here — and any mock written against this\n * interface is a faithful stand-in. The conformance is enforced by a\n * compile-time guard in `tests/helpers/mockAndroidPlugin.ts`.\n */\nexport interface AndroidSignerAppInfo {\n name: string;\n packageName: string;\n iconUrl?: string;\n}\n\nexport interface AndroidSignerPlugin {\n setPackageName(packageName: string): Promise<void>;\n getInstalledSignerApps(): Promise<{ apps: AndroidSignerAppInfo[] }>;\n getPublicKey(\n packageName?: string,\n permissions?: string,\n ): Promise<{ npub: string; package: string }>;\n signEvent(\n packageName: string,\n eventJson: string,\n id: string,\n npub: string,\n ): Promise<{ signature: string; id: string; event: string }>;\n nip04Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip04Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n}\n\nexport interface AndroidLoginOptions {\n /** The Android package name of the external signer app (e.g. com.greenart7c3.nostrsigner). */\n packageName?: string;\n /** Override the plugin for this call. Falls back to SignerConfig.androidSignerPlugin. */\n plugin?: AndroidSignerPlugin;\n}\n\nexport class AndroidSigner implements ActiveSigner {\n readonly #plugin: AndroidSignerPlugin;\n readonly #packageName: string;\n readonly #npub: string;\n readonly #pubkey: string;\n\n constructor(\n plugin: AndroidSignerPlugin,\n packageName: string,\n npub: string,\n pubkey: string,\n ) {\n this.#plugin = plugin;\n this.#packageName = packageName;\n this.#npub = npub;\n this.#pubkey = pubkey;\n }\n\n async getPublicKey(): Promise<string> {\n return this.#pubkey;\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n const unsigned = { ...event, pubkey: this.#pubkey };\n const eventId = getEventHash(unsigned);\n const result = await this.#plugin.signEvent(\n this.#packageName,\n JSON.stringify(unsigned),\n eventId,\n this.#npub,\n );\n return JSON.parse(result.event) as NostrEvent;\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n}\n\nexport interface AndroidLoginResult {\n signer: AndroidSigner;\n pubkey: string;\n npub: string;\n packageName: string;\n}\n\n/**\n * Render a debuggable summary of what the Android signer plugin returned\n * where an npub was expected. Includes type and length, plus a truncated\n * prefix that preserves the bech32 HRP (so callers can tell `nsec1…` /\n * `nprofile1…` / a raw hex pubkey apart) without leaking the full secret\n * material that an erroneous `nsec` response would carry.\n */\nfunction describeIdentifier(value: unknown): string {\n if (value === null) return 'null';\n if (value === undefined) return 'undefined';\n if (typeof value !== 'string') {\n return `<${typeof value}>`;\n }\n if (value.length === 0) return 'empty string';\n const prefix = value.slice(0, 12);\n const suffix = value.length > 12 ? '…' : '';\n return `\"${prefix}${suffix}\" (length=${value.length})`;\n}\n\nexport async function loginWithAndroidSigner(\n plugin: AndroidSignerPlugin,\n packageName?: string,\n): Promise<AndroidLoginResult> {\n if (packageName) {\n await plugin.setPackageName(packageName);\n }\n const { npub, package: pluginPackage } = await plugin.getPublicKey(packageName);\n const resolvedPackage = pluginPackage || packageName;\n if (!resolvedPackage) {\n throw new Error(\n '@formstr/signer: android signer did not return a package name and none was supplied',\n );\n }\n // Wrap nip19.decode so a bech32 failure (\"Data must be at least 6\n // characters long\", \"Invalid checksum\", ...) surfaces what the plugin\n // actually returned. Without this, callers see an opaque bech32 crash\n // and can't tell whether Amber sent back an empty string, a hex\n // pubkey, an nsec, or something else entirely.\n let decoded: ReturnType<typeof nip19.decode>;\n try {\n decoded = nip19.decode(npub);\n } catch (e) {\n // nostr-tools' nip19 decoder always throws Error instances on bech32\n // failures (\"Data must be at least 6 characters long\", \"Invalid\n // checksum\", \"Unknown prefix\", ...). Pass the message straight through.\n throw new Error(\n `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(npub)}): ${(e as Error).message}`,\n );\n }\n if (decoded.type !== 'npub') {\n throw new Error(\n `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(npub)})`,\n );\n }\n return {\n signer: new AndroidSigner(plugin, resolvedPackage, npub, decoded.data),\n pubkey: decoded.data,\n npub,\n packageName: resolvedPackage,\n };\n}\n"],"mappings":";AAAA,SAAS,gBAAAA,eAAc,SAAAC,cAAa;AACpC,SAAS,gBAAgBC,0BAAyB;;;ACKlD,IAAM,iBAAiB;AAEhB,SAAS,oBAAoB,SAAiB,gBAAgC;AACnF,QAAM,KAAK,MAAsB;AAC/B,QAAI;AACF,aAAO,OAAO,e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AAU,KAAK,OAAK,EAAE,WAAW,MAAM;AAC5D,QAAI,CAAC,QAAS,OAAM,IAAI,MAAM,wCAAwC,MAAM,EAAE;AAC9E,SAAK,gBAAgB;AACrB,SAAK,gBAAgB;AACrB,SAAK,eAAe;AACpB,SAAK,MAAM,EAAE,MAAM,UAAU,QAAQ,CAAC;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OAAO,QAAgC;AAC3C,UAAM,SAAS,UAAU,KAAK;AAC9B,QAAI,CAAC,OAAQ;AACb,SAAK,YAAY,KAAK,UAAU,OAAO,OAAK,EAAE,WAAW,MAAM;AAC/D,SAAK,iBAAiB;AACtB,QAAI,KAAK,kBAAkB,QAAQ;AACjC,WAAK,gBAAgB;AACrB,WAAK,gBAAgB;AACrB,WAAK,eAAe;AAAA,IACtB;AACA,SAAK,MAAM,EAAE,MAAM,UAAU,QAAQ,OAAO,CAAC;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,SAAS,IAA8C;AACrD,SAAK,WAAW,IAAI,EAAE;AACtB,WAAO,MAAM;AACX,WAAK,WAAW,OAAO,EAAE;AAAA,IAC3B;AAAA,EACF;AACF;AAGO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,OAAO,MAAM;AAC1B;","names":["getPublicKey","nip19","ToolsBunkerSigner","getPublicKey","generateSecretKey","getPublicKey","nip19","getPublicKey","nip19","ToolsBunkerSigner"]}
1
+ {"version":3,"sources":["../src/core/signer.ts","../src/core/storage.ts","../src/core/localSigner.ts","../src/nip49.ts","../src/nip07.ts","../src/nip46.ts","../src/nip55.ts"],"sourcesContent":["import { getPublicKey, nip19 } from 'nostr-tools';\nimport { BunkerSigner as ToolsBunkerSigner } from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type {\n ActiveSigner,\n BunkerLoginOptions,\n NostrConnectOptions,\n SignerConfig,\n SignerEvent,\n StoredAccount,\n UnlockOptions,\n} from './types.js';\nimport { localStorageAdapter, type StorageAdapter } from './storage.js';\nimport { LocalSigner } from './localSigner.js';\nimport { decryptNcryptsec, generateAccount } from '../nip49.js';\nimport { ExtensionSigner } from '../nip07.js';\nimport {\n BunkerSigner,\n bytesToHex,\n connectWithBunkerUri,\n hexToBytes,\n initiateNostrConnect,\n} from '../nip46.js';\nimport {\n AndroidSigner,\n loginWithAndroidSigner as connectWithAndroidSigner,\n type AndroidLoginOptions,\n type AndroidSignerAppInfo,\n type AndroidSignerPlugin,\n} from '../nip55.js';\n\nconst ACCOUNTS_KEY = 'accounts';\nconst ACTIVE_KEY = 'active-pubkey';\n\n/**\n * Multi-account Nostr signer with persistence.\n *\n * **Hydration.** The constructor reads previously-saved accounts from\n * the configured storage adapter. Every hydrated account starts\n * **locked**: present in `listAccounts()` and (if it was the active one\n * before) reachable via `getActiveAccount()`, but `getActiveSigner()`\n * returns `null` until the user re-authenticates. The matching\n * `loginWith*` method unlocks the active account.\n *\n * **Locked vs unlocked.** Use `getActiveAccount()` to render UI (\"logged\n * in as @alice\") and `getActiveSigner()` to decide whether the user can\n * actually sign. The pattern is \"show the account always, gate signing\n * on the signer.\"\n *\n * **Events.** Subscribe via `onChange()` to re-render when an account\n * is added, switched, or removed. See {@link SignerEvent}.\n */\nexport class Signer {\n readonly #storage: StorageAdapter;\n readonly #defaultAndroidPlugin: AndroidSignerPlugin | undefined;\n readonly #appMetadata: { name?: string; url?: string; image?: string };\n #accounts: StoredAccount[] = [];\n #activePubkey: string | null = null;\n #activeSigner: ActiveSigner | null = null;\n #listeners = new Set<(event: SignerEvent) => void>();\n\n constructor(config: SignerConfig = {}) {\n this.#storage = config.storage ?? localStorageAdapter(config.storageKeyPrefix);\n this.#defaultAndroidPlugin = config.androidSignerPlugin;\n this.#appMetadata = {\n name: config.appName,\n url: config.appUrl,\n image: config.appImage,\n };\n this.#hydrate();\n }\n\n #hydrate(): void {\n try {\n const raw = this.#storage.get(ACCOUNTS_KEY);\n if (raw) {\n const parsed = JSON.parse(raw);\n if (Array.isArray(parsed)) this.#accounts = parsed as StoredAccount[];\n }\n this.#activePubkey = this.#storage.get(ACTIVE_KEY);\n } catch {\n this.#accounts = [];\n this.#activePubkey = null;\n }\n }\n\n #persistAccounts(): void {\n this.#storage.set(ACCOUNTS_KEY, JSON.stringify(this.#accounts));\n }\n\n #persistActive(): void {\n if (this.#activePubkey) this.#storage.set(ACTIVE_KEY, this.#activePubkey);\n else this.#storage.remove(ACTIVE_KEY);\n }\n\n #upsertAccount(account: StoredAccount): void {\n const idx = this.#accounts.findIndex(a => a.pubkey === account.pubkey);\n if (idx >= 0) this.#accounts[idx] = account;\n else this.#accounts.push(account);\n this.#persistAccounts();\n }\n\n #setActive(account: StoredAccount, signer: ActiveSigner): void {\n const wasDifferent = this.#activePubkey !== null && this.#activePubkey !== account.pubkey;\n this.#activePubkey = account.pubkey;\n this.#activeSigner = signer;\n this.#persistActive();\n this.#emit({ type: wasDifferent ? 'switch' : 'login', account });\n }\n\n #emit(event: SignerEvent): void {\n for (const cb of this.#listeners) {\n try {\n cb(event);\n } catch {\n // listener errors are swallowed so one bad listener can't break others\n }\n }\n }\n\n /**\n * Generate a brand-new nsec, encrypt it with `passphrase` (NIP-49),\n * persist the resulting `ncryptsec` account, and activate it. Returns\n * the new account's `npub` and `ncryptsec` — the caller must surface\n * the `ncryptsec` to the user **immediately** since it is the only way\n * back into the account on a fresh device.\n *\n * @throws if `passphrase` is empty.\n */\n async createAccount(passphrase: string): Promise<{ npub: string; ncryptsec: string }> {\n if (!passphrase) throw new Error('createAccount: passphrase required');\n const { secretKey, pubkey, npub, ncryptsec } = generateAccount(passphrase);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return { npub, ncryptsec };\n }\n\n /**\n * Decrypt an ncryptsec with the user's passphrase, persist the account\n * (overwriting any previous entry for the same pubkey), and activate it.\n *\n * @throws if either argument is empty, or if the passphrase doesn't\n * decrypt the ncryptsec.\n */\n async loginWithNcryptsec(ncryptsec: string, passphrase: string): Promise<StoredAccount> {\n if (!ncryptsec) throw new Error('loginWithNcryptsec: ncryptsec required');\n if (!passphrase) throw new Error('loginWithNcryptsec: passphrase required');\n const secretKey = decryptNcryptsec(ncryptsec, passphrase);\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'ncryptsec', ncryptsec };\n this.#upsertAccount(account);\n this.#setActive(account, new LocalSigner(secretKey));\n return account;\n }\n\n /**\n * Connect via the NIP-07 browser extension exposed at `window.nostr`.\n * The extension prompts the user for permission on first use.\n *\n * @throws if no extension is installed or the user denies the request.\n */\n async loginWithExtension(): Promise<StoredAccount> {\n const extension = new ExtensionSigner();\n const pubkey = await extension.getPublicKey();\n const npub = nip19.npubEncode(pubkey);\n const account: StoredAccount = { npub, pubkey, method: 'extension' };\n this.#upsertAccount(account);\n this.#setActive(account, extension);\n return account;\n }\n\n /**\n * Connect to a NIP-46 remote signer via a `bunker://` URI. Relays are\n * read from the URI itself — no hardcoded fallbacks. Pass a `pool`\n * to reuse an existing relay connection; pass `clientSecretKey` to\n * resume a previous session (the hex from `StoredAccount.nip46`).\n *\n * @throws if the URI is malformed, no relay is reachable, or the\n * remote signer rejects pairing within the implementation's timeout.\n */\n async loginWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n ): Promise<StoredAccount> {\n const result = await connectWithBunkerUri(uri, options);\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Initiate a NIP-46 `nostrconnect://` pairing. Generates a client\n * keypair, publishes a connect request to the supplied `relays`, and\n * waits for a remote signer to pair. Call `options.onUri(uri)` to\n * render the URI as a QR code; the returned promise resolves once\n * pairing completes. Cancel by aborting `options.signal`.\n *\n * @throws if `relays` is empty, the user aborts, the pairing times\n * out, or no signer responds.\n */\n async loginWithNostrConnect(options: NostrConnectOptions): Promise<StoredAccount> {\n if (options.relays.length === 0) {\n throw new Error('loginWithNostrConnect: at least one relay required');\n }\n // Merge per-call metadata over SignerConfig defaults (appName/url/image).\n // `name` is required — Amber (and likely other NIP-55 signer apps) gate\n // the consent UI on having a recognizable client identity in the URI.\n // Without one they will receive the request but never surface\n // approve/deny buttons, leaving the pairing silently stuck.\n const metadata = {\n name: options.metadata?.name ?? this.#appMetadata.name,\n url: options.metadata?.url ?? this.#appMetadata.url,\n image: options.metadata?.image ?? this.#appMetadata.image,\n };\n if (!metadata.name) {\n throw new Error(\n '@formstr/signer: loginWithNostrConnect requires an app name. Set `appName` in createSigner() or pass `metadata.name` to loginWithNostrConnect().',\n );\n }\n const init = initiateNostrConnect({\n relays: options.relays,\n metadata,\n perms: options.perms,\n pool: options.pool,\n onAuth: options.onAuth,\n signal: options.signal,\n timeoutMs: options.timeoutMs,\n onRelayMismatch: options.onRelayMismatch,\n });\n options.onUri(init.uri);\n const result = await init.complete;\n const npub = nip19.npubEncode(result.pubkey);\n const account: StoredAccount = {\n npub,\n pubkey: result.pubkey,\n method: 'nip46',\n nip46: {\n uri: init.uri,\n remoteSignerPubkey: result.pointer.pubkey,\n relays: result.pointer.relays,\n clientSecretKey: bytesToHex(result.clientSecretKey),\n },\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /**\n * Enumerate NIP-55 signer apps installed on the device, via the\n * configured Android plugin (or `plugin` if supplied). Useful for\n * rendering a \"pick your signer\" list — the built-in UI does this\n * automatically when the Android tab is selected.\n *\n * Only meaningful inside a Capacitor Android shell. On web/iOS the\n * configured plugin is typically absent and this throws.\n *\n * @throws if no plugin is configured and none is passed in.\n */\n async listAndroidSignerApps(\n plugin?: AndroidSignerPlugin,\n ): Promise<AndroidSignerAppInfo[]> {\n const p = plugin ?? this.#defaultAndroidPlugin;\n if (!p) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to listAndroidSignerApps)',\n );\n }\n const { apps } = await p.getInstalledSignerApps();\n return apps;\n }\n\n /**\n * Sign in via a NIP-55 Android external signer (Amber, etc). If\n * `options.packageName` is given, that specific signer app is invoked;\n * otherwise the plugin picks a default (typically the only installed\n * signer, or an OS chooser). Pass `options.plugin` to override the\n * configured default for this call.\n *\n * @throws if no plugin is configured, the signer app cannot be\n * resolved to a package name, or the user denies the request.\n */\n async loginWithAndroidSigner(options: AndroidLoginOptions = {}): Promise<StoredAccount> {\n const plugin = options.plugin ?? this.#defaultAndroidPlugin;\n if (!plugin) {\n throw new Error(\n '@formstr/signer: no Android signer plugin configured (pass `androidSignerPlugin` to createSigner or `plugin` to loginWithAndroidSigner)',\n );\n }\n const result = await connectWithAndroidSigner(plugin, options.packageName);\n const account: StoredAccount = {\n npub: result.npub,\n pubkey: result.pubkey,\n method: 'android',\n androidPackageName: result.packageName,\n };\n this.#upsertAccount(account);\n this.#setActive(account, result.signer);\n return account;\n }\n\n /** Snapshot of every persisted account, in insertion order. */\n listAccounts(): StoredAccount[] {\n return [...this.#accounts];\n }\n\n /**\n * The currently selected account, or `null` if none. Present even when\n * the account is locked (no active signer yet). Use this to render\n * \"logged in as @alice\" — pair with {@link getActiveSigner} to decide\n * whether signing is actually available.\n */\n getActiveAccount(): StoredAccount | null {\n if (!this.#activePubkey) return null;\n return this.#accounts.find(a => a.pubkey === this.#activePubkey) ?? null;\n }\n\n /**\n * The unlocked signer for the active account, or `null` if locked.\n * After a fresh page load this is `null` for every account type\n * (passphrase / extension grant / signer-app handshake all need to\n * be redone). Calling the matching `loginWith*` method unlocks it.\n */\n getActiveSigner(): ActiveSigner | null {\n return this.#activeSigner;\n }\n\n /**\n * Silently unlock the active account from persisted state — no user\n * prompt, no fresh pairing. The package already keeps everything it\n * needs to reconstruct the runtime signer on disk; this method is the\n * way to actually use that on cold start instead of re-running each\n * method's first-time login flow.\n *\n * Behavior by method:\n *\n * - `extension`: constructs an {@link ExtensionSigner}, which just\n * proxies to `window.nostr`. No setup roundtrip — individual\n * operations may still prompt depending on the extension's own\n * permission state, but unlock itself does not.\n *\n * - `nip46`: reuses the stored `clientSecretKey` to construct a\n * {@link BunkerSigner} against the stored bunker pubkey + relays\n * via `BunkerSigner.fromBunker`. Deliberately skips the `connect`\n * request — the remote signer (Amber etc.) approved this client\n * pubkey on first pairing and re-sending `connect` is what surfaces\n * a fresh approval prompt every cold start. Requires `options.pool`\n * so the BunkerSigner has somewhere to listen for responses.\n * The cached user pubkey is fed into the wrapper so a follow-up\n * `getPublicKey()` is a memory read, not a relay request.\n *\n * - `android`: constructs an {@link AndroidSigner} directly from the\n * stored `androidPackageName` + `pubkey` + `npub`. Skips the\n * `getPublicKey` content-provider roundtrip that\n * {@link loginWithAndroidSigner} performs and that — on Amber —\n * surfaces as a permission prompt every cold start.\n *\n * - `ncryptsec`: returns `null`. There is no silent path — the user's\n * passphrase isn't (and shouldn't be) persisted. The caller must\n * drive the passphrase prompt and call {@link loginWithNcryptsec}.\n *\n * Returns `null` (without emitting any event or mutating state) when\n * there is no active account, when the account is missing fields\n * required to unlock, when `nip46` is the method but no `pool` was\n * supplied, or when `android` is the method but no plugin is\n * configured. On success emits the same `login` / `switch` event the\n * corresponding `loginWith*` would.\n */\n async unlock(options: UnlockOptions = {}): Promise<ActiveSigner | null> {\n const account = this.getActiveAccount();\n if (!account) return null;\n\n switch (account.method) {\n case 'extension': {\n const signer = new ExtensionSigner();\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'nip46': {\n if (!account.nip46) return null;\n if (!options.pool) return null;\n const { remoteSignerPubkey, relays, clientSecretKey } = account.nip46;\n if (!remoteSignerPubkey || !relays.length || !clientSecretKey) {\n return null;\n }\n const tools = ToolsBunkerSigner.fromBunker(\n hexToBytes(clientSecretKey),\n { pubkey: remoteSignerPubkey, relays, secret: null },\n { pool: options.pool },\n );\n // No tools.connect() — the bunker already approved this client\n // on first pairing; re-sending `connect` is what triggers the\n // cold-start approval prompt we are trying to avoid.\n const signer = new BunkerSigner(tools, account.pubkey);\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'android': {\n if (!account.androidPackageName) return null;\n if (!this.#defaultAndroidPlugin) return null;\n const signer = new AndroidSigner(\n this.#defaultAndroidPlugin,\n account.androidPackageName,\n account.npub,\n account.pubkey,\n );\n this.#setActive(account, signer);\n return signer;\n }\n\n case 'ncryptsec':\n // No silent path — passphrase is not (and must not be) persisted.\n return null;\n }\n }\n\n /**\n * Make `pubkey` the active account. Clears the in-memory signer —\n * the new account starts **locked** even if it was previously\n * unlocked in this session.\n *\n * @throws if `pubkey` does not match any persisted account.\n */\n async switchAccount(pubkey: string): Promise<void> {\n const account = this.#accounts.find(a => a.pubkey === pubkey);\n if (!account) throw new Error(`switchAccount: no account for pubkey ${pubkey}`);\n this.#activePubkey = pubkey;\n this.#activeSigner = null;\n this.#persistActive();\n this.#emit({ type: 'switch', account });\n }\n\n /**\n * Remove an account from storage. `pubkey` defaults to the active\n * account. If the active account is removed, the in-memory signer is\n * cleared. No-op if there is nothing to remove.\n */\n async logout(pubkey?: string): Promise<void> {\n const target = pubkey ?? this.#activePubkey;\n if (!target) return;\n this.#accounts = this.#accounts.filter(a => a.pubkey !== target);\n this.#persistAccounts();\n if (this.#activePubkey === target) {\n this.#activePubkey = null;\n this.#activeSigner = null;\n this.#persistActive();\n }\n this.#emit({ type: 'logout', pubkey: target });\n }\n\n /**\n * Subscribe to account-state changes. Returns an unsubscribe function.\n * Listener errors are swallowed so one bad listener can't break others.\n * See {@link SignerEvent} for the variants.\n */\n onChange(cb: (event: SignerEvent) => void): () => void {\n this.#listeners.add(cb);\n return () => {\n this.#listeners.delete(cb);\n };\n }\n}\n\n/** Convenience wrapper around `new Signer(config)`. */\nexport function createSigner(config: SignerConfig = {}): Signer {\n return new Signer(config);\n}\n","export interface StorageAdapter {\n get(key: string): string | null;\n set(key: string, value: string): void;\n remove(key: string): void;\n}\n\nconst DEFAULT_PREFIX = '@formstr/signer:';\n\nexport function localStorageAdapter(prefix: string = DEFAULT_PREFIX): StorageAdapter {\n const ls = (): Storage | null => {\n try {\n return typeof globalThis !== 'undefined' && globalThis.localStorage\n ? globalThis.localStorage\n : null;\n } catch {\n return null;\n }\n };\n return {\n get(key) {\n try {\n return ls()?.getItem(prefix + key) ?? null;\n } catch {\n return null;\n }\n },\n set(key, value) {\n try {\n ls()?.setItem(prefix + key, value);\n } catch {\n // swallow quota / privacy-mode errors\n }\n },\n remove(key) {\n try {\n ls()?.removeItem(prefix + key);\n } catch {\n // swallow\n }\n },\n };\n}\n","import {\n finalizeEvent,\n getPublicKey,\n nip04,\n nip44,\n type Event as NostrEvent,\n type EventTemplate,\n} from 'nostr-tools';\nimport type { ActiveSigner } from './types.js';\n\n/**\n * ActiveSigner backed by a raw secret key held in memory.\n * The secret key never leaves this object — there is no getter for it.\n */\nexport class LocalSigner implements ActiveSigner {\n readonly #secretKey: Uint8Array;\n\n constructor(secretKey: Uint8Array) {\n this.#secretKey = secretKey;\n }\n\n async getPublicKey(): Promise<string> {\n return getPublicKey(this.#secretKey);\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return finalizeEvent(event, this.#secretKey);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return nip04.encrypt(this.#secretKey, peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return nip04.decrypt(this.#secretKey, peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.encrypt(plaintext, key);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const key = nip44.v2.utils.getConversationKey(this.#secretKey, peerPubkey);\n return nip44.v2.decrypt(ciphertext, key);\n }\n}\n","import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';\nimport { encrypt as nip49Encrypt, decrypt as nip49Decrypt } from 'nostr-tools/nip49';\n\nexport function encryptSecretKey(secretKey: Uint8Array, passphrase: string): string {\n return nip49Encrypt(secretKey, passphrase);\n}\n\nexport function decryptNcryptsec(ncryptsec: string, passphrase: string): Uint8Array {\n return nip49Decrypt(ncryptsec, passphrase);\n}\n\nexport interface GeneratedAccount {\n secretKey: Uint8Array;\n pubkey: string;\n npub: string;\n ncryptsec: string;\n}\n\nexport function generateAccount(passphrase: string): GeneratedAccount {\n const secretKey = generateSecretKey();\n const pubkey = getPublicKey(secretKey);\n const npub = nip19.npubEncode(pubkey);\n const ncryptsec = nip49Encrypt(secretKey, passphrase);\n return { secretKey, pubkey, npub, ncryptsec };\n}\n","import type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\nexport interface WindowNostr {\n getPublicKey(): Promise<string>;\n signEvent(event: EventTemplate): Promise<NostrEvent>;\n getRelays?(): Promise<Record<string, { read: boolean; write: boolean }>>;\n nip04?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n nip44?: {\n encrypt(peerPubkey: string, plaintext: string): Promise<string>;\n decrypt(peerPubkey: string, ciphertext: string): Promise<string>;\n };\n}\n\nexport function getWindowNostr(): WindowNostr {\n const nostr = (globalThis as { nostr?: WindowNostr }).nostr;\n if (!nostr) {\n throw new Error(\n '@formstr/signer: NIP-07 extension not found (globalThis.nostr is undefined)',\n );\n }\n return nostr;\n}\n\nexport class ExtensionSigner implements ActiveSigner {\n async getPublicKey(): Promise<string> {\n return getWindowNostr().getPublicKey();\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n return getWindowNostr().signEvent(event);\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.encrypt(peerPubkey, plaintext);\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip04) throw new Error('NIP-07 extension does not expose nip04');\n return ext.nip04.decrypt(peerPubkey, ciphertext);\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.encrypt(peerPubkey, plaintext);\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const ext = getWindowNostr();\n if (!ext.nip44) throw new Error('NIP-07 extension does not expose nip44');\n return ext.nip44.decrypt(peerPubkey, ciphertext);\n }\n}\n","import { generateSecretKey, getPublicKey } from 'nostr-tools';\nimport {\n BunkerSigner as ToolsBunkerSigner,\n createNostrConnectURI,\n parseBunkerInput,\n type BunkerPointer,\n} from 'nostr-tools/nip46';\nimport type { AbstractSimplePool } from 'nostr-tools/abstract-pool';\nimport type { Event as NostrEvent, EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner, RelayMismatchHandler } from './core/types.js';\n\nexport type { BunkerPointer };\n\n/**\n * Thin wrapper around nostr-tools' BunkerSigner that exposes only the\n * ActiveSigner surface. We keep this layer so callers depend on a stable\n * interface even if we ever swap the underlying implementation.\n *\n * Optionally accepts a `cachedUserPubkey`. When supplied, {@link getPublicKey}\n * returns it without a bunker roundtrip. The user's signer pubkey is fixed\n * for a given paired account, so caching it after the initial `connect` —\n * or feeding it back in from persisted storage on unlock — avoids both a\n * network hop and a potential approval prompt on every cold start. Without\n * a cached value we fall back to asking the bunker, matching the prior\n * behavior.\n */\nexport class BunkerSigner implements ActiveSigner {\n readonly #delegate: ToolsBunkerSigner;\n readonly #cachedUserPubkey: string | null;\n\n constructor(delegate: ToolsBunkerSigner, cachedUserPubkey?: string) {\n this.#delegate = delegate;\n this.#cachedUserPubkey = cachedUserPubkey ?? null;\n }\n\n getPublicKey(): Promise<string> {\n if (this.#cachedUserPubkey !== null) {\n return Promise.resolve(this.#cachedUserPubkey);\n }\n return this.#delegate.getPublicKey();\n }\n signEvent(event: EventTemplate): Promise<NostrEvent> {\n return this.#delegate.signEvent(event);\n }\n nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip04Encrypt(peerPubkey, plaintext);\n }\n nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip04Decrypt(peerPubkey, ciphertext);\n }\n nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n return this.#delegate.nip44Encrypt(peerPubkey, plaintext);\n }\n nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n return this.#delegate.nip44Decrypt(peerPubkey, ciphertext);\n }\n async close(): Promise<void> {\n return this.#delegate.close();\n }\n}\n\nexport interface BunkerLoginOptions {\n /** Custom pool, e.g. for tests. Defaults to a new SimplePool inside nostr-tools. */\n pool?: AbstractSimplePool;\n /** Called when the remote signer needs the user to visit an auth URL. */\n onAuth?: (url: string) => void;\n /** Optional client session keypair (hex bytes). Auto-generated if omitted. */\n clientSecretKey?: Uint8Array;\n /** Notified when the bunker's preferred relays differ from the URI's. */\n onRelayMismatch?: RelayMismatchHandler;\n /**\n * NIP-46 permissions to request as the 3rd `connect` param\n * (e.g. `['sign_event:1', 'nip44_encrypt']`). When omitted, the\n * connect request carries no perms — bunker UIs may then skip the\n * approval prompt entirely, leaving the user with nothing to tap.\n */\n perms?: string[];\n}\n\nasync function fetchBunkerRelays(tools: ToolsBunkerSigner): Promise<string[] | null> {\n try {\n const resp = await tools.sendRequest('get_relays', []);\n const parsed = JSON.parse(resp) as unknown;\n if (Array.isArray(parsed)) {\n return parsed.filter((r): r is string => typeof r === 'string');\n }\n if (typeof parsed === 'object' && parsed !== null) {\n return Object.keys(parsed as Record<string, unknown>);\n }\n return null;\n } catch {\n return null;\n }\n}\n\nfunction relayListsMatch(a: string[], b: string[]): boolean {\n if (a.length !== b.length) return false;\n const sa = [...a].sort();\n const sb = [...b].sort();\n for (let i = 0; i < sa.length; i++) if (sa[i] !== sb[i]) return false;\n return true;\n}\n\nasync function resolveRelayChoice(\n tools: ToolsBunkerSigner,\n userRelays: string[],\n onRelayMismatch: RelayMismatchHandler | undefined,\n): Promise<string[]> {\n if (!onRelayMismatch) return userRelays;\n const bunkerRelays = await fetchBunkerRelays(tools);\n if (!bunkerRelays || relayListsMatch(userRelays, bunkerRelays)) return userRelays;\n const accept = await onRelayMismatch({ userRelays, bunkerRelays });\n return accept ? bunkerRelays : userRelays;\n}\n\nexport interface BunkerConnectResult {\n signer: BunkerSigner;\n pubkey: string;\n pointer: BunkerPointer;\n clientSecretKey: Uint8Array;\n}\n\n/**\n * Connect to a remote signer via a bunker:// URI (or a NIP-05 identifier).\n * Relays come from the URI — there is no fallback default list.\n */\nexport async function connectWithBunkerUri(\n uri: string,\n options: BunkerLoginOptions = {},\n): Promise<BunkerConnectResult> {\n const pointer = await parseBunkerInput(uri);\n if (!pointer) {\n throw new Error('@formstr/signer: invalid bunker URI');\n }\n if (!pointer.relays?.length) {\n throw new Error('@formstr/signer: bunker URI must include at least one relay');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const tools = ToolsBunkerSigner.fromBunker(clientSecretKey, pointer, {\n pool: options.pool,\n onauth: options.onAuth,\n });\n // nostr-tools' BunkerSigner.connect() hardcodes only [pubkey, secret],\n // dropping the optional 3rd `perms` arg defined by NIP-46. Without it\n // bunker UIs (Amber, etc.) have no permissions to authorize and may\n // skip the approval prompt entirely. We send the request directly.\n await tools.sendRequest('connect', [\n pointer.pubkey,\n pointer.secret ?? '',\n (options.perms ?? []).join(','),\n ]);\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n pointer.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...pointer, relays: resolvedRelays },\n clientSecretKey,\n };\n}\n\nexport interface NostrConnectInitOptions {\n /** User-supplied relays. The whole point of the strict-relay rule. */\n relays: string[];\n metadata?: { name?: string; url?: string; image?: string };\n /** Permissions to request (NIP-46 perms list, e.g. [\"sign_event:1\",\"nip44_encrypt\"]). */\n perms?: string[];\n pool?: AbstractSimplePool;\n onAuth?: (url: string) => void;\n /** Override the auto-generated client session keypair. */\n clientSecretKey?: Uint8Array;\n /** Override the auto-generated URI secret. */\n secret?: string;\n /** Abort the pairing wait. */\n signal?: AbortSignal;\n /** Max wait for pairing in ms (default 5 minutes). */\n timeoutMs?: number;\n /** Notified when the bunker's preferred relays differ from the user's. */\n onRelayMismatch?: RelayMismatchHandler;\n}\n\nexport interface NostrConnectInitiation {\n uri: string;\n clientPubkey: string;\n complete: Promise<BunkerConnectResult>;\n}\n\n/**\n * Generate a nostrconnect:// URI and wait for the remote signer to pair.\n * The caller displays the URI (typically as a QR code), and the returned\n * `complete` promise resolves once the signer connects back.\n */\nexport function initiateNostrConnect(options: NostrConnectInitOptions): NostrConnectInitiation {\n if (options.relays.length === 0) {\n throw new Error('@formstr/signer: at least one relay is required for nostrconnect');\n }\n const clientSecretKey = options.clientSecretKey ?? generateSecretKey();\n const clientPubkey = getPublicKey(clientSecretKey);\n const secret = options.secret ?? Math.random().toString(36).slice(2);\n const uri = createNostrConnectURI({\n clientPubkey,\n relays: options.relays,\n secret,\n perms: options.perms,\n name: options.metadata?.name,\n url: options.metadata?.url,\n image: options.metadata?.image,\n });\n const maxWaitOrAbort: number | AbortSignal =\n options.signal ?? options.timeoutMs ?? 300_000;\n // skipSwitchRelays:true — keep the caller-supplied relays authoritative,\n // never silently swap to whatever the bunker prefers.\n const complete = ToolsBunkerSigner.fromURI(\n clientSecretKey,\n uri,\n { pool: options.pool, onauth: options.onAuth, skipSwitchRelays: true },\n maxWaitOrAbort,\n ).then(async (tools) => {\n const pubkey = await tools.getPublicKey();\n const resolvedRelays = await resolveRelayChoice(\n tools,\n options.relays,\n options.onRelayMismatch,\n );\n return {\n signer: new BunkerSigner(tools, pubkey),\n pubkey,\n pointer: { ...tools.bp, relays: resolvedRelays },\n clientSecretKey,\n };\n });\n return { uri, clientPubkey, complete };\n}\n\nconst hexAlphabet = '0123456789abcdef';\n\nexport function bytesToHex(bytes: Uint8Array): string {\n let s = '';\n for (const b of bytes) s += hexAlphabet[b >> 4] + hexAlphabet[b & 0xf];\n return s;\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n if (hex.length % 2 !== 0) throw new Error('hexToBytes: odd-length hex string');\n const out = new Uint8Array(hex.length / 2);\n for (let i = 0; i < out.length; i++) {\n out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n }\n return out;\n}\n","import { getEventHash, nip19, type Event as NostrEvent, type EventTemplate } from 'nostr-tools';\nimport type { ActiveSigner } from './core/types.js';\n\n/**\n * Subset of `nostr-signer-capacitor-plugin`'s exported `NostrSignerPlugin`\n * that we depend on. Signatures intentionally mirror that library\n * (positional args, per-call `packageName`) so the real plugin is\n * structurally assignable here — and any mock written against this\n * interface is a faithful stand-in. The conformance is enforced by a\n * compile-time guard in `tests/helpers/mockAndroidPlugin.ts`.\n */\nexport interface AndroidSignerAppInfo {\n name: string;\n packageName: string;\n iconUrl?: string;\n}\n\nexport interface AndroidSignerPlugin {\n setPackageName(packageName: string): Promise<void>;\n getInstalledSignerApps(): Promise<{ apps: AndroidSignerAppInfo[] }>;\n getPublicKey(\n packageName?: string,\n permissions?: string,\n ): Promise<{ npub: string; package: string }>;\n signEvent(\n packageName: string,\n eventJson: string,\n id: string,\n npub: string,\n ): Promise<{ signature: string; id: string; event: string }>;\n nip04Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip04Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Encrypt(\n packageName: string,\n plainText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n nip44Decrypt(\n packageName: string,\n encryptedText: string,\n id: string,\n pubKey: string,\n npub: string,\n ): Promise<{ result: string; id: string }>;\n}\n\nexport interface AndroidLoginOptions {\n /** The Android package name of the external signer app (e.g. com.greenart7c3.nostrsigner). */\n packageName?: string;\n /** Override the plugin for this call. Falls back to SignerConfig.androidSignerPlugin. */\n plugin?: AndroidSignerPlugin;\n}\n\nexport class AndroidSigner implements ActiveSigner {\n readonly #plugin: AndroidSignerPlugin;\n readonly #packageName: string;\n readonly #npub: string;\n readonly #pubkey: string;\n\n constructor(\n plugin: AndroidSignerPlugin,\n packageName: string,\n npub: string,\n pubkey: string,\n ) {\n this.#plugin = plugin;\n this.#packageName = packageName;\n this.#npub = npub;\n this.#pubkey = pubkey;\n }\n\n async getPublicKey(): Promise<string> {\n return this.#pubkey;\n }\n\n async signEvent(event: EventTemplate): Promise<NostrEvent> {\n const unsigned = { ...event, pubkey: this.#pubkey };\n const eventId = getEventHash(unsigned);\n const result = await this.#plugin.signEvent(\n this.#packageName,\n JSON.stringify(unsigned),\n eventId,\n this.#npub,\n );\n return JSON.parse(result.event) as NostrEvent;\n }\n\n async nip04Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip04Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip04Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Encrypt(peerPubkey: string, plaintext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Encrypt(\n this.#packageName,\n plaintext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n\n async nip44Decrypt(peerPubkey: string, ciphertext: string): Promise<string> {\n const { result } = await this.#plugin.nip44Decrypt(\n this.#packageName,\n ciphertext,\n '',\n peerPubkey,\n this.#npub,\n );\n return result;\n }\n}\n\nexport interface AndroidLoginResult {\n signer: AndroidSigner;\n pubkey: string;\n npub: string;\n packageName: string;\n}\n\n/**\n * Render a debuggable summary of what the Android signer plugin returned\n * where an npub was expected. Includes type and length, plus a truncated\n * prefix that preserves the bech32 HRP (so callers can tell `nsec1…` /\n * `nprofile1…` / a raw hex pubkey apart) without leaking the full secret\n * material that an erroneous `nsec` response would carry.\n */\nfunction describeIdentifier(value: unknown): string {\n if (value === null) return 'null';\n if (value === undefined) return 'undefined';\n if (typeof value !== 'string') {\n return `<${typeof value}>`;\n }\n if (value.length === 0) return 'empty string';\n const prefix = value.slice(0, 12);\n const suffix = value.length > 12 ? '…' : '';\n return `\"${prefix}${suffix}\" (length=${value.length})`;\n}\n\n/**\n * Lowercase 32-byte hex pubkey shape. NIP-55 nominally returns an `npub`\n * in the `signature` extra, but recent Amber builds put the raw hex\n * pubkey there instead — we accept either form and normalize internally.\n */\nconst HEX_PUBKEY_RE = /^[0-9a-f]{64}$/i;\n\nexport async function loginWithAndroidSigner(\n plugin: AndroidSignerPlugin,\n packageName?: string,\n): Promise<AndroidLoginResult> {\n if (packageName) {\n await plugin.setPackageName(packageName);\n }\n const { npub: rawIdentifier, package: pluginPackage } =\n await plugin.getPublicKey(packageName);\n const resolvedPackage = pluginPackage || packageName;\n if (!resolvedPackage) {\n throw new Error(\n '@formstr/signer: android signer did not return a package name and none was supplied',\n );\n }\n const { pubkey, npub } = normalizeAndroidIdentifier(rawIdentifier);\n return {\n signer: new AndroidSigner(plugin, resolvedPackage, npub, pubkey),\n pubkey,\n npub,\n packageName: resolvedPackage,\n };\n}\n\n/**\n * Normalize whatever the Android signer plugin handed us in the `npub`\n * slot into a `{ pubkey, npub }` pair. Accepts either:\n * - a 32-byte lowercase hex pubkey (newer Amber builds return this),\n * - a bech32 `npub1…` (the original NIP-55 spec shape).\n * Throws a debuggable error for anything else, including the preview\n * of what was actually received so the caller can triage.\n */\nfunction normalizeAndroidIdentifier(\n rawIdentifier: unknown,\n): { pubkey: string; npub: string } {\n if (typeof rawIdentifier === 'string' && HEX_PUBKEY_RE.test(rawIdentifier)) {\n const pubkey = rawIdentifier.toLowerCase();\n return { pubkey, npub: nip19.npubEncode(pubkey) };\n }\n // Wrap nip19.decode so a bech32 failure (\"Data must be at least 6\n // characters long\", \"Invalid checksum\", ...) surfaces what the plugin\n // actually returned. Without this, callers see an opaque bech32 crash\n // and can't tell whether Amber sent back an empty string, an nsec, or\n // something else entirely.\n let decoded: ReturnType<typeof nip19.decode>;\n try {\n decoded = nip19.decode(rawIdentifier as string);\n } catch (e) {\n // nostr-tools' nip19 decoder always throws Error instances on bech32\n // failures (\"Data must be at least 6 characters long\", \"Invalid\n // checksum\", \"Unknown prefix\", ...). Pass the message straight through.\n throw new Error(\n `@formstr/signer: android signer returned an undecodable identifier (got ${describeIdentifier(rawIdentifier)}): ${(e as Error).message}`,\n );\n }\n if (decoded.type !== 'npub') {\n throw new Error(\n `@formstr/signer: android signer returned a non-npub identifier (type=${decoded.type}, got ${describeIdentifier(rawIdentifier)})`,\n );\n }\n return { pubkey: decoded.data, npub: rawIdentifier as string };\n}\n"],"mappings":";AAAA,SAAS,gBAAAA,eAAc,SAAAC,cAAa;AACpC,SAAS,gBAAgBC,0BAAyB;;;ACKlD,IAAM,iBAAiB;AAEhB,SAAS,oBAAoB,SAAiB,gBAAgC;AACnF,QAAM,KAAK,MAAsB;AAC/B,QAAI;AACF,aAAO,OAAO,eAAe,eAAe,WAAW,eACnD,WAAW,eACX;AAAA,IACN,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AAAA,IACL,IAAI,KAAK;AACP,UAAI;AACF,eAAO,GAAG,GAAG,QAAQ,SAAS,GAAG,KAAK;AAAA,MACxC,QAAQ;AACN,eAAO;AAAA,MACT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AA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,cAAc,QAA+B;AACjD,UAAM,UAAU,KAAK,UAAU,KAAK,OAAK,EAAE,WAAW,MAAM;AAC5D,QAAI,CAAC,QAAS,OAAM,IAAI,MAAM,wCAAwC,MAAM,EAAE;AAC9E,SAAK,gBAAgB;AACrB,SAAK,gBAAgB;AACrB,SAAK,eAAe;AACpB,SAAK,MAAM,EAAE,MAAM,UAAU,QAAQ,CAAC;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OAAO,QAAgC;AAC3C,UAAM,SAAS,UAAU,KAAK;AAC9B,QAAI,CAAC,OAAQ;AACb,SAAK,YAAY,KAAK,UAAU,OAAO,OAAK,EAAE,WAAW,MAAM;AAC/D,SAAK,iBAAiB;AACtB,QAAI,KAAK,kBAAkB,QAAQ;AACjC,WAAK,gBAAgB;AACrB,WAAK,gBAAgB;AACrB,WAAK,eAAe;AAAA,IACtB;AACA,SAAK,MAAM,EAAE,MAAM,UAAU,QAAQ,OAAO,CAAC;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,SAAS,IAA8C;AACrD,SAAK,WAAW,IAAI,EAAE;AACtB,WAAO,MAAM;AACX,WAAK,WAAW,OAAO,EAAE;AAAA,IAC3B;AAAA,EACF;AACF;AAGO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,OAAO,MAAM;AAC1B;","names":["getPublicKey","nip19","ToolsBunkerSigner","getPublicKey","generateSecretKey","getPublicKey","nip19","getPublicKey","nip19","ToolsBunkerSigner"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formstr/signer",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Nostr signer with login UI for NIP-07, NIP-46, NIP-49 (ncryptsec), and NIP-55",
5
5
  "license": "MIT",
6
6
  "author": "Abhay <toabhayraizada@gmail.com>",