From 634f969662eb13daf48efa4aa99b5ae550462d00 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 3 Jul 2026 00:44:59 -0700 Subject: [PATCH] Replace more toString calls and fix blake test. --- src/lib/crypto/bip44.ts | 9 +++++---- src/lib/ledger/cache.ts | 4 ++-- src/lib/ledger/sign.ts | 2 +- test/test.blake2b.mjs | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lib/crypto/bip44.ts b/src/lib/crypto/bip44.ts index 7431ebb..e831369 100644 --- a/src/lib/crypto/bip44.ts +++ b/src/lib/crypto/bip44.ts @@ -2,6 +2,7 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { Point, getPublicKey as secp256k1_getPublicKey } from '@noble/secp256k1' +import { dec } from '../convert' type Curve = 'Bitcoin seed' | 'ed25519 seed' type ExtendedKey = { @@ -41,25 +42,25 @@ export function Bip44 (curve: unknown, seed: unknown, coin: unknown, account: un throw new RangeError(`Invalid coin ${coin}`) } if (!Number.isSafeInteger(coin) || coin < 0 || coin > 0x7fffffff) { - throw new RangeError(`Invalid coin 0x${coin.toString(16)}`) + throw new RangeError(`Invalid coin 0x${dec.toHex(coin)}`) } if (typeof account !== 'number') { throw new RangeError(`Invalid account index ${account}`) } if (!Number.isSafeInteger(account) || account < 0 || account > 0x7fffffff) { - throw new RangeError(`Invalid account index 0x${account.toString(16)}`) + throw new RangeError(`Invalid account index 0x${dec.toHex(account)}`) } if (change !== undefined && typeof change !== 'number') { throw new RangeError(`Invalid change index ${change}`) } if (change !== undefined && (!Number.isSafeInteger(change) || change < 0 || change > 1)) { - throw new RangeError(`Invalid change index 0x${account.toString(16)}`) + throw new RangeError(`Invalid change index 0x${dec.toHex(account)}`) } if (address !== undefined && typeof address !== 'number') { throw new RangeError(`Invalid address index ${address}`) } if (address !== undefined && (!Number.isSafeInteger(address) || address < 0 || address > 0x7fffffff)) { - throw new RangeError(`Invalid address index 0x${account.toString(16)}`) + throw new RangeError(`Invalid address index 0x${dec.toHex(account)}`) } return ckd(curve, seed, coin, account, change, address) } diff --git a/src/lib/ledger/cache.ts b/src/lib/ledger/cache.ts index 255f909..de99122 100644 --- a/src/lib/ledger/cache.ts +++ b/src/lib/ledger/cache.ts @@ -2,7 +2,7 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { TransportStatusError } from '@ledgerhq/errors' -import { APDU_CODES, LedgerResponse, LedgerTransport, LISTEN_TIMEOUT, OPEN_TIMEOUT, STATUS_CODES } from '.' +import { APDU_CODES, LISTEN_TIMEOUT, LedgerResponse, LedgerTransport, OPEN_TIMEOUT, STATUS_CODES } from '.' import { Account } from '../account' import { Block } from '../block' import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants' @@ -29,7 +29,7 @@ export async function _cache (transport: LedgerTransport, index: number = 0, blo const previous = block.previous const link = block.link const representative = hex.toBytes(block.representative.publicKey, 32) - const balance = hex.toBytes(block.balance.toString(16), 16) + const balance = dec.toBytes(block.balance, 16) const signature = hex.toBytes(block.signature, 64) const data = new Uint8Array([APDU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature]) diff --git a/src/lib/ledger/sign.ts b/src/lib/ledger/sign.ts index ccd4f34..f73f616 100644 --- a/src/lib/ledger/sign.ts +++ b/src/lib/ledger/sign.ts @@ -50,7 +50,7 @@ async function signBlock (transport: LedgerTransport, index: number, block: Bloc const previous = block.previous const link = block.link const representative = hex.toBytes(block.representative.publicKey, 32) - const balance = hex.toBytes(block.balance.toString(16), 16) + const balance = dec.toBytes(block.balance, 16) const data = new Uint8Array([...DERIVATION_PATH, ...account, ...previous, ...link, ...representative, ...balance]) const res = await req(transport, APDU_CODES.signBlock, data as Buffer) diff --git a/test/test.blake2b.mjs b/test/test.blake2b.mjs index 87ab22f..2c3c9c5 100644 --- a/test/test.blake2b.mjs +++ b/test/test.blake2b.mjs @@ -33,7 +33,7 @@ await Promise.all([ try { /** @type {string} */ const hex = new Blake2b(64, key).update(input).digest('hex') - assert.equal(hex, test.out) + assert.equal(hex, test.out.toUpperCase()) } catch (err) { console.error(`blake2b reference test vector ${i} failed`, { cause: err }) } @@ -85,7 +85,7 @@ await Promise.all([ try { /** @type {string} */ const hex = new Blake2b(test.outlen ?? 64, key, salt, personal).update(input).digest('hex') - assert.equal(hex, test.out) + assert.equal(hex, test.out.toUpperCase()) } catch (err) { console.error(`blake2b libsodium test vector ${i} failed`, { cause: err }) } -- 2.52.0