From 7bf8f075e240cd2955884451963ab18259eadfc3 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 4 Aug 2025 16:28:48 -0700 Subject: [PATCH] Remove deprecated account signing method. Remove deprecated references to account encryption with passwords. Simplify account public key derivation since private key is not retained. --- src/lib/account.ts | 88 ++++++++++++---------------------------------- 1 file changed, 23 insertions(+), 65 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index cc6fa3f..05d175a 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -124,29 +124,28 @@ export class Account { static import (keypairs: KeyPair[]): Account[] /** * 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. + * a public key and then discarded. * - * @param {KeyPair} keypair - Index and keys of the account - * @param {Key} password - Used to encrypt the private key + * @param {KeyPair} keypair - Index and key of the account + * @param {string} type - Indicates a private key * @returns {Promise} Promise for a new Account object */ - static async import (keypair: KeyPair, password: Key): Promise + static async import (keypair: KeyPair, type: 'private'): Promise /** * 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. + * derive public keys and then discarded. * * @param {KeyPair[]} keypairs - Indexes and keys of the accounts - * @param {Key} password - Used to encrypt the private keys + * @param {string} type - Indicates private keys * @returns {Promise} Promise for array of new Account objects */ - static async import (keypairs: KeyPair[], password: Key): Promise - static import (input: Key | KeyPair | (Key | KeyPair)[], password?: Key): Account | Account[] | Promise { + static async import (keypairs: KeyPair[], type: 'private'): Promise + static import (input: Key | KeyPair | (Key | KeyPair)[], type?: 'private'): Account | Account[] | Promise { const isInputArray = Array.isArray(input) const inputs = isInputArray ? input : [input] - if (this.#isKeyPairs(inputs) && password != null) { + if (this.#isKeyPairs(inputs) && type === 'private') { return new Promise((resolve, reject): void => { - this.#fromPrivate(inputs, password) + this.#fromPrivate(inputs) .then(r => resolve(isInputArray ? r : r[0])) .catch(e => reject(e)) }) @@ -187,24 +186,6 @@ export class Account { this.#weight = BigInt(weight) } - /** - * Signs a block using the private key of the account. The signature is - * appended to the signature field of the block before being returned. - * - * @param {(ChangeBlock|ReceiveBlock|SendBlock)} block - The block data to be hashed and signed - * @param {Key} password - Required to decrypt the private key for signing - * @returns {Promise} Hexadecimal-formatted 64-byte signature - */ - // async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: string): Promise { - // try { - // const signature = await NanoNaCl.detached(hex.toBytes(block.hash), new Uint8Array(await this.#getPrivateKey(password))) - // block.signature = bytes.toHex(signature) - // return block.signature - // } catch (err) { - // throw new Error(`Failed to sign block`, { cause: err }) - // } - // } - /** * Validates a Nano address with 'nano' and 'xrb' prefixes * Derived from https://github.com/alecrios/nano-address-validator @@ -253,54 +234,31 @@ export class 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 + * the corresponding public key and then discarded. * * @param {KeyPair} keypairs - Indexes and keys of the accounts - * @param {number} [index] - Account number used when deriving the key * @returns {Account} A new Account object */ - static async #fromPrivate (keypairs: KeyPair[], password: Key): Promise { - if (typeof password === 'string') password = utf8.toBytes(password) - if (password == null || !(password instanceof Uint8Array)) { - throw new Error('Invalid password when importing Account') - } + static async #fromPrivate (keypairs: KeyPair[]): Promise { + try { + const accounts: Account[] = [] + for (let keypair of keypairs) { + let { index, privateKey } = keypair + if (index == null) { + throw new RangeError('Index missing for Account') + } + this.#validateKey(privateKey) + if (typeof privateKey === 'string') privateKey = hex.toBytes(privateKey) - const accounts: Account[] = [] - const privateAccounts: NamedData = { - method: 'store', - store: 'Account', - password: password.buffer - } - for (let keypair of keypairs) { - let { index, privateKey } = keypair - if (index == null) { - throw new RangeError('Index missing for Account') - } - this.#validateKey(privateKey) - if (typeof privateKey === 'string') privateKey = hex.toBytes(privateKey) - try { const publicKey = await NanoNaCl.convert(privateKey) - privateAccounts[bytes.toHex(publicKey)] = privateKey.buffer const address = this.#keyToAddress(publicKey) this.#isInternal = true accounts.push(new this(address, publicKey, index)) - } catch (err) { - throw new Error(`Failed to derive public key from private key`, { cause: err }) } - } - - try { - // const { result } = await SafeWorker.request(privateAccounts) - // if (!result) { - // throw null - // } return accounts } catch (err) { - throw new Error(`Failed to lock Accounts`, { cause: err }) - } finally { - bytes.erase(password) + throw new Error('Failed to import Accounts from private keys', { cause: err }) } } -- 2.47.3