|
| 1 | +/** |
| 2 | + * BunkerContext — an IdentityContext-compatible interface backed by a NIP-46 bunker. |
| 3 | + * |
| 4 | + * Instead of holding the secret key locally, all signing is delegated to a |
| 5 | + * remote bunker via encrypted Nostr relay messages. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * BUNKER_URI=bunker://pk?relay=wss://... |
| 9 | + * or |
| 10 | + * BUNKER_URI=bunker://pk?relay=wss://...&secret=clienthex |
| 11 | + */ |
| 12 | + |
| 13 | +import { BunkerSigner } from 'nostr-tools/nip46' |
| 14 | +import { useWebSocketImplementation, SimplePool } from 'nostr-tools/pool' |
| 15 | +import { generateSecretKey, getPublicKey } from 'nostr-tools/pure' |
| 16 | +import { npubEncode } from 'nostr-tools/nip19' |
| 17 | +import WebSocket from 'ws' |
| 18 | +import type { Event as NostrEvent, EventTemplate } from 'nostr-tools' |
| 19 | +import type { PublicIdentity, SignFn } from './types.js' |
| 20 | + |
| 21 | +useWebSocketImplementation(WebSocket) |
| 22 | + |
| 23 | +export interface BunkerConfig { |
| 24 | + pubkey: string |
| 25 | + relay: string |
| 26 | + secret?: string // client secret key hex |
| 27 | +} |
| 28 | + |
| 29 | +/** Parse a bunker:// URI */ |
| 30 | +export function parseBunkerUri(uri: string): BunkerConfig { |
| 31 | + // bunker://<pubkey>?relay=<url>&secret=<hex> |
| 32 | + const url = new URL(uri) |
| 33 | + const pubkey = url.hostname || url.pathname.replace('//', '') |
| 34 | + const relay = url.searchParams.get('relay') |
| 35 | + const secret = url.searchParams.get('secret') ?? undefined |
| 36 | + if (!pubkey || !relay) { |
| 37 | + throw new Error('Invalid bunker URI: missing pubkey or relay') |
| 38 | + } |
| 39 | + return { pubkey, relay, secret } |
| 40 | +} |
| 41 | + |
| 42 | +export class BunkerContext { |
| 43 | + private signer: BunkerSigner |
| 44 | + private pool: SimplePool |
| 45 | + private pubkeyHex: string | undefined |
| 46 | + private clientSk: Uint8Array |
| 47 | + |
| 48 | + private constructor(signer: BunkerSigner, pool: SimplePool, clientSk: Uint8Array) { |
| 49 | + this.signer = signer |
| 50 | + this.pool = pool |
| 51 | + this.clientSk = clientSk |
| 52 | + } |
| 53 | + |
| 54 | + /** Connect to a remote bunker. Blocks until the connection is established. */ |
| 55 | + static async connect(uri: string, timeoutMs = 15_000): Promise<BunkerContext> { |
| 56 | + const config = parseBunkerUri(uri) |
| 57 | + const clientSk = config.secret |
| 58 | + ? Buffer.from(config.secret, 'hex') |
| 59 | + : generateSecretKey() |
| 60 | + const pool = new SimplePool() |
| 61 | + |
| 62 | + const signer = BunkerSigner.fromBunker( |
| 63 | + clientSk, |
| 64 | + { pubkey: config.pubkey, relays: [config.relay], secret: null }, |
| 65 | + { pool }, |
| 66 | + ) |
| 67 | + |
| 68 | + // Connect and verify |
| 69 | + await signer.connect() |
| 70 | + await signer.ping() |
| 71 | + |
| 72 | + const ctx = new BunkerContext(signer, pool, clientSk) |
| 73 | + ctx.pubkeyHex = await signer.getPublicKey() |
| 74 | + return ctx |
| 75 | + } |
| 76 | + |
| 77 | + /** The remote identity's npub */ |
| 78 | + get activeNpub(): string { |
| 79 | + return npubEncode(this.pubkeyHex!) |
| 80 | + } |
| 81 | + |
| 82 | + /** The remote identity's hex pubkey */ |
| 83 | + get activePublicKeyHex(): string { |
| 84 | + return this.pubkeyHex! |
| 85 | + } |
| 86 | + |
| 87 | + /** Sign an event via the remote bunker */ |
| 88 | + getSigningFunction(): SignFn { |
| 89 | + return async (template: EventTemplate): Promise<NostrEvent> => { |
| 90 | + return this.signer.signEvent(template) as unknown as NostrEvent |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** List identities — bunker mode only has one (the remote key) */ |
| 95 | + listIdentities(): PublicIdentity[] { |
| 96 | + return [{ npub: this.activeNpub, purpose: 'bunker', index: 0 }] |
| 97 | + } |
| 98 | + |
| 99 | + /** NIP-44 encrypt via the remote bunker */ |
| 100 | + async nip44Encrypt(recipientPubkey: string, plaintext: string): Promise<string> { |
| 101 | + return this.signer.nip44Encrypt(recipientPubkey, plaintext) |
| 102 | + } |
| 103 | + |
| 104 | + /** NIP-44 decrypt via the remote bunker */ |
| 105 | + async nip44Decrypt(senderPubkey: string, ciphertext: string): Promise<string> { |
| 106 | + return this.signer.nip44Decrypt(senderPubkey, ciphertext) |
| 107 | + } |
| 108 | + |
| 109 | + /** Clean up */ |
| 110 | + destroy(): void { |
| 111 | + this.signer.close() |
| 112 | + this.pool.destroy() |
| 113 | + this.clientSk.fill(0) |
| 114 | + } |
| 115 | +} |
0 commit comments