Create normalizeBech32

This commit is contained in:
Simon Warta 2022-04-07 08:13:05 +02:00
parent 5fe6622f46
commit ba2c7bec84
3 changed files with 36 additions and 1 deletions

View File

@ -6,6 +6,10 @@ and this project adheres to
## [Unreleased]
### Added
- @cosmjs/encoding: Create `normalizeBech32`.
## [0.28.1] - 2022-03-30
### Added

View File

@ -1,4 +1,4 @@
import { fromBech32, toBech32 } from "./bech32";
import { fromBech32, normalizeBech32, toBech32 } from "./bech32";
import { fromHex } from "./hex";
describe("bech32", () => {
@ -89,4 +89,24 @@ describe("bech32", () => {
expect(() => fromBech32("eth1n48g2mjh9Ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i);
});
});
describe("normalizeBech32", () => {
it("works", () => {
expect(normalizeBech32("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toEqual(
"eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw",
);
expect(normalizeBech32("ETH1N48G2MJH9EZZ7ZJTYA37WTGG5R5EMR0DRKWLGW")).toEqual(
"eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw",
);
});
it("throws for mixed case addresses", () => {
// "Decoders MUST NOT accept strings where some characters are uppercase and some are lowercase (such strings are referred to as mixed case strings)."
// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
expect(() => normalizeBech32("Eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i);
expect(() => normalizeBech32("eTh1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i);
expect(() => normalizeBech32("ETH1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i);
expect(() => normalizeBech32("eth1n48g2mjh9Ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i);
});
});
});

View File

@ -16,6 +16,17 @@ export function fromBech32(
};
}
/**
* Takes a bech32 address and returns a normalized (i.e. lower case) representation of it.
*
* The input is validated along the way, which makes this significantly safer than
* using `address.toLowerCase()`.
*/
export function normalizeBech32(address: string): string {
const { prefix, data } = fromBech32(address);
return toBech32(prefix, data);
}
/**
* @deprecated This class is deprecated and will be removed soon. Please use fromBech32() and toBech32() instead. For more details please refer to https://github.com/cosmos/cosmjs/issues/1053.
*/