From ba2c7bec84e6cadce5e658f516fb210a1257a08f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 7 Apr 2022 08:13:05 +0200 Subject: [PATCH] Create normalizeBech32 --- CHANGELOG.md | 4 ++++ packages/encoding/src/bech32.spec.ts | 22 +++++++++++++++++++++- packages/encoding/src/bech32.ts | 11 +++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d83dce78..8fa3d3d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to ## [Unreleased] +### Added + +- @cosmjs/encoding: Create `normalizeBech32`. + ## [0.28.1] - 2022-03-30 ### Added diff --git a/packages/encoding/src/bech32.spec.ts b/packages/encoding/src/bech32.spec.ts index a68545bb..1806ddc4 100644 --- a/packages/encoding/src/bech32.spec.ts +++ b/packages/encoding/src/bech32.spec.ts @@ -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); + }); + }); }); diff --git a/packages/encoding/src/bech32.ts b/packages/encoding/src/bech32.ts index 38d4803e..d333611a 100644 --- a/packages/encoding/src/bech32.ts +++ b/packages/encoding/src/bech32.ts @@ -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. */