WASM: PoW

This commit is contained in:
2023-09-24 21:28:39 +01:00
parent 9f731da5be
commit 6e7a28a42b
19 changed files with 1146 additions and 52 deletions

View File

@ -71,11 +71,13 @@ export class EventPublisher {
}
/**
* Apply POW to every event
* Create a copy of this publisher with PoW
*/
pow(target: number, miner?: PowMiner) {
this.#pow = target;
this.#miner = miner;
const ret = new EventPublisher(this.#signer, this.#pubKey);
ret.#pow = target;
ret.#miner = miner;
return ret;
}
#eb(k: EventKind) {

View File

@ -20,15 +20,7 @@ export function minePow(e: NostrPowEvent, target: number) {
e.tags.push(["nonce", ctr.toString(), target.toString()]);
}
do {
//roll ctr and compute id
const now = Math.floor(new Date().getTime() / 1000);
// reset ctr if timestamp changed, this is not really needed but makes the ctr value smaller
if (now !== e.created_at) {
ctr = 0;
e.created_at = now;
}
e.tags[nonceTagIdx][1] = (++ctr).toString();
e.id = createId(e);
} while (countLeadingZeros(e.id) < target);