/**
* 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')
]
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.
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))
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 })
}