]> git.codecow.com Git - libnemo.git/commitdiff
Remove redundant type checking now handled by Address class.
authorChris Duncan <chris@zoso.dev>
Fri, 22 Aug 2025 22:23:34 +0000 (15:23 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 22 Aug 2025 22:23:34 +0000 (15:23 -0700)
src/lib/account/index.ts

index bd8f57d04dbb5a9be1923a3f717f44e3b23a5a50..222536fb1c16e9f4f6d1f4359e87554d8edf26b0 100644 (file)
@@ -4,8 +4,8 @@
 import { KeyPair } from '#types'\r
 import { Block } from '../block'\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 { bytes, hex } from '../convert'\r
+import { NanoNaCl } from '../crypto'\r
 import { Rpc } from '../rpc'\r
 import { Address } from './address'\r
 import { _refresh } from './refresh'\r
@@ -103,15 +103,13 @@ export class Account {
        set representative_block (v: string | undefined) { this.#representative_block = v }\r
        set weight (v: bigint | number | string) { this.#weight = BigInt(v) }\r
 \r
-       private constructor (address: string, publicKey: string | Uint8Array<ArrayBuffer>, index?: number) {\r
+       private constructor (address: Address, publicKey: Uint8Array<ArrayBuffer>, index?: number) {\r
                if (!Account.#isInternal) {\r
                        throw new Error('Account cannot be instantiated directly. Use `load()` instead.')\r
                }\r
                Account.#isInternal = false\r
-               this.#address = new Address(address)\r
-               this.#publicKey = typeof publicKey === 'string'\r
-                       ? hex.toBytes(publicKey)\r
-                       : publicKey\r
+               this.#address = address\r
+               this.#publicKey = publicKey\r
                this.#index = index\r
        }\r
 \r
@@ -242,22 +240,6 @@ export class Account {
                return _validate(address)\r
        }\r
 \r
-       /**\r
-       * Converts a Nano address to a public key.\r
-       *\r
-       * @param {string} address - Prefixed with `nano_`\r
-       * @returns Public key bytes as Uint8Array\r
-       */\r
-       static #addressToKey (address: string): Uint8Array<ArrayBuffer> {\r
-               const publicKey = base32.toBytes(address.slice(-60, -8))\r
-               const checksum = base32.toBytes(address.slice(-8))\r
-               const rechecksum = new Blake2b(5).update(publicKey).digest().reverse()\r
-               if (bytes.toHex(checksum) !== bytes.toHex(rechecksum)) {\r
-                       throw new Error('Checksum mismatch in address')\r
-               }\r
-               return publicKey\r
-       }\r
-\r
        /**\r
        * Instantiates an Account object from its private key which is used to derive\r
        * the corresponding public key and then discarded.\r
@@ -283,7 +265,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 = Address.fromPublicKey(publicKey)\r
+                               const address = new Address(publicKey)\r
                                this.#isInternal = true\r
                                accounts.push(new this(address, publicKey, index))\r
                        }\r
@@ -313,33 +295,14 @@ export class Account {
 \r
                        const accounts: Account[] = []\r
                        for (let keypair of keypairs) {\r
-                               const { index } = keypair\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 = 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 = new Address(address).toPublicKey()\r
-                                               this.#isInternal = true\r
-                                               accounts.push(new this(address, publicKey, index))\r
-                                       } else {\r
-                                               throw new TypeError('Invalid string', { cause: keypair.publicKey })\r
-                                       }\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 = Address.fromPublicKey(publicKey)\r
-                                               this.#isInternal = true\r
-                                               accounts.push(new this(address, publicKey, index))\r
-                                       } else {\r
-                                               throw new TypeError('Invalid buffer', { cause: keypair.publicKey })\r
-                                       }\r
-                               } else {\r
-                                       throw new TypeError('Invalid Account input', { cause: keypair.publicKey })\r
+                               if (keypair.publicKey == null) {\r
+                                       throw new TypeError('Account address or public key is required', { cause: keypair.publicKey })\r
                                }\r
+                               const { index } = keypair\r
+                               const address = new Address(keypair.publicKey)\r
+                               const publicKey = address.toPublicKey()\r
+                               this.#isInternal = true\r
+                               accounts.push(new this(address, publicKey, index))\r
                        }\r
                        return accounts\r
                } catch (err) {\r
@@ -389,19 +352,6 @@ export class Account {
                }\r
                return true\r
        }\r
-\r
-       /**\r
-       * Converts a public key to a Nano address.\r
-       *\r
-       * @param {Uint8Array} publicKey - Public key bytes as Uint8Array\r
-       * @returns Nano address string using `nano_` prefix\r
-       */\r
-       static #keyToAddress (publicKey: Uint8Array<ArrayBuffer>): string {\r
-               const checksum = new Blake2b(5).update(publicKey).digest().reverse()\r
-               const encodedPublicKey = bytes.toBase32(publicKey)\r
-               const encodedChecksum = bytes.toBase32(checksum)\r
-               return `${PREFIX}${encodedPublicKey}${encodedChecksum}`\r
-       }\r
 }\r
 \r
 export class AccountList extends Object {\r