fix build error

This commit is contained in:
ennmichael
2023-04-17 22:07:54 +02:00
parent d754551ebb
commit 1e5811b117
3 changed files with 10 additions and 24 deletions

View File

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

View File

@ -17,20 +17,6 @@ import { unwrap } from "Util";
import { EventBuilder } from "./EventBuilder"; import { EventBuilder } from "./EventBuilder";
import { EventExt } from "./EventExt"; 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 { interface Nip7QueueItem {
next: () => Promise<unknown>; next: () => Promise<unknown>;
resolve(v: unknown): void; resolve(v: unknown): void;
@ -88,12 +74,12 @@ export class EventPublisher {
async #sign(eb: EventBuilder) { async #sign(eb: EventBuilder) {
if (this.#hasNip07 && !this.#privateKey) { if (this.#hasNip07 && !this.#privateKey) {
const nip7PubKey = await barrierNip07(() => window.nostr.getPublicKey()); const nip7PubKey = await barrierNip07(() => unwrap(window.nostr).getPublicKey());
if (nip7PubKey !== this.#pubKey) { if (nip7PubKey !== this.#pubKey) {
throw new Error("Can't sign event, NIP-07 pubkey does not match"); throw new Error("Can't sign event, NIP-07 pubkey does not match");
} }
const ev = eb.build(); const ev = eb.build();
return await barrierNip07(() => window.nostr.signEvent(ev)); return await barrierNip07(() => unwrap(window.nostr).signEvent(ev));
} else if (this.#privateKey) { } else if (this.#privateKey) {
return await eb.buildAndSign(this.#privateKey); return await eb.buildAndSign(this.#privateKey);
} else { } else {
@ -103,11 +89,11 @@ export class EventPublisher {
async nip4Encrypt(content: string, key: HexKey) { async nip4Encrypt(content: string, key: HexKey) {
if (this.#hasNip07 && !this.#privateKey) { if (this.#hasNip07 && !this.#privateKey) {
const nip7PubKey = await barrierNip07(() => window.nostr.getPublicKey()); const nip7PubKey = await barrierNip07(() => unwrap(window.nostr).getPublicKey());
if (nip7PubKey !== this.#pubKey) { if (nip7PubKey !== this.#pubKey) {
throw new Error("Can't encrypt content, NIP-07 pubkey does not match"); 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) { } else if (this.#privateKey) {
return await EventExt.encryptData(content, key, this.#privateKey); return await EventExt.encryptData(content, key, this.#privateKey);
} else { } else {
@ -117,7 +103,7 @@ export class EventPublisher {
async nip4Decrypt(content: string, otherKey: HexKey) { async nip4Decrypt(content: string, otherKey: HexKey) {
if (this.#hasNip07 && !this.#privateKey) { 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) { } else if (this.#privateKey) {
return await EventExt.decryptDm(content, this.#privateKey, otherKey); return await EventExt.decryptDm(content, this.#privateKey, otherKey);
} else { } else {

View File

@ -7,9 +7,9 @@ declare global {
getPublicKey: () => Promise<PublicKey> getPublicKey: () => Promise<PublicKey>
signEvent: <T extends RawEvent>(event: Unsigned<T>) => Promise<T> signEvent: <T extends RawEvent>(event: Unsigned<T>) => Promise<T>
getRelays?: () => Promise<{ getRelays?: () => Promise<
[url: string]: { read: boolean; write: boolean } Record<string, { read: boolean; write: boolean }>
}> >
nip04?: { nip04?: {
encrypt?: (pubkey: PublicKey, plaintext: string) => Promise<string> encrypt?: (pubkey: PublicKey, plaintext: string) => Promise<string>