]> git.codecow.com Git - libnemo.git/commitdiff
Refactor block hash and link to be stored as bytes and converted as needed.
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 05:00:37 +0000 (22:00 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 05:00:37 +0000 (22:00 -0700)
src/lib/block.ts

index 0bf814c030e4ff721c25b89a61b19cef68c89362..6e51e601fa7e6283e6c533bbee29c2a12de8e9e8 100644 (file)
@@ -21,7 +21,7 @@ class Block {
        balance: bigint
        previous: string
        representative: Account
-       link?: string
+       link?: Uint8Array<ArrayBuffer>
        signature?: string
        work?: string
 
@@ -48,9 +48,9 @@ class Block {
        /**
        * Calculates the block hash using Blake2b.
        *
-       * @returns {Promise<string>} Block data hashed to a byte array
+       * @returns {Uint8Array<ArrayBuffer>} Block data hashed to a byte array
        */
-       get hash (): string {
+       get hash (): Uint8Array<ArrayBuffer> {
                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 })
                }