@cosmjs/encoding
Advanced tools
Comparing version
@@ -7,2 +7,9 @@ export declare function toBech32(prefix: string, data: Uint8Array, limit?: number): string; | ||
/** | ||
* 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 declare function normalizeBech32(address: string): string; | ||
/** | ||
* @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. | ||
@@ -9,0 +16,0 @@ */ |
@@ -22,3 +22,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Bech32 = exports.fromBech32 = exports.toBech32 = void 0; | ||
exports.Bech32 = exports.normalizeBech32 = exports.fromBech32 = exports.toBech32 = void 0; | ||
const bech32 = __importStar(require("bech32")); | ||
@@ -39,2 +39,13 @@ function toBech32(prefix, data, limit) { | ||
/** | ||
* 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()`. | ||
*/ | ||
function normalizeBech32(address) { | ||
const { prefix, data } = fromBech32(address); | ||
return toBech32(prefix, data); | ||
} | ||
exports.normalizeBech32 = normalizeBech32; | ||
/** | ||
* @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. | ||
@@ -41,0 +52,0 @@ */ |
@@ -5,7 +5,7 @@ "use strict"; | ||
const hex_1 = require("./hex"); | ||
describe("Bech32", () => { | ||
describe("bech32", () => { | ||
// test data generate using https://github.com/nym-zone/bech32 | ||
// bech32 -e -h eth 9d4e856e572e442f0a4b2763e72d08a0e99d8ded | ||
const ethAddressRaw = (0, hex_1.fromHex)("9d4e856e572e442f0a4b2763e72d08a0e99d8ded"); | ||
describe("encode", () => { | ||
describe("toBech32", () => { | ||
it("works", () => { | ||
@@ -39,3 +39,3 @@ expect((0, bech32_1.toBech32)("eth", ethAddressRaw)).toEqual("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw"); | ||
}); | ||
describe("decode", () => { | ||
describe("fromBech32", () => { | ||
it("works", () => { | ||
@@ -47,2 +47,10 @@ expect((0, bech32_1.fromBech32)("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toEqual({ | ||
}); | ||
it("works for upper case address", () => { | ||
// "For presentation, lowercase is usually preferable, but inside QR codes uppercase SHOULD be used, as those permit the use of alphanumeric mode, which is 45% more compact than the normal byte mode." | ||
// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki | ||
expect((0, bech32_1.fromBech32)("ETH1N48G2MJH9EZZ7ZJTYA37WTGG5R5EMR0DRKWLGW")).toEqual({ | ||
prefix: "eth", | ||
data: ethAddressRaw, | ||
}); | ||
}); | ||
it("works for addresses which exceed the specification limit of 90 characters", () => { | ||
@@ -56,4 +64,26 @@ // Example from https://github.com/cosmos/cosmos-sdk/pull/6237#issuecomment-658116534 | ||
}); | ||
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(() => (0, bech32_1.fromBech32)("Eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
expect(() => (0, bech32_1.fromBech32)("eTh1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
expect(() => (0, bech32_1.fromBech32)("ETH1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
expect(() => (0, bech32_1.fromBech32)("eth1n48g2mjh9Ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
}); | ||
}); | ||
describe("normalizeBech32", () => { | ||
it("works", () => { | ||
expect((0, bech32_1.normalizeBech32)("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toEqual("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw"); | ||
expect((0, bech32_1.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(() => (0, bech32_1.normalizeBech32)("Eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
expect(() => (0, bech32_1.normalizeBech32)("eTh1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
expect(() => (0, bech32_1.normalizeBech32)("ETH1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
expect(() => (0, bech32_1.normalizeBech32)("eth1n48g2mjh9Ezz7zjtya37wtgg5r5emr0drkwlgw")).toThrowError(/Mixed-case/i); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=bech32.spec.js.map |
{ | ||
"name": "@cosmjs/encoding", | ||
"version": "0.28.1", | ||
"version": "0.28.2", | ||
"description": "Encoding helpers for blockchain projects", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78996
6.54%663
7.8%