Skip to content

Commit

Permalink
add nostr signer extension
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Jul 30, 2024
1 parent a9035a9 commit e5583b0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 48 deletions.
99 changes: 52 additions & 47 deletions askeladd-dvm-marketplace/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function Home() {
const [timestampJob, setTimestampJob] = useState<number | undefined>();

const { ndk } = useNostrContext()
const {fetchEvents} = useFetchEvents()
const { fetchEvents } = useFetchEvents()
const { sendNote } = useSendNote()
useEffect(() => {
init()
Expand Down Expand Up @@ -67,27 +67,37 @@ export default function Home() {

// Define the timestamp before which you want to fetch events
setTimestampJob(new Date().getTime())
// if(typeof window != "undefined") {
// window.nostr.signEvent(event: { created_at: number, kind: number, tags: string[][], content: string }) // takes an event object, adds `id`, `pubkey` and `sig` and returns it
// }
let { result, event } = await sendNote({ content, tags, kind: 5600 })
console.log("event", event)

if (event?.id) {
setJobId(event?.id);
/** Use Nostr extension to send event */
if (typeof window !== "undefined" && window.nostr) {
const pubkey = await window.nostr.getPublicKey();
const event = await window.nostr.signEvent({ pubkey: pubkey, created_at: new Date().getTime(), kind: 5600, tags: tags, content: content }) // takes an event object, adds `id`, `pubkey` and `sig` and returns it
if (event?.sig) {
setJobId(event?.sig);
}
}

/** NDK event
* Generate or import private key after
*/
// let { result, event } = await sendNote({ content, tags, kind: 5600 })
// console.log("event", event)
// if (event?.sig) {
// setJobId(event?.sig);
// }

};

// Fetch Job result from the Prover
const fetchEventsProof = async () => {

const {events}= await fetchEvents()
if(!events) return;
const { events } = await fetchEvents()
if (!events) return;
console.log("events", events);
setEvents(events)
/** TODO fetch the correct event */
/** @TODO fetch the correct event
* - Tags: By reply of the event_id of the job request?
* - By author
* - Timestamp since/until (doesn't work as expected for me)
*/
let lastEvent = events[events?.length - 1]
setSelectedEvent(lastEvent)
setProof(lastEvent?.content)
Expand All @@ -99,61 +109,56 @@ export default function Home() {
console.log('proof serialize', proofSerialize);
setStarkProof(proofSerialize);
setProofStatus("received");


}

const waitingForJobResult = async () => {

setTimeout(() => {
console.log("waiting timeout")


fetchEventsProof()
setIsLoading(false);

}, 5000);
}

const verifyProofHandler = async () => {
if (proof) {
setIsLoading(true);

const prove_result = run_fibonacci_example(logSize, claim);
console.log("prove_result", prove_result);

const verify_result = run_verify_exemple(logSize, claim, JSON.stringify(starkProof));

console.log("verify result", verify_result);
console.log("verify result", verify_result.message);
console.log("verify success", verify_result.success);

if (verify_result?.success) {
console.log("is success verify result")
setProofStatus("verified");
} else {
setError(verify_result?.message)
}

/** ERROR verify loop between all stark proof */
for (let event of events) {
const jobProofSerialize: JobResultProver = JSON.parse(event?.content)
console.log('jobProofSerialize serialize', jobProofSerialize);

const proofSerialize = jobProofSerialize?.response?.proof;
console.log('proof serialize', proofSerialize);
const verify_result = run_verify_exemple(logSize, claim, JSON.stringify(proofSerialize));
console.log("verify result", verify_result.message);
try {
if (proof) {
setIsLoading(true);
const prove_result = run_fibonacci_example(logSize, claim);
console.log("prove_result", prove_result);
const verify_result = run_verify_exemple(logSize, claim, JSON.stringify(starkProof));
console.log("verify result", verify_result);
console.log("verify message", verify_result.message);
console.log("verify success", verify_result.success);

if (verify_result?.success) {

console.log("is success verify result")
setProofStatus("verified");
} else {
setError(verify_result?.message)
}

/** @TODO fix ERROR verify loop between all stark proof*/
for (let event of events) {
const jobProofSerialize: JobResultProver = JSON.parse(event?.content)
const proofSerialize = jobProofSerialize?.response?.proof;
const verify_result = run_verify_exemple(logSize, claim, JSON.stringify(proofSerialize));
console.log("loop verify result", verify_result.message);
console.log("loop verify success", verify_result.success);
if (verify_result?.success) {
console.log("is success verify result")
setProofStatus("verified");
} else {
setError(verify_result?.message)
}
}
setIsLoading(false);
}
} catch (e) {
console.log("Verify error", e);
} finally {
setIsLoading(false);

}
};

Expand Down
1 change: 1 addition & 0 deletions askeladd-dvm-marketplace/src/constants/relay.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const ASKELADD_RELAY= [
"ws://127.0.0.1:8080",
// "wss://relay.nostr.net"
]
1 change: 0 additions & 1 deletion askeladd-dvm-marketplace/src/context/NostrContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const NostrProvider: React.FC<React.PropsWithChildren> = ({children}) =>
const [ndk, setNdk] = useState<NDK>(
new NDK({
explicitRelayUrls: ASKELADD_RELAY,
signer:new NDKPrivateKeySigner(process.env.DEFAULT_NOSTR_USER_SK ?? privateKey)
}),
);

Expand Down

0 comments on commit e5583b0

Please sign in to comment.