Skip to content

Commit

Permalink
Merge branch 'main' into v0.47.3-network-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
kstoykov committed Oct 6, 2023
2 parents d92e90d + 67b296b commit d59dfda
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/ledgers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,15 @@ export const verifyArbitrarySignature = (signedTx: StdSignature, address: string
return verified
}

export const isValidCosmosAddress = (addr: string) => {
export const isValidCosmosAddress = (addr: string, requiredPrefix?: string) => {
if (addr === '' || addr === undefined) return false
try {
const { prefix: decodedPrefix } = bech32.decode(addr)
return addr.toLowerCase().startsWith(decodedPrefix.toLowerCase())

if (requiredPrefix) {
return decodedPrefix === requiredPrefix
} else {
return addr.toLowerCase().startsWith(decodedPrefix.toLowerCase())
}
} catch {
// invalid checksum
return false
Expand Down
15 changes: 2 additions & 13 deletions src/utils/checks.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { fromBech32 } from "@cosmjs/encoding";
import { BECH32_PREFIX_ACC_ADDR, BECH32_PREFIX_VAL_ADDR } from "./constants";

import { isValidCosmosAddress } from "../ledgers";

export function isValidAddress(address: string, requiredPrefix?: string): boolean {
if (requiredPrefix === undefined) {
requiredPrefix = BECH32_PREFIX_ACC_ADDR;
}

try {
const { prefix, data } = fromBech32(address);

if (prefix !== requiredPrefix) {
return false;
}
return data.length === 20 || data.length === 32;
} catch {
return false;
}
return isValidCosmosAddress(address, requiredPrefix)
}

export function checkValidAddress(address: string) {
Expand Down

0 comments on commit d59dfda

Please sign in to comment.