Skip to content

Commit c512de3

Browse files
committed
fix(faucet): isValidAddress should accept all bech32 addresses
- check length based on Bech32 from [BIP173](https://github.com/bitcoin/bips/blob/e1e7b77c027b3d40d07d306cc75c2b5859c91db2/bip-0173.mediawiki#bech32)
1 parent f71cdc3 commit c512de3

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

packages/faucet/src/addresses.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { isValidAddress } from "./addresses";
2+
3+
describe("isValidAddress", () => {
4+
it("accepts account address", () => {
5+
expect(isValidAddress('cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r', 'cosmos')).toBe(true);
6+
});
7+
8+
it("accepts an ics-27 address", () => {
9+
expect(isValidAddress('osmo1d6em9ea5y3dye6em0awqyss7ssp0a7sgjk792x8cx647cfs7a4msk0fr45', 'osmo')).toBe(true);
10+
});
11+
12+
it("rejects an invalid address", () => {
13+
expect(isValidAddress('cosmos1fail', 'cosmos')).toBe(false);
14+
});
15+
16+
it("requires a prefix argument", () => {
17+
// @ts-expect-error intentionally omitting an argument
18+
expect(isValidAddress('cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r')).toBe(false);
19+
});
20+
});

packages/faucet/src/addresses.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { fromBech32 } from "@cosmjs/encoding";
22

3+
/**
4+
* Check length based on Bech32 from {@link https://github.com/bitcoin/bips/blob/e1e7b77c027b3d40d07d306cc75c2b5859c91db2/bip-0173.mediawiki#bech32 | BIP173}
5+
*/
36
export function isValidAddress(input: string, requiredPrefix: string): boolean {
47
try {
58
const { prefix, data } = fromBech32(input);
6-
if (prefix !== requiredPrefix) {
7-
return false;
8-
}
9-
return data.length === 20;
9+
return (
10+
prefix === requiredPrefix &&
11+
input.length >= 8 &&
12+
input.length <= 90 &&
13+
data.length >= 4 // 6 chars = 30 bits (3.75 bytes), rounded to whole byte
14+
);
1015
} catch {
1116
return false;
1217
}

0 commit comments

Comments
 (0)