This commit is contained in:
2023-07-22 19:37:46 +01:00
parent 74a3cd7754
commit b1f93c9fd8
65 changed files with 1115 additions and 1029 deletions

View File

@ -6,56 +6,55 @@ const Nip7Queue: Array<WorkQueueItem> = [];
processWorkQueue(Nip7Queue);
declare global {
interface Window {
nostr?: {
getPublicKey: () => Promise<HexKey>;
signEvent: <T extends NostrEvent>(event: T) => Promise<T>;
interface Window {
nostr?: {
getPublicKey: () => Promise<HexKey>;
signEvent: <T extends NostrEvent>(event: T) => Promise<T>;
getRelays?: () => Promise<Record<string, { read: boolean; write: boolean }>>;
getRelays?: () => Promise<Record<string, { read: boolean; write: boolean }>>;
nip04?: {
encrypt?: (pubkey: HexKey, plaintext: string) => Promise<string>;
decrypt?: (pubkey: HexKey, ciphertext: string) => Promise<string>;
};
};
}
nip04?: {
encrypt?: (pubkey: HexKey, plaintext: string) => Promise<string>;
decrypt?: (pubkey: HexKey, ciphertext: string) => Promise<string>;
};
};
}
}
export class Nip7Signer implements EventSigner {
init(): Promise<void> {
return Promise.resolve();
}
init(): Promise<void> {
return Promise.resolve();
}
async getPubKey(): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
return await barrierQueue(Nip7Queue, () => unwrap(window.nostr).getPublicKey());
async getPubKey(): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
return await barrierQueue(Nip7Queue, () => unwrap(window.nostr).getPublicKey());
}
async nip4Encrypt(content: string, key: string): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
return await barrierQueue(Nip7Queue, () =>
unwrap(window.nostr?.nip04?.encrypt).call(window.nostr?.nip04, key, content)
);
async nip4Encrypt(content: string, key: string): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
return await barrierQueue(Nip7Queue, () =>
unwrap(window.nostr?.nip04?.encrypt).call(window.nostr?.nip04, key, content)
);
}
async nip4Decrypt(content: string, otherKey: string): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
return await barrierQueue(Nip7Queue, () =>
unwrap(window.nostr?.nip04?.decrypt).call(window.nostr?.nip04, otherKey, content)
);
async nip4Decrypt(content: string, otherKey: string): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
return await barrierQueue(Nip7Queue, () =>
unwrap(window.nostr?.nip04?.decrypt).call(window.nostr?.nip04, otherKey, content)
);
}
async sign(ev: NostrEvent): Promise<NostrEvent> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
return await barrierQueue(Nip7Queue, () => unwrap(window.nostr).signEvent(ev));
async sign(ev: NostrEvent): Promise<NostrEvent> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
}
}
return await barrierQueue(Nip7Queue, () => unwrap(window.nostr).signEvent(ev));
}
}