@chainflip/utils 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,25 +49,39 @@ function assert(condition, message = "assertion failed", Constructor = Error) {
49
49
  }
50
50
  }
51
51
  function assertString(value, message) {
52
- assert(isString(value), message ?? `expected a "string", got "${typeof value}"`);
52
+ if (!isString(value)) {
53
+ throw new TypeError(message ?? `expected a "string", got "${typeof value}"`);
54
+ }
53
55
  }
54
56
  function assertNumber(value, message) {
55
- assert(isNumber(value), message ?? `expected a "number", got "${typeof value}"`);
57
+ if (!isNumber(value)) {
58
+ throw new TypeError(message ?? `expected a "number", got "${typeof value}"`);
59
+ }
56
60
  }
57
61
  function assertBigInt(value, message) {
58
- assert(isBigInt(value), message ?? `expected a "bigint", got "${typeof value}"`);
62
+ if (!isBigInt(value)) {
63
+ throw new TypeError(message ?? `expected a "bigint", got "${typeof value}"`);
64
+ }
59
65
  }
60
66
  function assertBoolean(value, message) {
61
- assert(isBoolean(value), message ?? `expected a "boolean", got "${typeof value}"`);
67
+ if (!isBoolean(value)) {
68
+ throw new TypeError(message ?? `expected a "boolean", got "${typeof value}"`);
69
+ }
62
70
  }
63
71
  function assertSymbol(value, message) {
64
- assert(isSymbol(value), message ?? `expected a "symbol", got "${typeof value}"`);
72
+ if (!isSymbol(value)) {
73
+ throw new TypeError(message ?? `expected a "symbol", got "${typeof value}"`);
74
+ }
65
75
  }
66
76
  function assertObject(value, message) {
67
- assert(isObject(value), message ?? `expected an "object", got "${typeof value}"`);
77
+ if (!isObject(value)) {
78
+ throw new TypeError(message ?? `expected an "object", got "${typeof value}"`);
79
+ }
68
80
  }
69
81
  function assertUndefined(value, message) {
70
- assert(isUndefined(value), message ?? `expected "undefined", got "${typeof value}"`);
82
+ if (!isUndefined(value)) {
83
+ throw new TypeError(message ?? `expected "undefined", got "${typeof value}"`);
84
+ }
71
85
  }
72
86
  var unreachable = (x, message = "unreachable") => {
73
87
  throw new Error(message);
package/dist/assertion.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  assertSymbol,
9
9
  assertUndefined,
10
10
  unreachable
11
- } from "./chunk-NYJKCZRI.js";
11
+ } from "./chunk-MYP3UYWE.js";
12
12
  import "./chunk-CZNX6EUV.js";
13
13
  export {
14
14
  assert,
package/dist/base58.cjs CHANGED
@@ -35,6 +35,13 @@ var isSymbol = createIsGuard("symbol");
35
35
  var isObject = createIsGuard("object");
36
36
  var isUndefined = createIsGuard("undefined");
37
37
 
38
+ // src/assertion.ts
39
+ function assert(condition, message = "assertion failed", Constructor = Error) {
40
+ if (!condition) {
41
+ throw new Constructor(message);
42
+ }
43
+ }
44
+
38
45
  // src/bytes.ts
39
46
  var convertBase = (bytes, fromBase, toBase) => {
40
47
  const result = [];
@@ -59,9 +66,10 @@ var convertBase = (bytes, fromBase, toBase) => {
59
66
  };
60
67
  var encodeBytesWithCharset = (bytes, charset2) => convertBase(bytes, 256, charset2.length).map((charCode) => charset2.charAt(charCode)).join("");
61
68
  var decodeBytesWithCharset = (input, charset2) => {
69
+ assert(new RegExp(`^[${charset2}]*$`).test(input), "Invalid input");
62
70
  const charMap = Object.fromEntries([...charset2].map((char, index) => [char, index]));
63
71
  const bytes = input.split("").map((char) => charMap[char]);
64
- return convertBase(bytes, charset2.length, 256);
72
+ return new Uint8Array(convertBase(bytes, charset2.length, 256));
65
73
  };
66
74
 
67
75
  // src/base58.ts
package/dist/base58.d.cts CHANGED
@@ -1,4 +1,4 @@
1
1
  declare const encode: (bytes: Uint8Array | number[]) => string;
2
- declare const decode: (input: string) => number[];
2
+ declare const decode: (input: string) => Uint8Array;
3
3
 
4
4
  export { decode, encode };
package/dist/base58.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  declare const encode: (bytes: Uint8Array | number[]) => string;
2
- declare const decode: (input: string) => number[];
2
+ declare const decode: (input: string) => Uint8Array;
3
3
 
4
4
  export { decode, encode };
package/dist/base58.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  decode,
3
3
  encode
4
- } from "./chunk-MORKFLN4.js";
5
- import "./chunk-DGVZ5UDU.js";
6
- import "./chunk-NYJKCZRI.js";
4
+ } from "./chunk-O4WBUIAN.js";
5
+ import "./chunk-4RHM5374.js";
6
+ import "./chunk-MYP3UYWE.js";
7
7
  import "./chunk-CZNX6EUV.js";
