From f18a0ac0b8e6f7e1e06191d9633398aa41c9f383 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 22 Aug 2025 15:23:34 -0700 Subject: [PATCH] Remove redundant type checking now handled by Address class. --- src/lib/account/index.ts | 76 +++++++--------------------------------- 1 file changed, 13 insertions(+), 63 deletions(-) diff --git a/src/lib/account/index.ts b/src/lib/account/index.ts index bd8f57d..222536f 100644 --- a/src/lib/account/index.ts +++ b/src/lib/account/index.ts @@ -4,8 +4,8 @@ import { KeyPair } from '#types' import { Block } from '../block' import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, BURN_ADDRESS, PREFIX, PREFIX_LEGACY } from '../constants' -import { base32, bytes, hex } from '../convert' -import { Blake2b, NanoNaCl } from '../crypto' +import { bytes, hex } from '../convert' +import { NanoNaCl } from '../crypto' import { Rpc } from '../rpc' import { Address } from './address' import { _refresh } from './refresh' @@ -103,15 +103,13 @@ export class Account { set representative_block (v: string | undefined) { this.#representative_block = v } set weight (v: bigint | number | string) { this.#weight = BigInt(v) } - private constructor (address: string, publicKey: string | Uint8Array, index?: number) { + private constructor (address: Address, publicKey: Uint8Array, index?: number) { if (!Account.#isInternal) { throw new Error('Account cannot be instantiated directly. Use `load()` instead.') } Account.#isInternal = false - this.#address = new Address(address) - this.#publicKey = typeof publicKey === 'string' - ? hex.toBytes(publicKey) - : publicKey + this.#address = address + this.#publicKey = publicKey this.#index = index } @@ -242,22 +240,6 @@ export class Account { return _validate(address) } - /** - * Converts a Nano address to a public key. - * - * @param {string} address - Prefixed with `nano_` - * @returns Public key bytes as Uint8Array - */ - static #addressToKey (address: string): Uint8Array { - const publicKey = base32.toBytes(address.slice(-60, -8)) - const checksum = base32.toBytes(address.slice(-8)) - const rechecksum = new Blake2b(5).update(publicKey).digest().reverse() - if (bytes.toHex(checksum) !== bytes.toHex(rechecksum)) { - throw new Error('Checksum mismatch in address') - } - return publicKey - } - /** * Instantiates an Account object from its private key which is used to derive * the corresponding public key and then discarded. @@ -283,7 +265,7 @@ export class Account { throw new TypeError(`Private key must be ${ACCOUNT_KEY_BYTE_LENGTH} bytes`) } const publicKey = await NanoNaCl.convert(privateKey) - const address = Address.fromPublicKey(publicKey) + const address = new Address(publicKey) this.#isInternal = true accounts.push(new this(address, publicKey, index)) } @@ -313,33 +295,14 @@ export class Account { const accounts: Account[] = [] for (let keypair of keypairs) { - const { index } = keypair - if (typeof keypair.publicKey === 'string') { - if (RegExp(`^[A-F0-9]{${ACCOUNT_KEY_HEX_LENGTH}}$`, 'i').test(keypair.publicKey)) { - const publicKey = hex.toBytes(keypair.publicKey) - const address = Address.fromPublicKey(publicKey) - this.#isInternal = true - accounts.push(new this(address, publicKey, index)) - } else if (RegExp(`(${PREFIX}|${PREFIX_LEGACY})`).test(keypair.publicKey)) { - const address = keypair.publicKey - const publicKey = new Address(address).toPublicKey() - this.#isInternal = true - accounts.push(new this(address, publicKey, index)) - } else { - throw new TypeError('Invalid string', { cause: keypair.publicKey }) - } - } else if (keypair.publicKey instanceof ArrayBuffer) { - if (keypair.publicKey.byteLength === ACCOUNT_KEY_BYTE_LENGTH) { - const publicKey = new Uint8Array(keypair.publicKey) - const address = Address.fromPublicKey(publicKey) - this.#isInternal = true - accounts.push(new this(address, publicKey, index)) - } else { - throw new TypeError('Invalid buffer', { cause: keypair.publicKey }) - } - } else { - throw new TypeError('Invalid Account input', { cause: keypair.publicKey }) + if (keypair.publicKey == null) { + throw new TypeError('Account address or public key is required', { cause: keypair.publicKey }) } + const { index } = keypair + const address = new Address(keypair.publicKey) + const publicKey = address.toPublicKey() + this.#isInternal = true + accounts.push(new this(address, publicKey, index)) } return accounts } catch (err) { @@ -389,19 +352,6 @@ export class Account { } return true } - - /** - * Converts a public key to a Nano address. - * - * @param {Uint8Array} publicKey - Public key bytes as Uint8Array - * @returns Nano address string using `nano_` prefix - */ - static #keyToAddress (publicKey: Uint8Array): string { - const checksum = new Blake2b(5).update(publicKey).digest().reverse() - const encodedPublicKey = bytes.toBase32(publicKey) - const encodedChecksum = bytes.toBase32(checksum) - return `${PREFIX}${encodedPublicKey}${encodedChecksum}` - } } export class AccountList extends Object { -- 2.47.3