From: Chris Duncan Date: Tue, 19 Nov 2024 18:50:21 +0000 (-0800) Subject: Merge changes from main. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=ae2e5193fcf574f62b33e2f475e276aa292d76e9;p=libnemo.git Merge changes from main. --- diff --git a/.gitignore b/.gitignore index 596358f..50492de 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ lib-cov # Coverage directory used by tools like istanbul coverage +coverage.info # nyc test coverage .nyc_output diff --git a/package-lock.json b/package-lock.json index 1ec4b4b..5cdf11d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,18 @@ { "name": "libnemo", - "version": "0.0.19", + "version": "0.0.20", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "libnemo", - "version": "0.0.19", + "version": "0.0.20", "license": "(GPL-3.0-or-later AND MIT)", "dependencies": { - "blake2b-wasm": "^2.4.0", - "blakejs": "^1.2.1" + "blake2b": "^2.1.4" }, "devDependencies": { - "@types/blake2b-wasm": "^2.4.3", + "@types/blake2b": "^2.1.3", "@types/node": "^22.8.6", "@types/w3c-web-hid": "^1.0.6", "@types/w3c-web-usb": "^1.0.10", @@ -519,17 +518,17 @@ "license": "Apache-2.0", "optional": true }, - "node_modules/@types/blake2b-wasm": { - "version": "2.4.3", - "resolved": "https://registry.npmjs.org/@types/blake2b-wasm/-/blake2b-wasm-2.4.3.tgz", - "integrity": "sha512-emsOJOuF5shxg5zhN3CHOy4BO/a26O++yk0ncFW9fePquKSGs1g6PIps8u8zFmApJjIkMQr7neVUqvoic4BRFw==", + "node_modules/@types/blake2b": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@types/blake2b/-/blake2b-2.1.3.tgz", + "integrity": "sha512-MFCdX0MNxFBP/xEILO5Td0kv6nI7+Q2iRWZbTL/yzH2/eDVZS5Wd1LHdsmXClvsCyzqaZfHFzZaN6BUeUCfSDA==", "dev": true, "license": "MIT" }, "node_modules/@types/node": { - "version": "22.8.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.8.6.tgz", - "integrity": "sha512-tosuJYKrIqjQIlVCM4PEGxOmyg3FCPa/fViuJChnGeEIhjA46oy8FMVoF9su1/v8PNs2a8Q0iFNyOx0uOF91nw==", + "version": "22.9.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.9.0.tgz", + "integrity": "sha512-vuyHg81vvWA1Z1ELfvLko2c8f34gyA0zaic0+Rllc5lbCnbSyuvb2Oxpm6TAUAC/2xZN3QGqxBNggD1nNR2AfQ==", "dev": true, "license": "MIT", "dependencies": { @@ -563,6 +562,16 @@ "integrity": "sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg==", "license": "Apache-2.0" }, + "node_modules/blake2b": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/blake2b/-/blake2b-2.1.4.tgz", + "integrity": "sha512-AyBuuJNI64gIvwx13qiICz6H6hpmjvYS5DGkG6jbXMOT8Z3WUJ3V1X0FlhIoT1b/5JtHE3ki+xjtMvu1nn+t9A==", + "license": "ISC", + "dependencies": { + "blake2b-wasm": "^2.4.0", + "nanoassert": "^2.0.0" + } + }, "node_modules/blake2b-wasm": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/blake2b-wasm/-/blake2b-wasm-2.4.0.tgz", @@ -573,12 +582,6 @@ "nanoassert": "^2.0.0" } }, - "node_modules/blakejs": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/blakejs/-/blakejs-1.2.1.tgz", - "integrity": "sha512-QXUSXI3QVc/gJME0dBpXrag1kbzOqCjCX8/b54ntNyW6sjtoqxqRk3LTmXzaJoh71zMsDCjM+47jS7XiwN/+fQ==", - "license": "MIT" - }, "node_modules/esbuild": { "version": "0.24.0", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.24.0.tgz", diff --git a/package.json b/package.json index 276d2cf..e6d889e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "libnemo", - "version": "0.0.19", + "version": "0.0.20", "description": "Asynchronous, non-blocking Nano cryptocurrency integration toolkit.", "keywords": [ "nemo", @@ -48,8 +48,7 @@ "test:coverage:report": "npm run test:coverage -- --test-reporter=lcov --test-reporter-destination=coverage.info && genhtml coverage.info --output-directory test/coverage && rm coverage.info && xdg-open test/coverage/index.html" }, "dependencies": { - "blake2b-wasm": "^2.4.0", - "blakejs": "^1.2.1" + "blake2b": "^2.1.4" }, "optionalDependencies": { "@ledgerhq/hw-transport-web-ble": "^6.29.4", @@ -57,7 +56,7 @@ "@ledgerhq/hw-transport-webusb": "^6.29.4" }, "devDependencies": { - "@types/blake2b-wasm": "^2.4.3", + "@types/blake2b": "^2.1.3", "@types/node": "^22.8.6", "@types/w3c-web-hid": "^1.0.6", "@types/w3c-web-usb": "^1.0.10", diff --git a/src/lib/account.ts b/src/lib/account.ts index 7a111d3..1f54c92 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import { blake2b } from 'blakejs' +import blake2b from 'blake2b' import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants.js' import { base32, bytes, hex } from './convert.js' import Ed25519 from './ed25519.js' @@ -143,7 +143,8 @@ export class Account { const expectedChecksum = address.slice(-8) const keyBase32 = address.slice(address.indexOf('_') + 1, -8) const keyBuf = base32.toBytes(keyBase32) - const actualChecksumBuf = blake2b(keyBuf, undefined, 5).reverse() + const actualChecksumBuf = blake2b(5, undefined, undefined, undefined, true) + .update(keyBuf).digest().reverse() const actualChecksum = bytes.toBase32(actualChecksumBuf) if (expectedChecksum !== actualChecksum) { @@ -186,7 +187,8 @@ export class Account { static #addressToKey (v: string): string { const keyBytes = base32.toBytes(v.substring(0, 52)) const checksumBytes = base32.toBytes(v.substring(52, 60)) - const blakeHash = blake2b(keyBytes, undefined, 5).reverse() + const blakeHash = blake2b(5, undefined, undefined, undefined, true) + .update(keyBytes).digest().reverse() if (bytes.toHex(checksumBytes) !== bytes.toHex(blakeHash)) { throw new Error('Checksum mismatch in address') } @@ -195,7 +197,8 @@ export class Account { static async #keyToAddress (key: string): Promise { const publicKeyBytes = hex.toBytes(key) - const checksum = blake2b(publicKeyBytes, undefined, 5).reverse() + const checksum = blake2b(5, undefined, undefined, undefined, true) + .update(publicKeyBytes).digest().reverse() const encoded = bytes.toBase32(publicKeyBytes) const encodedChecksum = bytes.toBase32(checksum) return `${PREFIX}${encoded}${encodedChecksum}` diff --git a/src/lib/curve25519.ts b/src/lib/curve25519.ts index 14ba60f..1386d88 100644 --- a/src/lib/curve25519.ts +++ b/src/lib/curve25519.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import { blake2b } from 'blakejs' +import blake2b from 'blake2b' /** * Derived from: @@ -686,7 +686,7 @@ export default class Curve25519 { for (let i = 0; i < n; ++i) { input[i] = m[i] } - const hash = blake2b(input) + const hash = blake2b(64).update(input).digest() for (let i = 0; i < 64; ++i) { out[i] = hash[i] } diff --git a/src/lib/ed25519.ts b/src/lib/ed25519.ts index 395a409..60631cd 100644 --- a/src/lib/ed25519.ts +++ b/src/lib/ed25519.ts @@ -1,8 +1,7 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import blakejs from 'blakejs' -const { blake2b, blake2bInit, blake2bUpdate, blake2bFinal } = blakejs +import blake2b from 'blake2b' import { bytes, hex } from './convert.js' import Curve25519 from './curve25519.js' @@ -26,7 +25,7 @@ const L: Uint8Array = new Uint8Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, function getPublicKey (privateKey: string): string { const pk = new Uint8Array(32) const p = [curve.gf(), curve.gf(), curve.gf(), curve.gf()] - const h = blake2b(hex.toBytes(privateKey), undefined, 64).slice(0, 32) + const h = blake2b(64).update(hex.toBytes(privateKey)).digest().slice(0, 32) h[0] &= 0xf8 h[31] &= 0x7f @@ -94,11 +93,11 @@ function verify (msg: Uint8Array, publicKey: Uint8Array, signature: Uint8Array): return false } - const ctx = blake2bInit(64, undefined) - blake2bUpdate(ctx, signature.subarray(0, 32)) - blake2bUpdate(ctx, publicKey) - blake2bUpdate(ctx, msg) - let k = blake2bFinal(ctx) + const k = blake2b(64) + .update(signature.subarray(0, 32)) + .update(publicKey) + .update(msg) + .digest() reduce(k) scalarmult(p, q, k) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index d5c2472..796df72 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -1,8 +1,7 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import blakejs from 'blakejs' -const { blake2bInit, blake2bUpdate, blake2bFinal } = blakejs +import blake from 'blake2b' import { Account } from './account.js' import { UNITS } from './constants.js' import { bytes, hex } from './convert.js' @@ -19,9 +18,9 @@ import { SendBlock } from './block.js' */ export async function blake2b (data: string | string[]): Promise { if (!Array.isArray(data)) data = [data] - const ctx = blake2bInit(32) - data.forEach(str => blake2bUpdate(ctx, hex.toBytes(str))) - return blake2bFinal(ctx) + const hash = blake(32) + data.forEach(str => hash.update(hex.toBytes(str))) + return hash.digest() } /** diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index f2a79b9..a7736eb 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import blake2b from 'blake2b-wasm' +import blake2b from 'blake2b' import { Account } from './account.js' import { nanoCKD } from './bip32-key-derivation.js' import { Bip39Mnemonic } from './bip39-mnemonic.js' @@ -548,7 +548,7 @@ export class Blake2bWallet extends Wallet { */ async ckd (index: number): Promise { const input = `${this.seed}${dec.toHex(index, 8)}` - const key = blake2b().update(hex.toBytes(input)).digest('hex') + const key = blake2b(32).update(hex.toBytes(input)).digest('hex') if (typeof key !== 'string') { throw new TypeError('BLAKE2b child key derivation returned invalid data') } diff --git a/test/TEST_VECTORS.js b/test/TEST_VECTORS.js index b3cbeac..c94e08b 100644 --- a/test/TEST_VECTORS.js +++ b/test/TEST_VECTORS.js @@ -11,7 +11,6 @@ * https://github.com/trezor/python-mnemonic/blob/master/vectors.json * https://tools.nanos.cc/?tool=seed */ - export const GENESIS_ADDRESS = 'nano_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3' export const RAW_MAX = '340282366920938463463374607431768211455' export const SUPPLY_MAX = '133248297920938463463374607431768211455' diff --git a/test/derive-accounts.test.mjs b/test/derive-accounts.test.mjs index 226a875..7d0754b 100644 --- a/test/derive-accounts.test.mjs +++ b/test/derive-accounts.test.mjs @@ -72,9 +72,12 @@ describe('derive child accounts from the same seed', async () => { assert.ok(a.index != null) } }) +}) + +describe('Ledger device accounts', { skip: true }, async () => { + const wallet = await LedgerWallet.create() - it('should fetch the first account from a Ledger device', { skip: true }, async () => { - const wallet = await LedgerWallet.create() + it('should fetch the first account from a Ledger device', async () => { const accounts = await wallet.accounts() assert.equal(accounts.length, 1) @@ -82,3 +85,23 @@ describe('derive child accounts from the same seed', async () => { assert.ok(accounts[0].address) }) }) + +describe('child key derivation performance', { skip: true }, async () => { + it('performance test of BIP-44 ckd', async function () { + const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + const accounts = await wallet.accounts(0, 0x7fff) + + assert.equal(accounts.length, 0x8000) + }) + + it('performance test of BLAKE2b ckd', async function () { + const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + const accounts = await wallet.accounts(0, 0x7fff) + + assert.equal(accounts.length, 0x8000) + }) +}) diff --git a/test/lock-unlock-wallet.mjs b/test/lock-unlock-wallet.mjs index e75b109..a8b3042 100644 --- a/test/lock-unlock-wallet.mjs +++ b/test/lock-unlock-wallet.mjs @@ -9,9 +9,7 @@ import { strict as assert } from 'assert' import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet } from '../dist/main.js' -const skip = false - -describe('locking and unlocking a Bip44Wallet', { skip }, async () => { +describe('locking and unlocking a Bip44Wallet', async () => { it('should succeed with a password', async () => { const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) @@ -128,7 +126,7 @@ describe('locking and unlocking a Bip44Wallet', { skip }, async () => { }) }) -describe('locking and unlocking a Blake2bWallet', { skip }, async () => { +describe('locking and unlocking a Blake2bWallet', async () => { it('should succeed with a password', async () => { const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)