Create toBech32 and fromBech32 in @cosmjs/encoding (#1058)
* Create toBech32 and fromBech32 * Add changes to CHANGELOG.md * Fix linting * Update packages/encoding/src/bech32.ts Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update packages/encoding/src/bech32.ts Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update bech32.ts Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com>
This commit is contained in:
co-authored by
Simon Warta
parent
9b80dff493
commit
4c8b278c1d
@@ -1,4 +1,4 @@
|
||||
import { Bech32 } from "./bech32";
|
||||
import { fromBech32, toBech32 } from "./bech32";
|
||||
import { fromHex } from "./hex";
|
||||
|
||||
describe("Bech32", () => {
|
||||
@@ -8,30 +8,30 @@ describe("Bech32", () => {
|
||||
|
||||
describe("encode", () => {
|
||||
it("works", () => {
|
||||
expect(Bech32.encode("eth", ethAddressRaw)).toEqual("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw");
|
||||
expect(toBech32("eth", ethAddressRaw)).toEqual("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw");
|
||||
});
|
||||
|
||||
it("works for very short data", () => {
|
||||
expect(() => Bech32.encode("eth", new Uint8Array(1))).not.toThrow();
|
||||
expect(() => toBech32("eth", new Uint8Array(1))).not.toThrow();
|
||||
});
|
||||
|
||||
it("works for very long prefixes", () => {
|
||||
expect(() => Bech32.encode("p".repeat(70), new Uint8Array(20))).toThrowError(/exceeds length limit/i);
|
||||
expect(() => toBech32("p".repeat(70), new Uint8Array(20))).toThrowError(/exceeds length limit/i);
|
||||
});
|
||||
|
||||
// See https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#Bech32
|
||||
it("works if result is 90 characters", () => {
|
||||
const result = Bech32.encode("eth", new Uint8Array(50));
|
||||
const result = toBech32("eth", new Uint8Array(50));
|
||||
expect(result.length).toEqual(90);
|
||||
});
|
||||
|
||||
it("throws if result exceeds 90 characters", () => {
|
||||
expect(() => Bech32.encode("eth", new Uint8Array(51))).toThrowError(/exceeds length limit/i);
|
||||
expect(() => toBech32("eth", new Uint8Array(51))).toThrowError(/exceeds length limit/i);
|
||||
});
|
||||
|
||||
it("works if a limit parameter is provided", () => {
|
||||
const limit = 1024;
|
||||
const result = Bech32.encode("eth", new Uint8Array(51), limit);
|
||||
const result = toBech32("eth", new Uint8Array(51), limit);
|
||||
expect(result).toEqual(
|
||||
"eth1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqug55er",
|
||||
);
|
||||
@@ -40,13 +40,13 @@ describe("Bech32", () => {
|
||||
|
||||
it("throws if result exceeds the provided limit parameter", () => {
|
||||
const limit = 10;
|
||||
expect(() => Bech32.encode("eth", ethAddressRaw, limit)).toThrowError(/exceeds length limit/i);
|
||||
expect(() => toBech32("eth", ethAddressRaw, limit)).toThrowError(/exceeds length limit/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("decode", () => {
|
||||
it("works", () => {
|
||||
expect(Bech32.decode("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toEqual({
|
||||
expect(fromBech32("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toEqual({
|
||||
prefix: "eth",
|
||||
data: ethAddressRaw,
|
||||
});
|
||||
@@ -55,7 +55,7 @@ describe("Bech32", () => {
|
||||
it("works for addresses which exceed the specification limit of 90 characters", () => {
|
||||
// Example from https://github.com/cosmos/cosmos-sdk/pull/6237#issuecomment-658116534
|
||||
expect(() =>
|
||||
Bech32.decode(
|
||||
fromBech32(
|
||||
"cosmospub1ytql0csgqvfzd666axrjzqmn5q2ucztcyxw8hvlzen94ay05tegaerkug5pn3xn8wqdymt598ufzd666axrjzqsxllmwacap3f6xyc4x30jl8ecrcs2tze3zzgxkmthcsqxnqxhwwgfzd666axrjzqs2rlu3wz5gnslgpprszjr8r65n0d6y39q657th77eyvengtk3z0y6h2pnk",
|
||||
),
|
||||
).not.toThrow();
|
||||
@@ -64,7 +64,7 @@ describe("Bech32", () => {
|
||||
it("throws for addresses which exceed the specification limit of 90 characters if a limit is specified", () => {
|
||||
// Example from https://github.com/cosmos/cosmos-sdk/pull/6237#issuecomment-658116534
|
||||
expect(() =>
|
||||
Bech32.decode(
|
||||
fromBech32(
|
||||
"cosmospub1ytql0csgqvfzd666axrjzqmn5q2ucztcyxw8hvlzen94ay05tegaerkug5pn3xn8wqdymt598ufzd666axrjzqsxllmwacap3f6xyc4x30jl8ecrcs2tze3zzgxkmthcsqxnqxhwwgfzd666axrjzqs2rlu3wz5gnslgpprszjr8r65n0d6y39q657th77eyvengtk3z0y6h2pnk",
|
||||
90,
|
||||
),
|
||||
|
||||
@@ -1,19 +1,39 @@
|
||||
import * as bech32 from "bech32";
|
||||
|
||||
export function toBech32(prefix: string, data: Uint8Array, limit?: number): string {
|
||||
const address = bech32.encode(prefix, bech32.toWords(data), limit);
|
||||
return address;
|
||||
}
|
||||
|
||||
export function fromBech32(
|
||||
address: string,
|
||||
limit = Infinity,
|
||||
): { readonly prefix: string; readonly data: Uint8Array } {
|
||||
const decodedAddress = bech32.decode(address, limit);
|
||||
return {
|
||||
prefix: decodedAddress.prefix,
|
||||
data: new Uint8Array(bech32.fromWords(decodedAddress.words)),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
export class Bech32 {
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
public static encode(prefix: string, data: Uint8Array, limit?: number): string {
|
||||
const address = bech32.encode(prefix, bech32.toWords(data), limit);
|
||||
return address;
|
||||
return toBech32(prefix, data, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
public static decode(
|
||||
address: string,
|
||||
limit = Infinity,
|
||||
): { readonly prefix: string; readonly data: Uint8Array } {
|
||||
const decodedAddress = bech32.decode(address, limit);
|
||||
return {
|
||||
prefix: decodedAddress.prefix,
|
||||
data: new Uint8Array(bech32.fromWords(decodedAddress.words)),
|
||||
};
|
||||
return fromBech32(address, limit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export { fromAscii, toAscii } from "./ascii";
|
||||
export { fromBase64, toBase64 } from "./base64";
|
||||
export { Bech32 } from "./bech32";
|
||||
export { Bech32, fromBech32, toBech32 } from "./bech32";
|
||||
export { fromHex, toHex } from "./hex";
|
||||
export { fromRfc3339, toRfc3339 } from "./rfc3339";
|
||||
export { fromUtf8, toUtf8 } from "./utf8";
|
||||
|
||||
Reference in New Issue
Block a user