// SPDX-License-Identifier: GPL-3.0-or-later\r
\r
import { Account } from './account.js'\r
-import { Blake2b } from './blake2b.js'\r
+import { default as blake2b, 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 { hex } from './convert.js'\r
+import { bytes, hex } from './convert.js'\r
import { Entropy } from './entropy.js'\r
import { Pool } from './pool.js'\r
import { Rpc } from './rpc.js'\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 }))\r
+ results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index, blake2b }))\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
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(publicKey, privateKey, index)\r
+ this.#accounts[keypair.index] = Account.fromKnownKeys(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
}\r
* Listens for messages from a calling function.\r
*/\r
addEventListener('message', (message) => {\r
- const { privateKey, index } = message.data ?? message\r
- keyPair.fromSeed(privateKey).then(publicKey => postMessage({ publicKey, privateKey, index }))\r
+ const { privateKey, index, blake2b } = message.data ?? message\r
+ const { publicKey } = keyPair.fromSeed(privateKey, blake2b)\r
+ postMessage({ publicKey, privateKey, index })\r
})\r
\r
\r
}\r
\r
// Note: difference from TweetNaCl - BLAKE2b used to hash instead of SHA-512.\r
-function crypto_hash(out, m, n) {\r
+function crypto_hash (out, m, n, b2b) {\r
+ const Blake2b = Function(`return ${b2b}`)()\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?) {\r
+function crypto_sign_keypair (pk, sk, seeded?, b2b) {\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);\r
+ crypto_hash(d, sk, 32, b2b);\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) {\r
+ fromSeed (seed, b2b) {\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);\r
+ crypto_sign_keypair(pk, sk, true, b2b);\r
return {publicKey: pk, secretKey: sk};\r
}\r
}\r