Skip to content

Commit

Permalink
change gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
scobru committed Dec 13, 2023
1 parent 0f1bff5 commit f83ae0b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 113 deletions.
2 changes: 1 addition & 1 deletion packages/nextjs/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const Header = () => {
<Image alt="SE2 logo" className="cursor-pointer" fill src="/logo.svg" />
</div> */}
<div className="flex flex-col">
<span className="font-bold leading-tight text-3xl">NOSTR3</span>
<span className="font-extrabold leading-tight text-3xl">NOSTR3</span>
<span className="text-xs">Link Web3 to nostr</span>
</div>
</Link>
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"tailwindcss": "^3.3.3",
"type-fest": "^4.6.0",
"typescript": "^5.1.6",
"vercel": "^32.5.5"
"vercel": "latest"
},
"engines": {
"node": ">=14.0.0"
Expand Down
118 changes: 59 additions & 59 deletions packages/nextjs/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ const Login: NextPage = () => {
<button className="btn text-left mb-5 mt-5" onClick={() => openKeysModal()}>
Show Keys
</button>
<dialog id="keys_modal" className="modal bg-gradient-to-br from-primary to-secondary">
<div className="modal-box">
<dialog id="keys_modal" className="modal bg-gradient-to-br from-secondary to-slate-900">
<div className="modal-box shadow-base-300 shadow-xl">
<div className="w-fit bg-base-100 text-base-content rounded-lg p-5 text-left break-all mt-4">
<ul className="space-y-2">
{publicKey && (
Expand Down Expand Up @@ -259,6 +259,52 @@ const Login: NextPage = () => {
await relay?.publish(signedEvent);
};

const handleTipNotRegisterd = async () => {
const key = generateRandomPassword();
if (!isExtension) {
console.log("Not Extension");
const nostr3 = new Nostr3(privateKey);
const message = `Tip ${amountToTip} OPETH to ${pubKeyReceiver}. Go to https://nostr3.vercel.app/claim and use this key to retrive your tip: ${key}`;
const decodedPubKey = await nip19.decode(pubKeyReceiver);
const encrypted = await nostr3.encryptDM(message, String(decodedPubKey.data));

// Send message with key encryted to the npub
const newEvent = {
kind: 4,
created_at: Math.floor(Date.now() / 1000),
tags: [["p", String(decodedPubKey.data)]],
content: String(encrypted),
pubkey: publicKey,
};

const encoded = encodeAbiParameters(parseAbiParameters("string"), [key]);

// Deposit on the contract with HASH
const hash = keccak256(encoded);

await nostr3ctx?.write?.deposit([hash], { value: parseEther(String(amountToTip)) });
const publish = await publishEvent(newEvent as UnsignedEvent, privateKey);
console.log(publish);
} else {
const message = `Tip ${amountToTip} ETH to ${pubKeyReceiver}. Use this key to retrive your tip: ${key}`;
const decodedPubKey = nip19.decode(pubKeyReceiver);
const encrypted = await window.nostr.nip04.encrypt(decodedPubKey, message);
const newEvent = {
kind: 4,
created_at: Math.floor(Date.now() / 1000),
tags: [["p", pubKeyReceiver]],
content: encrypted,
pubkey: publicKey,
};

const hash = keccak256(String(key) as any);
await nostr3ctx?.write?.deposit([hash], { value: parseEther(String(amountToTip)) });

const signedEvent = await window.nostr.signEvent(newEvent);
await relay?.publish(signedEvent);
}
};

const handleTip = async (receiver: any) => {
let message;
let newEvent;
Expand Down Expand Up @@ -923,7 +969,7 @@ const Login: NextPage = () => {
Relay
</button>
<dialog id="relay_modal" className="modal">
<div className="modal-box">
<div className="modal-box shadow-base-300 shadow-xl">
<h1 className="text-3xl font-thin mb-4">RELAY</h1>
<button
className=" w-full btn btn-primary mb-5"
Expand Down Expand Up @@ -1022,8 +1068,8 @@ const Login: NextPage = () => {
</div>
)}
</div>
<dialog id="tip_modal" className="modal bg-gradient-to-br from-primary to-secondary">
<div className="modal-box">
<dialog id="tip_modal" className="modal bg-gradient-to-br from-secondary to-slate-900">
<div className="modal-box shadow-base-300 shadow-xl">
<div className="flex flex-col font-black text-2xl mb-4 mx-auto items-center justify-center">TIP</div>
<input
className="mx-4"
Expand Down Expand Up @@ -1059,63 +1105,17 @@ const Login: NextPage = () => {
<button
className="btn btn-primary mt-4"
onClick={async () => {
if (!isSecretTip) {
const decodedPubKey = await nip19.decode(pubKeyReceiver);
const receiver = pubKeyEthAddressList.find(
(item: { pubkey: string }) => item.pubkey === decodedPubKey.data,
)?.evmAddress;
const decodedPubKey = await nip19.decode(pubKeyReceiver);
const receiver = pubKeyEthAddressList.find(
(item: { pubkey: string }) => item.pubkey === decodedPubKey.data,
)?.evmAddress;

if (receiver) {
console.log(receiver, evmAddress);
setEvmAddress(evmAddress);
if (receiver) {
await handleTip(receiver);
} else {
notification.error("Profile not registred");
}
await handleTip(receiver);
} else {
const key = generateRandomPassword();

if (!isExtension) {
console.log("Not Extension");
const nostr3 = new Nostr3(privateKey);
const message = `Tip ${amountToTip} OPETH to ${pubKeyReceiver}. Go to https://nostr3.vercel.app/claim and use this key to retrive your tip: ${key}`;
const decodedPubKey = await nip19.decode(pubKeyReceiver);
const encrypted = await nostr3.encryptDM(message, String(decodedPubKey.data));

// Send message with key encryted to the npub
const newEvent = {
kind: 4,
created_at: Math.floor(Date.now() / 1000),
tags: [["p", String(decodedPubKey.data)]],
content: String(encrypted),
pubkey: publicKey,
};

const encoded = encodeAbiParameters(parseAbiParameters("string"), [key]);

// Deposit on the contract with HASH
const hash = keccak256(encoded);

await nostr3ctx?.write?.deposit([hash], { value: parseEther(String(amountToTip)) });
const publish = await publishEvent(newEvent as UnsignedEvent, privateKey);
console.log(publish);
} else {
const message = `Tip ${amountToTip} ETH to ${pubKeyReceiver}. Use this key to retrive your tip: ${key}`;
const decodedPubKey = nip19.decode(pubKeyReceiver);
const encrypted = await window.nostr.nip04.encrypt(decodedPubKey, message);
const newEvent = {
kind: 4,
created_at: Math.floor(Date.now() / 1000),
tags: [["p", pubKeyReceiver]],
content: encrypted,
pubkey: publicKey,
};

const hash = keccak256(String(key) as any);
await nostr3ctx?.write?.deposit([hash], { value: parseEther(String(amountToTip)) });

const signedEvent = await window.nostr.signEvent(newEvent);
await relay?.publish(signedEvent);
}
await handleTipNotRegisterd();
}
}}
>
Expand Down
76 changes: 66 additions & 10 deletions packages/nextjs/pages/tip/[npub]/[nnote].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import Link from "next/link";
import { useRouter } from "next/router";
import { NextPage } from "next";
import { Relay, nip19, relayInit } from "nostr-tools";
import { parseEther } from "viem";
import { keccak256, parseEther } from "viem";
import { usePublicClient, useWalletClient } from "wagmi";
import { useScaffoldContract } from "~~/hooks/scaffold-eth";
import { useGlobalState } from "~~/services/store/store";
import { notification } from "~~/utils/scaffold-eth";

// Import other necessary hooks and services

declare global {
interface Window {
nostr: {
nip04: any;
getPublicKey: () => Promise<any>;
signEvent: (event: any) => Promise<any>;
encrypt: (data: string, key: string) => Promise<any>;
decrypt: (data: string, key: string) => Promise<any>;
};
}
}

const Tip: NextPage = () => {
const router = useRouter();
const { npub, nnote } = router.query;
Expand All @@ -25,6 +38,12 @@ const Tip: NextPage = () => {
const [connected, setConnected] = useState(false);
const [relay, setRelay] = useState<Relay>();
const [isRegistred, setIsRegistred] = useState(false);
const [consoleMessage, setConsoleMessage] = useState("");
const { data: nostr3ctx } = useScaffoldContract({
contractName: "Nostr3",
walletClient: signer,
});

const handleGetList = async () => {
try {
const url = `/api/getAll`;
Expand Down Expand Up @@ -163,6 +182,41 @@ const Tip: NextPage = () => {
}
};

function generateRandomPassword(maxLength = 20) {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+";
let password = "";

for (let i = 0; i < maxLength; i++) {
password += characters.charAt(Math.floor(Math.random() * characters.length));
}

return password;
}

const handleTipNotRegistred = async (
pubkey: string | nip19.ProfilePointer | nip19.EventPointer | nip19.AddressPointer,
) => {
const key = generateRandomPassword();
const message = `Tip ${amountToTip} ETH to ${npub}. Use this key to retrive your tip: ${key}`;

const encrypted = await window.nostr.nip04.encrypt(pubkey, message);

const newEvent = {
kind: 4,
created_at: Math.floor(Date.now() / 1000),
tags: [["p", pubkey]],
content: encrypted,
pubkey: publicKey,
};
console.log(newEvent, encrypted);
const hash = keccak256(String(key) as any);

await nostr3ctx?.write?.deposit([hash], { value: parseEther(String(amountToTip)) });

const signedEvent = await window.nostr.signEvent(newEvent);
await relay?.publish(signedEvent);
};

useEffect(() => {
// Function to verify npub
const verifyNpub = async () => {
Expand Down Expand Up @@ -220,14 +274,10 @@ const Tip: NextPage = () => {
{!isRegistred ? (
<div>Registerd {publicKey}</div>
) : (
<div className="mx-auto items-center">
<Link className="btn btn-lg bg-base-100" href="/login">
LOGIN
</Link>
</div>
<div className="mx-auto items-center text-4xl text-center">Thanks for using nostr3.</div>
)}
<dialog id="tip_modal" className="modal bg-gradient-to-br from-primary to-secondary">
<div className="modal-box">
<dialog id="tip_modal" className="modal bg-gradient-to-br from-secondary to-slate-900">
<div className="modal-box shadow-base-300 shadow-xl">
<div className="flex flex-col font-black text-4xl mb-4 mx-auto items-center justify-center">NOSTR3</div>

<input type="text" value={nnote} className="input input-primary w-full mb-4" placeholder="Note ID" />
Expand All @@ -246,20 +296,26 @@ const Tip: NextPage = () => {
<button
className="btn btn-primary mt-4"
onClick={async () => {
setConsoleMessage("Sending tip...");
const decoded = nip19.decode(String(npub));
const receiver = pubKeyEthAddressList.find(
(item: { pubkey: string }) => item.pubkey === decoded.data,
)?.evmAddress;
if (receiver) {
setConsoleMessage("Start Tx...");
await handleTip(receiver);
} else {
alert("Npub is not registred to nostr3");
notification.error("Profile not registred");
setConsoleMessage("Start Tx...");
await handleTipNotRegistred(decoded.data);
}
setConsoleMessage("Tip Complete");
// redirect to login
router.push("/login");
}}
>
Send
</button>
<span className="font-thin my-3 text-xl"> {consoleMessage}</span>
<div className="modal-action">
<form method="dialog">
{/* if there is a button in form, it will close the modal */}
Expand Down
Loading

0 comments on commit f83ae0b

Please sign in to comment.