@chainflip/utils 0.1.1 → 0.1.3
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.
- package/dist/assertion.cjs +21 -7
- package/dist/assertion.js +1 -1
- package/dist/async.cjs +34 -0
- package/dist/async.d.cts +5 -1
- package/dist/async.d.ts +5 -1
- package/dist/async.js +33 -0
- package/dist/base58.cjs +9 -1
- package/dist/base58.d.cts +1 -1
- package/dist/base58.d.ts +1 -1
- package/dist/base58.js +3 -3
- package/dist/bytes.cjs +2 -1
- package/dist/bytes.d.cts +1 -1
- package/dist/bytes.d.ts +1 -1
- package/dist/bytes.js +2 -2
- package/dist/{chunk-DGVZ5UDU.js → chunk-4RHM5374.js} +3 -2
- package/dist/{chunk-NYJKCZRI.js → chunk-MYP3UYWE.js} +21 -7
- package/dist/{chunk-MORKFLN4.js → chunk-O4WBUIAN.js} +1 -1
- package/dist/ss58.cjs +5 -4
- package/dist/ss58.js +6 -6
- package/package.json +1 -1
package/dist/assertion.cjs
CHANGED
|
@@ -49,25 +49,39 @@ function assert(condition, message = "assertion failed", Constructor = Error) {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
function assertString(value, message) {
|
|
52
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
package/dist/async.cjs
CHANGED
|
@@ -23,6 +23,7 @@ __export(async_exports, {
|
|
|
23
23
|
Queue: () => Queue,
|
|
24
24
|
RateLimiter: () => RateLimiter,
|
|
25
25
|
deferredPromise: () => deferredPromise,
|
|
26
|
+
once: () => once,
|
|
26
27
|
sleep: () => sleep
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(async_exports);
|
|
@@ -80,10 +81,43 @@ var RateLimiter = class {
|
|
|
80
81
|
return this.queues[nextIndex].enqueue(this.fn, ...args);
|
|
81
82
|
}
|
|
82
83
|
};
|
|
84
|
+
var once = async (target, event, opts) => {
|
|
85
|
+
const deferred = deferredPromise();
|
|
86
|
+
const onSuccess = () => {
|
|
87
|
+
target.removeEventListener("error", onError);
|
|
88
|
+
deferred.resolve();
|
|
89
|
+
};
|
|
90
|
+
const onError = () => {
|
|
91
|
+
target.removeEventListener(event, onSuccess);
|
|
92
|
+
deferred.reject(new Error("error"));
|
|
93
|
+
};
|
|
94
|
+
target.addEventListener(event, onSuccess, { once: true, signal: opts?.signal });
|
|
95
|
+
target.addEventListener("error", onError, { once: true, signal: opts?.signal });
|
|
96
|
+
if (opts?.signal) {
|
|
97
|
+
const signal = opts.signal;
|
|
98
|
+
const abort = () => {
|
|
99
|
+
deferred.reject(new Error("aborted"));
|
|
100
|
+
};
|
|
101
|
+
signal.addEventListener("abort", abort);
|
|
102
|
+
deferred.promise = deferred.promise.finally(() => {
|
|
103
|
+
signal.removeEventListener("abort", abort);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (opts?.timeout) {
|
|
107
|
+
const timer = setTimeout(() => {
|
|
108
|
+
deferred.reject(new Error("timeout"));
|
|
109
|
+
}, opts.timeout);
|
|
110
|
+
deferred.promise = deferred.promise.finally(() => {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
await deferred.promise;
|
|
115
|
+
};
|
|
83
116
|
// Annotate the CommonJS export names for ESM import in node:
|
|
84
117
|
0 && (module.exports = {
|
|
85
118
|
Queue,
|
|
86
119
|
RateLimiter,
|
|
87
120
|
deferredPromise,
|
|
121
|
+
once,
|
|
88
122
|
sleep
|
|
89
123
|
});
|
package/dist/async.d.cts
CHANGED
|
@@ -25,5 +25,9 @@ declare class RateLimiter<T, Args extends unknown[]> {
|
|
|
25
25
|
});
|
|
26
26
|
request(...args: Args): Promise<T>;
|
|
27
27
|
}
|
|
28
|
+
declare const once: <T extends EventTarget, K extends string>(target: T, event: K, opts?: {
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
timeout?: number;
|
|
31
|
+
}) => Promise<void>;
|
|
28
32
|
|
|
29
|
-
export { type DeferredPromise, Queue, RateLimiter, deferredPromise, sleep };
|
|
33
|
+
export { type DeferredPromise, Queue, RateLimiter, deferredPromise, once, sleep };
|
package/dist/async.d.ts
CHANGED
|
@@ -25,5 +25,9 @@ declare class RateLimiter<T, Args extends unknown[]> {
|
|
|
25
25
|
});
|
|
26
26
|
request(...args: Args): Promise<T>;
|
|
27
27
|
}
|
|
28
|
+
declare const once: <T extends EventTarget, K extends string>(target: T, event: K, opts?: {
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
timeout?: number;
|
|
31
|
+
}) => Promise<void>;
|
|
28
32
|
|
|
29
|
-
export { type DeferredPromise, Queue, RateLimiter, deferredPromise, sleep };
|
|
33
|
+
export { type DeferredPromise, Queue, RateLimiter, deferredPromise, once, sleep };
|
package/dist/async.js
CHANGED
|
@@ -53,9 +53,42 @@ var RateLimiter = class {
|
|
|
53
53
|
return this.queues[nextIndex].enqueue(this.fn, ...args);
|
|
54
54
|
}
|
|
55
55
|
};
|
|
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.signal;
|
|
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
|
+
};
|
|
56
88
|
export {
|
|
57
89
|
Queue,
|
|
58
90
|
RateLimiter,
|
|
59
91
|
deferredPromise,
|
|
92
|
+
once,
|
|
60
93
|
sleep
|
|
61
94
|
};
|
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
package/dist/base58.d.ts
CHANGED
package/dist/base58.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
decode,
|
|
3
3
|
encode
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import "./chunk-
|
|
6
|
-
import "./chunk-
|
|
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) =>
|
|
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) =>
|
|
6
|
+
declare const decodeBytesWithCharset: (input: string, charset: string) => Uint8Array;
|
|
7
7
|
|
|
8
8
|
export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes };
|
package/dist/bytes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
assert
|
|
3
|
-
} from "./chunk-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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);
|
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.
|
|
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-
|
|
4
|
+
} from "./chunk-O4WBUIAN.js";
|
|
5
5
|
import {
|
|
6
6
|
hexToBytes
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-4RHM5374.js";
|
|
8
8
|
import {
|
|
9
9
|
assert
|
|
10
|
-
} from "./chunk-
|
|
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.
|
|
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]}`
|