better ransom

This commit is contained in:
zmeyer44 2023-10-18 16:08:01 -04:00
parent c1787c98ae
commit eecc3875fe
5 changed files with 55 additions and 42 deletions

View File

@ -67,12 +67,12 @@ export default function Header({ event }: { event: NDKEvent }) {
}, [isMember, currentUser]);
async function handleCheckPayment() {
if (!event || !currentUser) return;
if (!event || !currentUser || !ndk) return;
setCheckingPayment(true);
console.log("Checking payment");
try {
const result = await checkPayment(
ndk!,
ndk,
event.tagId(),
currentUser.pubkey,
rawEvent,
@ -88,11 +88,11 @@ export default function Header({ event }: { event: NDKEvent }) {
}
}
async function handleSyncUsers() {
if (!event) return;
if (!event || !ndk) return;
setSyncingUsers(true);
try {
console.log("handleSyncUsers");
await updateListUsersFromZaps(ndk!, event.tagId(), rawEvent);
await updateListUsersFromZaps(ndk, event.tagId(), rawEvent);
toast.success("Users Synced!");
} catch (err) {
console.log("error syncing users", err);
@ -171,39 +171,43 @@ export default function Header({ event }: { event: NDKEvent }) {
</Button>
</>
)}
{subscriptionsEnabled && !isMember && (
<Button
onClick={() =>
modal?.show(
<ConfirmModal
title={`Subscribe to ${title}`}
onConfirm={handleSendZap}
ctaBody={
<>
<span>Zap to Subscribe</span>
<HiOutlineLightningBolt className="h-4 w-4" />
</>
}
>
<p className="text-muted-forground">
{`Pay ${priceInBTC} BTC (${formatNumber(
btcToSats(priceInBTC),
)} sats) for year long access until ${formatDate(
new Date(
new Date().setFullYear(
new Date().getFullYear() + 1,
{subscriptionsEnabled &&
!isMember &&
(hasValidPayment ? (
<Button variant={"outline"}>Pending Sync</Button>
) : (
<Button
onClick={() =>
modal?.show(
<ConfirmModal
title={`Subscribe to ${title}`}
onConfirm={handleSendZap}
ctaBody={
<>
<span>Zap to Subscribe</span>
<HiOutlineLightningBolt className="h-4 w-4" />
</>
}
>
<p className="text-muted-forground">
{`Pay ${priceInBTC} BTC (${formatNumber(
btcToSats(priceInBTC),
)} sats) for year long access until ${formatDate(
new Date(
new Date().setFullYear(
new Date().getFullYear() + 1,
),
),
),
"MMM Do, YYYY",
)}`}
</p>
</ConfirmModal>,
)
}
>
Subscribe
</Button>
)}
"MMM Do, YYYY",
)}`}
</p>
</ConfirmModal>,
)
}
>
Subscribe
</Button>
))}
</div>
</div>

View File

@ -3,7 +3,7 @@ import FormModal from "./FormModal";
import { z } from "zod";
import { useModal } from "@/app/_providers/modal/provider";
import { toast } from "sonner";
import { generateRandomString } from "@/lib/nostr";
import { generateRandomString, randomId } from "@/lib/nostr";
import { satsToBtc } from "@/lib/utils";
import useCurrentUser from "@/lib/hooks/useCurrentUser";
import { useNDK } from "@/app/_providers/ndk";
@ -34,7 +34,7 @@ export default function CreateList() {
const { getSigner } = useSigner()!;
async function handleSubmit(data: CreateListType) {
setIsLoading(true);
const random = generateRandomString();
const random = randomId();
const tags = [
["title", data.title],
["name", data.title],

View File

@ -7,7 +7,7 @@ import NDK, {
type NostrEvent,
NDKUser,
} from "@nostr-dev-kit/ndk";
import { generateRandomString, encryptMessage } from "@/lib/nostr";
import { generateRandomString, encryptMessage, randomId } from "@/lib/nostr";
import { unixTimeNowInSeconds } from "@/lib/nostr/dates";
import { getTagsValues } from "@/lib/nostr/utils";
@ -155,7 +155,7 @@ export async function createList(
tags: [
["name", title],
["description", description ?? ""],
["d", generateRandomString()],
["d", randomId()],
],
});
}

View File

@ -39,7 +39,6 @@ export async function checkPayment(
pubkey: string,
event: NostrEvent,
) {
console.log("Running check payment");
const paymentEvent = await ndk.fetchEvent({
kinds: [9735],
["#a"]: [tagId],

View File

@ -142,7 +142,17 @@ function create32ByteBuffer(inputString: string) {
const buffer = Buffer.from(hash, "hex");
return buffer;
}
export function randomId() {
// @ts-ignore
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11)
.replace(/[018]/g, (c: any) =>
(
c ^
(crypto.getRandomValues(new Uint8Array(1))[0]! & (15 >> (c / 4)))
).toString(16),
)
.slice(0, 8) as string;
}
export function generateRandomString() {
return crypto.randomBytes(32).toString("hex");
}