@cosmjs/encoding
Advanced tools
Comparing version 0.26.6 to 0.26.8
@@ -6,17 +6,17 @@ "use strict"; | ||
it("encodes to ascii", () => { | ||
expect(ascii_1.toAscii("")).toEqual(new Uint8Array([])); | ||
expect(ascii_1.toAscii("abc")).toEqual(new Uint8Array([0x61, 0x62, 0x63])); | ||
expect(ascii_1.toAscii(" ?=-n|~+-*/\\")).toEqual(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c])); | ||
expect(() => ascii_1.toAscii("ö")).toThrow(); | ||
expect(() => ascii_1.toAscii("ß")).toThrow(); | ||
expect((0, ascii_1.toAscii)("")).toEqual(new Uint8Array([])); | ||
expect((0, ascii_1.toAscii)("abc")).toEqual(new Uint8Array([0x61, 0x62, 0x63])); | ||
expect((0, ascii_1.toAscii)(" ?=-n|~+-*/\\")).toEqual(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c])); | ||
expect(() => (0, ascii_1.toAscii)("ö")).toThrow(); | ||
expect(() => (0, ascii_1.toAscii)("ß")).toThrow(); | ||
}); | ||
it("decodes from ascii", () => { | ||
expect(ascii_1.fromAscii(new Uint8Array([]))).toEqual(""); | ||
expect(ascii_1.fromAscii(new Uint8Array([0x61, 0x62, 0x63]))).toEqual("abc"); | ||
expect(ascii_1.fromAscii(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c]))).toEqual(" ?=-n|~+-*/\\"); | ||
expect(() => ascii_1.fromAscii(new Uint8Array([0x00]))).toThrow(); | ||
expect(() => ascii_1.fromAscii(new Uint8Array([0x7f]))).toThrow(); | ||
expect(() => ascii_1.fromAscii(new Uint8Array([0xff]))).toThrow(); | ||
expect((0, ascii_1.fromAscii)(new Uint8Array([]))).toEqual(""); | ||
expect((0, ascii_1.fromAscii)(new Uint8Array([0x61, 0x62, 0x63]))).toEqual("abc"); | ||
expect((0, ascii_1.fromAscii)(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c]))).toEqual(" ?=-n|~+-*/\\"); | ||
expect(() => (0, ascii_1.fromAscii)(new Uint8Array([0x00]))).toThrow(); | ||
expect(() => (0, ascii_1.fromAscii)(new Uint8Array([0x7f]))).toThrow(); | ||
expect(() => (0, ascii_1.fromAscii)(new Uint8Array([0xff]))).toThrow(); | ||
}); | ||
}); | ||
//# sourceMappingURL=ascii.spec.js.map |
@@ -6,55 +6,55 @@ "use strict"; | ||
it("encodes to base64", () => { | ||
expect(base64_1.toBase64(new Uint8Array([]))).toEqual(""); | ||
expect(base64_1.toBase64(new Uint8Array([0x00]))).toEqual("AA=="); | ||
expect(base64_1.toBase64(new Uint8Array([0x00, 0x00]))).toEqual("AAA="); | ||
expect(base64_1.toBase64(new Uint8Array([0x00, 0x00, 0x00]))).toEqual("AAAA"); | ||
expect(base64_1.toBase64(new Uint8Array([0x00, 0x00, 0x00, 0x00]))).toEqual("AAAAAA=="); | ||
expect(base64_1.toBase64(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00]))).toEqual("AAAAAAA="); | ||
expect(base64_1.toBase64(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))).toEqual("AAAAAAAA"); | ||
expect(base64_1.toBase64(new Uint8Array([0x61]))).toEqual("YQ=="); | ||
expect(base64_1.toBase64(new Uint8Array([0x62]))).toEqual("Yg=="); | ||
expect(base64_1.toBase64(new Uint8Array([0x63]))).toEqual("Yw=="); | ||
expect(base64_1.toBase64(new Uint8Array([0x61, 0x62, 0x63]))).toEqual("YWJj"); | ||
expect((0, base64_1.toBase64)(new Uint8Array([]))).toEqual(""); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x00]))).toEqual("AA=="); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x00, 0x00]))).toEqual("AAA="); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x00, 0x00, 0x00]))).toEqual("AAAA"); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x00, 0x00, 0x00, 0x00]))).toEqual("AAAAAA=="); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00]))).toEqual("AAAAAAA="); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))).toEqual("AAAAAAAA"); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x61]))).toEqual("YQ=="); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x62]))).toEqual("Yg=="); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x63]))).toEqual("Yw=="); | ||
expect((0, base64_1.toBase64)(new Uint8Array([0x61, 0x62, 0x63]))).toEqual("YWJj"); | ||
}); | ||
it("decodes from base64", () => { | ||
expect(base64_1.fromBase64("")).toEqual(new Uint8Array([])); | ||
expect(base64_1.fromBase64("AA==")).toEqual(new Uint8Array([0x00])); | ||
expect(base64_1.fromBase64("AAA=")).toEqual(new Uint8Array([0x00, 0x00])); | ||
expect(base64_1.fromBase64("AAAA")).toEqual(new Uint8Array([0x00, 0x00, 0x00])); | ||
expect(base64_1.fromBase64("AAAAAA==")).toEqual(new Uint8Array([0x00, 0x00, 0x00, 0x00])); | ||
expect(base64_1.fromBase64("AAAAAAA=")).toEqual(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00])); | ||
expect(base64_1.fromBase64("AAAAAAAA")).toEqual(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00])); | ||
expect(base64_1.fromBase64("YQ==")).toEqual(new Uint8Array([0x61])); | ||
expect(base64_1.fromBase64("Yg==")).toEqual(new Uint8Array([0x62])); | ||
expect(base64_1.fromBase64("Yw==")).toEqual(new Uint8Array([0x63])); | ||
expect(base64_1.fromBase64("YWJj")).toEqual(new Uint8Array([0x61, 0x62, 0x63])); | ||
expect((0, base64_1.fromBase64)("")).toEqual(new Uint8Array([])); | ||
expect((0, base64_1.fromBase64)("AA==")).toEqual(new Uint8Array([0x00])); | ||
expect((0, base64_1.fromBase64)("AAA=")).toEqual(new Uint8Array([0x00, 0x00])); | ||
expect((0, base64_1.fromBase64)("AAAA")).toEqual(new Uint8Array([0x00, 0x00, 0x00])); | ||
expect((0, base64_1.fromBase64)("AAAAAA==")).toEqual(new Uint8Array([0x00, 0x00, 0x00, 0x00])); | ||
expect((0, base64_1.fromBase64)("AAAAAAA=")).toEqual(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00])); | ||
expect((0, base64_1.fromBase64)("AAAAAAAA")).toEqual(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00])); | ||
expect((0, base64_1.fromBase64)("YQ==")).toEqual(new Uint8Array([0x61])); | ||
expect((0, base64_1.fromBase64)("Yg==")).toEqual(new Uint8Array([0x62])); | ||
expect((0, base64_1.fromBase64)("Yw==")).toEqual(new Uint8Array([0x63])); | ||
expect((0, base64_1.fromBase64)("YWJj")).toEqual(new Uint8Array([0x61, 0x62, 0x63])); | ||
// invalid length | ||
expect(() => base64_1.fromBase64("a")).toThrow(); | ||
expect(() => base64_1.fromBase64("aa")).toThrow(); | ||
expect(() => base64_1.fromBase64("aaa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("a")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaa")).toThrow(); | ||
// proper length including invalid character | ||
expect(() => base64_1.fromBase64("aaa!")).toThrow(); | ||
expect(() => base64_1.fromBase64("aaa*")).toThrow(); | ||
expect(() => base64_1.fromBase64("aaaä")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaa!")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaa*")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaaä")).toThrow(); | ||
// proper length plus invalid character | ||
expect(() => base64_1.fromBase64("aaaa!")).toThrow(); | ||
expect(() => base64_1.fromBase64("aaaa*")).toThrow(); | ||
expect(() => base64_1.fromBase64("aaaaä")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaaa!")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaaa*")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaaaä")).toThrow(); | ||
// extra spaces | ||
expect(() => base64_1.fromBase64("aaaa ")).toThrow(); | ||
expect(() => base64_1.fromBase64(" aaaa")).toThrow(); | ||
expect(() => base64_1.fromBase64("aa aa")).toThrow(); | ||
expect(() => base64_1.fromBase64("aaaa\n")).toThrow(); | ||
expect(() => base64_1.fromBase64("\naaaa")).toThrow(); | ||
expect(() => base64_1.fromBase64("aa\naa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaaa ")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)(" aaaa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aa aa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aaaa\n")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("\naaaa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("aa\naa")).toThrow(); | ||
// position of = | ||
expect(() => base64_1.fromBase64("=aaa")).toThrow(); | ||
expect(() => base64_1.fromBase64("==aa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("=aaa")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("==aa")).toThrow(); | ||
// concatenated base64 strings should not be supported | ||
// see https://github.com/beatgammit/base64-js/issues/42 | ||
expect(() => base64_1.fromBase64("AAA=AAA=")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("AAA=AAA=")).toThrow(); | ||
// wrong number of = | ||
expect(() => base64_1.fromBase64("a===")).toThrow(); | ||
expect(() => (0, base64_1.fromBase64)("a===")).toThrow(); | ||
}); | ||
}); | ||
//# sourceMappingURL=base64.spec.js.map |
@@ -0,3 +1,24 @@ | ||
export declare function toBech32(prefix: string, data: Uint8Array, limit?: number): string; | ||
export declare function fromBech32(address: string, limit?: number): { | ||
readonly prefix: string; | ||
readonly data: Uint8Array; | ||
}; | ||
/** | ||
* 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. | ||
*/ | ||
export declare 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. | ||
*/ | ||
static encode(prefix: string, data: Uint8Array, limit?: number): 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. | ||
*/ | ||
static decode(address: string, limit?: number): { | ||
@@ -4,0 +25,0 @@ readonly prefix: string; |
@@ -22,15 +22,43 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Bech32 = void 0; | ||
exports.Bech32 = exports.normalizeBech32 = exports.fromBech32 = exports.toBech32 = void 0; | ||
const bech32 = __importStar(require("bech32")); | ||
function toBech32(prefix, data, limit) { | ||
const address = bech32.encode(prefix, bech32.toWords(data), limit); | ||
return address; | ||
} | ||
exports.toBech32 = toBech32; | ||
function fromBech32(address, limit = Infinity) { | ||
const decodedAddress = bech32.decode(address, limit); | ||
return { | ||
prefix: decodedAddress.prefix, | ||
data: new Uint8Array(bech32.fromWords(decodedAddress.words)), | ||
}; | ||
} | ||
exports.fromBech32 = 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()`. | ||
*/ | ||
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. | ||
*/ | ||
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. | ||
*/ | ||
static encode(prefix, data, limit) { | ||
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. | ||
*/ | ||
static decode(address, limit = Infinity) { | ||
const decodedAddress = bech32.decode(address, limit); | ||
return { | ||
prefix: decodedAddress.prefix, | ||
data: new Uint8Array(bech32.fromWords(decodedAddress.words)), | ||
}; | ||
return fromBech32(address, limit); | ||
} | ||
@@ -37,0 +65,0 @@ } |
@@ -5,27 +5,27 @@ "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 = hex_1.fromHex("9d4e856e572e442f0a4b2763e72d08a0e99d8ded"); | ||
describe("encode", () => { | ||
const ethAddressRaw = (0, hex_1.fromHex)("9d4e856e572e442f0a4b2763e72d08a0e99d8ded"); | ||
describe("toBech32", () => { | ||
it("works", () => { | ||
expect(bech32_1.Bech32.encode("eth", ethAddressRaw)).toEqual("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw"); | ||
expect((0, bech32_1.toBech32)("eth", ethAddressRaw)).toEqual("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw"); | ||
}); | ||
it("works for very short data", () => { | ||
expect(() => bech32_1.Bech32.encode("eth", new Uint8Array(1))).not.toThrow(); | ||
expect(() => (0, bech32_1.toBech32)("eth", new Uint8Array(1))).not.toThrow(); | ||
}); | ||
it("works for very long prefixes", () => { | ||
expect(() => bech32_1.Bech32.encode("p".repeat(70), new Uint8Array(20))).toThrowError(/exceeds length limit/i); | ||
expect(() => (0, bech32_1.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_1.Bech32.encode("eth", new Uint8Array(50)); | ||
const result = (0, bech32_1.toBech32)("eth", new Uint8Array(50)); | ||
expect(result.length).toEqual(90); | ||
}); | ||
it("throws if result exceeds 90 characters", () => { | ||
expect(() => bech32_1.Bech32.encode("eth", new Uint8Array(51))).toThrowError(/exceeds length limit/i); | ||
expect(() => (0, bech32_1.toBech32)("eth", new Uint8Array(51))).toThrowError(/exceeds length limit/i); | ||
}); | ||
it("works if a limit parameter is provided", () => { | ||
const limit = 1024; | ||
const result = bech32_1.Bech32.encode("eth", new Uint8Array(51), limit); | ||
const result = (0, bech32_1.toBech32)("eth", new Uint8Array(51), limit); | ||
expect(result).toEqual("eth1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqug55er"); | ||
@@ -36,8 +36,8 @@ expect(result.length).toBeGreaterThan(90); | ||
const limit = 10; | ||
expect(() => bech32_1.Bech32.encode("eth", ethAddressRaw, limit)).toThrowError(/exceeds length limit/i); | ||
expect(() => (0, bech32_1.toBech32)("eth", ethAddressRaw, limit)).toThrowError(/exceeds length limit/i); | ||
}); | ||
}); | ||
describe("decode", () => { | ||
describe("fromBech32", () => { | ||
it("works", () => { | ||
expect(bech32_1.Bech32.decode("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toEqual({ | ||
expect((0, bech32_1.fromBech32)("eth1n48g2mjh9ezz7zjtya37wtgg5r5emr0drkwlgw")).toEqual({ | ||
prefix: "eth", | ||
@@ -47,12 +47,42 @@ data: ethAddressRaw, | ||
}); | ||
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", () => { | ||
// Example from https://github.com/cosmos/cosmos-sdk/pull/6237#issuecomment-658116534 | ||
expect(() => bech32_1.Bech32.decode("cosmospub1ytql0csgqvfzd666axrjzqmn5q2ucztcyxw8hvlzen94ay05tegaerkug5pn3xn8wqdymt598ufzd666axrjzqsxllmwacap3f6xyc4x30jl8ecrcs2tze3zzgxkmthcsqxnqxhwwgfzd666axrjzqs2rlu3wz5gnslgpprszjr8r65n0d6y39q657th77eyvengtk3z0y6h2pnk")).not.toThrow(); | ||
expect(() => (0, bech32_1.fromBech32)("cosmospub1ytql0csgqvfzd666axrjzqmn5q2ucztcyxw8hvlzen94ay05tegaerkug5pn3xn8wqdymt598ufzd666axrjzqsxllmwacap3f6xyc4x30jl8ecrcs2tze3zzgxkmthcsqxnqxhwwgfzd666axrjzqs2rlu3wz5gnslgpprszjr8r65n0d6y39q657th77eyvengtk3z0y6h2pnk")).not.toThrow(); | ||
}); | ||
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_1.Bech32.decode("cosmospub1ytql0csgqvfzd666axrjzqmn5q2ucztcyxw8hvlzen94ay05tegaerkug5pn3xn8wqdymt598ufzd666axrjzqsxllmwacap3f6xyc4x30jl8ecrcs2tze3zzgxkmthcsqxnqxhwwgfzd666axrjzqs2rlu3wz5gnslgpprszjr8r65n0d6y39q657th77eyvengtk3z0y6h2pnk", 90)).toThrowError(/exceeds length limit/i); | ||
expect(() => (0, bech32_1.fromBech32)("cosmospub1ytql0csgqvfzd666axrjzqmn5q2ucztcyxw8hvlzen94ay05tegaerkug5pn3xn8wqdymt598ufzd666axrjzqsxllmwacap3f6xyc4x30jl8ecrcs2tze3zzgxkmthcsqxnqxhwwgfzd666axrjzqs2rlu3wz5gnslgpprszjr8r65n0d6y39q657th77eyvengtk3z0y6h2pnk", 90)).toThrowError(/exceeds length limit/i); | ||
}); | ||
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 |
@@ -16,13 +16,14 @@ "use strict"; | ||
} | ||
const listOfInts = []; | ||
for (let i = 0; i < hexstring.length; i += 2) { | ||
const hexByteAsString = hexstring.substr(i, 2); | ||
const out = new Uint8Array(hexstring.length / 2); | ||
for (let i = 0; i < out.length; i++) { | ||
const j = 2 * i; | ||
const hexByteAsString = hexstring.slice(j, j + 2); | ||
if (!hexByteAsString.match(/[0-9a-f]{2}/i)) { | ||
throw new Error("hex string contains invalid characters"); | ||
} | ||
listOfInts.push(parseInt(hexByteAsString, 16)); | ||
out[i] = parseInt(hexByteAsString, 16); | ||
} | ||
return new Uint8Array(listOfInts); | ||
return out; | ||
} | ||
exports.fromHex = fromHex; | ||
//# sourceMappingURL=hex.js.map |
@@ -7,21 +7,21 @@ "use strict"; | ||
// simple | ||
expect(hex_1.fromHex("")).toEqual(new Uint8Array([])); | ||
expect(hex_1.fromHex("00")).toEqual(new Uint8Array([0x00])); | ||
expect(hex_1.fromHex("01")).toEqual(new Uint8Array([0x01])); | ||
expect(hex_1.fromHex("10")).toEqual(new Uint8Array([0x10])); | ||
expect(hex_1.fromHex("11")).toEqual(new Uint8Array([0x11])); | ||
expect(hex_1.fromHex("112233")).toEqual(new Uint8Array([0x11, 0x22, 0x33])); | ||
expect(hex_1.fromHex("0123456789abcdef")).toEqual(new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])); | ||
expect((0, hex_1.fromHex)("")).toEqual(new Uint8Array([])); | ||
expect((0, hex_1.fromHex)("00")).toEqual(new Uint8Array([0x00])); | ||
expect((0, hex_1.fromHex)("01")).toEqual(new Uint8Array([0x01])); | ||
expect((0, hex_1.fromHex)("10")).toEqual(new Uint8Array([0x10])); | ||
expect((0, hex_1.fromHex)("11")).toEqual(new Uint8Array([0x11])); | ||
expect((0, hex_1.fromHex)("112233")).toEqual(new Uint8Array([0x11, 0x22, 0x33])); | ||
expect((0, hex_1.fromHex)("0123456789abcdef")).toEqual(new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])); | ||
// capital letters | ||
expect(hex_1.fromHex("AA")).toEqual(new Uint8Array([0xaa])); | ||
expect(hex_1.fromHex("aAbBcCdDeEfF")).toEqual(new Uint8Array([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])); | ||
expect((0, hex_1.fromHex)("AA")).toEqual(new Uint8Array([0xaa])); | ||
expect((0, hex_1.fromHex)("aAbBcCdDeEfF")).toEqual(new Uint8Array([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])); | ||
// error | ||
expect(() => hex_1.fromHex("a")).toThrow(); | ||
expect(() => hex_1.fromHex("aaa")).toThrow(); | ||
expect(() => hex_1.fromHex("a!")).toThrow(); | ||
expect(() => hex_1.fromHex("a ")).toThrow(); | ||
expect(() => hex_1.fromHex("aa ")).toThrow(); | ||
expect(() => hex_1.fromHex(" aa")).toThrow(); | ||
expect(() => hex_1.fromHex("a a")).toThrow(); | ||
expect(() => hex_1.fromHex("gg")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)("a")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)("aaa")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)("a!")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)("a ")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)("aa ")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)(" aa")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)("a a")).toThrow(); | ||
expect(() => (0, hex_1.fromHex)("gg")).toThrow(); | ||
}); | ||
@@ -31,11 +31,11 @@ }); | ||
it("works", () => { | ||
expect(hex_1.toHex(new Uint8Array([]))).toEqual(""); | ||
expect(hex_1.toHex(new Uint8Array([0x00]))).toEqual("00"); | ||
expect(hex_1.toHex(new Uint8Array([0x01]))).toEqual("01"); | ||
expect(hex_1.toHex(new Uint8Array([0x10]))).toEqual("10"); | ||
expect(hex_1.toHex(new Uint8Array([0x11]))).toEqual("11"); | ||
expect(hex_1.toHex(new Uint8Array([0x11, 0x22, 0x33]))).toEqual("112233"); | ||
expect(hex_1.toHex(new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]))).toEqual("0123456789abcdef"); | ||
expect((0, hex_1.toHex)(new Uint8Array([]))).toEqual(""); | ||
expect((0, hex_1.toHex)(new Uint8Array([0x00]))).toEqual("00"); | ||
expect((0, hex_1.toHex)(new Uint8Array([0x01]))).toEqual("01"); | ||
expect((0, hex_1.toHex)(new Uint8Array([0x10]))).toEqual("10"); | ||
expect((0, hex_1.toHex)(new Uint8Array([0x11]))).toEqual("11"); | ||
expect((0, hex_1.toHex)(new Uint8Array([0x11, 0x22, 0x33]))).toEqual("112233"); | ||
expect((0, hex_1.toHex)(new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]))).toEqual("0123456789abcdef"); | ||
}); | ||
}); | ||
//# sourceMappingURL=hex.spec.js.map |
export { fromAscii, toAscii } from "./ascii"; | ||
export { fromBase64, toBase64 } from "./base64"; | ||
export { Bech32 } from "./bech32"; | ||
export { Bech32, fromBech32, normalizeBech32, toBech32 } from "./bech32"; | ||
export { fromHex, toHex } from "./hex"; | ||
export { fromRfc3339, toRfc3339 } from "./rfc3339"; | ||
export { fromUtf8, toUtf8 } from "./utf8"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toUtf8 = exports.fromUtf8 = exports.toRfc3339 = exports.fromRfc3339 = exports.toHex = exports.fromHex = exports.Bech32 = exports.toBase64 = exports.fromBase64 = exports.toAscii = exports.fromAscii = void 0; | ||
exports.toUtf8 = exports.fromUtf8 = exports.toRfc3339 = exports.fromRfc3339 = exports.toHex = exports.fromHex = exports.toBech32 = exports.normalizeBech32 = exports.fromBech32 = exports.Bech32 = exports.toBase64 = exports.fromBase64 = exports.toAscii = exports.fromAscii = void 0; | ||
var ascii_1 = require("./ascii"); | ||
@@ -12,2 +12,5 @@ Object.defineProperty(exports, "fromAscii", { enumerable: true, get: function () { return ascii_1.fromAscii; } }); | ||
Object.defineProperty(exports, "Bech32", { enumerable: true, get: function () { return bech32_1.Bech32; } }); | ||
Object.defineProperty(exports, "fromBech32", { enumerable: true, get: function () { return bech32_1.fromBech32; } }); | ||
Object.defineProperty(exports, "normalizeBech32", { enumerable: true, get: function () { return bech32_1.normalizeBech32; } }); | ||
Object.defineProperty(exports, "toBech32", { enumerable: true, get: function () { return bech32_1.toBech32; } }); | ||
var hex_1 = require("./hex"); | ||
@@ -14,0 +17,0 @@ Object.defineProperty(exports, "fromHex", { enumerable: true, get: function () { return hex_1.fromHex; } }); |
@@ -7,46 +7,46 @@ "use strict"; | ||
// time zone +/- 0 | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+00:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-00:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+00:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-00:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
// time zone positive (full hours) | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+01:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 1, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+02:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 2, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+03:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 3, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+11:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 11, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+01:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 1, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+02:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 2, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+03:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 3, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+11:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 11, 12, 13))); | ||
// time zone negative (full hours) | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-01:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 1, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-02:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 2, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-03:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 3, 12, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-11:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 11, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-01:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 1, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-02:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 2, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-03:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 3, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-11:00")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 11, 12, 13))); | ||
// time zone positive (minutes only) | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+00:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 - 1, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+00:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 - 30, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+00:45")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 - 45, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+00:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 - 1, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+00:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 - 30, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+00:45")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 - 45, 13))); | ||
// time zone negative (minutes only) | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-00:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 + 1, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-00:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 + 30, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-00:45")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 + 45, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-00:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 + 1, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-00:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 + 30, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-00:45")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12 + 45, 13))); | ||
// time zone positive (hours and minutes) | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+01:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 1, 12 - 1, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+04:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 4, 12 - 30, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13+10:20")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 10, 12 - 20, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+01:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 1, 12 - 1, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+04:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 4, 12 - 30, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+10:20")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 - 10, 12 - 20, 13))); | ||
// time zone negative (hours and minutes) | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-01:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 1, 12 + 1, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-04:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 4, 12 + 30, 13))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13-10:20")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 10, 12 + 20, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-01:01")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 1, 12 + 1, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-04:30")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 4, 12 + 30, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13-10:20")).toEqual(new Date(Date.UTC(2002, 9, 2, 11 + 10, 12 + 20, 13))); | ||
}); | ||
it("parses dates with milliseconds", () => { | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.123Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.123Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
}); | ||
it("parses dates with low precision fractional seconds", () => { | ||
// 1 digit | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.0Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.1Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 100))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.9Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 900))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.0Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.1Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 100))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.9Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 900))); | ||
// 2 digit | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.00Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.12Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 120))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.99Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 990))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.00Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.12Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 120))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.99Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 990))); | ||
}); | ||
@@ -56,25 +56,25 @@ it("parses dates with high precision fractional seconds", () => { | ||
// 4 digits | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.0000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.1234Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.9999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.0000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.1234Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.9999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
// 5 digits | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.00000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.12345Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.99999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.00000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.12345Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.99999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
// 6 digits | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.123456Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.123456Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
// 7 digits | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.0000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.1234567Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.9999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.0000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.1234567Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.9999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
// 8 digits | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.00000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.12345678Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.99999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.00000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.12345678Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.99999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
// 9 digits | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.000000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.123456789Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect(rfc3339_1.fromRfc3339("2002-10-02T11:12:13.999999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.000000000Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.123456789Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 123))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13.999999999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999))); | ||
}); | ||
@@ -85,37 +85,37 @@ it("accepts space separators", () => { | ||
// to specify a full-date and full-time separated by (say) a space character. | ||
expect(rfc3339_1.fromRfc3339("2002-10-02 11:12:13Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
expect((0, rfc3339_1.fromRfc3339)("2002-10-02 11:12:13Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13))); | ||
}); | ||
it("throws for invalid format", () => { | ||
// extra whitespace | ||
expect(() => rfc3339_1.fromRfc3339(" 2002-10-02T11:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11:12:13Z ")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11:12:13 Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)(" 2002-10-02T11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13Z ")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13 Z")).toThrow(); | ||
// wrong date separators | ||
expect(() => rfc3339_1.fromRfc3339("2002:10-02T11:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10:02T11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002:10-02T11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10:02T11:12:13Z")).toThrow(); | ||
// wrong time separators | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11-12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11:12-13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11-12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11:12-13Z")).toThrow(); | ||
// wrong separator | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02TT11:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02 T11:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T 11:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02t11:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02x11:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02311:12:13Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02.11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02TT11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02 T11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T 11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02t11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02x11:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02311:12:13Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02.11:12:13Z")).toThrow(); | ||
// wrong time zone | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11:12:13")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11:12:13z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11:12:13 00:00")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2002-10-02T11:12:13+0000")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13 00:00")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2002-10-02T11:12:13+0000")).toThrow(); | ||
// wrong fractional seconds | ||
expect(() => rfc3339_1.fromRfc3339("2018-07-30T19:21:12345Z")).toThrow(); | ||
expect(() => rfc3339_1.fromRfc3339("2018-07-30T19:21:12.Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2018-07-30T19:21:12345Z")).toThrow(); | ||
expect(() => (0, rfc3339_1.fromRfc3339)("2018-07-30T19:21:12.Z")).toThrow(); | ||
}); | ||
it("encodes dates", () => { | ||
expect(rfc3339_1.toRfc3339(new Date(Date.UTC(0, 0, 1, 0, 0, 0)))).toEqual("1900-01-01T00:00:00.000Z"); | ||
expect(rfc3339_1.toRfc3339(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 456)))).toEqual("2002-10-02T11:12:13.456Z"); | ||
expect((0, rfc3339_1.toRfc3339)(new Date(Date.UTC(0, 0, 1, 0, 0, 0)))).toEqual("1900-01-01T00:00:00.000Z"); | ||
expect((0, rfc3339_1.toRfc3339)(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 456)))).toEqual("2002-10-02T11:12:13.456Z"); | ||
}); | ||
}); | ||
//# sourceMappingURL=rfc3339.spec.js.map |
@@ -6,48 +6,48 @@ "use strict"; | ||
it("encodes ascii strings", () => { | ||
expect(utf8_1.toUtf8("")).toEqual(new Uint8Array([])); | ||
expect(utf8_1.toUtf8("abc")).toEqual(new Uint8Array([0x61, 0x62, 0x63])); | ||
expect(utf8_1.toUtf8(" ?=-n|~+-*/\\")).toEqual(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c])); | ||
expect((0, utf8_1.toUtf8)("")).toEqual(new Uint8Array([])); | ||
expect((0, utf8_1.toUtf8)("abc")).toEqual(new Uint8Array([0x61, 0x62, 0x63])); | ||
expect((0, utf8_1.toUtf8)(" ?=-n|~+-*/\\")).toEqual(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c])); | ||
}); | ||
it("decodes ascii string", () => { | ||
expect(utf8_1.fromUtf8(new Uint8Array([]))).toEqual(""); | ||
expect(utf8_1.fromUtf8(new Uint8Array([0x61, 0x62, 0x63]))).toEqual("abc"); | ||
expect(utf8_1.fromUtf8(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c]))).toEqual(" ?=-n|~+-*/\\"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([]))).toEqual(""); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0x61, 0x62, 0x63]))).toEqual("abc"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0x20, 0x3f, 0x3d, 0x2d, 0x6e, 0x7c, 0x7e, 0x2b, 0x2d, 0x2a, 0x2f, 0x5c]))).toEqual(" ?=-n|~+-*/\\"); | ||
}); | ||
it("encodes null character", () => { | ||
expect(utf8_1.toUtf8("\u0000")).toEqual(new Uint8Array([0x00])); | ||
expect((0, utf8_1.toUtf8)("\u0000")).toEqual(new Uint8Array([0x00])); | ||
}); | ||
it("decodes null byte", () => { | ||
expect(utf8_1.fromUtf8(new Uint8Array([0x00]))).toEqual("\u0000"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0x00]))).toEqual("\u0000"); | ||
}); | ||
it("encodes Basic Multilingual Plane strings", () => { | ||
expect(utf8_1.toUtf8("ö")).toEqual(new Uint8Array([0xc3, 0xb6])); | ||
expect(utf8_1.toUtf8("¥")).toEqual(new Uint8Array([0xc2, 0xa5])); | ||
expect(utf8_1.toUtf8("Ф")).toEqual(new Uint8Array([0xd0, 0xa4])); | ||
expect(utf8_1.toUtf8("ⱴ")).toEqual(new Uint8Array([0xe2, 0xb1, 0xb4])); | ||
expect(utf8_1.toUtf8("ⵘ")).toEqual(new Uint8Array([0xe2, 0xb5, 0x98])); | ||
expect((0, utf8_1.toUtf8)("ö")).toEqual(new Uint8Array([0xc3, 0xb6])); | ||
expect((0, utf8_1.toUtf8)("¥")).toEqual(new Uint8Array([0xc2, 0xa5])); | ||
expect((0, utf8_1.toUtf8)("Ф")).toEqual(new Uint8Array([0xd0, 0xa4])); | ||
expect((0, utf8_1.toUtf8)("ⱴ")).toEqual(new Uint8Array([0xe2, 0xb1, 0xb4])); | ||
expect((0, utf8_1.toUtf8)("ⵘ")).toEqual(new Uint8Array([0xe2, 0xb5, 0x98])); | ||
}); | ||
it("decodes Basic Multilingual Plane strings", () => { | ||
expect(utf8_1.fromUtf8(new Uint8Array([0xc3, 0xb6]))).toEqual("ö"); | ||
expect(utf8_1.fromUtf8(new Uint8Array([0xc2, 0xa5]))).toEqual("¥"); | ||
expect(utf8_1.fromUtf8(new Uint8Array([0xd0, 0xa4]))).toEqual("Ф"); | ||
expect(utf8_1.fromUtf8(new Uint8Array([0xe2, 0xb1, 0xb4]))).toEqual("ⱴ"); | ||
expect(utf8_1.fromUtf8(new Uint8Array([0xe2, 0xb5, 0x98]))).toEqual("ⵘ"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0xc3, 0xb6]))).toEqual("ö"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0xc2, 0xa5]))).toEqual("¥"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0xd0, 0xa4]))).toEqual("Ф"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0xe2, 0xb1, 0xb4]))).toEqual("ⱴ"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0xe2, 0xb5, 0x98]))).toEqual("ⵘ"); | ||
}); | ||
it("encodes Supplementary Multilingual Plane strings", () => { | ||
// U+1F0A1 | ||
expect(utf8_1.toUtf8("🂡")).toEqual(new Uint8Array([0xf0, 0x9f, 0x82, 0xa1])); | ||
expect((0, utf8_1.toUtf8)("🂡")).toEqual(new Uint8Array([0xf0, 0x9f, 0x82, 0xa1])); | ||
// U+1034A | ||
expect(utf8_1.toUtf8("𐍊")).toEqual(new Uint8Array([0xf0, 0x90, 0x8d, 0x8a])); | ||
expect((0, utf8_1.toUtf8)("𐍊")).toEqual(new Uint8Array([0xf0, 0x90, 0x8d, 0x8a])); | ||
}); | ||
it("decodes Supplementary Multilingual Plane strings", () => { | ||
// U+1F0A1 | ||
expect(utf8_1.fromUtf8(new Uint8Array([0xf0, 0x9f, 0x82, 0xa1]))).toEqual("🂡"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0xf0, 0x9f, 0x82, 0xa1]))).toEqual("🂡"); | ||
// U+1034A | ||
expect(utf8_1.fromUtf8(new Uint8Array([0xf0, 0x90, 0x8d, 0x8a]))).toEqual("𐍊"); | ||
expect((0, utf8_1.fromUtf8)(new Uint8Array([0xf0, 0x90, 0x8d, 0x8a]))).toEqual("𐍊"); | ||
}); | ||
it("throws on invalid utf8 bytes", () => { | ||
// Broken UTF8 example from https://github.com/nodejs/node/issues/16894 | ||
expect(() => utf8_1.fromUtf8(new Uint8Array([0xf0, 0x80, 0x80]))).toThrow(); | ||
expect(() => (0, utf8_1.fromUtf8)(new Uint8Array([0xf0, 0x80, 0x80]))).toThrow(); | ||
}); | ||
}); | ||
//# sourceMappingURL=utf8.spec.js.map |
{ | ||
"name": "@cosmjs/encoding", | ||
"version": "0.26.6", | ||
"version": "0.26.8", | ||
"description": "Encoding helpers for blockchain projects", | ||
@@ -31,3 +31,3 @@ "contributors": [ | ||
"format-text": "prettier --write \"./*.md\"", | ||
"test-node": "node --require esm jasmine-testrunner.js", | ||
"test-node": "yarn node jasmine-testrunner.js", | ||
"test-edge": "yarn pack-web && karma start --single-run --browsers Edge", | ||
@@ -57,4 +57,4 @@ "test-firefox": "yarn pack-web && karma start --single-run --browsers Firefox", | ||
"@types/node": "^15.0.1", | ||
"@typescript-eslint/eslint-plugin": "^4.28", | ||
"@typescript-eslint/parser": "^4.28", | ||
"@typescript-eslint/eslint-plugin": "^5.13.0", | ||
"@typescript-eslint/parser": "^5.13.0", | ||
"eslint": "^7.5", | ||
@@ -68,6 +68,5 @@ "eslint-config-prettier": "^8.3.0", | ||
"glob": "^7.1.6", | ||
"jasmine": "^3.8", | ||
"jasmine-core": "^3.7.1", | ||
"jasmine": "^3.99", | ||
"jasmine-spec-reporter": "^6", | ||
"karma": "^6.1.1", | ||
"karma": "^6.3.14", | ||
"karma-chrome-launcher": "^3.1.0", | ||
@@ -82,4 +81,4 @@ "karma-firefox-launcher": "^2.1.0", | ||
"ts-node": "^8", | ||
"typedoc": "^0.21", | ||
"typescript": "~4.3", | ||
"typedoc": "^0.22", | ||
"typescript": "~4.4", | ||
"webpack": "^5.32.0", | ||
@@ -86,0 +85,0 @@ "webpack-cli": "^4.6.0" |
@@ -13,5 +13,5 @@ # @cosmjs/encoding | ||
``` | ||
>> Bech32.encode("tiov", fromHex("1234ABCD0000AA0000FFFF0000AA00001234ABCD")) | ||
>> toBech32("tiov", fromHex("1234ABCD0000AA0000FFFF0000AA00001234ABCD")) | ||
'tiov1zg62hngqqz4qqq8lluqqp2sqqqfrf27dzrrmea' | ||
>> toHex(Bech32.decode("tiov1zg62hngqqz4qqq8lluqqp2sqqqfrf27dzrrmea").data) | ||
>> toHex(fromBech32("tiov1zg62hngqqz4qqq8lluqqp2sqqqfrf27dzrrmea").data) | ||
'1234abcd0000aa0000ffff0000aa00001234abcd' | ||
@@ -18,0 +18,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79159
34
664