From: Chris Duncan Date: Mon, 9 Dec 2024 16:04:41 +0000 (-0800) Subject: Fix import paths. Fix entropy usage. Ensure blake2b is always included in NanoNaCl... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=8ad7bf78873319e4445e44b5c108c7ec52820292;p=libnemo.git Fix import paths. Fix entropy usage. Ensure blake2b is always included in NanoNaCl worker string instead of relying on calling function. --- diff --git a/src/lib/account.ts b/src/lib/account.ts index 3e3d932..001208c 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -6,7 +6,7 @@ import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants import { base32, bytes, hex } from './convert.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' -import { NanoNaCl } from './nano-nacl.js' +import { NanoNaCl } from './workers/nano-nacl.js' /** * Represents a single Nano address and the associated public key. To include the diff --git a/src/lib/bip39-mnemonic.ts b/src/lib/bip39-mnemonic.ts index 87ac879..1d9fe2d 100644 --- a/src/lib/bip39-mnemonic.ts +++ b/src/lib/bip39-mnemonic.ts @@ -119,7 +119,7 @@ export class Bip39Mnemonic { return false } - const entropy = new Entropy(bin.toBytes(entropyBits)) + const entropy = await Entropy.import(bin.toBytes(entropyBits)) const expectedChecksum = await this.checksum(entropy) if (expectedChecksum !== checksumBits) { diff --git a/src/lib/block.ts b/src/lib/block.ts index 58ee6fd..024877b 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -5,7 +5,7 @@ import { BURN_ADDRESS, PREAMBLE, THRESHOLD_RECEIVE, THRESHOLD_SEND } from './con import { Account } from './account.js' import { Blake2b } from './blake2b.js' import { dec, hex } from './convert.js' -import { NanoNaCl } from './nano-nacl.js' +import { NanoNaCl } from './workers/nano-nacl.js' import { Pool } from './pool.js' import { Rpc } from './rpc.js' import { Pow } from './workers.js' diff --git a/src/lib/safe.ts b/src/lib/safe.ts index e22503f..457dc93 100644 --- a/src/lib/safe.ts +++ b/src/lib/safe.ts @@ -41,7 +41,7 @@ export class Safe { throw new Error(ERR_MSG) } - const iv = new Entropy() + const iv = await Entropy.create() if (typeof passkey === 'string') { try { passkey = await subtle.importKey('raw', utf8.toBytes(passkey), 'PBKDF2', false, ['deriveBits', 'deriveKey']) @@ -98,7 +98,7 @@ export class Safe { } const record = JSON.parse(item) const encrypted = hex.toBytes(record.encrypted) - const iv = new Entropy(record.iv) + const iv = await Entropy.import(record.iv) try { if (typeof passkey === 'string') { diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 69cadcd..fe934fe 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -8,7 +8,7 @@ import { hex } from './convert.js' import { Rpc } from './rpc.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './wallet.js' import { SendBlock } from './block.js' -import { NanoNaCl } from './nano-nacl.js' +import { NanoNaCl } from './workers/nano-nacl.js' function hash (data: string | string[], encoding?: 'hex'): string { if (!Array.isArray(data)) data = [data] diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 213b543..447c205 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -93,7 +93,7 @@ abstract class Wallet { let results = await this.ckd(indexes) const data: any = [] results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index })) - const pool = new Pool(`const Blake2b = ${Blake2b}\n${NanoNaCl}`) + const pool = new Pool(NanoNaCl) const keypairs: KeyPair[] = await pool.work('divide', data) pool.dismiss() for (const keypair of keypairs) { diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index 2f64ce1..962e1ae 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -3,7 +3,7 @@ 'use strict' -import { Blake2b } from '#src/lib/blake2b.js' +import { Blake2b } from '../blake2b.js' // Ported in 2014 by Dmitry Chestnykh and Devi Mandiri. // Public domain. @@ -854,4 +854,5 @@ export const NanoNaCl = n() const start = n.toString().indexOf('{') + 1 const end = n.toString().lastIndexOf('return') -export const worker = n.toString().substring(start, end) +const blake2b = `const Blake2b = ${Blake2b}\n` +export const worker = `${blake2b}${n.toString().substring(start, end)}`