diff options
| author | Gab Virebent <gabriel1@virebent.art> | 2026-08-03 17:13:50 +0200 |
|---|---|---|
| committer | Gab Virebent <gabriel1@virebent.art> | 2026-08-03 17:13:50 +0200 |
| commit | cd58d593789c6facb1d05b7f255bc2c5f2e46010 (patch) | |
| tree | 87a226313e49e2c6f7b7028d77d4957db416d1c1 /internal/assets/web/static/powWorker.js | |
| download | n2usenet-cd58d593789c6facb1d05b7f255bc2c5f2e46010.tar.gz n2usenet-cd58d593789c6facb1d05b7f255bc2c5f2e46010.tar.xz n2usenet-cd58d593789c6facb1d05b7f255bc2c5f2e46010.zip | |
Initial import: n2usenet HTTPS/Nym Usenet gateway
Diffstat (limited to 'internal/assets/web/static/powWorker.js')
| -rw-r--r-- | internal/assets/web/static/powWorker.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/internal/assets/web/static/powWorker.js b/internal/assets/web/static/powWorker.js new file mode 100644 index 0000000..fa668a1 --- /dev/null +++ b/internal/assets/web/static/powWorker.js @@ -0,0 +1,57 @@ +self.onmessage = async ({ data }) => { + const { prefix, targetZeros, startNonce, step } = data; + const zeroStr = '0'.repeat(targetZeros); + let nonce = startNonce; + let checked = 0; + + // Batch size: controllare più nonce per ogni aggiornamento di progresso + const BATCH_SIZE = 500; + + // Parallelizzazione del lavoro utilizzando più promise contemporaneamente + const CONCURRENT_PROMISES = 12; + + // Funzione per verificare un singolo nonce + const checkNonce = async (nonceToCheck) => { + const buf = await crypto.subtle.digest('SHA-1', new TextEncoder().encode(prefix + nonceToCheck)); + const hex = Array.from(new Uint8Array(buf)).map(b => b.toString(16).padStart(2, '0')).join(''); + return { nonce: nonceToCheck, hex }; + }; + + // Funzione per verificare un batch di nonce in parallelo + const processBatch = async () => { + const promises = []; + for (let i = 0; i < CONCURRENT_PROMISES; i++) { + let batchPromises = []; + for (let j = 0; j < BATCH_SIZE; j++) { + const currentNonce = nonce; + batchPromises.push(checkNonce(currentNonce)); + nonce += step; + } + promises.push(Promise.all(batchPromises)); + } + + // Attende il completamento di tutti i batch + const results = await Promise.all(promises); + + // Appiattisce i risultati e controlla se c'è una corrispondenza + const allResults = results.flat(); + for (const result of allResults) { + if (result.hex.startsWith(zeroStr)) { + return { found: true, nonce: result.nonce }; + } + } + + checked += CONCURRENT_PROMISES * BATCH_SIZE; + self.postMessage({ type: 'progress', checked }); + return { found: false }; + }; + + // Loop principale + while (true) { + const { found, nonce: foundNonce } = await processBatch(); + if (found) { + self.postMessage({ type: 'found', nonce: foundNonce }); + break; + } + } +}; |
