From 16ef021182209bd6d7e5009f3d21fb951b6bd4b0 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 20 Nov 2024 00:11:24 -0800 Subject: [PATCH] In Tools, deprecate blake2b function in favor of simpler hash function already exported to reduce confusion with package of the same name, and add an encoding flag to allow strings already encoded to hexadecimal like nano blocks. --- src/lib/block.ts | 6 ++++-- src/lib/tools.ts | 45 +++++++++++++++++---------------------------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index 4e23a7d..577e6c9 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -69,7 +69,8 @@ abstract class Block { dec.toHex(this.balance, 32), this.link ] - return Tools.blake2b(data) + const hash = await Tools.hash(data, 'hex') + return hex.toBytes(hash) } /** @@ -194,8 +195,9 @@ abstract class Block { dec.toHex(this.balance, 32), this.link ] + const hash = await Tools.hash(data, 'hex') return Ed25519.verify( - await Tools.blake2b(data), + hex.toBytes(hash), hex.toBytes(key), hex.toBytes(this.signature ?? '') ) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 796df72..6763413 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import blake from 'blake2b' +import blake2b from 'blake2b' import { Account } from './account.js' import { UNITS } from './constants.js' import { bytes, hex } from './convert.js' @@ -11,20 +11,7 @@ import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './wallet.js' import { SendBlock } from './block.js' /** -* Hashes data using BLAKE2b. -* -* @param {string|string[]} data - Hexadecimal-formatted data to hash -* @returns {Promise} Array of bytes representing hashed input data -*/ -export async function blake2b (data: string | string[]): Promise { - if (!Array.isArray(data)) data = [data] - const hash = blake(32) - data.forEach(str => hash.update(hex.toBytes(str))) - return hash.digest() -} - -/** -* Converts a decimal amount from one unit divider to another. +* Converts a decimal amount of nano from one unit divider to another. * * @param {bigint|string} amount - Decimal amount to convert * @param {string} inputUnit - Current denomination @@ -77,14 +64,18 @@ export async function convert (amount: bigint | string, inputUnit: string, outpu * Converts one or more strings to hexadecimal and hashes them with BLAKE2b. * * @param {string|string[]} data - Input to hash -* @returns {Promise} Hash of the input +* @returns {Promise} 64-character hexadecimal hash of the input */ -export async function hash (data: string | string[]): Promise { +export async function hash (data: string | string[], encoding?: 'hex'): Promise { if (!Array.isArray(data)) data = [data] - const enc = new TextEncoder() - data = data.map(str => { return bytes.toHex(enc.encode(str)) }) - const hash = await blake2b(data) - return bytes.toHex(hash) + const hash = blake2b(32) + if (encoding === 'hex') { + data.forEach(str => hash.update(hex.toBytes(str))) + } else { + const enc = new TextEncoder() + data.forEach(str => hash.update(enc.encode(str))) + } + return hash.digest('hex').toUpperCase() } /** @@ -95,10 +86,9 @@ export async function hash (data: string | string[]): Promise { * @returns {Promise} Hexadecimal-formatted signature */ export async function sign (key: string, ...input: string[]): Promise { - const enc = new TextEncoder() - const data = input.map(str => { return bytes.toHex(enc.encode(str)) }) + const data = await hash(input) const signature = Ed25519.sign( - await blake2b(data), + hex.toBytes(data), hex.toBytes(key)) return bytes.toHex(signature) } @@ -167,12 +157,11 @@ export async function sweep (rpc: Rpc | string | URL, wallet: Blake2bWallet | Bi * @returns {boolean} True if the data was signed by the public key's matching private key */ export async function verify (key: string, signature: string, ...input: string[]): Promise { - const enc = new TextEncoder() - const data = input.map(str => { return bytes.toHex(enc.encode(str)) }) + const data = await hash(input) return Ed25519.verify( - await blake2b(data), + hex.toBytes(data), hex.toBytes(key), hex.toBytes(signature)) } -export default { blake2b, convert, hash, sign, sweep, verify } +export default { convert, hash, sign, sweep, verify } -- 2.34.1