]> zoso.dev Git - libnemo.git/commitdiff
Merge changes from main.
authorChris Duncan <chris@zoso.dev>
Tue, 19 Nov 2024 18:50:21 +0000 (10:50 -0800)
committerChris Duncan <chris@zoso.dev>
Tue, 19 Nov 2024 18:50:21 +0000 (10:50 -0800)
.gitignore
package-lock.json
package.json
src/lib/account.ts
src/lib/curve25519.ts
src/lib/ed25519.ts
src/lib/tools.ts
src/lib/wallet.ts
test/TEST_VECTORS.js
test/derive-accounts.test.mjs
test/lock-unlock-wallet.mjs

index 596358fc8e6b9033f8a08ef3fe84db2df80d8e5c..50492de2c6c595ad5c2b31d16c29046572dafd2d 100644 (file)
@@ -19,6 +19,7 @@ lib-cov
 \r
 # Coverage directory used by tools like istanbul\r
 coverage\r
+coverage.info\r
 \r
 # nyc test coverage\r
 .nyc_output\r
index 1ec4b4b32bb906a59b5887fcda14568456dac135..5cdf11d8fe4c3f12670df96e523bfbe505c50fc9 100644 (file)
@@ -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",
                        "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": {
                        "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",
                                "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",
index 276d2cf929df61d59a300284825315cf5f8a1c31..e6d889e2b6de85a1d9604aeab2fd9245b355e9b0 100644 (file)
@@ -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",
index 7a111d3df7f0f5ae7c7bc4bd86ec8daea50626dc..1f54c92b166ca5208996603c5a6a3a469ae6b1a4 100644 (file)
@@ -1,7 +1,7 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>\r
 // SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
-import { blake2b } from 'blakejs'\r
+import blake2b from 'blake2b'\r
 import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants.js'\r
 import { base32, bytes, hex } from './convert.js'\r
 import Ed25519 from './ed25519.js'\r
@@ -143,7 +143,8 @@ export class Account {
                const expectedChecksum = address.slice(-8)\r
                const keyBase32 = address.slice(address.indexOf('_') + 1, -8)\r
                const keyBuf = base32.toBytes(keyBase32)\r
-               const actualChecksumBuf = blake2b(keyBuf, undefined, 5).reverse()\r
+               const actualChecksumBuf = blake2b(5, undefined, undefined, undefined, true)\r
+                       .update(keyBuf).digest().reverse()\r
                const actualChecksum = bytes.toBase32(actualChecksumBuf)\r
 \r
                if (expectedChecksum !== actualChecksum) {\r
@@ -186,7 +187,8 @@ export class Account {
        static #addressToKey (v: string): string {\r
                const keyBytes = base32.toBytes(v.substring(0, 52))\r
                const checksumBytes = base32.toBytes(v.substring(52, 60))\r
-               const blakeHash = blake2b(keyBytes, undefined, 5).reverse()\r
+               const blakeHash = blake2b(5, undefined, undefined, undefined, true)\r
+                       .update(keyBytes).digest().reverse()\r
                if (bytes.toHex(checksumBytes) !== bytes.toHex(blakeHash)) {\r
                        throw new Error('Checksum mismatch in address')\r
                }\r
@@ -195,7 +197,8 @@ export class Account {
 \r
        static async #keyToAddress (key: string): Promise<string> {\r
                const publicKeyBytes = hex.toBytes(key)\r
-               const checksum = blake2b(publicKeyBytes, undefined, 5).reverse()\r
+               const checksum = blake2b(5, undefined, undefined, undefined, true)\r
+                       .update(publicKeyBytes).digest().reverse()\r
                const encoded = bytes.toBase32(publicKeyBytes)\r
                const encodedChecksum = bytes.toBase32(checksum)\r
                return `${PREFIX}${encoded}${encodedChecksum}`\r
index 14ba60f5162da283ff77fae3f22605993a3c9ff7..1386d88708fc82078d6cc8fa9e4eeb359e497483 100644 (file)
@@ -1,7 +1,7 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
 // 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]
                }
index 395a409ac27031077c65a50713c6939263b393fe..60631cd28804e661916b80fdfca0bf3b22c7352f 100644 (file)
@@ -1,8 +1,7 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>\r
 // SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
-import blakejs from 'blakejs'\r
-const { blake2b, blake2bInit, blake2bUpdate, blake2bFinal } = blakejs\r
+import blake2b from 'blake2b'\r
 import { bytes, hex } from './convert.js'\r
 import Curve25519 from './curve25519.js'\r
 \r
@@ -26,7 +25,7 @@ const L: Uint8Array = new Uint8Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12,
 function getPublicKey (privateKey: string): string {\r
        const pk = new Uint8Array(32)\r
        const p = [curve.gf(), curve.gf(), curve.gf(), curve.gf()]\r
-       const h = blake2b(hex.toBytes(privateKey), undefined, 64).slice(0, 32)\r
+       const h = blake2b(64).update(hex.toBytes(privateKey)).digest().slice(0, 32)\r
 \r
        h[0] &= 0xf8\r
        h[31] &= 0x7f\r
@@ -94,11 +93,11 @@ function verify (msg: Uint8Array, publicKey: Uint8Array, signature: Uint8Array):
                return false\r
        }\r
 \r
-       const ctx = blake2bInit(64, undefined)\r
-       blake2bUpdate(ctx, signature.subarray(0, 32))\r
-       blake2bUpdate(ctx, publicKey)\r
-       blake2bUpdate(ctx, msg)\r
-       let k = blake2bFinal(ctx)\r
+       const k = blake2b(64)\r
+               .update(signature.subarray(0, 32))\r
+               .update(publicKey)\r
+               .update(msg)\r
+               .digest()\r
        reduce(k)\r
        scalarmult(p, q, k)\r
 \r
index d5c24728c52137571f83143f864e049d118c6732..796df72173a3f5037e839f2221a5bd5b736d6705 100644 (file)
@@ -1,8 +1,7 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
 // 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<Uint8Array> {
        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()
 }
 
 /**
index f2a79b9fb62384417b5fe844720e49096aed34ca..a7736eb6dc37ccd14a9db5ca014c9ce58c63e5d9 100644 (file)
@@ -1,7 +1,7 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>\r
 // SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
-import blake2b from 'blake2b-wasm'\r
+import blake2b from 'blake2b'\r
 import { Account } from './account.js'\r
 import { nanoCKD } from './bip32-key-derivation.js'\r
 import { Bip39Mnemonic } from './bip39-mnemonic.js'\r
@@ -548,7 +548,7 @@ export class Blake2bWallet extends Wallet {
        */\r
        async ckd (index: number): Promise<Account> {\r
                const input = `${this.seed}${dec.toHex(index, 8)}`\r
-               const key = blake2b().update(hex.toBytes(input)).digest('hex')\r
+               const key = blake2b(32).update(hex.toBytes(input)).digest('hex')\r
                if (typeof key !== 'string') {\r
                        throw new TypeError('BLAKE2b child key derivation returned invalid data')\r
                }\r
index b3cbeac791489531289a8f2a05a2e440830a1903..c94e08b0d91edf26526eeead39244aa1746b43a3 100644 (file)
@@ -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'
index 226a875ae771ac6c2b62b0e2f0015b21a3b20468..7d0754b470626ace0189f72277edcac6679e6920 100644 (file)
@@ -72,9 +72,12 @@ describe('derive child accounts from the same seed', async () => {
                        assert.ok(a.index != null)\r
                }\r
        })\r
+})\r
+\r
+describe('Ledger device accounts', { skip: true }, async () => {\r
+       const wallet = await LedgerWallet.create()\r
 \r
-       it('should fetch the first account from a Ledger device', { skip: true }, async () => {\r
-               const wallet = await LedgerWallet.create()\r
+       it('should fetch the first account from a Ledger device', async () => {\r
                const accounts = await wallet.accounts()\r
 \r
                assert.equal(accounts.length, 1)\r
@@ -82,3 +85,23 @@ describe('derive child accounts from the same seed', async () => {
                assert.ok(accounts[0].address)\r
        })\r
 })\r
+\r
+describe('child key derivation performance', { skip: true }, async () => {\r
+       it('performance test of BIP-44 ckd', async function () {\r
+               const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)\r
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               const accounts = await wallet.accounts(0, 0x7fff)\r
+\r
+               assert.equal(accounts.length, 0x8000)\r
+       })\r
+\r
+       it('performance test of BLAKE2b ckd', async function () {\r
+               const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)\r
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               const accounts = await wallet.accounts(0, 0x7fff)\r
+\r
+               assert.equal(accounts.length, 0x8000)\r
+       })\r
+})\r
index e75b1097bacecf767d6052599549436211113cba..a8b3042a1edcd414a97b261392c2045124f24e66 100644 (file)
@@ -9,9 +9,7 @@ import { strict as assert } from 'assert'
 import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js'\r
 import { Bip44Wallet, Blake2bWallet } from '../dist/main.js'\r
 \r
-const skip = false\r
-\r
-describe('locking and unlocking a Bip44Wallet', { skip }, async () => {\r
+describe('locking and unlocking a Bip44Wallet', async () => {\r
        it('should succeed with a password', async () => {\r
                const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
 \r
@@ -128,7 +126,7 @@ describe('locking and unlocking a Bip44Wallet', { skip }, async () => {
        })\r
 })\r
 \r
-describe('locking and unlocking a Blake2bWallet', { skip }, async () => {\r
+describe('locking and unlocking a Blake2bWallet', async () => {\r
        it('should succeed with a password', async () => {\r
                const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)\r
 \r