@chainflip/utils 0.11.3 → 2.1.0-beta.0

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.
Files changed (71) hide show
  1. package/dist/_virtual/rolldown_runtime.cjs +29 -0
  2. package/dist/addresses.cjs +15 -40
  3. package/dist/addresses.d.ts +4 -3
  4. package/dist/addresses.js +14 -15
  5. package/dist/assertion.cjs +23 -82
  6. package/dist/assertion.d.ts +3 -2
  7. package/dist/assertion.js +32 -22
  8. package/dist/async.cjs +96 -115
  9. package/dist/async.d.ts +24 -21
  10. package/dist/async.js +92 -87
  11. package/dist/base58.cjs +9 -93
  12. package/dist/base58.d.ts +4 -4
  13. package/dist/base58.js +9 -13
  14. package/dist/bytes.cjs +49 -97
  15. package/dist/bytes.d.ts +4 -4
  16. package/dist/bytes.js +51 -15
  17. package/dist/chainflip.cjs +428 -327
  18. package/dist/chainflip.d.ts +188 -185
  19. package/dist/chainflip.js +425 -41
  20. package/dist/consts.cjs +306 -277
  21. package/dist/consts.d.ts +18 -17
  22. package/dist/consts.js +296 -243
  23. package/dist/date.cjs +74 -131
  24. package/dist/date.d.ts +9 -8
  25. package/dist/date.js +60 -95
  26. package/dist/guard.cjs +28 -63
  27. package/dist/guard.d.ts +10 -9
  28. package/dist/guard.js +17 -28
  29. package/dist/math.cjs +7 -32
  30. package/dist/math.d.ts +3 -2
  31. package/dist/math.js +6 -7
  32. package/dist/number.cjs +5 -29
  33. package/dist/number.d.ts +4 -4
  34. package/dist/number.js +5 -5
  35. package/dist/ss58.cjs +62 -794
  36. package/dist/ss58.d.ts +12 -9
  37. package/dist/ss58.js +59 -712
  38. package/dist/string.cjs +29 -66
  39. package/dist/string.d.ts +4 -4
  40. package/dist/string.js +20 -33
  41. package/dist/tickMath.cjs +32 -232
  42. package/dist/tickMath.d.ts +7 -6
  43. package/dist/tickMath.js +24 -29
  44. package/dist/types.cjs +0 -18
  45. package/dist/types.d.ts +35 -34
  46. package/dist/types.js +1 -0
  47. package/dist/url.cjs +16 -37
  48. package/dist/url.d.ts +5 -4
  49. package/dist/url.js +16 -13
  50. package/package.json +5 -5
  51. package/dist/addresses.d.cts +0 -6
  52. package/dist/assertion.d.cts +0 -11
  53. package/dist/async.d.cts +0 -33
  54. package/dist/base58.d.cts +0 -8
  55. package/dist/bytes.d.cts +0 -11
  56. package/dist/chainflip.d.cts +0 -236
  57. package/dist/chunk-3P6TXYEI.js +0 -15
  58. package/dist/chunk-HBIFE4XN.js +0 -29
  59. package/dist/chunk-LJJH7U4M.js +0 -302
  60. package/dist/chunk-XGNPN5CY.js +0 -61
  61. package/dist/chunk-ZHIKNZLU.js +0 -66
  62. package/dist/consts.d.cts +0 -33
  63. package/dist/date.d.cts +0 -26
  64. package/dist/guard.d.cts +0 -23
  65. package/dist/math.d.cts +0 -4
  66. package/dist/number.d.cts +0 -6
  67. package/dist/ss58.d.cts +0 -14
  68. package/dist/string.d.cts +0 -16
  69. package/dist/tickMath.d.cts +0 -15
  70. package/dist/types.d.cts +0 -41
  71. package/dist/url.d.cts +0 -6
