From 175e26752703e984e1216a249788138d9fb13059 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 1 Aug 2025 22:51:49 -0700 Subject: [PATCH] Fix account handling array buffer. --- src/lib/account.ts | 19 +++++++++++-------- src/lib/blake2b.ts | 3 +++ test/test.blocks.mjs | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index 2d75e92..cc6fa3f 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -2,7 +2,6 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { Blake2b } from './blake2b' -import { ChangeBlock, ReceiveBlock, SendBlock } from './block' import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants' import { base32, bytes, hex, utf8 } from './convert' import { NanoNaCl } from './nano-nacl' @@ -124,9 +123,8 @@ export class Account { */ static import (keypairs: KeyPair[]): Account[] /** - * Instantiates an Account object from its private key which is then encrypted - * and stored in IndexedDB. The corresponding public key will automatically be - * derived and saved. + * Instantiates an Account object from its private key which is used to derive + * a public key and then discarded. The public key is saved in the database. * * @param {KeyPair} keypair - Index and keys of the account * @param {Key} password - Used to encrypt the private key @@ -134,9 +132,9 @@ export class Account { */ static async import (keypair: KeyPair, password: Key): Promise /** - * Instantiates Account objects from their private keys which are then - * encrypted and stored in IndexedDB. The corresponding public keys will - * automatically be derived and saved. + * Instantiates Account objects from their private keys which are used to + * derive public keys and then discarded. The public keys are saved in the + * database. * * @param {KeyPair[]} keypairs - Indexes and keys of the accounts * @param {Key} password - Used to encrypt the private keys @@ -329,7 +327,9 @@ export class Account { let index for (let keypair of keypairs) { let keyError, addressError - const key = keypair.publicKey + const key = keypair.publicKey instanceof ArrayBuffer + ? new Uint8Array(keypair.publicKey) + : keypair.publicKey try { this.#validateKey(key) publicKey = (typeof key === 'string') @@ -419,6 +419,9 @@ export class Account { if (key === undefined) { throw new TypeError(`Key is undefined`) } + if (key instanceof ArrayBuffer) { + key = new Uint8Array(key) + } if (typeof key !== 'string' && !(key instanceof Uint8Array)) { throw new TypeError(`Key must be a string or Uint8Array`) } diff --git a/src/lib/blake2b.ts b/src/lib/blake2b.ts index 6bde3d7..dc3a98c 100644 --- a/src/lib/blake2b.ts +++ b/src/lib/blake2b.ts @@ -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`) } diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index 1e53637..db876c3 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -114,7 +114,7 @@ await Promise.all([ ) await assert.rejects(wallet.sign(0, block)) assert.equal(block.hash, NANO_TEST_VECTORS.OPEN_BLOCK.hash) - assert.notEqual(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature) + assert.ok(block.signature === undefined) }) await test('sign open block with private key', async () => { -- 2.47.3