Merge pull request 'nostr package: NIP-07' (#503) from nostr-package-window-nostr-object into main

Reviewed-on: #503
This commit is contained in:
2023-05-04 12:56:51 +00:00
12 changed files with 150 additions and 36 deletions

View File

@ -109,8 +109,8 @@ export default function LoginPage() {
}
async function doNip07Login() {
const relays = "getRelays" in window.nostr ? await window.nostr.getRelays() : undefined;
const pubKey = await window.nostr.getPublicKey();
const relays = "getRelays" in unwrap(window.nostr) ? await unwrap(window.nostr?.getRelays)() : undefined;
const pubKey = await unwrap(window.nostr).getPublicKey();
LoginStore.loginWithPubkey(pubKey, relays);
}

View File

@ -17,20 +17,6 @@ import { unwrap } from "Util";
import { EventBuilder } from "./EventBuilder";
import { EventExt } from "./EventExt";
declare global {
interface Window {
nostr: {
getPublicKey: () => Promise<HexKey>;
signEvent: (event: RawEvent) => Promise<RawEvent>;
getRelays: () => Promise<Record<string, { read: boolean; write: boolean }>>;
nip04: {
encrypt: (pubkey: HexKey, content: string) => Promise<string>;
decrypt: (pubkey: HexKey, content: string) => Promise<string>;
};
};
}
}
interface Nip7QueueItem {
next: () => Promise<unknown>;
resolve(v: unknown): void;
@ -91,12 +77,12 @@ export class EventPublisher {
async #sign(eb: EventBuilder) {
if (this.#hasNip07 && !this.#privateKey) {
const nip7PubKey = await barrierNip07(() => window.nostr.getPublicKey());
const nip7PubKey = await barrierNip07(() => unwrap(window.nostr).getPublicKey());
if (nip7PubKey !== this.#pubKey) {
throw new Error("Can't sign event, NIP-07 pubkey does not match");
}
const ev = eb.build();
return await barrierNip07(() => window.nostr.signEvent(ev));
return await barrierNip07(() => unwrap(window.nostr).signEvent(ev));
} else if (this.#privateKey) {
return await eb.buildAndSign(this.#privateKey);
} else {
@ -106,11 +92,11 @@ export class EventPublisher {
async nip4Encrypt(content: string, key: HexKey) {
if (this.#hasNip07 && !this.#privateKey) {
const nip7PubKey = await barrierNip07(() => window.nostr.getPublicKey());
const nip7PubKey = await barrierNip07(() => unwrap(window.nostr).getPublicKey());
if (nip7PubKey !== this.#pubKey) {
throw new Error("Can't encrypt content, NIP-07 pubkey does not match");
}
return await barrierNip07(() => window.nostr.nip04.encrypt(key, content));
return await barrierNip07(() => unwrap(window.nostr?.nip04?.encrypt)(key, content));
} else if (this.#privateKey) {
return await EventExt.encryptData(content, key, this.#privateKey);
} else {
@ -120,7 +106,7 @@ export class EventPublisher {
async nip4Decrypt(content: string, otherKey: HexKey) {
if (this.#hasNip07 && !this.#privateKey) {
return await barrierNip07(() => window.nostr.nip04.decrypt(otherKey, content));
return await barrierNip07(() => unwrap(window.nostr?.nip04?.decrypt)(otherKey, content));
} else if (this.#privateKey) {
return await EventExt.decryptDm(content, this.#privateKey, otherKey);
} else {