-
Notifications
You must be signed in to change notification settings - Fork 22
/
address.ts
54 lines (46 loc) · 1.21 KB
/
address.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { fromBech32 } from '@cosmjs/encoding'
export const isValidWalletAddress = (
address: string,
prefix?: string
): boolean => isValidBech32Address(address, prefix, 20)
export const isValidValidatorAddress = (
address: string,
prefix: string
): boolean => isValidBech32Address(address, prefix + 'valoper')
// Validates any bech32 prefix, optionally requiring a specific prefix and/or
// length.
export const isValidBech32Address = (
address: string,
// If passed, the prefix must match this value.
prefix?: string,
// If passed, the address must contain this many bytes.
length?: number
): boolean => {
try {
const decoded = fromBech32(address)
if (prefix && decoded.prefix !== prefix) {
return false
}
if (length !== undefined && decoded.data.length !== length) {
return false
}
return true
} catch (err) {
return false
}
}
export const isValidTokenFactoryDenom = (
denom: string,
// If passed, the prefix must match this value.
prefix?: string
) => {
if (!denom?.length) {
return false
}
const [factory, owner, name] = denom.split('/')
return (
factory.toLowerCase() === 'factory' &&
isValidBech32Address(owner, prefix) &&
!!name
)
}