]> zoso.dev Git - libnemo.git/commitdiff
Return block hash as hex instead of bytes, and update relevant tests.
authorChris Duncan <chris@zoso.dev>
Mon, 25 Nov 2024 20:43:57 +0000 (12:43 -0800)
committerChris Duncan <chris@zoso.dev>
Mon, 25 Nov 2024 20:43:57 +0000 (12:43 -0800)
src/lib/block.ts
test/sign-blocks.test.mjs

index 91ca288234616255432dedbc1005883232c7f2c3..a0d706f3802337b3ef01b6c916dd029c4378237b 100644 (file)
@@ -58,9 +58,9 @@ abstract class Block {
        /**
        * Hashes the block using Blake2b.
        *
-       * @returns {Promise<Uint8Array>} Block data hashed to a byte array
+       * @returns {Promise<string>} Block data hashed to a byte array
        */
-       async hash (): Promise<Uint8Array> {
+       async hash (): Promise<string> {
                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)
index 2e9001ad03ace35ebdd9cc770c1d205eb7f26446..35f7c23bd7dda3baba354452cefbf37643b3ccdf 100644 (file)
@@ -65,14 +65,15 @@ describe('block signing tests using official test vectors', async () => {
        it('should create a valid signature for an open block', async () => {\r
                const block = new ReceiveBlock(\r
                        NANO_TEST_VECTORS.OPEN_BLOCK.account,\r
-                       NANO_TEST_VECTORS.OPEN_BLOCK.balance,\r
-                       NANO_TEST_VECTORS.OPEN_BLOCK.link,\r
                        '0',\r
+                       NANO_TEST_VECTORS.OPEN_BLOCK.link,\r
+                       NANO_TEST_VECTORS.OPEN_BLOCK.balance,\r
                        NANO_TEST_VECTORS.OPEN_BLOCK.representative,\r
                        NANO_TEST_VECTORS.OPEN_BLOCK.previous,\r
                        NANO_TEST_VECTORS.OPEN_BLOCK.work\r
                )\r
                await block.sign(NANO_TEST_VECTORS.OPEN_BLOCK.key)\r
+               assert.equal(await block.hash(), NANO_TEST_VECTORS.OPEN_BLOCK.hash)\r
                assert.equal(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature)\r
        })\r
 \r
@@ -87,6 +88,7 @@ describe('block signing tests using official test vectors', async () => {
                        NANO_TEST_VECTORS.RECEIVE_BLOCK.work\r
                )\r
                await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key)\r
+               assert.equal(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash)\r
                assert.equal(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature)\r
        })\r
 \r
@@ -100,6 +102,7 @@ describe('block signing tests using official test vectors', async () => {
                        NANO_TEST_VECTORS.RECEIVE_BLOCK.previous\r
                )\r
                await block.sign(NANO_TEST_VECTORS.RECEIVE_BLOCK.key)\r
+               assert.equal(await block.hash(), NANO_TEST_VECTORS.RECEIVE_BLOCK.hash)\r
                assert.equal(block.signature, NANO_TEST_VECTORS.RECEIVE_BLOCK.signature)\r
                assert.equal(block.work, '')\r
        })\r
@@ -115,6 +118,7 @@ describe('block signing tests using official test vectors', async () => {
                        NANO_TEST_VECTORS.SEND_BLOCK.work\r
                )\r
                await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key)\r
+               assert.equal(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash)\r
                assert.equal(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature)\r
        })\r
 \r
@@ -128,6 +132,7 @@ describe('block signing tests using official test vectors', async () => {
                        NANO_TEST_VECTORS.SEND_BLOCK.previous\r
                )\r
                await block.sign(NANO_TEST_VECTORS.SEND_BLOCK.key)\r
+               assert.equal(await block.hash(), NANO_TEST_VECTORS.SEND_BLOCK.hash)\r
                assert.equal(block.signature, NANO_TEST_VECTORS.SEND_BLOCK.signature)\r
                assert.equal(block.work, '')\r
        })\r