import { base32, bytes, hex } from "../convert"
import { Blake2b } from "../crypto"
-class Address {
+export class Address {
#address: string
/**
*
* @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<ArrayBuffer> {
+ 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')
\r
import { KeyPair } from '#types'\r
import { Block } from '../block'\r
-import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from '../constants'\r
+import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, BURN_ADDRESS, PREFIX, PREFIX_LEGACY } from '../constants'\r
import { base32, bytes, hex } from '../convert'\r
import { Blake2b, NanoNaCl } from '../crypto'\r
import { Rpc } from '../rpc'\r
+import { Address } from './address'\r
import { _refresh } from './refresh'\r
import { _validate } from './validate'\r
\r
\r
static #isInternal: boolean = false\r
\r
- #address: string\r
+ #address?: Address\r
#index?: number\r
#publicKey: Uint8Array<ArrayBuffer>\r
\r
#representative_block?: string\r
#weight?: bigint\r
\r
- get address (): string { return `${PREFIX}${this.#address}` }\r
+ get address (): string {\r
+ if (this.#address == null) {\r
+ throw new Error('Account not found')\r
+ }\r
+ return this.#address.toString()\r
+ }\r
get index (): number | undefined { return this.#index }\r
get publicKey (): string { return bytes.toHex(this.#publicKey) }\r
\r
throw new Error('Account cannot be instantiated directly. Use `load()` instead.')\r
}\r
Account.#isInternal = false\r
- this.#address = address\r
- .replace(PREFIX, '')\r
- .replace(PREFIX_LEGACY, '')\r
+ this.#address = new Address(address)\r
this.#publicKey = typeof publicKey === 'string'\r
? hex.toBytes(publicKey)\r
: publicKey\r
* Releases variable references to allow garbage collection.\r
*/\r
async destroy (): Promise<void> {\r
- this.#address = ''\r
+ this.#address = undefined\r
this.#index = undefined\r
this.#publicKey.fill(0)\r
\r
throw new TypeError(`Private key must be ${ACCOUNT_KEY_BYTE_LENGTH} bytes`)\r
}\r
const publicKey = await NanoNaCl.convert(privateKey)\r
- const address = this.#keyToAddress(publicKey)\r
+ const address = Address.fromPublicKey(publicKey)\r
this.#isInternal = true\r
accounts.push(new this(address, publicKey, index))\r
}\r
if (typeof keypair.publicKey === 'string') {\r
if (RegExp(`^[A-F0-9]{${ACCOUNT_KEY_HEX_LENGTH}}$`, 'i').test(keypair.publicKey)) {\r
const publicKey = hex.toBytes(keypair.publicKey)\r
- const address = this.#keyToAddress(publicKey)\r
+ const address = Address.fromPublicKey(publicKey)\r
this.#isInternal = true\r
accounts.push(new this(address, publicKey, index))\r
} else if (RegExp(`(${PREFIX}|${PREFIX_LEGACY})`).test(keypair.publicKey)) {\r
const address = keypair.publicKey\r
- const publicKey = this.#addressToKey(address)\r
+ const publicKey = new Address(address).toPublicKey()\r
this.#isInternal = true\r
accounts.push(new this(address, publicKey, index))\r
} else {\r
} else if (keypair.publicKey instanceof ArrayBuffer) {\r
if (keypair.publicKey.byteLength === ACCOUNT_KEY_BYTE_LENGTH) {\r
const publicKey = new Uint8Array(keypair.publicKey)\r
- const address = this.#keyToAddress(publicKey)\r
+ const address = Address.fromPublicKey(publicKey)\r
this.#isInternal = true\r
accounts.push(new this(address, publicKey, index))\r
} else {\r