From b1ed24a533ab6da96e485c271be032c70743a660 Mon Sep 17 00:00:00 2001 From: Sagar Saija Date: Mon, 21 Mar 2022 15:28:50 -0700 Subject: [PATCH 1/4] Add utils package --- apps/dapp/next.config.js | 2 +- apps/dapp/package.json | 3 +- apps/dapp/util/formValidation.ts | 10 +++--- apps/dapp/util/isValidAddress.ts | 47 ---------------------------- packages/utils/index.tsx | 1 + packages/utils/isValidAddress.ts | 53 ++++++++++++++++++++++++++++++++ packages/utils/package.json | 11 +++++++ packages/utils/tsconfig.json | 5 +++ 8 files changed, 79 insertions(+), 53 deletions(-) delete mode 100644 apps/dapp/util/isValidAddress.ts create mode 100644 packages/utils/index.tsx create mode 100644 packages/utils/isValidAddress.ts create mode 100644 packages/utils/package.json create mode 100644 packages/utils/tsconfig.json diff --git a/apps/dapp/next.config.js b/apps/dapp/next.config.js index b952c58b26..0aa5538594 100644 --- a/apps/dapp/next.config.js +++ b/apps/dapp/next.config.js @@ -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', diff --git a/apps/dapp/package.json b/apps/dapp/package.json index 7a4e27e5f9..a7927cfdee 100644 --- a/apps/dapp/package.json +++ b/apps/dapp/package.json @@ -1,5 +1,5 @@ { - "name": "dao-dao-dapp", + "name": "@dao-dao/dapp", "version": "0.3.0", "private": true, "scripts": { @@ -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", diff --git a/apps/dapp/util/formValidation.ts b/apps/dapp/util/formValidation.ts index 65c5ea3b11..d97e42724e 100644 --- a/apps/dapp/util/formValidation.ts +++ b/apps/dapp/util/formValidation.ts @@ -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' @@ -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 { diff --git a/apps/dapp/util/isValidAddress.ts b/apps/dapp/util/isValidAddress.ts deleted file mode 100644 index cf1fcdc681..0000000000 --- a/apps/dapp/util/isValidAddress.ts +++ /dev/null @@ -1,47 +0,0 @@ -const CHAIN_PREFIX = process.env.NEXT_PUBLIC_CHAIN_BECH32_PREFIX as string - -// We need to remove the chain prefix in these functions because otherwise it is -// impossible to tell when addresses are the right length. For example: -// determine if junoabd...1ef belongs to a chain called junoa, or junoab, or -// jun. - -export function isValidWalletAddress(address: string): boolean { - const bech32Regex = /^[a-km-zA-HJ-NP-Z0-9]{39}$/im - if (!address?.length) { - return false - } - if (!address.startsWith(CHAIN_PREFIX)) { - return false - } - const unprefixed = address.replace(CHAIN_PREFIX, '') - return !!unprefixed.match(bech32Regex) -} - -export function isValidValidatorAddress(address: 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(CHAIN_PREFIX, '') - return !!unprefixed.match(bech32Regex) -} - -export function isValidContractAddress(address: string): boolean { - const bech32Regex = /^[a-km-zA-HJ-NP-Z0-9]{59}$/im - if (!address?.length) { - return false - } - if (!address.startsWith(CHAIN_PREFIX)) { - return false - } - const unprefixed = address.replace(CHAIN_PREFIX, '') - return !!unprefixed.match(bech32Regex) -} - -// Validates a bech32 address. -export function isValidAddress(address: string): boolean { - return isValidWalletAddress(address) || isValidContractAddress(address) -} diff --git a/packages/utils/index.tsx b/packages/utils/index.tsx new file mode 100644 index 0000000000..cb877561af --- /dev/null +++ b/packages/utils/index.tsx @@ -0,0 +1 @@ +export * from './isValidAddress' diff --git a/packages/utils/isValidAddress.ts b/packages/utils/isValidAddress.ts new file mode 100644 index 0000000000..b280e93741 --- /dev/null +++ b/packages/utils/isValidAddress.ts @@ -0,0 +1,53 @@ +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 { + console.log(111, address, chainPrefix) + return ( + isValidWalletAddress(address, chainPrefix) || + isValidContractAddress(address, chainPrefix) + ) +} diff --git a/packages/utils/package.json b/packages/utils/package.json new file mode 100644 index 0000000000..0412b41e48 --- /dev/null +++ b/packages/utils/package.json @@ -0,0 +1,11 @@ +{ + "name": "@dao-dao/utils", + "version": "0.0.0", + "main": "index.tsx", + "license": "MIT", + "devDependencies": { + "tsconfig": "*", + "config": "*", + "typescript": "^4.5.3" + } +} diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json new file mode 100644 index 0000000000..6fe0a4efd0 --- /dev/null +++ b/packages/utils/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "tsconfig/base.json", + "include": ["."], + "exclude": ["dist", "build", "node_modules"] + } \ No newline at end of file From 3ddc483326b6c40fbea4717ef7d57f8da902b251 Mon Sep 17 00:00:00 2001 From: Sagar Saija Date: Mon, 21 Mar 2022 15:31:07 -0700 Subject: [PATCH 2/4] newline at the end of tsconfig.json --- packages/utils/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json index 6fe0a4efd0..3a4c0a0839 100644 --- a/packages/utils/tsconfig.json +++ b/packages/utils/tsconfig.json @@ -2,4 +2,4 @@ "extends": "tsconfig/base.json", "include": ["."], "exclude": ["dist", "build", "node_modules"] - } \ No newline at end of file +} From 8103b8a1febdd16d8cd800693917aa25d2228ca6 Mon Sep 17 00:00:00 2001 From: Sagar Saija Date: Mon, 21 Mar 2022 15:32:20 -0700 Subject: [PATCH 3/4] remove debugging console.log --- packages/utils/isValidAddress.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/utils/isValidAddress.ts b/packages/utils/isValidAddress.ts index b280e93741..fe40f6402f 100644 --- a/packages/utils/isValidAddress.ts +++ b/packages/utils/isValidAddress.ts @@ -45,7 +45,6 @@ export function isValidContractAddress( // Validates a bech32 address. export function isValidAddress(address: string, chainPrefix: string): boolean { - console.log(111, address, chainPrefix) return ( isValidWalletAddress(address, chainPrefix) || isValidContractAddress(address, chainPrefix) From 351a4ba2d09908a15fb5cd67512cdd568cc6c400 Mon Sep 17 00:00:00 2001 From: Jake Hartnell Date: Fri, 25 Mar 2022 11:21:45 -0700 Subject: [PATCH 4/4] Update packages/utils/package.json Co-authored-by: Noah Saso --- packages/utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/package.json b/packages/utils/package.json index 0412b41e48..6ea37b23b6 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@dao-dao/utils", - "version": "0.0.0", + "version": "0.1.0", "main": "index.tsx", "license": "MIT", "devDependencies": {