diff --git a/package-lock.json b/package-lock.json index 3eb8628..cb0175c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "thor-devkit", - "version": "0.2.5", + "version": "0.2.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7e348f9..1da0794 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "thor-devkit", - "version": "0.2.5", + "version": "0.2.6", "description": "Typescript library to aid DApp development on VeChain Thor", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index 0c9a8e3..3c53a71 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -43,11 +43,11 @@ export function publicKeyToAddress(pubKey: Buffer) { } /** - * validate the address - * @param address the address + * to check if a value presents an address + * @param v the value to be checked */ -export function isValidAddress(address: string) { - return /^0x[0-9a-f]{40}$/i.test(address) +export function isAddress(v: any): v is string { + return typeof v === 'string' && /^0x[0-9a-f]{40}$/i.test(v) } /** @@ -55,7 +55,7 @@ export function isValidAddress(address: string) { * @param address input address */ export function toChecksumAddress(address: string) { - if (!isValidAddress(address)) { + if (!isAddress(address)) { throw new Error('invalid address') } address = address.slice(2).toLowerCase() diff --git a/tests/crypto.test.ts b/tests/crypto.test.ts index 326cf92..f1912be 100644 --- a/tests/crypto.test.ts +++ b/tests/crypto.test.ts @@ -22,9 +22,9 @@ describe('hash', () => { describe('isValidAddress', () => { it('validate address', () => { - expect(cry.isValidAddress('not an address')).equal(false) - expect(cry.isValidAddress('52908400098527886E0F7030069857D2E4169EE7')).equal(false) - expect(cry.isValidAddress('0x52908400098527886E0F7030069857D2E4169EE7')).equal(true) + expect(cry.isAddress('not an address')).equal(false) + expect(cry.isAddress('52908400098527886E0F7030069857D2E4169EE7')).equal(false) + expect(cry.isAddress('0x52908400098527886E0F7030069857D2E4169EE7')).equal(true) }) })