snort/packages/system/src/impl/nip7.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-07-05 10:41:47 +00:00
import { WorkQueueItem, processWorkQueue, barrierQueue, unwrap } from "@snort/shared";
2023-08-17 18:54:14 +00:00
import { EventSigner, HexKey, NostrEvent } from "..";
2023-07-05 10:41:47 +00:00
const Nip7Queue: Array<WorkQueueItem> = [];
processWorkQueue(Nip7Queue);
export class Nip7Signer implements EventSigner {
2023-09-05 13:57:50 +00:00
get supports(): string[] {
return ["nip04"];
}
2023-09-05 14:16:50 +00:00
2023-07-22 18:37:46 +00:00
init(): Promise<void> {
return Promise.resolve();
}
2023-07-06 14:00:28 +00:00
2023-07-22 18:37:46 +00:00
async getPubKey(): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
2023-07-05 10:41:47 +00:00
}
2023-07-22 18:37:46 +00:00
return await barrierQueue(Nip7Queue, () => unwrap(window.nostr).getPublicKey());
}
2023-07-05 10:41:47 +00:00
2023-07-22 18:37:46 +00:00
async nip4Encrypt(content: string, key: string): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
2023-07-05 10:41:47 +00:00
}
2023-07-22 18:37:46 +00:00
return await barrierQueue(Nip7Queue, () =>
2023-07-24 14:30:21 +00:00
unwrap(window.nostr?.nip04?.encrypt).call(window.nostr?.nip04, key, content),
2023-07-22 18:37:46 +00:00
);
}
async nip4Decrypt(content: string, otherKey: string): Promise<string> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
2023-07-05 10:41:47 +00:00
}
2023-07-22 18:37:46 +00:00
return await barrierQueue(Nip7Queue, () =>
2023-07-24 14:30:21 +00:00
unwrap(window.nostr?.nip04?.decrypt).call(window.nostr?.nip04, otherKey, content),
2023-07-22 18:37:46 +00:00
);
}
2023-08-17 18:54:14 +00:00
async nip44Encrypt(content: string, key: string): Promise<string> {
throw new Error("Method not implemented.");
}
async nip44Decrypt(content: string, otherKey: string): Promise<string> {
throw new Error("Method not implemented.");
}
2023-07-22 18:37:46 +00:00
async sign(ev: NostrEvent): Promise<NostrEvent> {
if (!window.nostr) {
throw new Error("Cannot use NIP-07 signer, not found!");
2023-07-05 10:41:47 +00:00
}
2024-04-23 12:08:23 +00:00
return await barrierQueue(Nip7Queue, async () => {
const signed = await unwrap(window.nostr).signEvent(ev);
return {
...ev,
sig: signed.sig,
};
});
2023-07-22 18:37:46 +00:00
}
}