Skip to content

Commit

Permalink
Add limit parameter to Bech32Address
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunnini committed Apr 5, 2024
1 parent b148c3d commit ea96853
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/cosmos/src/bech32/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ export class Bech32Address {
return prefix + "1" + former + "..." + latter;
}

static fromBech32(bech32Address: string, prefix?: string): Bech32Address {
const decoded = bech32.decode(bech32Address);
static fromBech32(
bech32Address: string,
prefix?: string,
limit?: number
): Bech32Address {
const decoded = bech32.decode(bech32Address, limit);
if (prefix && decoded.prefix !== prefix) {
throw new Error("Unmatched prefix");
}

return new Bech32Address(new Uint8Array(fromWords(decoded.words)));
}

static validate(bech32Address: string, prefix?: string) {
const { prefix: decodedPrefix } = bech32.decode(bech32Address);
static validate(bech32Address: string, prefix?: string, limit?: number) {
const { prefix: decodedPrefix } = bech32.decode(bech32Address, limit);
if (prefix && prefix !== decodedPrefix) {
throw new Error(
`Unexpected prefix (expected: ${prefix}, actual: ${decodedPrefix})`
Expand Down Expand Up @@ -75,9 +79,9 @@ export class Bech32Address {

constructor(public readonly address: Uint8Array) {}

toBech32(prefix: string): string {
toBech32(prefix: string, limit?: number): string {
const words = bech32.toWords(this.address);
return bech32.encode(prefix, words);
return bech32.encode(prefix, words, limit);
}

toHex(mixedCaseChecksum: boolean = true): string {
Expand Down

0 comments on commit ea96853

Please sign in to comment.