package/dist/async.js CHANGED
@@ -1,94 +1,99 @@
1
- // src/async.ts
2
- var deferredPromise = () => {
3
- let resolve;
4
- let reject;
5
- const promise = new Promise((res, rej) => {
6
- resolve = res;
7
- reject = rej;
8
- });
9
- return { promise, resolve, reject };
1
+ //#region src/async.ts
2
+ const deferredPromise = () => {
3
+ let resolve;
4
+ let reject;
5
+ return {
6
+ promise: new Promise((res, rej) => {
7
+ resolve = res;
8
+ reject = rej;
9
+ }),
10
+ resolve,
11
+ reject
12
+ };
10
13
  };
11
- var sleep = (ms, { signal } = {}) => {
12
- const deferred = deferredPromise();
13
- const timeout = setTimeout(deferred.resolve, ms);
14
- if (signal) {
15
- const abort = () => {
16
- clearTimeout(timeout);
17
- deferred.resolve();
18
- };
19
- signal.addEventListener("abort", abort);
20
- deferred.promise = deferred.promise.finally(() => {
21
- signal.removeEventListener("abort", abort);
22
- });
23
- }
24
- return deferred.promise;
14
+ const sleep = (ms, { signal } = {}) => {
15
+ const deferred = deferredPromise();
16
+ const timeout = setTimeout(deferred.resolve, ms);
17
+ if (signal) {
18
+ const abort = () => {
19
+ clearTimeout(timeout);
20
+ deferred.resolve();
21
+ };
22
+ signal.addEventListener("abort", abort);
23
+ deferred.promise = deferred.promise.finally(() => {
24
+ signal.removeEventListener("abort", abort);
25
+ });
26
+ }
27
+ return deferred.promise;
25
28
  };
26
29
  var Queue = class {
27
- constructor(debounce) {
28
- this.debounce = debounce;
29
- }
30
- promise = Promise.resolve();
31
- enqueue(fn, ...args) {
32
- return new Promise((resolve, reject) => {
33
- this.promise = this.promise.then(async () => {
34
- const sleepPromise = this.debounce ? sleep(this.debounce) : Promise.resolve();
35
- await Promise.all([fn(...args).then(resolve, reject), sleepPromise]);
36
- });
37
- });
38
- }
30
+ promise = Promise.resolve();
31
+ constructor(debounce) {
32
+ this.debounce = debounce;
33
+ }
34
+ enqueue(fn, ...args) {
35
+ return new Promise((resolve, reject) => {
36
+ this.promise = this.promise.then(async () => {
37
+ const sleepPromise = this.debounce ? sleep(this.debounce) : Promise.resolve();
38
+ await Promise.all([fn(...args).then(resolve, reject), sleepPromise]);
39
+ });
40
+ });
41
+ }
39
42
  };
40
43
  var RateLimiter = class {
41
- fn;
42
- debounce;
43
- queues;
44
- index = 0;
45
- constructor(opts) {
46
- this.fn = opts.fn;
47
- this.debounce = opts.debounce;
48
- this.queues = Array.from({ length: opts.maxConcurrency }, () => new Queue(this.debounce));
49
- }
50
- request(...args) {
51
- const nextIndex = this.index;
52
- this.index = (this.index + 1) % this.queues.length;
53
- return this.queues[nextIndex].enqueue(this.fn, ...args);
54
- }
44
+ fn;
45
+ debounce;
46
+ queues;
47
+ index = 0;
48
+ constructor(opts) {
49
+ this.fn = opts.fn;
50
+ this.debounce = opts.debounce;
51
+ this.queues = Array.from({ length: opts.maxConcurrency }, () => new Queue(this.debounce));
52
+ }
53
+ request(...args) {
54
+ const nextIndex = this.index;
55
+ this.index = (this.index + 1) % this.queues.length;
56
+ return this.queues[nextIndex].enqueue(this.fn, ...args);
57
+ }
55
58
  };
56
- var once = async (target, event, opts) => {
57
- const deferred = deferredPromise();
58
- const onSuccess = () => {
59
- target.removeEventListener("error", onError);
60
- deferred.resolve();
61
- };
62
- const onError = () => {
63
- target.removeEventListener(event, onSuccess);
64
- deferred.reject(new Error("error"));
65
- };
66
- target.addEventListener(event, onSuccess, { once: true, signal: opts?.signal });
67
- target.addEventListener("error", onError, { once: true, signal: opts?.signal });
68
- if (opts?.signal) {
69
- const { signal } = opts;
70
- const abort = () => {
71
- deferred.reject(new Error("aborted"));
72
- };
73
- signal.addEventListener("abort", abort);
74
- deferred.promise = deferred.promise.finally(() => {
75
- signal.removeEventListener("abort", abort);
76
- });
77
- }
78
- if (opts?.timeout) {
79
- const timer = setTimeout(() => {
80
- deferred.reject(new Error("timeout"));
81
- }, opts.timeout);
82
- deferred.promise = deferred.promise.finally(() => {
83
- clearTimeout(timer);
84
- });
85
- }
86
- await deferred.promise;
87
- };
88
- export {
89
- Queue,
90
- RateLimiter,
91
- deferredPromise,
92
- once,
93
- sleep
59
+ const once = async (target, event, opts) => {
60
+ const deferred = deferredPromise();
61
+ const onSuccess = () => {
62
+ target.removeEventListener("error", onError);
63
+ deferred.resolve();
64
+ };
65
+ const onError = () => {
66
+ target.removeEventListener(event, onSuccess);
67
+ deferred.reject(/* @__PURE__ */ new Error("error"));
68
+ };
69
+ target.addEventListener(event, onSuccess, {
70
+ once: true,
71
+ signal: opts?.signal
72
+ });
73
+ target.addEventListener("error", onError, {
74
+ once: true,
75
+ signal: opts?.signal
76
+ });
77
+ if (opts?.signal) {
78
+ const { signal } = opts;
79
+ const abort = () => {
80
+ deferred.reject(/* @__PURE__ */ new Error("aborted"));
81
+ };
82
+ signal.addEventListener("abort", abort);
83
+ deferred.promise = deferred.promise.finally(() => {
84
+ signal.removeEventListener("abort", abort);
85
+ });
86
+ }
87
+ if (opts?.timeout) {
88
+ const timer = setTimeout(() => {
89
+ deferred.reject(/* @__PURE__ */ new Error("timeout"));
90
+ }, opts.timeout);
91
+ deferred.promise = deferred.promise.finally(() => {
92
+ clearTimeout(timer);
93
+ });
94
+ }
95
+ await deferred.promise;
94
96
  };
97
+
98
+ //#endregion
99
+ export { Queue, RateLimiter, deferredPromise, once, sleep };
package/dist/base58.cjs CHANGED
@@ -1,95 +1,11 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ const require_bytes = require('./bytes.cjs');
19
2
 
20
- // src/base58.ts
21
- var base58_exports = {};
22
- __export(base58_exports, {
23
- CHARSET: () => CHARSET,
24
- decode: () => decode,
25
- encode: () => encode
26
- });
27
- module.exports = __toCommonJS(base58_exports);
3
+ //#region src/base58.ts
4
+ const CHARSET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
5
+ const encode = (bytes) => require_bytes.encodeBytesWithCharset(bytes, CHARSET);
6
+ const decode = (input) => require_bytes.decodeBytesWithCharset(input, CHARSET);
28
7
 
29
- // src/guard.ts
30
- var createIsGuard = (type) => (value) => typeof value === type;
31
- var isString = createIsGuard("string");
32
- var isNumber = createIsGuard("number");
33
- var isBigInt = createIsGuard("bigint");
34
- var isBoolean = createIsGuard("boolean");
35
- var isSymbol = createIsGuard("symbol");
36
- var isObject = createIsGuard("object");
37
- var isUndefined = createIsGuard("undefined");
38
-
39
- // src/assertion.ts
40
- function assert(condition, message = "assertion failed", Constructor = Error) {
41
- if (!condition) {
42
- throw new Constructor(message);
43
- }
44
- }
45
-
46
- // src/bytes.ts
47
- var hexToBytes = (input) => {
48
- assert(/^0x[\da-f]*$/i.test(input) && input.length % 2 === 0, "Invalid hex string");
49
- const hex = input.slice(2);
50
- const bytes = new Uint8Array(hex.length / 2);
51
- for (let i = 0; i < hex.length; i += 2) {
52
- bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16);
53
- }
54
- return bytes;
55
- };
56
- var convertBase = (inputBytes, fromBase, toBase) => {
57
- const bytes = typeof inputBytes === "string" ? hexToBytes(inputBytes) : new Uint8Array(inputBytes);
58
- const result = [];
59
- for (const byte of bytes) {
60
- let carry = byte;
61
- for (let i = 0; i < result.length; i += 1) {
62
- carry += result[i] * fromBase;
63
- result[i] = carry % toBase;
64
- carry = Math.floor(carry / toBase);
65
- }
66
- while (carry !== 0) {
67
- result.push(carry % toBase);
68
- carry = Math.floor(carry / toBase);
69
- }
70
- }
71
- let leadingZeros = 0;
72
- while (bytes[leadingZeros] === 0) {
73
- leadingZeros += 1;
74
- result.push(0);
75
- }
76
- return result.reverse();
77
- };
78
- var encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
79
- var decodeBytesWithCharset = (input, charset) => {
80
- assert(new RegExp(`^[${charset}]*$`).test(input), "Invalid input");
81
- const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
82
- const bytes = input.split("").map((char) => charMap[char]);
83
- return new Uint8Array(convertBase(bytes, charset.length, 256));
84
- };
85
-
86
- // src/base58.ts
87
- var CHARSET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
88
- var encode = (bytes) => encodeBytesWithCharset(bytes, CHARSET);
89
- var decode = (input) => decodeBytesWithCharset(input, CHARSET);
90
- // Annotate the CommonJS export names for ESM import in node:
91
- 0 && (module.exports = {
92
- CHARSET,
93
- decode,
94
- encode
95
- });
8
+ //#endregion
9
+ exports.CHARSET = CHARSET;
10
+ exports.decode = decode;
11
+ exports.encode = encode;
package/dist/base58.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { Bytelike } from './types.js';
2
- import './chainflip.js';
1
+ import { Bytelike } from "./types.js";
3
2
 
3
+ //#region src/base58.d.ts
4
4
  declare const CHARSET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
5
5
  declare const encode: (bytes: Bytelike) => string;
6
6
  declare const decode: (input: string) => Uint8Array;
7
-
8
- export { CHARSET, decode, encode };
7
+ //#endregion
8
+ export { CHARSET, decode, encode };
package/dist/base58.js CHANGED
@@ -1,13 +1,9 @@
1
- import {
2
- CHARSET,
3
- decode,
4
- encode
5
- } from "./chunk-3P6TXYEI.js";
6
- import "./chunk-XGNPN5CY.js";
7
- import "./chunk-ZHIKNZLU.js";
8
- import "./chunk-HBIFE4XN.js";
9
- export {
10
- CHARSET,
11
- decode,
12
- encode
13
- };
1
+ import { decodeBytesWithCharset, encodeBytesWithCharset } from "./bytes.js";
2
+
3
+ //#region src/base58.ts
4
+ const CHARSET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
5
+ const encode = (bytes) => encodeBytesWithCharset(bytes, CHARSET);
6
+ const decode = (input) => decodeBytesWithCharset(input, CHARSET);
7
+
8
+ //#endregion
9
+ export { CHARSET, decode, encode };
package/dist/bytes.cjs CHANGED
@@ -1,104 +1,56 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/bytes.ts
21
- var bytes_exports = {};
22
- __export(bytes_exports, {
23
- bytesToHex: () => bytesToHex,
24
- decodeBytesWithCharset: () => decodeBytesWithCharset,
25
- encodeBytesWithCharset: () => encodeBytesWithCharset,
26
- hexToBytes: () => hexToBytes,
27
- reverseBytes: () => reverseBytes
28
- });
29
- module.exports = __toCommonJS(bytes_exports);
30
-
31
- // src/guard.ts
32
- var createIsGuard = (type) => (value) => typeof value === type;
33
- var isString = createIsGuard("string");
34
- var isNumber = createIsGuard("number");
35
- var isBigInt = createIsGuard("bigint");
36
- var isBoolean = createIsGuard("boolean");
37
- var isSymbol = createIsGuard("symbol");
38
- var isObject = createIsGuard("object");
39
- var isUndefined = createIsGuard("undefined");
1
+ const require_assertion = require('./assertion.cjs');
40
2
 
41
- // src/assertion.ts
42
- function assert(condition, message = "assertion failed", Constructor = Error) {
43
- if (!condition) {
44
- throw new Constructor(message);
45
- }
46
- }
47
-
48
- // src/bytes.ts
49
- var bytesToHex = (input) => {
50
- const bytes = new Uint8Array(input);
51
- return `0x${Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
3
+ //#region src/bytes.ts
4
+ const bytesToHex = (input) => {
5
+ const bytes = new Uint8Array(input);
6
+ return `0x${Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
52
7
  };
53
- var hexToBytes = (input) => {
54
- assert(/^0x[\da-f]*$/i.test(input) && input.length % 2 === 0, "Invalid hex string");
55
- const hex = input.slice(2);
56
- const bytes = new Uint8Array(hex.length / 2);
57
- for (let i = 0; i < hex.length; i += 2) {
58
- bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16);
59
- }
60
- return bytes;
8
+ const hexToBytes = (input) => {
9
+ require_assertion.assert(/^0x[\da-f]*$/i.test(input) && input.length % 2 === 0, "Invalid hex string");
10
+ const hex = input.slice(2);
11
+ const bytes = new Uint8Array(hex.length / 2);
12
+ for (let i = 0; i < hex.length; i += 2) bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16);
13
+ return bytes;
61
14
  };
62
- var convertBase = (inputBytes, fromBase, toBase) => {
63
- const bytes = typeof inputBytes === "string" ? hexToBytes(inputBytes) : new Uint8Array(inputBytes);
64
- const result = [];
65
- for (const byte of bytes) {
66
- let carry = byte;
67
- for (let i = 0; i < result.length; i += 1) {
68
- carry += result[i] * fromBase;
69
- result[i] = carry % toBase;
70
- carry = Math.floor(carry / toBase);
71
- }
72
- while (carry !== 0) {
73
- result.push(carry % toBase);
74
- carry = Math.floor(carry / toBase);
75
- }
76
- }
77
- let leadingZeros = 0;
78
- while (bytes[leadingZeros] === 0) {
79
- leadingZeros += 1;
80
- result.push(0);
81
- }
82
- return result.reverse();
15
+ const convertBase = (inputBytes, fromBase, toBase) => {
16
+ const bytes = typeof inputBytes === "string" ? hexToBytes(inputBytes) : new Uint8Array(inputBytes);
17
+ const result = [];
18
+ for (const byte of bytes) {
19
+ let carry = byte;
20
+ for (let i = 0; i < result.length; i += 1) {
21
+ carry += result[i] * fromBase;
22
+ result[i] = carry % toBase;
23
+ carry = Math.floor(carry / toBase);
24
+ }
25
+ while (carry !== 0) {
26
+ result.push(carry % toBase);
27
+ carry = Math.floor(carry / toBase);
28
+ }
29
+ }
30
+ let leadingZeros = 0;
31
+ while (bytes[leadingZeros] === 0) {
32
+ leadingZeros += 1;
33
+ result.push(0);
34
+ }
35
+ return result.reverse();
83
36
  };
84
- var encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
85
- var decodeBytesWithCharset = (input, charset) => {
86
- assert(new RegExp(`^[${charset}]*$`).test(input), "Invalid input");
87
- const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
88
- const bytes = input.split("").map((char) => charMap[char]);
89
- return new Uint8Array(convertBase(bytes, charset.length, 256));
37
+ const encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
38
+ const decodeBytesWithCharset = (input, charset) => {
39
+ require_assertion.assert(new RegExp(`^[${charset}]*$`).test(input), "Invalid input");
40
+ const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
41
+ const bytes = input.split("").map((char) => charMap[char]);
42
+ return new Uint8Array(convertBase(bytes, charset.length, 256));
90
43
  };
91
44
  function reverseBytes(input) {
92
- const bytes = /^(?:0x)?([\da-f]+)$/gi.exec(input)?.[1];
93
- assert(bytes && bytes.length % 2 === 0 && bytes.length > 0, "Invalid hex string");
94
- const reversed = bytes.match(/.{2}/g).reverse().join("");
95
- return input.startsWith("0x") ? `0x${reversed}` : reversed;
45
+ const bytes = /^(?:0x)?([\da-f]+)$/gi.exec(input)?.[1];
46
+ require_assertion.assert(bytes && bytes.length % 2 === 0 && bytes.length > 0, "Invalid hex string");
47
+ const reversed = bytes.match(/.{2}/g).reverse().join("");
48
+ return input.startsWith("0x") ? `0x${reversed}` : reversed;
96
49
  }
97
- // Annotate the CommonJS export names for ESM import in node:
98
- 0 && (module.exports = {
99
- bytesToHex,
100
- decodeBytesWithCharset,
101
- encodeBytesWithCharset,
102
- hexToBytes,
103
- reverseBytes
104
- });
50
+
51
+ //#endregion
52
+ exports.bytesToHex = bytesToHex;
53
+ exports.decodeBytesWithCharset = decodeBytesWithCharset;
54
+ exports.encodeBytesWithCharset = encodeBytesWithCharset;
55
+ exports.hexToBytes = hexToBytes;
56
+ exports.reverseBytes = reverseBytes;
package/dist/bytes.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { HexString, Bytelike } from './types.js';
2
- import './chainflip.js';
1
+ import { Bytelike, HexString } from "./types.js";
3
2
 
3
+ //#region src/bytes.d.ts
4
4
  declare const bytesToHex: (input: Uint8Array | number[]) => `0x${string}`;
5
5
  declare const hexToBytes: (input: HexString) => Uint8Array;
6
6
  declare const encodeBytesWithCharset: (bytes: Bytelike, charset: string) => string;
7
7
  declare const decodeBytesWithCharset: (input: string, charset: string) => Uint8Array;
8
8
  declare function reverseBytes(input: HexString): HexString;
9
9
  declare function reverseBytes(input: string): string;
10
-
11
- export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes, reverseBytes };
10
+ //#endregion
11
+ export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes, reverseBytes };
package/dist/bytes.js CHANGED
@@ -1,16 +1,52 @@
1
- import {
2
- bytesToHex,
3
- decodeBytesWithCharset,
4
- encodeBytesWithCharset,
5
- hexToBytes,
6
- reverseBytes
7
- } from "./chunk-XGNPN5CY.js";
8
- import "./chunk-ZHIKNZLU.js";
9
- import "./chunk-HBIFE4XN.js";
10
- export {
11
- bytesToHex,
12
- decodeBytesWithCharset,
13
- encodeBytesWithCharset,
14
- hexToBytes,
15
- reverseBytes
1
+ import { assert } from "./assertion.js";
2
+
3
+ //#region src/bytes.ts
4
+ const bytesToHex = (input) => {
5
+ const bytes = new Uint8Array(input);
6
+ return `0x${Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
16
7
  };
8
+ const hexToBytes = (input) => {
9
+ assert(/^0x[\da-f]*$/i.test(input) && input.length % 2 === 0, "Invalid hex string");
10
+ const hex = input.slice(2);
11
+ const bytes = new Uint8Array(hex.length / 2);
12
+ for (let i = 0; i < hex.length; i += 2) bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16);
13
+ return bytes;
14
+ };
15
+ const convertBase = (inputBytes, fromBase, toBase) => {
16
+ const bytes = typeof inputBytes === "string" ? hexToBytes(inputBytes) : new Uint8Array(inputBytes);
17
+ const result = [];
18
+ for (const byte of bytes) {
19
+ let carry = byte;
20
+ for (let i = 0; i < result.length; i += 1) {
21
+ carry += result[i] * fromBase;
22
+ result[i] = carry % toBase;
23
+ carry = Math.floor(carry / toBase);
24
+ }
25
+ while (carry !== 0) {
26
+ result.push(carry % toBase);
27
+ carry = Math.floor(carry / toBase);
28
+ }
29
+ }
30
+ let leadingZeros = 0;
31
+ while (bytes[leadingZeros] === 0) {
32
+ leadingZeros += 1;
33
+ result.push(0);
34
+ }
35
+ return result.reverse();
36
+ };
37
+ const encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
38
+ const decodeBytesWithCharset = (input, charset) => {
39
+ assert(new RegExp(`^[${charset}]*$`).test(input), "Invalid input");
40
+ const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
41
+ const bytes = input.split("").map((char) => charMap[char]);
42
+ return new Uint8Array(convertBase(bytes, charset.length, 256));
43
+ };
44
+ function reverseBytes(input) {
45
+ const bytes = /^(?:0x)?([\da-f]+)$/gi.exec(input)?.[1];
46
+ assert(bytes && bytes.length % 2 === 0 && bytes.length > 0, "Invalid hex string");
47
+ const reversed = bytes.match(/.{2}/g).reverse().join("");
48
+ return input.startsWith("0x") ? `0x${reversed}` : reversed;
49
+ }
50
+
51
+ //#endregion
52
+ export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes, reverseBytes };