]> git.codecow.com Git - libnemo.git/commitdiff
Fix account handling array buffer.
authorChris Duncan <chris@zoso.dev>
Sat, 2 Aug 2025 05:51:49 +0000 (22:51 -0700)
committerChris Duncan <chris@zoso.dev>
Sat, 2 Aug 2025 05:51:49 +0000 (22:51 -0700)
src/lib/account.ts
src/lib/blake2b.ts
test/test.blocks.mjs

index 2d75e9227534d4f4da41b2fe526d30fa7e8b0e59..cc6fa3fba7b9914e445d4298df37576b443b0ac0 100644 (file)
@@ -2,7 +2,6 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
 import { Blake2b } from './blake2b'\r
-import { ChangeBlock, ReceiveBlock, SendBlock } from './block'\r
 import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants'\r
 import { base32, bytes, hex, utf8 } from './convert'\r
 import { NanoNaCl } from './nano-nacl'\r
@@ -124,9 +123,8 @@ export class Account {
        */\r
        static import (keypairs: KeyPair[]): Account[]\r
        /**\r
-       * Instantiates an Account object from its private key which is then encrypted\r
-       * and stored in IndexedDB. The corresponding public key will automatically be\r
-       * derived and saved.\r
+       * Instantiates an Account object from its private key which is used to derive\r
+       * a public key and then discarded. The public key is saved in the database.\r
        *\r
        * @param {KeyPair} keypair - Index and keys of the account\r
        * @param {Key} password - Used to encrypt the private key\r
@@ -134,9 +132,9 @@ export class Account {
        */\r
        static async import (keypair: KeyPair, password: Key): Promise<Account>\r
        /**\r
-       * Instantiates Account objects from their private keys which are then\r
-       * encrypted and stored in IndexedDB. The corresponding public keys will\r
-       * automatically be derived and saved.\r
+       * Instantiates Account objects from their private keys which are used to\r
+       * derive public keys and then discarded. The public keys are saved in the\r
+       * database.\r
        *\r
        * @param {KeyPair[]} keypairs - Indexes and keys of the accounts\r
        * @param {Key} password - Used to encrypt the private keys\r
@@ -329,7 +327,9 @@ export class Account {
                let index\r
                for (let keypair of keypairs) {\r
                        let keyError, addressError\r
-                       const key = keypair.publicKey\r
+                       const key = keypair.publicKey instanceof ArrayBuffer\r
+                               ? new Uint8Array(keypair.publicKey)\r
+                               : keypair.publicKey\r
                        try {\r
                                this.#validateKey(key)\r
                                publicKey = (typeof key === 'string')\r
@@ -419,6 +419,9 @@ export class Account {
                if (key === undefined) {\r
                        throw new TypeError(`Key is undefined`)\r
                }\r
+               if (key instanceof ArrayBuffer) {\r
+                       key = new Uint8Array(key)\r
+               }\r
                if (typeof key !== 'string' && !(key instanceof Uint8Array)) {\r
                        throw new TypeError(`Key must be a string or Uint8Array`)\r
                }\r
index 6bde3d7a733595174d55c06ef7cb96981e0e5173..dc3a98c331a993a0fb10a4815f12bd713825bee8 100644 (file)
@@ -257,6 +257,9 @@ export class Blake2b {
        }
 
        update (input: Uint8Array): Blake2b {
+               if (input instanceof ArrayBuffer) {
+                       input = new Uint8Array(input)
+               }
                if (!(input instanceof Uint8Array)) {
                        throw new TypeError(`input must be Uint8Array or Buffer`)
                }
index 1e536377233c55db2834c7741a1b74d543681f50..db876c34c641b5e453f9ff41d756ae1c7046d75a 100644 (file)
@@ -114,7 +114,7 @@ await Promise.all([
                        )\r
                        await assert.rejects(wallet.sign(0, block))\r
                        assert.equal(block.hash, NANO_TEST_VECTORS.OPEN_BLOCK.hash)\r
-                       assert.notEqual(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature)\r
+                       assert.ok(block.signature === undefined)\r
                })\r
 \r
                await test('sign open block with private key', async () => {\r