]> git.codecow.com Git - libnemo.git/commitdiff
Create type guard from validate function. Compare lock and unlock password with insta...
authorChris Duncan <chris@zoso.dev>
Mon, 7 Jul 2025 13:13:58 +0000 (06:13 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 7 Jul 2025 13:13:58 +0000 (06:13 -0700)
src/lib/account.ts

index 7c6452b51fd9e7791f95988e49d304bd4adf40f4..e23548119762f13c61ef15b8aa95aeda5a5fbc09 100644 (file)
@@ -126,7 +126,7 @@ export class Account {
        * Instantiates an Account object from its private key. The corresponding\r
        * public key will automatically be derived and saved.\r
        *\r
-       * @param {string} privateKey - Private key of the account\r
+       * @param {(string|Uint8Array)} privateKey - Private key of the account\r
        * @param {number} [index] - Account number used when deriving the key\r
        * @returns {Account} A new Account object\r
        */\r
@@ -156,10 +156,8 @@ export class Account {
        * @returns True if successfully locked\r
        */\r
        async lock (password: string | Uint8Array): Promise<boolean> {\r
-               if (typeof password === 'string') {\r
-                       password = utf8.toBytes(password)\r
-               }\r
-               if (password == null || password.constructor.name !== 'Uint8Array') {\r
+               if (typeof password === 'string') password = utf8.toBytes(password)\r
+               if (password == null || !(password instanceof Uint8Array)) {\r
                        throw new Error('Failed to lock account')\r
                }\r
                try {\r
@@ -195,10 +193,8 @@ export class Account {
        * @returns True if successfully unlocked\r
        */\r
        async unlock (password: string | Uint8Array): Promise<boolean> {\r
-               if (typeof password === 'string') {\r
-                       password = utf8.toBytes(password)\r
-               }\r
-               if (password == null || password.constructor.name !== 'Uint8Array') {\r
+               if (typeof password === 'string') password = utf8.toBytes(password)\r
+               if (password == null || !(password instanceof Uint8Array)) {\r
                        throw new Error('Failed to unlock account')\r
                }\r
                try {\r
@@ -229,15 +225,14 @@ export class Account {
        * @param {string} address - Nano address to validate\r
        * @throws Error if address is undefined, not a string, or an invalid format\r
        */\r
-       static validate (address: string): void {\r
+       static validate (address: unknown): asserts address is string {\r
                if (address === undefined) {\r
                        throw new ReferenceError('Address is undefined.')\r
                }\r
                if (typeof address !== 'string') {\r
                        throw new TypeError('Address must be a string.')\r
                }\r
-               const pattern = new RegExp(`^(${PREFIX}|${PREFIX_LEGACY})[13]{1}[${ALPHABET}]{59}$`,\r
-               )\r
+               const pattern = new RegExp(`^(${PREFIX}|${PREFIX_LEGACY})[13]{1}[${ALPHABET}]{59}$`)\r
                if (!pattern.test(address)) {\r
                        throw new RangeError('Invalid address format')\r
                }\r