session.ts (1447B)
1 import { nostr_public_key_from_secret, nostr_signer_nip07_get } from "@radroots/nostr"; 2 import { 3 dropSession, 4 loginWithNip01, 5 loginWithNip07, 6 loginWithNip46, 7 loginWithNip55, 8 loginWithPubkey, 9 pubkey, 10 } from "@welshman/app"; 11 12 export type NostrLoginNip46Options = { 13 pubkey: string; 14 client_secret: string; 15 signer_pubkey: string; 16 relays: string[]; 17 }; 18 19 export type NostrLoginNip55Options = { 20 pubkey: string; 21 signer: string; 22 }; 23 24 export const nostr_login_nip01 = (secret_key: string): string => { 25 loginWithNip01(secret_key); 26 return nostr_public_key_from_secret(secret_key); 27 }; 28 29 export const nostr_login_nip07 = async (): Promise<string | undefined> => { 30 const nip07 = nostr_signer_nip07_get(); 31 if (!nip07) return undefined; 32 const pubkey_val = nip07.getPublicKey(); 33 if (!pubkey_val) return undefined; 34 loginWithNip07(pubkey_val); 35 return pubkey_val; 36 }; 37 38 export const nostr_login_nip46 = (opts: NostrLoginNip46Options): void => { 39 loginWithNip46(opts.pubkey, opts.client_secret, opts.signer_pubkey, opts.relays); 40 }; 41 42 export const nostr_login_nip55 = (opts: NostrLoginNip55Options): void => { 43 loginWithNip55(opts.pubkey, opts.signer); 44 }; 45 46 export const nostr_login_pubkey = (pubkey_val: string): void => { 47 loginWithPubkey(pubkey_val); 48 }; 49 50 export const nostr_logout = (): void => { 51 const pubkey_val = pubkey.get(); 52 if (pubkey_val) dropSession(pubkey_val); 53 };