This commit is contained in:
zmeyer44 2023-10-18 18:51:17 -04:00
parent f89535cf2d
commit 4a35a785a7
4 changed files with 19 additions and 18 deletions

View File

@ -33,7 +33,7 @@ const ConfirmModal = dynamic(() => import("@/components/Modals/Confirm"), {
export default function Header({ event }: { event: NDKEvent }) { export default function Header({ event }: { event: NDKEvent }) {
const { currentUser } = useCurrentUser(); const { currentUser } = useCurrentUser();
const modal = useModal(); const modal = useModal();
const { ndk, signer } = useNDK(); const { ndk } = useNDK();
const [checkingPayment, setCheckingPayment] = useState(false); const [checkingPayment, setCheckingPayment] = useState(false);
const [hasValidPayment, setHasValidPayment] = useState(false); const [hasValidPayment, setHasValidPayment] = useState(false);
const [syncingUsers, setSyncingUsers] = useState(false); const [syncingUsers, setSyncingUsers] = useState(false);
@ -88,11 +88,11 @@ export default function Header({ event }: { event: NDKEvent }) {
} }
} }
async function handleSyncUsers() { async function handleSyncUsers() {
if (!event || !ndk || !signer) return; if (!event || !ndk) return;
setSyncingUsers(true); setSyncingUsers(true);
try { try {
console.log("handleSyncUsers"); console.log("handleSyncUsers");
await updateListUsersFromZaps(ndk, event.tagId(), rawEvent, signer); await updateListUsersFromZaps(ndk, event.tagId(), rawEvent);
toast.success("Users Synced!"); toast.success("Users Synced!");
} catch (err) { } catch (err) {
console.log("error syncing users", err); console.log("error syncing users", err);

View File

@ -60,6 +60,7 @@ export default function CreateList() {
kind: 30001, kind: 30001,
tags: tags, tags: tags,
}); });
console.log("EVENT Created", event);
if (event && getTagValues("subscriptions", event.tags)) { if (event && getTagValues("subscriptions", event.tags)) {
await getSigner(new NDKList(ndk, event.rawEvent())) await getSigner(new NDKList(ndk, event.rawEvent()))
.then((delegateSigner) => .then((delegateSigner) =>

View File

@ -101,15 +101,16 @@ function generateContent(
async function generateTags(mainSigner: NDKSigner, opts: ISaveOpts = {}) { async function generateTags(mainSigner: NDKSigner, opts: ISaveOpts = {}) {
const mainUser = await mainSigner.user(); const mainUser = await mainSigner.user();
const tags = [ const tags = [
["p", mainUser.hexpubkey], ["p", mainUser.pubkey],
["client", "flockstr"], ["client", "flockstr"],
]; ];
if (opts.associatedEvent) { if (opts.associatedEvent) {
// TODO: This is trivially reversable; better to encrypt it or hash it with the hexpubkey const encodedEvent = opts.associatedEvent.encode();
const hashedEventReference = await getHashedKeyName( console.log("encodedEvent", encodedEvent);
opts.associatedEvent.encode(), // TODO: This is trivially reversable; better to encrypt it or hash it with the pubkey
); const hashedEventReference = await getHashedKeyName(encodedEvent);
console.log("hashedEventReference", hashedEventReference);
tags.push(["e", hashedEventReference]); tags.push(["e", hashedEventReference]);
} }
@ -139,7 +140,7 @@ export async function saveEphemeralSigner(
content: generateContent(targetSigner, opts), content: generateContent(targetSigner, opts),
tags: await generateTags(mainSigner, opts), tags: await generateTags(mainSigner, opts),
} as NostrEvent); } as NostrEvent);
event.pubkey = mainUser.hexpubkey; event.pubkey = mainUser.pubkey;
await event.encrypt(mainUser, mainSigner); await event.encrypt(mainUser, mainSigner);
await event.publish(); await event.publish();
@ -152,7 +153,7 @@ export async function saveEphemeralSigner(
content: JSON.stringify(opts.keyProfile), content: JSON.stringify(opts.keyProfile),
tags: [] as NDKTag[], tags: [] as NDKTag[],
} as NostrEvent); } as NostrEvent);
event.pubkey = user.hexpubkey; event.pubkey = user.pubkey;
await event.sign(targetSigner); await event.sign(targetSigner);
await event.publish(); await event.publish();
} }

View File

@ -104,7 +104,6 @@ export async function updateListUsersFromZaps(
ndk: NDK, ndk: NDK,
tagId: string, tagId: string,
event: NostrEvent, event: NostrEvent,
signer: NDKSigner,
) { ) {
const SECONDS_IN_MONTH = 2_628_000; const SECONDS_IN_MONTH = 2_628_000;
const SECONDS_IN_YEAR = SECONDS_IN_MONTH * 365; const SECONDS_IN_YEAR = SECONDS_IN_MONTH * 365;
@ -122,7 +121,7 @@ export async function updateListUsersFromZaps(
([pubkey, relay, petname, expiryUnix]) => ([pubkey, relay, petname, expiryUnix]) =>
parseInt(expiryUnix ?? "0") > unixTimeNowInSeconds(), parseInt(expiryUnix ?? "0") > unixTimeNowInSeconds(),
); );
const newUsers: string[] = []; const newUsers: string[] = currentUsers.map(([pub]) => pub as string);
for (const paymentInvoice of paymentInvoices) { for (const paymentInvoice of paymentInvoices) {
if ( if (
@ -149,7 +148,7 @@ export async function updateListUsersFromZaps(
// Send old codes to user // Send old codes to user
} }
} }
await sendCodesToNewUsers(ndk, newUsers, tagId, signer); await sendCodesToNewUsers(ndk, newUsers, tagId, event);
// Add self // Add self
console.log("Adding self"); console.log("Adding self");
@ -172,11 +171,10 @@ async function sendCodesToNewUsers(
ndk: NDK, ndk: NDK,
users: string[], users: string[],
tagId: string, tagId: string,
signer_: NDKSigner, event: NostrEvent,
) { ) {
console.log("sendCodesToNewUsers", users, signer_); const signer = await findEphemeralSigner(ndk, ndk.signer!, {
const signer = await findEphemeralSigner(ndk, signer_, { associatedEventNip19: new NDKEvent(ndk, event).encode(),
associatedEventNip19: tagId,
}); });
console.log("Signer", signer); console.log("Signer", signer);
if (!signer) return; if (!signer) return;
@ -188,9 +186,10 @@ async function sendCodesToNewUsers(
}); });
const codes: [string, string][] = []; const codes: [string, string][] = [];
for (const message of Array.from(messages)) { for (const message of Array.from(messages)) {
await message.decrypt(delegate, signer); await message.decrypt();
codes.push([getTagValues("e", message.tags) ?? "", message.content]); codes.push([getTagValues("e", message.tags) ?? "", message.content]);
} }
console.log("codes", codes);
for (const user of users) { for (const user of users) {
for (const [event, code] of codes) { for (const [event, code] of codes) {