From ccba1f9040c80cde27537ab68a8704b8229d893d Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 25 Nov 2024 12:43:57 -0800 Subject: [PATCH] Return block hash as hex instead of bytes, and update relevant tests. --- src/lib/block.ts | 8 ++++---- test/sign-blocks.test.mjs | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index 91ca288..a0d706f 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -58,9 +58,9 @@ abstract class Block { /** * Hashes the block using Blake2b. * - * @returns {Promise} Block data hashed to a byte array + * @returns {Promise} Block data hashed to a byte array */ - async hash (): Promise { + async hash (): Promise { const data = [ PREAMBLE, this.account.publicKey, @@ -70,7 +70,7 @@ abstract class Block { this.link ] const hash = await Tools.hash(data, 'hex') - return hex.toBytes(hash) + return hash } /** @@ -140,7 +140,7 @@ abstract class Block { throw new Error('No valid key found to sign block') } const signature = Ed25519.sign( - await this.hash(), + hex.toBytes(await this.hash()), hex.toBytes(key) ) this.signature = bytes.toHex(signature) diff --git a/test/sign-blocks.test.mjs b/test/sign-blocks.test.mjs index 2e9001a..35f7c23 100644 --- a/test/sign-blocks.test.mjs +++ b/test/sign-blocks.test.mjs @@ -65,14 +65,15 @@ describe('block signing tests using official test vectors', async () => { it('should create a valid signature for an open block', async () => { const block = new ReceiveBlock( NANO_TEST_VECTORS.OPEN_BLOCK.account, - NANO_TEST_VECTORS.OPEN_BLOCK.balance, - NANO_TEST_VECTORS.OPEN_BLOCK.link, '0', + NANO_TEST_VECTORS.OPEN_BLOCK.link, + NANO_TEST_VECTORS.OPEN_BLOCK.balance, NANO_TEST_VECTORS.OPEN_BLOCK.representative, NANO_TEST_VECTORS.OPEN_BLOCK.previous, NANO_TEST_VECTORS.OPEN_BLOCK.work ) await block.sign(NANO_TEST_VECTORS.OPEN_BLOCK.key) + assert.equal(await block.hash(), NANO_TEST_VECTORS.OPEN_BLOCK.hash) assert.equal(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature) }) @@ -87,6 +88,7 @@ describe('block signing tests using official test vectors', async () => { NANO_TEST_VECTORS.RECEIVE_BLOCK.work ) await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key) + assert.equal(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash) assert.equal(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature) }) @@ -100,6 +102,7 @@ describe('block signing tests using official test vectors', async () => { NANO_TEST_VECTORS.RECEIVE_BLOCK.previous ) await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key) + assert.equal(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash) assert.equal(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature) assert.equal(block.work, '') }) @@ -115,6 +118,7 @@ describe('block signing tests using official test vectors', async () => { NANO_TEST_VECTORS.SEND_BLOCK.work ) await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key) + assert.equal(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash) assert.equal(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature) }) @@ -128,6 +132,7 @@ describe('block signing tests using official test vectors', async () => { NANO_TEST_VECTORS.SEND_BLOCK.previous ) await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key) + assert.equal(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash) assert.equal(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature) assert.equal(block.work, '') }) -- 2.34.1