8
8
  export {
9
9
  decode,
package/dist/bytes.cjs CHANGED
@@ -81,9 +81,10 @@ var convertBase = (bytes, fromBase, toBase) => {
81
81
  };
82
82
  var encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
83
83
  var decodeBytesWithCharset = (input, charset) => {
84
+ assert(new RegExp(`^[${charset}]*$`).test(input), "Invalid input");
84
85
  const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
85
86
  const bytes = input.split("").map((char) => charMap[char]);
86
- return convertBase(bytes, charset.length, 256);
87
+ return new Uint8Array(convertBase(bytes, charset.length, 256));
87
88
  };
88
89
  // Annotate the CommonJS export names for ESM import in node:
89
90
  0 && (module.exports = {
package/dist/bytes.d.cts CHANGED
@@ -3,6 +3,6 @@ import { HexString } from './types.cjs';
3
3
  declare const bytesToHex: (input: Uint8Array | number[]) => `0x${string}`;
4
4
  declare const hexToBytes: (input: HexString) => Uint8Array;
5
5
  declare const encodeBytesWithCharset: (bytes: Uint8Array | number[], charset: string) => string;
6
- declare const decodeBytesWithCharset: (input: string, charset: string) => number[];
6
+ declare const decodeBytesWithCharset: (input: string, charset: string) => Uint8Array;
7
7
 
8
8
  export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes };
package/dist/bytes.d.ts CHANGED
@@ -3,6 +3,6 @@ import { HexString } from './types.js';
3
3
  declare const bytesToHex: (input: Uint8Array | number[]) => `0x${string}`;
4
4
  declare const hexToBytes: (input: HexString) => Uint8Array;
5
5
  declare const encodeBytesWithCharset: (bytes: Uint8Array | number[], charset: string) => string;
6
- declare const decodeBytesWithCharset: (input: string, charset: string) => number[];
6
+ declare const decodeBytesWithCharset: (input: string, charset: string) => Uint8Array;
7
7
 
8
8
  export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes };
package/dist/bytes.js CHANGED
@@ -3,8 +3,8 @@ import {
3
3
  decodeBytesWithCharset,
4
4
  encodeBytesWithCharset,
5
5
  hexToBytes
6
- } from "./chunk-DGVZ5UDU.js";
7
- import "./chunk-NYJKCZRI.js";
6
+ } from "./chunk-4RHM5374.js";
7
+ import "./chunk-MYP3UYWE.js";
8
8
  import "./chunk-CZNX6EUV.js";
9
9
  export {
10
10
  bytesToHex,
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  assert
3
- } from "./chunk-NYJKCZRI.js";
3
+ } from "./chunk-MYP3UYWE.js";
4
4
 
5
5
  // src/bytes.ts
