]> git.codecow.com Git - libnemo.git/commitdiff
Implement new address class in Account and pass its public key as bytes.
authorChris Duncan <chris@zoso.dev>
Fri, 22 Aug 2025 09:44:49 +0000 (02:44 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 22 Aug 2025 09:44:49 +0000 (02:44 -0700)
src/lib/account/address.ts
src/lib/account/index.ts

index 2633ee7d4085d5d0560706129eda9ee758cb47c0..0e6f5a585be44c1dc3d2dac3fa8b2790b170e077 100644 (file)
@@ -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<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')
index b957c11204f65b64b88102fa30e541ba69794f40..bd8f57d04dbb5a9be1923a3f717f44e3b23a5a50 100644 (file)
@@ -3,10 +3,11 @@
 \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
@@ -22,7 +23,7 @@ export class Account {
 \r
        static #isInternal: boolean = false\r
 \r
-       #address: string\r
+       #address?: Address\r
        #index?: number\r
        #publicKey: Uint8Array<ArrayBuffer>\r
 \r
@@ -43,7 +44,12 @@ export class Account {
        #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
@@ -102,9 +108,7 @@ export class Account {
                        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
@@ -115,7 +119,7 @@ export class Account {
        * 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
@@ -279,7 +283,7 @@ export class Account {
                                        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
@@ -313,12 +317,12 @@ export class Account {
                                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
@@ -327,7 +331,7 @@ export class Account {
                                } 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