fix into last commit
ci/woodpecker/push/woodpecker Pipeline was successful Details
ci/woodpecker/pr/woodpecker Pipeline was successful Details

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.
OFF0 2 years ago
parent da269debc3
commit cf92997ac5
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -1,10 +1,10 @@
import {relayPool, generatePrivateKey, getPublicKey, signEvent} from 'nostr-tools'; import {relayPool, generatePrivateKey, getPublicKey, signEvent} from 'nostr-tools';
import {zeroLeadingBitsCount} from './cryptoutils';
import {elem, parseTextContent} from './domutil.js'; import {elem, parseTextContent} from './domutil.js';
import {dateTime, formatTime} from './timeutil.js'; import {dateTime, formatTime} from './timeutil.js';
// curl -H 'accept: application/nostr+json' https://relay.nostr.ch/ // curl -H 'accept: application/nostr+json' https://relay.nostr.ch/
const pool = relayPool(); const pool = relayPool();
pool.addRelay('wss://relay.nostr.info', {read: true, write: true}); pool.addRelay('wss://relay.nostr.info', {read: true, write: true});
pool.addRelay('wss://nostr.openchain.fr', {read: true, write: true}); pool.addRelay('wss://nostr.openchain.fr', {read: true, write: true});
// pool.addRelay('wss://relay.damus.io', {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) { function validatePow(evt) {
const tag = evt.tags.find(tag => tag[0] === 'nonce'); const tag = evt.tags.find(tag => tag[0] === 'nonce');
if (!tag) { if (!tag) {
return false; return false;
} }
const [, , difficulty2] = tag; const [, , difficultyCommitment] = tag;
if (difficulty2 < 16) { return zeroLeadingBitsCount(evt.id) === difficultyCommitment;
return false;
}
return evt.id.substring(0, difficulty2 / 4) === '00'.repeat(difficulty2 / 8);
} }
/** /**
@ -988,10 +990,7 @@ function validatePow(evt) {
* a zero timeout makes mineEvent run without a time limit. * a zero timeout makes mineEvent run without a time limit.
*/ */
function powEvent(evt, difficulty, timeout) { function powEvent(evt, difficulty, timeout) {
const privatekey = localStorage.getItem('private_key');
return new Promise((resolve, reject) => { 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'); const worker = new Worker('./worker.js');
worker.onmessage = (msg) => { worker.onmessage = (msg) => {
@ -1008,7 +1007,6 @@ function powEvent(evt, difficulty, timeout) {
reject(err); reject(err);
}; };
worker.postMessage({event: evt, difficulty, privatekey, timeout}); worker.postMessage({event: evt, difficulty, timeout});
// URL.revokeObjectURL(webWorkerURL); // one-time worker; no longer need the URL obj
}); });
} }

@ -1,7 +1,7 @@
import {getEventHash} from 'nostr-tools'; import {getEventHash} from 'nostr-tools';
import {zeroLeadingBitsCount} from './cryptoutils.js'; import {zeroLeadingBitsCount} from './cryptoutils.js';
function mine(event, difficulty, privatekey, timeout) { function mine(event, difficulty, timeout) {
const max = 256; // arbitrary const max = 256; // arbitrary
if (!Number.isInteger(difficulty) || difficulty < 0 || difficulty > max) { if (!Number.isInteger(difficulty) || difficulty < 0 || difficulty > max) {
throw new Error(`difficulty must be an integer between 0 and ${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 // n = BigInt(0); // could reset nonce as we have a new timestamp
} }
event.tags[0][1] = (++n).toString(); event.tags[0][1] = (++n).toString();
const id = getEventHash(event, privatekey); const id = getEventHash(event);
if (zeroLeadingBitsCount(id) === difficulty) { if (zeroLeadingBitsCount(id) === difficulty) {
console.log(event.tags[0][1], id); console.log(event.tags[0][1], id);
console.timeEnd('pow'); console.timeEnd('pow');
@ -36,11 +36,10 @@ addEventListener('message', async (msg) => {
const { const {
difficulty, difficulty,
event, event,
privatekey,
timeout, timeout,
} = msg.data; } = msg.data;
try { try {
const minedEvent = mine(event, difficulty, privatekey, timeout); const minedEvent = mine(event, difficulty, timeout);
postMessage({event: minedEvent}); postMessage({event: minedEvent});
} catch (err) { } catch (err) {
postMessage({error: err}); postMessage({error: err});

Loading…
Cancel
Save