From: Chris Duncan Date: Thu, 7 Aug 2025 19:42:55 +0000 (-0700) Subject: Revert block hash output but implement it as a wrapper for private hash byte method. X-Git-Tag: v0.10.5~43^2~53 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=97c52f5ccd3c5949c1bfc21d9e8e683c9849fb1e;p=libnemo.git Revert block hash output but implement it as a wrapper for private hash byte method. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index 44fed06..7ac1bc0 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -121,7 +121,7 @@ export class Block { * * @returns {Uint8Array} Block data hashed to a byte array */ - get hash (): Uint8Array { + get #hash (): Uint8Array { 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 }) } diff --git a/src/lib/ledger.ts b/src/lib/ledger.ts index 0b9a00a..babb9bd 100644 --- a/src/lib/ledger.ts +++ b/src/lib/ledger.ts @@ -12,7 +12,7 @@ import { bytes, dec, hex } from './convert' import { Database } from './database' import { Rpc } from './rpc' import { Wallet } from './wallet' -import { DeviceStatus, KeyPair, LedgerAccountResponse, LedgerResponse, LedgerSignResponse, LedgerVersionResponse } from '#types' +import { DeviceStatus, LedgerAccountResponse, LedgerResponse, LedgerSignResponse, LedgerVersionResponse } from '#types' /** * Ledger hardware wallet created by communicating with a Ledger device via ADPU @@ -358,7 +358,7 @@ export class Ledger extends Wallet { const testOpenBlock = await new Block(testAccount.address, '0', testAccount.publicKey, testAccount.address) .receive(testAccount.publicKey, 0) .sign(testWallet, 0) - const testSendBlock = new Block(testAccount.address, '0', bytes.toHex(testOpenBlock.hash), testAccount.address) + const testSendBlock = new Block(testAccount.address, '0', testOpenBlock.hash, testAccount.address) .send(testAccount.address, 0) const testSignature = await testWallet.sign(0, testOpenBlock, 'hex') try { @@ -543,7 +543,7 @@ export class Ledger extends Wallet { return { status, signature: null } } if (response.byteLength === 98) { - const hash = response.slice(0, 32) + const hash = bytes.toHex(response.slice(0, 32)) const signature = bytes.toHex(response.slice(32, 96)) return { status, signature, hash } } diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 5617a01..37ea751 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -380,7 +380,7 @@ export class Wallet { const { signature } = await this.#safe.request({ action: 'sign', index, - data: block.hash.buffer + data: hex.toBuffer(block.hash) }) const sig = new Uint8Array(signature) block.signature = bytes.toHex(sig) diff --git a/src/types.d.ts b/src/types.d.ts index 21201e1..4e954e2 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -251,9 +251,9 @@ export declare 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 */ - get hash (): Uint8Array + 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 + hash?: string } /**