Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outbox #71

Merged
merged 15 commits into from
Jun 27, 2024
11 changes: 7 additions & 4 deletions components/display-products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NostrEvent } from "../utils/types/types";
import {
ProductContext,
ProfileMapContext,
FollowsContext,
FollowsAndRelaysContext,
} from "../utils/context/context";
import ProductCard from "./utility-components/product-card";
import DisplayProductModal from "./display-product-modal";
Expand Down Expand Up @@ -38,7 +38,7 @@ const DisplayEvents = ({
const [isProductsLoading, setIsProductLoading] = useState(true);
const productEventContext = useContext(ProductContext);
const profileMapContext = useContext(ProfileMapContext);
const followsContext = useContext(FollowsContext);
const followsAndRelaysContext = useContext(FollowsAndRelaysContext);
const [focusedProduct, setFocusedProduct] = useState(""); // product being viewed in modal
const [showModal, setShowModal] = useState(false);
const [isLoadingMore, setIsLoadingMore] = useState<boolean>(false);
Expand All @@ -58,8 +58,11 @@ const DisplayEvents = ({
let parsedProductData: ProductData[] = [];
sortedProductEvents.forEach((event) => {
if (wotFilter) {
if (!followsContext.isLoading && followsContext.followList) {
const followList = followsContext.followList;
if (
!followsAndRelaysContext.isLoading &&
followsAndRelaysContext.followList
) {
const followList = followsAndRelaysContext.followList;
if (followList.length > 0 && followList.includes(event.pubkey)) {
let parsedData = parseTags(event);
if (parsedData) parsedProductData.push(parsedData);
Expand Down
60 changes: 50 additions & 10 deletions components/sign-in/SignInModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useContext } from "react";
import {
Modal,
ModalContent,
Expand All @@ -13,6 +13,7 @@ import {
setLocalStorageDataOnSignIn,
validateNSecKey,
} from "@/components/utility/nostr-helper-functions";
import { FollowsAndRelaysContext } from "../../utils/context/context";
import { getPublicKey, nip19 } from "nostr-tools";
import CryptoJS from "crypto-js";
import { useRouter } from "next/router";
Expand All @@ -31,16 +32,36 @@ export default function SignInModal({

const [showNsecSignIn, setShowNsecSignIn] = useState(false);

const followsAndRelaysContext = useContext(FollowsAndRelaysContext);

const router = useRouter();

const startExtensionLogin = async () => {
try {
// @ts-ignore
var pk = await window.nostr.getPublicKey();
setLocalStorageDataOnSignIn({
signInMethod: "extension",
pubkey: pk,
});
if (
!followsAndRelaysContext.isLoading &&
followsAndRelaysContext.relayList.length >= 0 &&
followsAndRelaysContext.readRelayList &&
followsAndRelaysContext.writeRelayList
) {
const allRelays = [
...followsAndRelaysContext.relayList,
...followsAndRelaysContext.readRelayList,
...followsAndRelaysContext.writeRelayList,
];
setLocalStorageDataOnSignIn({
signInMethod: "extension",
pubkey: pk,
relays: allRelays,
});
} else {
setLocalStorageDataOnSignIn({
signInMethod: "extension",
pubkey: pk,
});
}
onClose();
} catch (error) {
alert("Extension sign in failed!");
Expand All @@ -67,11 +88,30 @@ export default function SignInModal({
onClose(); // avoids tree walker issue by closing modal
}, 500);

setLocalStorageDataOnSignIn({
signInMethod: "nsec",
pubkey: pk,
encryptedPrivateKey: encryptedPrivateKey,
});
if (
!followsAndRelaysContext.isLoading &&
followsAndRelaysContext.relayList.length >= 0 &&
followsAndRelaysContext.readRelayList &&
followsAndRelaysContext.writeRelayList
) {
const allRelays = [
...followsAndRelaysContext.relayList,
...followsAndRelaysContext.readRelayList,
...followsAndRelaysContext.writeRelayList,
];
setLocalStorageDataOnSignIn({
signInMethod: "nsec",
pubkey: pk,
encryptedPrivateKey: encryptedPrivateKey,
relays: allRelays,
});
} else {
setLocalStorageDataOnSignIn({
signInMethod: "nsec",
pubkey: pk,
encryptedPrivateKey: encryptedPrivateKey,
});
}
}
} else {
alert(
Expand Down
9 changes: 5 additions & 4 deletions components/utility-components/shopstr-slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { useState, useEffect, useContext } from "react";
import { Button } from "@nextui-org/react";
import { Slider } from "@nextui-org/react";
import { useTheme } from "next-themes";
import { FollowsContext } from "../../utils/context/context";
import { FollowsAndRelaysContext } from "../../utils/context/context";
import { getLocalStorageData } from "../../components/utility/nostr-helper-functions";
import { SHOPSTRBUTTONCLASSNAMES } from "../../components/utility/STATIC-VARIABLES";

const ShopstrSlider = () => {
const { theme, setTheme } = useTheme();

const followsContext = useContext(FollowsContext);
const followsAndRelaysContext = useContext(FollowsAndRelaysContext);

const [wot, setWot] = useState(getLocalStorageData().wot);
const [wotIsChanged, setWotIsChanged] = useState(false);
Expand All @@ -33,8 +33,9 @@ const ShopstrSlider = () => {
label="Minimum Follower Count:"
showSteps={true}
maxValue={
!followsContext.isLoading && followsContext.firstDegreeFollowsLength
? followsContext.firstDegreeFollowsLength
!followsAndRelaysContext.isLoading &&
followsAndRelaysContext.firstDegreeFollowsLength
? followsAndRelaysContext.firstDegreeFollowsLength
: wot
}
minValue={1}
Expand Down
95 changes: 76 additions & 19 deletions components/utility/nostr-helper-functions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import * as CryptoJS from "crypto-js";
import {
finalizeEvent,
getPublicKey,
nip04,
nip19,
nip98,
SimplePool,
} from "nostr-tools";
import { finalizeEvent, nip04, nip19, nip98, SimplePool } from "nostr-tools";
import axios from "axios";
import { NostrEvent } from "@/utils/types/types";
import { ProductFormValues } from "@/pages/api/nostr/post-event";
Expand All @@ -15,7 +8,8 @@
values: ProductFormValues,
passphrase: string,
) {
const { signInMethod, userPubkey, relays } = getLocalStorageData();
const { signInMethod, userPubkey, relays, writeRelays } =
getLocalStorageData();
const summary = values.find(([key]) => key === "summary")?.[1] || "";

const dValue = values.find(([key]) => key === "d")?.[1] || undefined;
Expand Down Expand Up @@ -65,11 +59,22 @@

const pool = new SimplePool();

await Promise.any(pool.publish(relays, signedEvent));
await Promise.any(pool.publish(relays, signedRecEvent));
await Promise.any(pool.publish(relays, signedHandlerEvent));
const allWriteRelays = [...writeRelays, ...relays];
const blastrRelay = "wss://relay.mutinywallet.com";
if (!allWriteRelays.includes(blastrRelay)) {
Fixed Show fixed Hide fixed
allWriteRelays.push(blastrRelay);
}

await Promise.any(pool.publish(allWriteRelays, signedEvent));
await Promise.any(pool.publish(allWriteRelays, signedRecEvent));
await Promise.any(pool.publish(allWriteRelays, signedHandlerEvent));
return signedEvent;
} else {
const allWriteRelays = [...writeRelays, ...relays];
const blastrRelay = "wss://relay.mutinywallet.com";
if (!allWriteRelays.includes(blastrRelay)) {
Fixed Show fixed Hide fixed
allWriteRelays.push(blastrRelay);
}
const res = await axios({
method: "POST",
url: "/api/nostr/post-event",
Expand All @@ -84,7 +89,7 @@
// kind: 30018,
tags: updatedValues,
content: summary,
relays: relays,
relays: allWriteRelays,
},
});
return {
Expand Down Expand Up @@ -144,7 +149,7 @@
encryptedMessageEvent: EncryptedMessageEvent,
passphrase?: string,
) {
const { signInMethod, relays } = getLocalStorageData();
const { signInMethod, relays, writeRelays } = getLocalStorageData();
let signedEvent;
if (signInMethod === "extension") {
signedEvent = await window.nostr.signEvent(encryptedMessageEvent);
Expand All @@ -154,15 +159,20 @@
signedEvent = finalizeEvent(encryptedMessageEvent, senderPrivkey);
}
const pool = new SimplePool();
await Promise.any(pool.publish(relays, signedEvent));
const allWriteRelays = [...writeRelays, ...relays];
const blastrRelay = "wss://relay.mutinywallet.com";
if (!allWriteRelays.includes(blastrRelay)) {
Fixed Show fixed Hide fixed
allWriteRelays.push(blastrRelay);
}
await Promise.any(pool.publish(allWriteRelays, signedEvent));
}

export async function finalizeAndSendNostrEvent(
nostrEvent: NostrEvent,
passphrase?: string,
) {
try {
const { signInMethod, relays } = getLocalStorageData();
const { signInMethod, relays, writeRelays } = getLocalStorageData();
let signedEvent;
if (signInMethod === "extension") {
signedEvent = await window.nostr.signEvent(nostrEvent);
Expand All @@ -172,7 +182,12 @@
signedEvent = finalizeEvent(nostrEvent, senderPrivkey);
}
const pool = new SimplePool();
await Promise.any(pool.publish(relays, signedEvent));
const allWriteRelays = [...writeRelays, ...relays];
const blastrRelay = "wss://relay.mutinywallet.com";
if (!allWriteRelays.includes(blastrRelay)) {
Fixed Show fixed Hide fixed
allWriteRelays.push(blastrRelay);
}
await Promise.any(pool.publish(allWriteRelays, signedEvent));
} catch (e: any) {
console.log("Error: ", e);
alert("Failed to send event: " + e.message);
Expand Down Expand Up @@ -296,6 +311,8 @@
userPubkey: "userPubkey",
encryptedPrivateKey: "encryptedPrivateKey",
relays: "relays",
readRelays: "readRelays",
writeRelays: "writeRelays",
mints: "mints",
tokens: "tokens",
history: "history",
Expand All @@ -307,13 +324,17 @@
pubkey,
encryptedPrivateKey,
relays,
readRelays,
writeRelays,
mints,
wot,
}: {
signInMethod: string;
pubkey: string;
encryptedPrivateKey?: string;
relays?: string[];
readRelays?: string[];
writeRelays?: string[];
mints?: string[];
wot?: number;
}) => {
Expand All @@ -333,7 +354,7 @@
localStorage.setItem(
LOCALSTORAGECONSTANTS.relays,
JSON.stringify(
relays
relays && relays.length != 0
? relays
: [
"wss://relay.damus.io",
Expand All @@ -343,6 +364,16 @@
),
);

localStorage.setItem(
LOCALSTORAGECONSTANTS.readRelays,
JSON.stringify(readRelays && readRelays.length != 0 ? readRelays : []),
);

localStorage.setItem(
LOCALSTORAGECONSTANTS.writeRelays,
JSON.stringify(writeRelays && writeRelays.length != 0 ? writeRelays : []),
);

localStorage.setItem(
LOCALSTORAGECONSTANTS.mints,
JSON.stringify(mints ? mints : ["https://mint.minibits.cash/Bitcoin"]),
Expand All @@ -364,6 +395,8 @@
userNPub: string;
userPubkey: string;
relays: string[];
readRelays: string[];
writeRelays: string[];
mints: string[];
tokens: [];
history: [];
Expand All @@ -377,6 +410,8 @@
let userNPub;
let userPubkey;
let relays;
let readRelays;
let writeRelays;
let mints;
let tokens;
let history;
Expand Down Expand Up @@ -413,14 +448,31 @@

if (!relays) {
relays = defaultRelays;
localStorage.setItem("relays", JSON.stringify(relays));
} else {
try {
relays = (JSON.parse(relays) as string[]).filter((r) => r);
} catch {
relays = defaultRelays;
localStorage.setItem("relays", JSON.stringify(relays));
}
}
localStorage.setItem("relays", JSON.stringify(relays));

readRelays = localStorage.getItem(LOCALSTORAGECONSTANTS.readRelays)
? (
JSON.parse(
localStorage.getItem(LOCALSTORAGECONSTANTS.readRelays) as string,
) as string[]
).filter((r) => r)
: [];

writeRelays = localStorage.getItem(LOCALSTORAGECONSTANTS.writeRelays)
? (
JSON.parse(
localStorage.getItem(LOCALSTORAGECONSTANTS.writeRelays) as string,
) as string[]
).filter((r) => r)
: [];

mints = localStorage.getItem(LOCALSTORAGECONSTANTS.mints)
? JSON.parse(localStorage.getItem("mints") as string)
Expand Down Expand Up @@ -455,6 +507,8 @@
userNPub: userNPub as string,
userPubkey: userPubkey as string,
relays: relays || [],
readRelays: readRelays || [],
writeRelays: writeRelays || [],
mints,
tokens: tokens || [],
history: history || [],
Expand All @@ -472,6 +526,9 @@
localStorage.removeItem(LOCALSTORAGECONSTANTS.userNPub);
localStorage.removeItem(LOCALSTORAGECONSTANTS.userPubkey);
localStorage.removeItem(LOCALSTORAGECONSTANTS.encryptedPrivateKey);
localStorage.removeItem(LOCALSTORAGECONSTANTS.relays);
localStorage.removeItem(LOCALSTORAGECONSTANTS.readRelays);
localStorage.removeItem(LOCALSTORAGECONSTANTS.writeRelays);

window.dispatchEvent(new Event("storage"));
};
Expand Down
Loading
Loading