Skip to content

Commit

Permalink
isValidAddress renamed to isAddress; ver 0.2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Sep 26, 2018
1 parent b802376 commit 6303751
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 5 additions & 5 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ 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)
}

/**
* encode the address to checksum address that is compatible with eip-55
* @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()
Expand Down
6 changes: 3 additions & 3 deletions tests/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down

0 comments on commit 6303751

Please sign in to comment.