From cf92997ac5f25879715dce87594b290acb7de50d Mon Sep 17 00:00:00 2001 From: OFF0 Date: Tue, 3 Jan 2023 13:04:43 +0100 Subject: [PATCH] fix into last commit cleanup, and updated validatePow, but this commit does not yet have acceptedDifficulty, it only checks if the pow is as desired. next commit should also add timeout and a userfacing error. --- src/main.js | 20 +++++++++----------- src/worker.js | 7 +++---- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main.js b/src/main.js index c6ce965..5bb97e0 100644 --- a/src/main.js +++ b/src/main.js @@ -1,10 +1,10 @@ import {relayPool, generatePrivateKey, getPublicKey, signEvent} from 'nostr-tools'; +import {zeroLeadingBitsCount} from './cryptoutils'; import {elem, parseTextContent} from './domutil.js'; import {dateTime, formatTime} from './timeutil.js'; // curl -H 'accept: application/nostr+json' https://relay.nostr.ch/ const pool = relayPool(); - pool.addRelay('wss://relay.nostr.info', {read: true, write: true}); pool.addRelay('wss://nostr.openchain.fr', {read: true, write: true}); // pool.addRelay('wss://relay.damus.io', {read: true, write: true}); @@ -967,16 +967,18 @@ profileForm.addEventListener('submit', async (e) => { } }); +/** + * check that the event has the id has the desired number of leading zero bits + * @param {EventObj} evt to validate + * @returns boolean + */ function validatePow(evt) { const tag = evt.tags.find(tag => tag[0] === 'nonce'); if (!tag) { return false; } - const [, , difficulty2] = tag; - if (difficulty2 < 16) { - return false; - } - return evt.id.substring(0, difficulty2 / 4) === '00'.repeat(difficulty2 / 8); + const [, , difficultyCommitment] = tag; + return zeroLeadingBitsCount(evt.id) === difficultyCommitment; } /** @@ -988,10 +990,7 @@ function validatePow(evt) { * a zero timeout makes mineEvent run without a time limit. */ function powEvent(evt, difficulty, timeout) { - const privatekey = localStorage.getItem('private_key'); return new Promise((resolve, reject) => { - // const webWorkerURL = URL.createObjectURL(new Blob(['(', powEventWorker(), ')()'], {type: 'application/javascript'})); - // const worker = new Worker(webWorkerURL); const worker = new Worker('./worker.js'); worker.onmessage = (msg) => { @@ -1008,7 +1007,6 @@ function powEvent(evt, difficulty, timeout) { reject(err); }; - worker.postMessage({event: evt, difficulty, privatekey, timeout}); - // URL.revokeObjectURL(webWorkerURL); // one-time worker; no longer need the URL obj + worker.postMessage({event: evt, difficulty, timeout}); }); } diff --git a/src/worker.js b/src/worker.js index cad755f..ca1e9f0 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,7 +1,7 @@ import {getEventHash} from 'nostr-tools'; import {zeroLeadingBitsCount} from './cryptoutils.js'; -function mine(event, difficulty, privatekey, timeout) { +function mine(event, difficulty, timeout) { const max = 256; // arbitrary if (!Number.isInteger(difficulty) || difficulty < 0 || difficulty > max) { throw new Error(`difficulty must be an integer between 0 and ${max}`); @@ -23,7 +23,7 @@ function mine(event, difficulty, privatekey, timeout) { // n = BigInt(0); // could reset nonce as we have a new timestamp } event.tags[0][1] = (++n).toString(); - const id = getEventHash(event, privatekey); + const id = getEventHash(event); if (zeroLeadingBitsCount(id) === difficulty) { console.log(event.tags[0][1], id); console.timeEnd('pow'); @@ -36,11 +36,10 @@ addEventListener('message', async (msg) => { const { difficulty, event, - privatekey, timeout, } = msg.data; try { - const minedEvent = mine(event, difficulty, privatekey, timeout); + const minedEvent = mine(event, difficulty, timeout); postMessage({event: minedEvent}); } catch (err) { postMessage({error: err});