mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
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.
This commit is contained in:
10 files changed
+262
-33
No files matched your search
@@ -133,7 +133,7 @@ export function useCurrentUser() {
|
||||
);
|
||||
}
|
||||
case 'extension':
|
||||
return new NUser(login.type, login.pubkey, new NBrowserSignerBtc());
|
||||
return new NUser(login.type, login.pubkey, new NBrowserSignerBtc({ pubkey: login.pubkey }));
|
||||
default:
|
||||
throw new Error(`Unsupported login type: ${login.type}`);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import type { NostrSigner } from '@nostrify/types';
|
||||
import { NSecSigner, NBrowserSigner, NConnectSigner } from '@nostrify/nostrify';
|
||||
import { NSecSigner, NConnectSigner } from '@nostrify/nostrify';
|
||||
import type { NConnectSignerOpts } from '@nostrify/nostrify';
|
||||
|
||||
import { signPsbtLocal } from '@/lib/bitcoin';
|
||||
import { Nip07Signer } from '@/lib/Nip07Signer';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// BtcSigner interface
|
||||
@@ -56,28 +57,28 @@ export class NSecSignerBtc extends NSecSigner implements BtcSigner {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Extends `NBrowserSigner` with NIP-07 `window.nostr.signPsbt()` support.
|
||||
* Extends the template's `Nip07Signer` with NIP-07 `window.nostr.signPsbt()`
|
||||
* support. Like `Nip07Signer`, it acts as the login's account (`pubkey`)
|
||||
* through the extension's `getSigner` where the extension offers it.
|
||||
*
|
||||
* Calls the extension's `signPsbt` method if available. If the extension does
|
||||
* not expose `signPsbt`, an error is thrown with a user-friendly message.
|
||||
*/
|
||||
export class NBrowserSignerBtc extends NBrowserSigner implements BtcSigner {
|
||||
constructor(opts?: { timeout?: number }) {
|
||||
export class NBrowserSignerBtc extends Nip07Signer implements BtcSigner {
|
||||
constructor(opts?: { pubkey?: string; timeout?: number }) {
|
||||
super(opts);
|
||||
}
|
||||
|
||||
async signPsbt(psbtHex: string): Promise<string> {
|
||||
// `awaitNostr` is TypeScript-private but JavaScript-public at runtime.
|
||||
const nostr = await (this as unknown as { awaitNostr(): Promise<Record<string, unknown>> }).awaitNostr();
|
||||
const signer = await this.signer();
|
||||
|
||||
if (typeof nostr.signPsbt !== 'function') {
|
||||
if (typeof signer.signPsbt !== 'function') {
|
||||
throw new Error(
|
||||
"Your browser extension doesn't support sending Bitcoin. Try a different extension, or log in with your secret key.",
|
||||
);
|
||||
}
|
||||
|
||||
const signPsbt = nostr.signPsbt as (hex: string) => Promise<string>;
|
||||
return signPsbt(psbtHex);
|
||||
return signer.signPsbt(psbtHex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user