6
6
  var bytesToHex = (input) => {
@@ -39,9 +39,10 @@ var convertBase = (bytes, fromBase, toBase) => {
39
39
  };
40
40
  var encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
41
41
  var decodeBytesWithCharset = (input, charset) => {
42
+ assert(new RegExp(`^[${charset}]*$`).test(input), "Invalid input");
42
43
  const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
43
44
  const bytes = input.split("").map((char) => charMap[char]);
44
- return convertBase(bytes, charset.length, 256);
45
+ return new Uint8Array(convertBase(bytes, charset.length, 256));
45
46
  };
46
47
 
47
48
  export {
@@ -15,25 +15,39 @@ function assert(condition, message = "assertion failed", Constructor = Error) {
15
15
  }
16
16
  }
17
17
  function assertString(value, message) {
18
- assert(isString(value), message ?? `expected a "string", got "${typeof value}"`);
18
+ if (!isString(value)) {
19
+ throw new TypeError(message ?? `expected a "string", got "${typeof value}"`);
20
+ }
19
21
  }
20
22
  function assertNumber(value, message) {
21
- assert(isNumber(value), message ?? `expected a "number", got "${typeof value}"`);
23
+ if (!isNumber(value)) {
24
+ throw new TypeError(message ?? `expected a "number", got "${typeof value}"`);
25
+ }
22
26
  }
23
27
  function assertBigInt(value, message) {
24
- assert(isBigInt(value), message ?? `expected a "bigint", got "${typeof value}"`);
28
+ if (!isBigInt(value)) {
29
+ throw new TypeError(message ?? `expected a "bigint", got "${typeof value}"`);
30
+ }
25
31
  }
26
32
  function assertBoolean(value, message) {
27
- assert(isBoolean(value), message ?? `expected a "boolean", got "${typeof value}"`);
33
+ if (!isBoolean(value)) {
34
+ throw new TypeError(message ?? `expected a "boolean", got "${typeof value}"`);
35
+ }
28
36
  }
29
37
  function assertSymbol(value, message) {
30
- assert(isSymbol(value), message ?? `expected a "symbol", got "${typeof value}"`);
38
+ if (!isSymbol(value)) {
39
+ throw new TypeError(message ?? `expected a "symbol", got "${typeof value}"`);
40
+ }
31
41
  }
32
42
  function assertObject(value, message) {
33
- assert(isObject(value), message ?? `expected an "object", got "${typeof value}"`);
43
+ if (!isObject(value)) {
44
+ throw new TypeError(message ?? `expected an "object", got "${typeof value}"`);
45
+ }
34
46
  }
35
47
  function assertUndefined(value, message) {
36
- assert(isUndefined(value), message ?? `expected "undefined", got "${typeof value}"`);
48
+ if (!isUndefined(value)) {
49
+ throw new TypeError(message ?? `expected "undefined", got "${typeof value}"`);
50
+ }
37
51
  }
38
52
  var unreachable = (x, message = "unreachable") => {
39
53
  throw new Error(message);
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  decodeBytesWithCharset,
3
3
  encodeBytesWithCharset
4
- } from "./chunk-DGVZ5UDU.js";
4
+ } from "./chunk-4RHM5374.js";
5
5
 
6
6
  // src/base58.ts
7
7
  var charset = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
package/dist/ss58.cjs CHANGED
@@ -674,9 +674,10 @@ var convertBase = (bytes2, fromBase, toBase) => {
674
674
  };
675
675
  var encodeBytesWithCharset = (bytes2, charset2) => convertBase(bytes2, 256, charset2.length).map((charCode) => charset2.charAt(charCode)).join("");
676
676
  var decodeBytesWithCharset = (input, charset2) => {
677
+ assert(new RegExp(`^[${charset2}]*$`).test(input), "Invalid input");
677
678
  const charMap = Object.fromEntries([...charset2].map((char, index) => [char, index]));
678
679
  const bytes2 = input.split("").map((char) => charMap[char]);
679
- return convertBase(bytes2, charset2.length, 256);
680
+ return new Uint8Array(convertBase(bytes2, charset2.length, 256));
680
681
  };
681
682
 
682
683
  // src/base58.ts
@@ -707,10 +708,10 @@ var decode2 = (input) => {
707
708
  ss58Format = decodedBytes[0];
708
709
  }
709
710
  assert(!RESERVED_PREFIXES.includes(ss58Format), `Reserved prefix: ${ss58Format}`);
710
- const checksumBytes = decodedBytes.splice(-CHECKSUM_BYTE_LENGTH);
711
- const data = decodedBytes.slice(ss58FormatLen);
711
+ const checksumBytes = decodedBytes.slice(-CHECKSUM_BYTE_LENGTH);
712
+ const data = decodedBytes.slice(ss58FormatLen, -CHECKSUM_BYTE_LENGTH);
712
713
  assert(data.length === DATA_LENGTH, `Invalid data length: ${data.length}`);
713
- const checksum = computeChecksum(decodedBytes);
714
+ const checksum = computeChecksum(decodedBytes.slice(0, -CHECKSUM_BYTE_LENGTH));
714
715
  assert(
715
716
  checksumBytes[0] === checksum[0],
716
717
  `Invalid checksum: ${checksumBytes[0]} !== ${checksum[0]}`
package/dist/ss58.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  decode,
3
3
  encode
4
- } from "./chunk-MORKFLN4.js";
4
+ } from "./chunk-O4WBUIAN.js";
5
5
  import {
6
6
  hexToBytes
7
- } from "./chunk-DGVZ5UDU.js";
7
+ } from "./chunk-4RHM5374.js";
8
8
  import {
9
9
  assert
10
- } from "./chunk-NYJKCZRI.js";
10
+ } from "./chunk-MYP3UYWE.js";
11
11
  import "./chunk-CZNX6EUV.js";
