nostr: add Nostr sign module

environments/review-nostr-mach-rhfii1/deployments/4088
Alex Gleason 2023-10-04 17:07:02 -05:00
rodzic 1b199dfd9f
commit 84ca2a269d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
1 zmienionych plików z 52 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,52 @@
import {
type Event,
type EventTemplate,
generatePrivateKey,
getPublicKey as _getPublicKey,
finishEvent,
nip04 as _nip04,
} from 'nostr-tools';
/** localStorage key for the Nostr private key (if not using NIP-07). */
const LOCAL_KEY = 'soapbox:nostr:privateKey';
/** Get the private key from the browser, or generate one. */
const getPrivateKey = (): string => {
const local = localStorage.getItem(LOCAL_KEY);
if (!local) {
const key = generatePrivateKey();
localStorage.setItem(LOCAL_KEY, key);
return key;
}
return local;
};
/** Get the user's public key from NIP-07, or generate one. */
async function getPublicKey(): Promise<string> {
return window.nostr ? window.nostr.getPublicKey() : _getPublicKey(getPrivateKey());
}
/** Sign an event with NIP-07, or the locally generated key. */
async function signEvent<K extends number>(event: EventTemplate<K>): Promise<Event<K>> {
return window.nostr ? window.nostr.signEvent(event) as Promise<Event<K>> : finishEvent(event, getPrivateKey()) ;
}
/** Crypto function with NIP-07, or the local key. */
const nip04 = {
/** Encrypt with NIP-07, or the local key. */
encrypt: async (pubkey: string, content: string) => {
return window.nostr?.nip04
? window.nostr.nip04.encrypt(pubkey, content)
: _nip04.encrypt(getPrivateKey(), pubkey, content);
},
/** Decrypt with NIP-07, or the local key. */
decrypt: async (pubkey: string, content: string) => {
return window.nostr?.nip04
? window.nostr.nip04.decrypt(pubkey, content)
: _nip04.decrypt(getPrivateKey(), pubkey, content);
},
};
export { getPublicKey, signEvent, nip04 };