From 6e5ab7f2ab329ac1a5dc39f870a952760613402b Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 1 Dec 2024 22:34:08 -0800 Subject: [PATCH] RNG directly instead of from Entropy. Tweak blake hashing call. Clarify some var names. Set known Nano constants. Import to main.js for testing. --- src/lib/pow.ts | 30 +++++++++++++++++++----------- src/main.ts | 3 ++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/lib/pow.ts b/src/lib/pow.ts index 38e9782..2be8a41 100644 --- a/src/lib/pow.ts +++ b/src/lib/pow.ts @@ -2,9 +2,12 @@ // SPDX-License-Identifier: GPL-3.0-or-later import { Blake2b } from './blake2b.js' -import { Entropy } from './entropy.js' const p = () => { + const NONCE_BYTES = 8 + const RECEIVE_THRESHOLD = 'fffffe0000000000' + const SEND_THRESHOLD = 'fffffff800000000' + /** * Listens for messages from a calling function. */ @@ -18,18 +21,23 @@ const p = () => { postMessage(buf, [buf]) }) - function find (hash: string, threshold: string) { - let nonce = null + async function find (hash: string, threshold: string = SEND_THRESHOLD) { + let count = 0 + let result = null do { - const e: Entropy = new Entropy(16) - const bytes: Uint8Array = parseHex(`${e.hex}${hash}`) - const result: string = new Blake2b(16).update(bytes).digest('hex') as string - const delta = BigInt(`0x${result}`) - BigInt(`0x${threshold}`) - if (delta >= 0) { - nonce = result + count++ + const nonce: Uint8Array = new Uint8Array(NONCE_BYTES) + crypto.getRandomValues(nonce) + const test: string = new Blake2b(NONCE_BYTES) + .update(nonce) + .update(parseHex(hash)) + .digest('hex') as string + if (count % 1000 === 0) console.log(`${count} hashes...`) + if (BigInt(`0x${test}`) >= BigInt(`0x${threshold}`)) { + result = nonce } - } while (nonce == null) - return nonce + } while (result == null) + return result } function parseHex (hex: string) { diff --git a/src/main.ts b/src/main.ts index d836cec..98f034e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,10 +3,11 @@ import { Account } from './lib/account.js' import { SendBlock, ReceiveBlock, ChangeBlock } from './lib/block.js' +import { Pow } from './lib/pow.js' import { Rpc } from './lib/rpc.js' import { Rolodex } from './lib/rolodex.js' import { Safe } from './lib/safe.js' import Tools from './lib/tools.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './lib/wallet.js' -export { Account, SendBlock, ReceiveBlock, ChangeBlock, Rpc, Rolodex, Safe, Tools, Bip44Wallet, Blake2bWallet, LedgerWallet } +export { Account, SendBlock, ReceiveBlock, ChangeBlock, Pow, Rpc, Rolodex, Safe, Tools, Bip44Wallet, Blake2bWallet, LedgerWallet } -- 2.34.1