This commit is contained in:
Kieran 2023-05-04 12:57:59 +01:00
parent 190f2f467c
commit 0069b73914
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
6 changed files with 1040 additions and 39 deletions

View File

@ -10,6 +10,7 @@
"@lightninglabs/lnc-web": "^0.2.3-alpha",
"@noble/hashes": "^1.2.0",
"@noble/secp256k1": "^1.7.0",
"@nostr-dev-kit/ndk": "^0.3.10",
"@protobufjs/base64": "^1.1.2",
"@reduxjs/toolkit": "^1.9.1",
"@scure/bip32": "^1.1.5",

View File

@ -1,7 +1,11 @@
import { useSyncExternalStore } from "react";
import { useMemo, useSyncExternalStore } from "react";
import NDK, { NDKEvent, NDKFilter } from "@nostr-dev-kit/ndk";
import { RequestBuilder, System } from "System";
import { EmptySnapshot, NoteStore, StoreSnapshot } from "System/NoteCollection";
import { unwrap } from "Util";
import useLogin from "./useLogin";
import { TaggedRawEvent } from "@snort/nostr";
const useRequestBuilder = <TStore extends NoteStore, TSnapshot = ReturnType<TStore["getSnapshotData"]>>(
type: { new (): TStore },
@ -43,4 +47,35 @@ const useRequestBuilder = <TStore extends NoteStore, TSnapshot = ReturnType<TSto
);
};
export default useRequestBuilder;
export const NostrNDK = new NDK({ explicitRelayUrls: ["wss://relay.snort.social"] });
NostrNDK.connect();
const useNDK = <TStore extends NoteStore, TSnapshot = ReturnType<TStore["getSnapshotData"]>>(
type: { new (): TStore },
rb: RequestBuilder | null
) => {
const store = useMemo(() => {
return new type();
}, []);
if (rb) {
const sub = NostrNDK.subscribe({
...rb.build(),
});
sub.on("event", (ev, relay, forSub) => {
if (forSub !== sub) return;
store.add({
...(ev as NDKEvent).rawEvent(),
relays: relay ? [relay.url] : [],
} as TaggedRawEvent);
});
sub.start();
return useSyncExternalStore<StoreSnapshot<TSnapshot>>(
c => store.hook(c),
() => store.snapshot as StoreSnapshot<TSnapshot>
);
}
return EmptySnapshot as StoreSnapshot<TSnapshot>;
};
export default useNDK;

View File

@ -70,7 +70,7 @@ export default function Layout() {
}
}, [pub]);
useEffect(() => {
/*useEffect(() => {
if (relays) {
(async () => {
for (const [k, v] of Object.entries(relays.item)) {
@ -83,7 +83,7 @@ export default function Layout() {
}
})();
}
}, [relays]);
}, [relays]);*/
function setTheme(theme: "light" | "dark") {
const elm = document.documentElement;

View File

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

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,16 @@ 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));
const { sig } = await barrierNip07(() => unwrap(window.nostr).signEvent(ev));
return {
...ev,
sig,
};
} else if (this.#privateKey) {
return await eb.buildAndSign(this.#privateKey);
} else {
@ -106,11 +96,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 +110,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 {

1001
yarn.lock

File diff suppressed because it is too large Load Diff