Files
mkstack/src/lib/nip07.ts
T
Alex Gleason 9d43fdbcc3 Use window.nostr.connect, getSigner and disconnect where the extension offers them
Din's and Tenna's NIP-07 providers add three optional methods, after NIP-46's
names. Logging in through an extension now asks connect() for an account
(skipping the Welcome back preview, since the extension shows the account),
each extension login signs through getSigner(pubkey) so switching accounts
in the extension doesn't change who the app signs as, and logging out calls
disconnect(pubkey). Each is feature-detected on its own; extensions without
them work as before. The onchain-bitcoin skill's NBrowserSignerBtc now
builds on the same signer.
2026-11-28 16:50:29 -06:00

76 lines
2.9 KiB
TypeScript

import type { NostrSigner } from '@nostrify/types';
/**
* The NIP-07 provider at `window.nostr`, with the optional multi-account
* additions some providers (Din, Tenna) offer. Each may be missing, so check
* for it before calling it:
*
* - `connect()` asks the user to give the site an account and resolves with
* a signer for it. It always asks, so the provider shows the account.
* - `getSigner(pubkey)` resolves, without asking, with a signer pinned to an
* account the site holds, whatever account the provider itself is acting
* as. It rejects with a `NotFoundError` DOMException if the site doesn't
* hold that account.
* - `disconnect(pubkey)` lets the site's account go, without asking.
*
* A signer from `connect` or `getSigner` keeps its account, and its
* `getPublicKey()` answers without asking, even once the site no longer
* holds the account. Its other methods then reject with `NotFoundError`.
*/
export interface Nip07Provider extends NostrSigner {
connect?(): Promise<NostrSigner>;
getSigner?(pubkey: string): Promise<NostrSigner>;
disconnect?(pubkey: string): Promise<void>;
signPsbt?(psbtHex: string): Promise<string>;
}
/** The provider at `window.nostr`, if one has been injected. */
export function getNip07Provider(): Nip07Provider | undefined {
const provider = (globalThis as { nostr?: unknown }).nostr;
if (provider && typeof (provider as Nip07Provider).getPublicKey === 'function') {
return provider as Nip07Provider;
}
return undefined;
}
/** Whether the provider can ask the user for an account to give the site. */
export function hasConnect(
provider: Nip07Provider | undefined,
): provider is Nip07Provider & Required<Pick<Nip07Provider, 'connect'>> {
return typeof provider?.connect === 'function';
}
/** Whether the provider can hand out signers pinned to an account. */
export function hasGetSigner(
provider: Nip07Provider | undefined,
): provider is Nip07Provider & Required<Pick<Nip07Provider, 'getSigner'>> {
return typeof provider?.getSigner === 'function';
}
/** Whether the provider lets the site give an account back. */
export function hasDisconnect(
provider: Nip07Provider | undefined,
): provider is Nip07Provider & Required<Pick<Nip07Provider, 'disconnect'>> {
return typeof provider?.disconnect === 'function';
}
/** Wait for `window.nostr`, polling every 100 ms up to `timeout`. */
export function awaitNip07Provider(timeout = 3000): Promise<Nip07Provider> {
const provider = getNip07Provider();
if (provider) return Promise.resolve(provider);
const deadline = Date.now() + timeout;
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const provider = getNip07Provider();
if (provider) {
clearInterval(interval);
resolve(provider);
} else if (Date.now() >= deadline) {
clearInterval(interval);
reject(new Error('Browser extension not available'));
}
}, 100);
});
}