]> git.codecow.com Git - libnemo.git/commitdiff
Return block hash as hex only and convert back to bytes if needed.
authorChris Duncan <chris@zoso.dev>
Sat, 20 Sep 2025 22:00:19 +0000 (15:00 -0700)
committerChris Duncan <chris@zoso.dev>
Sat, 20 Sep 2025 22:00:19 +0000 (15:00 -0700)
src/lib/block.ts

index 16bafc72e4c3d397dc048db9a1dfbb58c0b0991b..306e3c44ea7b4bd637fb09b58a524182734b9720 100644 (file)
@@ -124,11 +124,9 @@ export class Block {
        /**
        * Calculates the block hash using Blake2b.
        *
-       * @returns {Uint8Array<ArrayBuffer>} Block data hashed to a byte array
+       * @returns {string} Hexadecimal representation of 32-byte hash of block data
        */
-       #hash (): Uint8Array<ArrayBuffer>
-       #hash (format: 'hex'): string
-       #hash (format?: unknown): string | Uint8Array<ArrayBuffer> {
+       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 })
                }