From: Chris Duncan Date: Wed, 6 Aug 2025 05:00:37 +0000 (-0700) Subject: Refactor block hash and link to be stored as bytes and converted as needed. X-Git-Tag: v0.10.5~43^2~73 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=7554a77e010b4c597b3bf5fdc44693cf7ecb33f0;p=libnemo.git Refactor block hash and link to be stored as bytes and converted as needed. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index 0bf814c..6e51e60 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -21,7 +21,7 @@ class Block { balance: bigint previous: string representative: Account - link?: string + link?: Uint8Array signature?: string work?: string @@ -48,9 +48,9 @@ class Block { /** * Calculates the block hash using Blake2b. * - * @returns {Promise} Block data hashed to a byte array + * @returns {Uint8Array} Block data hashed to a byte array */ - get hash (): string { + get hash (): Uint8Array { try { if (this.link == null) { throw new Error('Block link is required') @@ -61,11 +61,11 @@ class Block { this.previous.padStart(64, '0'), this.representative.publicKey, dec.toHex(this.balance, 32), - this.link.padStart(64, '0') + this.link ] const hash = new Blake2b(32) - data.forEach(str => hash.update(hex.toBytes(str))) - return hash.digest('hex').toUpperCase() + data.forEach(d => typeof d === 'string' ? hash.update(hex.toBytes(d)) : d) + return hash.digest() } catch (err) { console.error(err) throw new Error('Failed to hash block', { cause: err }) @@ -88,7 +88,7 @@ class Block { "previous": this.previous, "representative": this.representative.address ?? '', "balance": this.balance.toString(), - "link": this.link, + "link": bytes.toHex(this.link), "signature": this.signature ?? '', "work": this.work ?? '' } @@ -109,7 +109,7 @@ class Block { } try { this.subtype = 'change' - this.link = Account.import(BURN_ADDRESS).publicKey + this.link = hex.toBytes(Account.import(BURN_ADDRESS).publicKey) return this } catch (err) { this.subtype = undefined @@ -141,7 +141,7 @@ class Block { throw new TypeError('Invalid subtype') } this.link = (typeof send === 'string') - ? send + ? hex.toBytes(send) : send.hash return this } catch (err) { @@ -341,7 +341,7 @@ class Block { throw new TypeError('Invalid input') } else if (typeof input === 'string') { - const sig = await NanoNaCl.detached(hex.toBytes(this.hash), hex.toBytes(input)) + const sig = await NanoNaCl.detached(this.hash, hex.toBytes(input)) this.signature = bytes.toHex(sig) return this @@ -408,8 +408,8 @@ class Block { } case 'send': { this.link = (typeof account === 'string') - ? Account.import(account).publicKey - : account.publicKey + ? hex.toBytes(Account.import(account).publicKey) + : hex.toBytes(account.publicKey) break } default: { @@ -435,7 +435,7 @@ class Block { throw new Error('Provide a key for block signature verification.') } try { - return await NanoNaCl.verify(hex.toBytes(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 derive public key from private key`, { cause: err }) }