]> git.codecow.com Git - libnemo.git/commitdiff
Revert block hash output but implement it as a wrapper for private hash byte method.
authorChris Duncan <chris@zoso.dev>
Thu, 7 Aug 2025 19:42:55 +0000 (12:42 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 7 Aug 2025 19:42:55 +0000 (12:42 -0700)
src/lib/block.ts
src/lib/ledger.ts
src/lib/wallet.ts
src/types.d.ts

index 44fed0609f376c4690d6e7c349b471d8cf92c748..7ac1bc052a6082abeef73cb3fee35ad933c8b4c8 100644 (file)
@@ -121,7 +121,7 @@ export class Block {
        *
        * @returns {Uint8Array<ArrayBuffer>} Block data hashed to a byte array
        */
-       get hash (): Uint8Array<ArrayBuffer> {
+       get #hash (): Uint8Array<ArrayBuffer> {
                try {
                        if (this.link == null) {
                                throw new Error('Link is required')
@@ -145,6 +145,14 @@ export class Block {
                        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 bytes.toHex(this.#hash)
+       }
 
        /**
        * Converts the block to JSON format as expected by the `process` RPC.
@@ -302,7 +310,7 @@ export class Block {
                        }
                        this.link = (typeof sendBlock === 'string')
                                ? hex.toBytes(sendBlock)
-                               : sendBlock.hash
+                               : hex.toBytes(sendBlock.hash)
 
                        return this
                } catch (err) {
@@ -402,7 +410,7 @@ export class Block {
                                if (typeof input !== 'number' && typeof input !== 'string' && !(input instanceof Wallet)) {
                                        throw new TypeError('Invalid signing input')
                                } else if (typeof input === 'string' && /^[A-F0-9]{64}$/.test(input)) {
-                                       const sig = await NanoNaCl.detached(this.hash, hex.toBytes(input))
+                                       const sig = await NanoNaCl.detached(this.#hash, hex.toBytes(input))
                                        this.signature = bytes.toHex(sig)
                                } else if (input instanceof Wallet && typeof param === 'number') {
                                        const wallet = input
@@ -448,7 +456,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(this.#hash, hex.toBytes(this.signature ?? ''), hex.toBytes(key))
                } catch (err) {
                        throw new Error('Failed to verify block signature', { cause: err })
                }
index 0b9a00a786d6dd843cb8704fd78c0140c437f082..babb9bdc836101a88f3881df548810d4c595b264 100644 (file)
@@ -12,7 +12,7 @@ import { bytes, dec, hex } from './convert'
 import { Database } from './database'\r
 import { Rpc } from './rpc'\r
 import { Wallet } from './wallet'\r
-import { DeviceStatus, KeyPair, LedgerAccountResponse, LedgerResponse, LedgerSignResponse, LedgerVersionResponse } from '#types'\r
+import { DeviceStatus, LedgerAccountResponse, LedgerResponse, LedgerSignResponse, LedgerVersionResponse } from '#types'\r
 \r
 /**\r
 * Ledger hardware wallet created by communicating with a Ledger device via ADPU\r
@@ -358,7 +358,7 @@ export class Ledger extends Wallet {
                const testOpenBlock = await new Block(testAccount.address, '0', testAccount.publicKey, testAccount.address)\r
                        .receive(testAccount.publicKey, 0)\r
                        .sign(testWallet, 0)\r
-               const testSendBlock = new Block(testAccount.address, '0', bytes.toHex(testOpenBlock.hash), testAccount.address)\r
+               const testSendBlock = new Block(testAccount.address, '0', testOpenBlock.hash, testAccount.address)\r
                        .send(testAccount.address, 0)\r
                const testSignature = await testWallet.sign(0, testOpenBlock, 'hex')\r
                try {\r
@@ -543,7 +543,7 @@ export class Ledger extends Wallet {
                        return { status, signature: null }\r
                }\r
                if (response.byteLength === 98) {\r
-                       const hash = response.slice(0, 32)\r
+                       const hash = bytes.toHex(response.slice(0, 32))\r
                        const signature = bytes.toHex(response.slice(32, 96))\r
                        return { status, signature, hash }\r
                }\r
index 5617a016309704c430906f81ea95ac96d00cac27..37ea751bccf25116cbec0528c37f037ec4add1a1 100644 (file)
@@ -380,7 +380,7 @@ export class Wallet {
                        const { signature } = await this.#safe.request<ArrayBuffer>({\r
                                action: 'sign',\r
                                index,\r
-                               data: block.hash.buffer\r
+                               data: hex.toBuffer(block.hash)\r
                        })\r
                        const sig = new Uint8Array(signature)\r
                        block.signature = bytes.toHex(sig)\r
index 21201e158a45f60835d529ca1a05fdea58c0f69a..4e954e2b4f351a40911b0fa96985b9f61618be0c 100644 (file)
@@ -251,9 +251,9 @@ export declare 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
        */
-       get hash (): Uint8Array<ArrayBuffer>
+       get hash (): string
        /**
        * Converts the block to JSON format as expected by the `process` RPC.
        *
@@ -747,7 +747,7 @@ interface LedgerAccountResponse extends LedgerResponse {
 
 interface LedgerSignResponse extends LedgerResponse {
        signature: string | null,
-       hash?: Uint8Array<ArrayBuffer>
+       hash?: string
 }
 
 /**