-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from DA0-DA0/util-package
Utils package
- Loading branch information
Showing
8 changed files
with
78 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './isValidAddress' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export function isValidWalletAddress( | ||
address: string, | ||
chainPrefix: string | ||
): boolean { | ||
const bech32Regex = /^[a-km-zA-HJ-NP-Z0-9]{39}$/im | ||
if (!address?.length) { | ||
return false | ||
} | ||
if (!address.startsWith(chainPrefix)) { | ||
return false | ||
} | ||
const unprefixed = address.replace(chainPrefix, '') | ||
return !!unprefixed.match(bech32Regex) | ||
} | ||
|
||
export function isValidValidatorAddress( | ||
address: string, | ||
chainPrefix: string | ||
): boolean { | ||
const bech32Regex = /^[a-km-zA-HJ-NP-Z0-9]{46}$/im | ||
if (!address?.length) { | ||
return false | ||
} | ||
if (address.search('valoper') < 0) { | ||
return false | ||
} | ||
const unprefixed = address.replace(chainPrefix, '') | ||
return !!unprefixed.match(bech32Regex) | ||
} | ||
|
||
export function isValidContractAddress( | ||
address: string, | ||
chainPrefix: string | ||
): boolean { | ||
const bech32Regex = /^[a-km-zA-HJ-NP-Z0-9]{59}$/im | ||
if (!address?.length) { | ||
return false | ||
} | ||
if (!address.startsWith(chainPrefix)) { | ||
return false | ||
} | ||
const unprefixed = address.replace(chainPrefix, '') | ||
return !!unprefixed.match(bech32Regex) | ||
} | ||
|
||
// Validates a bech32 address. | ||
export function isValidAddress(address: string, chainPrefix: string): boolean { | ||
return ( | ||
isValidWalletAddress(address, chainPrefix) || | ||
isValidContractAddress(address, chainPrefix) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@dao-dao/utils", | ||
"version": "0.1.0", | ||
"main": "index.tsx", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"tsconfig": "*", | ||
"config": "*", | ||
"typescript": "^4.5.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "tsconfig/base.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"] | ||
} |