From: Chris Duncan Date: Fri, 29 Nov 2024 09:39:10 +0000 (-0800) Subject: Aha moment: the entire Blake2b class can be passed as an argument into the stringifie... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=3623b77dc2fa85bd9bd389e845aaf92f315cb7b8;p=libnemo.git Aha moment: the entire Blake2b class can be passed as an argument into the stringified NanoNaCl worker using an IIFE construct. Brilliant, it works now! Also fix object property name being passed from blake ckd, and update references to renamed account keypair factory method. --- diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 2f74b8e..7c6f000 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later import { Account } from './account.js' -import { default as blake2b, Blake2b } from './blake2b.js' +import { Blake2b } from './blake2b.js' import { Bip39Mnemonic } from './bip39-mnemonic.js' import { ADDRESS_GAP, SEED_LENGTH_BIP44, SEED_LENGTH_BLAKE2B } from './constants.js' import { bytes, hex } from './convert.js' @@ -60,7 +60,7 @@ abstract class Wallet { ? new Entropy(id) : new Entropy(16) this.#mnemonic = mnemonic ?? null - this.#pool = new Pool(nanoNacl) + this.#pool = new Pool(`(${nanoNacl})(${Blake2b})`) this.#safe = new Safe() this.#seed = seed ?? null } @@ -98,18 +98,18 @@ abstract class Wallet { let results = await this.ckd(indexes) if (!Array.isArray(results)) results = [results] const data: any = [] - results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index, blake2b })) + results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index })) let now = performance.now() const keypairs: [{ publicKey: string, privateKey: string, index: number }] = await this.#pool.work(data) - console.log(`accounts: ${-now + (now = performance.now())} ms`) + console.log(`keypairs: ${-now + (now = performance.now())} ms`) for (const keypair of keypairs) { if (keypair.publicKey == null) throw new RangeError('Account public key missing') if (keypair.privateKey == null) throw new RangeError('Account private key missing') if (keypair.index == null) throw new RangeError('Account keys derived but index missing') const { publicKey, privateKey, index } = keypair - this.#accounts[keypair.index] = Account.fromKnownKeys(bytes.toHex(publicKey as unknown as Uint8Array), bytes.toHex(privateKey as unknown as Uint8Array), index) + this.#accounts[keypair.index] = Account.fromKeypair(bytes.toHex(publicKey as unknown as Uint8Array), bytes.toHex(privateKey as unknown as Uint8Array), index) } - console.log(`done: ${-now + (now = performance.now())} ms`) + console.log(`accounts: ${-now + (now = performance.now())} ms`) } return this.#accounts.slice(from, to + 1) } @@ -585,8 +585,8 @@ export class Blake2bWallet extends Wallet { const inputHex = `${this.seed}${indexHex}`.padStart(72, '0') const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16)) const inputBytes = Uint8Array.from(inputArray) - const key: string = new Blake2b(32).update(inputBytes).digest('hex') as string - return { key, index } + const privateKey: string = new Blake2b(32).update(inputBytes).digest('hex') as string + return { privateKey, index } }) console.log(`ckd: ${-now + (now = performance.now())} ms`) return results diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index 49a373f..fa6abcf 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -1,7 +1,6 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later //@ts-nocheck -import { Blake2b } from '../blake2b.js' 'use strict'; @@ -17,13 +16,13 @@ import { Blake2b } from '../blake2b.js' // See for details: https://docs.nano.org/integration-guides/the-basics/ // Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js -async function fn () { +async function NanoNaCl (Blake2b) { /** * Listens for messages from a calling function. */ addEventListener('message', (message) => { - const { privateKey, index, blake2b } = message.data ?? message - const { publicKey } = keyPair.fromSeed(privateKey, blake2b) + const { privateKey, index } = message.data ?? message + const { publicKey } = keyPair.fromSeed(privateKey) postMessage({ publicKey, privateKey, index }) }) @@ -528,8 +527,7 @@ function pow2523(o, i) { } // Note: difference from TweetNaCl - BLAKE2b used to hash instead of SHA-512. -function crypto_hash (out, m, n, b2b) { - const Blake2b = Function(`return ${b2b}`)() +function crypto_hash (out, m, n) { const input = new Uint8Array(n) for (let i = 0; i < n; ++i) { input[i] = m[i] @@ -607,13 +605,13 @@ function scalarbase(p, s) { scalarmult(p, q, s); } -function crypto_sign_keypair (pk, sk, seeded?, b2b) { +function crypto_sign_keypair (pk, sk, seeded?) { var d = new Uint8Array(64); var p = [gf(), gf(), gf(), gf()]; var i; if (!seeded) randombytes(sk, 32); - crypto_hash(d, sk, 32, b2b); + crypto_hash(d, sk, 32); d[0] &= 248; d[31] &= 127; d[31] |= 64; @@ -840,14 +838,14 @@ const keyPair = { return {publicKey: pk, secretKey: new Uint8Array(secretKey)}; }, - fromSeed (seed, b2b) { + fromSeed (seed) { checkArrayTypes(seed); if (seed.length !== crypto_sign_SEEDBYTES) throw new Error('bad seed size'); var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES); for (var i = 0; i < 32; i++) sk[i] = seed[i]; - crypto_sign_keypair(pk, sk, true, b2b); + crypto_sign_keypair(pk, sk, true); return {publicKey: pk, secretKey: sk}; } } @@ -895,10 +893,10 @@ const setPRNG = function(fn) { } export const nacl = { - sign: fn.sign, - open: fn.open, - detached: fn.detached, - verify: fn.verify, - keyPair: fn.keyPair + sign: NanoNaCl.sign, + open: NanoNaCl.open, + detached: NanoNaCl.detached, + verify: NanoNaCl.verify, + keyPair: NanoNaCl.keyPair } -export default `(${fn.toString()})()` +export default NanoNaCl.toString()