12
12
 
13
13
  // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_assert.js
@@ -632,10 +632,10 @@ var decode2 = (input) => {
632
632
  ss58Format = decodedBytes[0];
633
633
  }
634
634
  assert(!RESERVED_PREFIXES.includes(ss58Format), `Reserved prefix: ${ss58Format}`);
635
- const checksumBytes = decodedBytes.splice(-CHECKSUM_BYTE_LENGTH);
636
- const data = decodedBytes.slice(ss58FormatLen);
635
+ const checksumBytes = decodedBytes.slice(-CHECKSUM_BYTE_LENGTH);
636
+ const data = decodedBytes.slice(ss58FormatLen, -CHECKSUM_BYTE_LENGTH);
637
637
  assert(data.length === DATA_LENGTH, `Invalid data length: ${data.length}`);
638
- const checksum = computeChecksum(decodedBytes);
638
+ const checksum = computeChecksum(decodedBytes.slice(0, -CHECKSUM_BYTE_LENGTH));
639
639
  assert(
640
640
  checksumBytes[0] === checksum[0],
641
641
  `Invalid checksum: ${checksumBytes[0]} !== ${checksum[0]}`
package/dist/string.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var string_exports = {};
22
22
  __export(string_exports, {
23
23
  capitalize: () => capitalize,
24
+ isHex: () => isHex,
24
25
  split: () => split,
25
26
  toLowerCase: () => toLowerCase,
26
27
  toUpperCase: () => toUpperCase,
@@ -32,9 +33,11 @@ var toLowerCase = (str) => str.toLowerCase();
32
33
  var split = (str, delimiter) => str.split(delimiter);
33
34
  var capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
34
35
  var uncapitalize = (str) => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
36
+ var isHex = (str) => /^0x[\da-f]+$/i.test(str);
35
37
  // Annotate the CommonJS export names for ESM import in node:
36
38
  0 && (module.exports = {
37
39
  capitalize,
40
+ isHex,
38
41
  split,
39
42
  toLowerCase,
40
43
  toUpperCase,
package/dist/string.d.cts CHANGED
@@ -4,5 +4,6 @@ type Split<T extends string, D extends string> = T extends `${infer L}${D}${infe
4
4
  declare const split: <const T extends string, D extends string>(str: T, delimiter: D) => Split<T, D>;
5
5
  declare const capitalize: <const T extends string>(str: T) => Capitalize<T>;
6
6
  declare const uncapitalize: <const T extends string>(str: T) => Uncapitalize<T>;
7
+ declare const isHex: (str: string) => str is `0x${string}`;
7
8
 
8
- export { capitalize, split, toLowerCase, toUpperCase, uncapitalize };
9
+ export { capitalize, isHex, split, toLowerCase, toUpperCase, uncapitalize };
package/dist/string.d.ts CHANGED
@@ -4,5 +4,6 @@ type Split<T extends string, D extends string> = T extends `${infer L}${D}${infe
4
4
  declare const split: <const T extends string, D extends string>(str: T, delimiter: D) => Split<T, D>;
5
5
  declare const capitalize: <const T extends string>(str: T) => Capitalize<T>;
6
6
  declare const uncapitalize: <const T extends string>(str: T) => Uncapitalize<T>;
7
+ declare const isHex: (str: string) => str is `0x${string}`;
7
8
 
8
- export { capitalize, split, toLowerCase, toUpperCase, uncapitalize };
9
+ export { capitalize, isHex, split, toLowerCase, toUpperCase, uncapitalize };
package/dist/string.js CHANGED
@@ -4,8 +4,10 @@ var toLowerCase = (str) => str.toLowerCase();
4
4
  var split = (str, delimiter) => str.split(delimiter);
5
5
  var capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
6
6
  var uncapitalize = (str) => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
7
+ var isHex = (str) => /^0x[\da-f]+$/i.test(str);
7
8
  export {
8
9
  capitalize,
10
+ isHex,
9
11
  split,
10
12
  toLowerCase,
11
13
  toUpperCase,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainflip/utils",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",