From e34e0c40809e12fea88d5accdd180f6653a24ce5 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 22 Aug 2025 02:44:49 -0700 Subject: [PATCH] Implement new address class in Account and pass its public key as bytes. --- src/lib/account/address.ts | 8 ++++---- src/lib/account/index.ts | 26 +++++++++++++++----------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/lib/account/address.ts b/src/lib/account/address.ts index 2633ee7..0e6f5a5 100644 --- a/src/lib/account/address.ts +++ b/src/lib/account/address.ts @@ -5,7 +5,7 @@ import { ACCOUNT_KEY_HEX_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from "../cons import { base32, bytes, hex } from "../convert" import { Blake2b } from "../crypto" -class Address { +export class Address { #address: string /** @@ -62,9 +62,9 @@ class Address { * * @returns Public key bytes as ArrayBuffer */ - toPublicKey (): ArrayBuffer { - const publicKey = base32.toBuffer(this.#address.slice(-60, -8)) - const checksum = base32.toBuffer(this.#address.slice(-8)) + toPublicKey (): Uint8Array { + const publicKey = base32.toBytes(this.#address.slice(-60, -8)) + const checksum = base32.toBytes(this.#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') diff --git a/src/lib/account/index.ts b/src/lib/account/index.ts index b957c11..bd8f57d 100644 --- a/src/lib/account/index.ts +++ b/src/lib/account/index.ts @@ -3,10 +3,11 @@ import { KeyPair } from '#types' import { Block } from '../block' -import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from '../constants' +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 { Rpc } from '../rpc' +import { Address } from './address' import { _refresh } from './refresh' import { _validate } from './validate' @@ -22,7 +23,7 @@ export class Account { static #isInternal: boolean = false - #address: string + #address?: Address #index?: number #publicKey: Uint8Array @@ -43,7 +44,12 @@ export class Account { #representative_block?: string #weight?: bigint - get address (): string { return `${PREFIX}${this.#address}` } + get address (): string { + if (this.#address == null) { + throw new Error('Account not found') + } + return this.#address.toString() + } get index (): number | undefined { return this.#index } get publicKey (): string { return bytes.toHex(this.#publicKey) } @@ -102,9 +108,7 @@ export class Account { throw new Error('Account cannot be instantiated directly. Use `load()` instead.') } Account.#isInternal = false - this.#address = address - .replace(PREFIX, '') - .replace(PREFIX_LEGACY, '') + this.#address = new Address(address) this.#publicKey = typeof publicKey === 'string' ? hex.toBytes(publicKey) : publicKey @@ -115,7 +119,7 @@ export class Account { * Releases variable references to allow garbage collection. */ async destroy (): Promise { - this.#address = '' + this.#address = undefined this.#index = undefined this.#publicKey.fill(0) @@ -279,7 +283,7 @@ export class Account { throw new TypeError(`Private key must be ${ACCOUNT_KEY_BYTE_LENGTH} bytes`) } const publicKey = await NanoNaCl.convert(privateKey) - const address = this.#keyToAddress(publicKey) + const address = Address.fromPublicKey(publicKey) this.#isInternal = true accounts.push(new this(address, publicKey, index)) } @@ -313,12 +317,12 @@ export class Account { 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 = this.#keyToAddress(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 = this.#addressToKey(address) + const publicKey = new Address(address).toPublicKey() this.#isInternal = true accounts.push(new this(address, publicKey, index)) } else { @@ -327,7 +331,7 @@ export class Account { } else if (keypair.publicKey instanceof ArrayBuffer) { if (keypair.publicKey.byteLength === ACCOUNT_KEY_BYTE_LENGTH) { const publicKey = new Uint8Array(keypair.publicKey) - const address = this.#keyToAddress(publicKey) + const address = Address.fromPublicKey(publicKey) this.#isInternal = true accounts.push(new this(address, publicKey, index)) } else { -- 2.47.3