Skip to content

Commit

Permalink
Merge pull request #438 from DA0-DA0/util-package
Browse files Browse the repository at this point in the history
Utils package
  • Loading branch information
JakeHartnell authored Mar 25, 2022
2 parents edd0ff1 + 351a4ba commit 8046549
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 53 deletions.
2 changes: 1 addition & 1 deletion apps/dapp/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const withTM = require('next-transpile-modules')(['ui'])
const withTM = require('next-transpile-modules')(['ui', '@dao-dao/utils'])

const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
Expand Down
3 changes: 2 additions & 1 deletion apps/dapp/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "dao-dao-dapp",
"name": "@dao-dao/dapp",
"version": "0.3.0",
"private": true,
"scripts": {
Expand All @@ -23,6 +23,7 @@
"@cosmjs/proto-signing": "^0.27.1",
"@cosmjs/stargate": "^0.27.1",
"@dao-dao/types": "^0.0.8",
"@dao-dao/utils": "*",
"@headlessui/react": "^1.4.2",
"@heroicons/react": "^1.0.5",
"@keplr-wallet/types": "^0.9.10",
Expand Down
10 changes: 6 additions & 4 deletions apps/dapp/util/formValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
isValidAddress,
isValidContractAddress,
isValidValidatorAddress,
} from './isValidAddress'
} from '@dao-dao/utils'
import { isValidUrl } from './isValidUrl'

const CHAIN_PREFIX = process.env.NEXT_PUBLIC_CHAIN_BECH32_PREFIX as string

export const validateRequired = (v: string | boolean) => {
if (typeof v === 'string') {
return v.trim().length != 0 || 'Field is required'
Expand All @@ -27,17 +29,17 @@ export const validatePercent = (v: string) => {
}

export const validateAddress = (v: string) =>
isValidAddress(v) || 'Invalid address'
isValidAddress(v, CHAIN_PREFIX) || 'Invalid address'

export const validateValidatorAddress = (v: string) =>
isValidValidatorAddress(v) || 'Invalid address'
isValidValidatorAddress(v, CHAIN_PREFIX) || 'Invalid address'

export const validateUrl = (v: string) =>
isValidUrl(v) ||
'Invalid URL link, must start with https and end with png/jpeg/gif.'

export const validateContractAddress = (v: string) =>
isValidContractAddress(v) || 'Invalid contract address'
isValidContractAddress(v, CHAIN_PREFIX) || 'Invalid contract address'

export const validateJSON = (v: string) => {
try {
Expand Down
47 changes: 0 additions & 47 deletions apps/dapp/util/isValidAddress.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/utils/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './isValidAddress'
52 changes: 52 additions & 0 deletions packages/utils/isValidAddress.ts
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)
)
}
11 changes: 11 additions & 0 deletions packages/utils/package.json
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"
}
}
5 changes: 5 additions & 0 deletions packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "tsconfig/base.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
}

0 comments on commit 8046549

Please sign in to comment.