// SPDX-License-Identifier: GPL-3.0-or-later\r
\r
import { Account } from './account.js'\r
-import { default as blake2b, Blake2b } from './blake2b.js'\r
+import { Blake2b } from './blake2b.js'\r
import { Bip39Mnemonic } from './bip39-mnemonic.js'\r
import { ADDRESS_GAP, SEED_LENGTH_BIP44, SEED_LENGTH_BLAKE2B } from './constants.js'\r
import { bytes, hex } from './convert.js'\r
? new Entropy(id)\r
: new Entropy(16)\r
this.#mnemonic = mnemonic ?? null\r
- this.#pool = new Pool(nanoNacl)\r
+ this.#pool = new Pool(`(${nanoNacl})(${Blake2b})`)\r
this.#safe = new Safe()\r
this.#seed = seed ?? null\r
}\r
let results = await this.ckd(indexes)\r
if (!Array.isArray(results)) results = [results]\r
const data: any = []\r
- results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index, blake2b }))\r
+ results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index }))\r
let now = performance.now()\r
const keypairs: [{ publicKey: string, privateKey: string, index: number }] = await this.#pool.work(data)\r
- console.log(`accounts: ${-now + (now = performance.now())} ms`)\r
+ console.log(`keypairs: ${-now + (now = performance.now())} ms`)\r
for (const keypair of keypairs) {\r
if (keypair.publicKey == null) throw new RangeError('Account public key missing')\r
if (keypair.privateKey == null) throw new RangeError('Account private key missing')\r
if (keypair.index == null) throw new RangeError('Account keys derived but index missing')\r
const { publicKey, privateKey, index } = keypair\r
- this.#accounts[keypair.index] = Account.fromKnownKeys(bytes.toHex(publicKey as unknown as Uint8Array), bytes.toHex(privateKey as unknown as Uint8Array), index)\r
+ this.#accounts[keypair.index] = Account.fromKeypair(bytes.toHex(publicKey as unknown as Uint8Array), bytes.toHex(privateKey as unknown as Uint8Array), index)\r
}\r
- console.log(`done: ${-now + (now = performance.now())} ms`)\r
+ console.log(`accounts: ${-now + (now = performance.now())} ms`)\r
}\r
return this.#accounts.slice(from, to + 1)\r
}\r
const inputHex = `${this.seed}${indexHex}`.padStart(72, '0')\r
const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16))\r
const inputBytes = Uint8Array.from(inputArray)\r
- const key: string = new Blake2b(32).update(inputBytes).digest('hex') as string\r
- return { key, index }\r
+ const privateKey: string = new Blake2b(32).update(inputBytes).digest('hex') as string\r
+ return { privateKey, index }\r
})\r
console.log(`ckd: ${-now + (now = performance.now())} ms`)\r
return results\r
// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>\r
// SPDX-License-Identifier: GPL-3.0-or-later\r
//@ts-nocheck\r
-import { Blake2b } from '../blake2b.js'\r
\r
'use strict';\r
\r
// See for details: https://docs.nano.org/integration-guides/the-basics/\r
// Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js\r
\r
-async function fn () {\r
+async function NanoNaCl (Blake2b) {\r
/**\r
* Listens for messages from a calling function.\r
*/\r
addEventListener('message', (message) => {\r
- const { privateKey, index, blake2b } = message.data ?? message\r
- const { publicKey } = keyPair.fromSeed(privateKey, blake2b)\r
+ const { privateKey, index } = message.data ?? message\r
+ const { publicKey } = keyPair.fromSeed(privateKey)\r
postMessage({ publicKey, privateKey, index })\r
})\r
\r
}\r
\r
// Note: difference from TweetNaCl - BLAKE2b used to hash instead of SHA-512.\r
-function crypto_hash (out, m, n, b2b) {\r
- const Blake2b = Function(`return ${b2b}`)()\r
+function crypto_hash (out, m, n) {\r
const input = new Uint8Array(n)\r
for (let i = 0; i < n; ++i) {\r
input[i] = m[i]\r
scalarmult(p, q, s);\r
}\r
\r
-function crypto_sign_keypair (pk, sk, seeded?, b2b) {\r
+function crypto_sign_keypair (pk, sk, seeded?) {\r
var d = new Uint8Array(64);\r
var p = [gf(), gf(), gf(), gf()];\r
var i;\r
\r
if (!seeded) randombytes(sk, 32);\r
- crypto_hash(d, sk, 32, b2b);\r
+ crypto_hash(d, sk, 32);\r
d[0] &= 248;\r
d[31] &= 127;\r
d[31] |= 64;\r
return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
},\r
\r
- fromSeed (seed, b2b) {\r
+ fromSeed (seed) {\r
checkArrayTypes(seed);\r
if (seed.length !== crypto_sign_SEEDBYTES)\r
throw new Error('bad seed size');\r
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);\r
for (var i = 0; i < 32; i++) sk[i] = seed[i];\r
- crypto_sign_keypair(pk, sk, true, b2b);\r
+ crypto_sign_keypair(pk, sk, true);\r
return {publicKey: pk, secretKey: sk};\r
}\r
}\r
}\r
\r
export const nacl = {\r
- sign: fn.sign,\r
- open: fn.open,\r
- detached: fn.detached,\r
- verify: fn.verify,\r
- keyPair: fn.keyPair\r
+ sign: NanoNaCl.sign,\r
+ open: NanoNaCl.open,\r
+ detached: NanoNaCl.detached,\r
+ verify: NanoNaCl.verify,\r
+ keyPair: NanoNaCl.keyPair\r
}\r
-export default `(${fn.toString()})()`\r
+export default NanoNaCl.toString()\r