]> git.codecow.com Git - libnemo.git/commitdiff
Improve address validation.
authorChris Duncan <chris@codecow.com>
Mon, 3 Aug 2026 20:49:31 +0000 (13:49 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 3 Aug 2026 20:49:31 +0000 (13:49 -0700)
src/lib/account/address.ts

index 83bfd27e5e9e98a315dc449aa91d558dcf7257b6..e531d38ce3aff391706a98ae1462b7f31fd3da2f 100644 (file)
@@ -1,9 +1,10 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { ACCOUNT_KEY_HEX_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from "../constants"
-import { base32, bytes, hex } from "../convert"
-import { Blake2b } from "../crypto"
+import { ACCOUNT_KEY_HEX_LENGTH, PREFIX, PREFIX_LEGACY } from '../constants'
+import { base32, bytes, hex } from '../convert'
+import { Blake2b } from '../crypto'
+import { _validate } from './validate'
 
 export class Address {
        #address: string
@@ -28,12 +29,11 @@ export class Address {
                        throw new TypeError('Invalid address input', { cause: value })
                }
                if (typeof value === 'string') {
-                       if (RegExp(`^[A-F0-9]{${ACCOUNT_KEY_HEX_LENGTH}}$`, 'i').test(value)) {
+                       if (hex.is(value) && value.length === ACCOUNT_KEY_HEX_LENGTH) {
                                this.#address = (this.constructor as typeof Address).fromPublicKey(hex.toBuffer(value))
-                       } else if (RegExp(`^(${PREFIX}|${PREFIX_LEGACY})[13][${ALPHABET}]{59}$`).test(value)) {
-                               this.#address = value
                        } else {
-                               throw new TypeError('Invalid address input', { cause: value })
+                               _validate(value)
+                               this.#address = value
                        }
                } else if (value instanceof ArrayBuffer || value instanceof Uint8Array) {
                        const v = new Uint8Array(value)