From: Chris Duncan Date: Thu, 28 Nov 2024 10:52:27 +0000 (-0800) Subject: Fix nano-nacl exports. Fix bip44 ckd return value. Pass account private key to worker... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=ddbf312c7df259967e99a0c7cb2e6f308d8e30ee;p=libnemo.git Fix nano-nacl exports. Fix bip44 ckd return value. Pass account private key to worker as bytes. --- diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index e052176..4b760a1 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -5,6 +5,7 @@ import { Account } from './account.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 { hex } from './convert.js' import { Entropy } from './entropy.js' import { Pool } from './pool.js' import { Rpc } from './rpc.js' @@ -97,7 +98,7 @@ abstract class Wallet { let results = await this.ckd(indexes) if (!Array.isArray(results)) results = [results] const data: any = [] - results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index })) + 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`) diff --git a/src/lib/workers/ckdBip44.ts b/src/lib/workers/ckdBip44.ts index 47f94c2..1e8c0a9 100644 --- a/src/lib/workers/ckdBip44.ts +++ b/src/lib/workers/ckdBip44.ts @@ -17,7 +17,7 @@ async function fn () { */ addEventListener('message', (message) => { const { seed, index } = message.data ?? message - nanoCKD(seed, index).then(key => postMessage({ index, key })) + nanoCKD(seed, index).then(privateKey => postMessage({ privateKey, index })) }) /** diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index 8e2b9b4..38bb648 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -23,7 +23,7 @@ async function fn () { */ addEventListener('message', (message) => { const { privateKey, index } = message.data ?? message - keyPair.fromSeed(seed).then(publicKey => postMessage({ publicKey, privateKey, index })) + keyPair.fromSeed(privateKey).then(publicKey => postMessage({ publicKey, privateKey, index })) }) @@ -779,7 +779,7 @@ function cleanup(arr) { for (var i = 0; i < arr.length; i++) arr[i] = 0; } -export const sign = function(msg, secretKey) { +const sign = function(msg, secretKey) { checkArrayTypes(msg, secretKey); if (secretKey.length !== crypto_sign_SECRETKEYBYTES) throw new Error('bad secret key size'); @@ -788,7 +788,7 @@ export const sign = function(msg, secretKey) { return signedMsg; }; -export const open = function(signedMsg, publicKey) { +const open = function(signedMsg, publicKey) { checkArrayTypes(signedMsg, publicKey); if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) throw new Error('bad public key size'); @@ -800,14 +800,14 @@ export const open = function(signedMsg, publicKey) { return m; }; -export const detached = function(msg, secretKey) { +const detached = function(msg, secretKey) { var signedMsg = sign(msg, secretKey); var sig = new Uint8Array(crypto_sign_BYTES); for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i]; return sig; }; -export const verify = function(msg, sig, publicKey) { +const verify = function(msg, sig, publicKey) { checkArrayTypes(msg, sig, publicKey); if (sig.length !== crypto_sign_BYTES) throw new Error('bad signature size'); @@ -821,7 +821,7 @@ export const verify = function(msg, sig, publicKey) { return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0); }; -export const keyPair = { +const keyPair = { create() { var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES); @@ -850,10 +850,10 @@ export const keyPair = { } } -export const publicKeyLength = crypto_sign_PUBLICKEYBYTES; -export const secretKeyLength = crypto_sign_SECRETKEYBYTES; -export const seedLength = crypto_sign_SEEDBYTES; -export const signatureLength = crypto_sign_BYTES; +const publicKeyLength = crypto_sign_PUBLICKEYBYTES; +const secretKeyLength = crypto_sign_SECRETKEYBYTES; +const seedLength = crypto_sign_SEEDBYTES; +const signatureLength = crypto_sign_BYTES; const setPRNG = function(fn) { randombytes = fn; @@ -892,5 +892,11 @@ const setPRNG = function(fn) { })(); } -export const nacl = { sign, open, detached, verify, keyPair, publicKeyLength, secretKeyLength, seedLength, signatureLength } +export const nacl = { + sign: fn.sign, + open: fn.open, + detached: fn.detached, + verify: fn.verify, + keyPair: fn.keyPair +} export default `(${fn.toString()})()`