diff --git a/client/package.json b/client/package.json index 7421bec..0080ac7 100644 --- a/client/package.json +++ b/client/package.json @@ -11,7 +11,7 @@ "@types/node": "^17.0.45", "@types/react": "^18.2.21", "@types/react-dom": "^18.2.7", - "nostr-tools": "^1.14.2", + "nostr-tools": "latest", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.15.0", diff --git a/client/src/App.tsx b/client/src/App.tsx index 6af07fb..f90f0e2 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -4,6 +4,12 @@ import './App.css'; import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import Home from './components/Home'; +declare global { + interface Window { + nostr?: any; + } +} + function App() { return ( diff --git a/client/src/components/Home.tsx b/client/src/components/Home.tsx index 29be5ba..4abb16b 100644 --- a/client/src/components/Home.tsx +++ b/client/src/components/Home.tsx @@ -12,7 +12,7 @@ interface Event { // Add other fields if necessary } -const relay = relayInit('wss://relay.damus.io'); +const relay = relayInit('wss://nostr.lu.ke'); const Home = () => { // Define the type of the state variable @@ -24,7 +24,7 @@ const Home = () => { const eventList = await relay.list([ { - ids: ['0000'], + ids: ['00'], kinds: [1], limit: 10, }, @@ -46,12 +46,12 @@ const Home = () => {
- {events.map((event, index) => ( + {events.sort((a, b) => a.created_at - b.created_at).map((event, index) => ( ))}
-
+ {/*
*/} ); }; diff --git a/client/src/components/PostCard/NewThreadCard.tsx b/client/src/components/PostCard/NewThreadCard.tsx index a904599..c983687 100644 --- a/client/src/components/PostCard/NewThreadCard.tsx +++ b/client/src/components/PostCard/NewThreadCard.tsx @@ -1,7 +1,65 @@ import CardContainer from './CardContainer'; import { ArrowUpTrayIcon } from '@heroicons/react/24/outline'; +import { useState } from 'react'; +import { generatePrivateKey, getPublicKey, finishEvent, relayInit} from 'nostr-tools'; +import { minePow } from '../../func/mine'; + +const difficulty = 10 const NewThreadCard = () => { + const [comment, setComment] = useState(""); + + const handleSubmit = async (event: React.FormEvent) => { + event.preventDefault(); + let sk = generatePrivateKey() + let pk = getPublicKey(sk) + + const relay = relayInit('wss://nostr.lu.ke') + relay.on('connect', () => { + console.log(`connected to ${relay.url}`) + }) + relay.on('error', () => { + console.log(`failed to connect to ${relay.url}`) + }) + + await relay.connect() + + try { + const event = minePow({ + kind: 1, + tags: [], + content: 'Hello, world!', + created_at: Math.floor(Date.now() / 1000), //needs to be date To Unix + pubkey: pk, + }, difficulty) + + const signedEvent = finishEvent(event, sk) + await relay.publish(signedEvent) + console.log(signedEvent.id) + + } catch (error) { + setComment(comment + " " + error); + } + relay.close() + }; + + // async function attachFile(file_input: File | null) { + // try { + // if (file_input) { + // const rx = await NostrImg(file_input); + // if (rx.url) { + // setComment(comment + " " + rx.url); + // } else if (rx?.error) { + // setComment(comment + " " + rx.error); + // } + // } + // } catch (error: unknown) { + // if (error instanceof Error) { + // setComment(comment + " " + error?.message); + // } + // } + // } + return ( <> @@ -10,6 +68,7 @@ const NewThreadCard = () => { method="post" encType="multipart/form-data" className="" + onSubmit={handleSubmit} >