@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.
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/addresses.cjs +15 -40
- package/dist/addresses.d.ts +4 -3
- package/dist/addresses.js +14 -15
- package/dist/assertion.cjs +23 -82
- package/dist/assertion.d.ts +3 -2
- package/dist/assertion.js +32 -22
- package/dist/async.cjs +96 -115
- package/dist/async.d.ts +24 -21
- package/dist/async.js +92 -87
- package/dist/base58.cjs +9 -93
- package/dist/base58.d.ts +4 -4
- package/dist/base58.js +9 -13
- package/dist/bytes.cjs +49 -97
- package/dist/bytes.d.ts +4 -4
- package/dist/bytes.js +51 -15
- package/dist/chainflip.cjs +428 -327
- package/dist/chainflip.d.ts +188 -185
- package/dist/chainflip.js +425 -41
- package/dist/consts.cjs +306 -277
- package/dist/consts.d.ts +18 -17
- package/dist/consts.js +296 -243
- package/dist/date.cjs +74 -131
- package/dist/date.d.ts +9 -8
- package/dist/date.js +60 -95
- package/dist/guard.cjs +28 -63
- package/dist/guard.d.ts +10 -9
- package/dist/guard.js +17 -28
- package/dist/math.cjs +7 -32
- package/dist/math.d.ts +3 -2
- package/dist/math.js +6 -7
- package/dist/number.cjs +5 -29
- package/dist/number.d.ts +4 -4
- package/dist/number.js +5 -5
- package/dist/ss58.cjs +62 -794
- package/dist/ss58.d.ts +12 -9
- package/dist/ss58.js +59 -712
- package/dist/string.cjs +29 -66
- package/dist/string.d.ts +4 -4
- package/dist/string.js +20 -33
- package/dist/tickMath.cjs +32 -232
- package/dist/tickMath.d.ts +7 -6
- package/dist/tickMath.js +24 -29
- package/dist/types.cjs +0 -18
- package/dist/types.d.ts +35 -34
- package/dist/types.js +1 -0
- package/dist/url.cjs +16 -37
- package/dist/url.d.ts +5 -4
- package/dist/url.js +16 -13
- package/package.json +5 -5
- package/dist/addresses.d.cts +0 -6
- package/dist/assertion.d.cts +0 -11
- package/dist/async.d.cts +0 -33
- package/dist/base58.d.cts +0 -8
- package/dist/bytes.d.cts +0 -11
- package/dist/chainflip.d.cts +0 -236
- package/dist/chunk-3P6TXYEI.js +0 -15
- package/dist/chunk-HBIFE4XN.js +0 -29
- package/dist/chunk-LJJH7U4M.js +0 -302
- package/dist/chunk-XGNPN5CY.js +0 -61
- package/dist/chunk-ZHIKNZLU.js +0 -66
- package/dist/consts.d.cts +0 -33
- package/dist/date.d.cts +0 -26
- package/dist/guard.d.cts +0 -23
- package/dist/math.d.cts +0 -4
- package/dist/number.d.cts +0 -6
- package/dist/ss58.d.cts +0 -14
- package/dist/string.d.cts +0 -16
- package/dist/tickMath.d.cts +0 -15
- package/dist/types.d.cts +0 -41
- package/dist/url.d.cts +0 -6
package/dist/async.js
CHANGED
|
@@ -1,94 +1,99 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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 {
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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 };
|