From 22a497501b0a11aaf3a4cf442f5d0d82a21cdf40 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 20 Sep 2025 15:00:19 -0700 Subject: [PATCH] Return block hash as hex only and convert back to bytes if needed. --- src/lib/block.ts | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index 16bafc7..306e3c4 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -124,11 +124,9 @@ export class Block { /** * Calculates the block hash using Blake2b. * - * @returns {Uint8Array} Block data hashed to a byte array + * @returns {string} Hexadecimal representation of 32-byte hash of block data */ - #hash (): Uint8Array - #hash (format: 'hex'): string - #hash (format?: unknown): string | Uint8Array { + get hash (): string { try { if (this.link == null) { throw new Error('Link is required') @@ -146,20 +144,12 @@ export class Block { ] const hash = new Blake2b(32) data.forEach(d => typeof d === 'string' ? hash.update(hex.toBytes(d)) : hash.update(d)) - return format === 'hex' ? bytes.toHex(hash.digest()) : hash.digest() + return bytes.toHex(hash.digest()).toUpperCase() } catch (err) { console.error(err) throw new Error('Failed to hash block', { cause: err }) } } - /** - * Calculates the block hash using Blake2b. - * - * @returns {string} Hexadecimal representation of 32-byte hash of block data - */ - get hash (): string { - return this.#hash('hex').toUpperCase() - } /** * Converts the block to JSON format as expected by the `process` RPC. @@ -421,7 +411,7 @@ export class Block { return new Promise(async (resolve, reject) => { try { if (typeof input === 'string' && /^[A-F0-9]{64}$/i.test(input)) { - const signature = await NanoNaCl.detached(this.#hash(), hex.toBytes(input)) + const signature = await NanoNaCl.detached(hex.toBytes(this.hash), hex.toBytes(input)) this.signature = bytes.toHex(signature) } else if (input instanceof Wallet && typeof index === 'number' && (frontier === undefined || frontier instanceof (this.constructor as typeof Block)) @@ -453,7 +443,7 @@ export class Block { if (typeof key !== 'string') { throw new Error('Invalid key') } - return await NanoNaCl.verify(this.#hash(), hex.toBytes(this.signature ?? ''), hex.toBytes(key)) + return await NanoNaCl.verify(hex.toBytes(this.hash), hex.toBytes(this.signature ?? ''), hex.toBytes(key)) } catch (err) { throw new Error('Failed to verify block signature', { cause: err }) } -- 2.47.3