@chainflip/utils 0.1.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/assertion.cjs +86 -0
- package/dist/assertion.d.cts +11 -0
- package/dist/assertion.d.ts +11 -0
- package/dist/assertion.js +23 -0
- package/dist/async.cjs +89 -0
- package/dist/async.d.cts +29 -0
- package/dist/async.d.ts +29 -0
- package/dist/async.js +61 -0
- package/dist/base58.cjs +75 -0
- package/dist/base58.d.cts +4 -0
- package/dist/base58.d.ts +4 -0
- package/dist/base58.js +11 -0
- package/dist/bytes.cjs +94 -0
- package/dist/bytes.d.cts +8 -0
- package/dist/bytes.d.ts +8 -0
- package/dist/bytes.js +14 -0
- package/dist/chunk-CZNX6EUV.js +23 -0
- package/dist/chunk-DGVZ5UDU.js +52 -0
- package/dist/chunk-MORKFLN4.js +14 -0
- package/dist/chunk-NYJKCZRI.js +52 -0
- package/dist/guard.cjs +55 -0
- package/dist/guard.d.cts +20 -0
- package/dist/guard.d.ts +20 -0
- package/dist/guard.js +22 -0
- package/dist/ss58.cjs +756 -0
- package/dist/ss58.d.cts +12 -0
- package/dist/ss58.d.ts +12 -0
- package/dist/ss58.js +680 -0
- package/dist/string.cjs +42 -0
- package/dist/string.d.cts +8 -0
- package/dist/string.d.ts +8 -0
- package/dist/string.js +13 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.cts +3 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +0 -0
- package/package.json +32 -0
|
@@ -0,0 +1,86 @@
|
|
|
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/assertion.ts
|
|
21
|
+
var assertion_exports = {};
|
|
22
|
+
__export(assertion_exports, {
|
|
23
|
+
assert: () => assert,
|
|
24
|
+
assertBigInt: () => assertBigInt,
|
|
25
|
+
assertBoolean: () => assertBoolean,
|
|
26
|
+
assertNumber: () => assertNumber,
|
|
27
|
+
assertObject: () => assertObject,
|
|
28
|
+
assertString: () => assertString,
|
|
29
|
+
assertSymbol: () => assertSymbol,
|
|
30
|
+
assertUndefined: () => assertUndefined,
|
|
31
|
+
unreachable: () => unreachable
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(assertion_exports);
|
|
34
|
+
|
|
35
|
+
// src/guard.ts
|
|
36
|
+
var createIsGuard = (type) => (value) => typeof value === type;
|
|
37
|
+
var isString = createIsGuard("string");
|
|
38
|
+
var isNumber = createIsGuard("number");
|
|
39
|
+
var isBigInt = createIsGuard("bigint");
|
|
40
|
+
var isBoolean = createIsGuard("boolean");
|
|
41
|
+
var isSymbol = createIsGuard("symbol");
|
|
42
|
+
var isObject = createIsGuard("object");
|
|
43
|
+
var isUndefined = createIsGuard("undefined");
|
|
44
|
+
|
|
45
|
+
// src/assertion.ts
|
|
46
|
+
function assert(condition, message = "assertion failed", Constructor = Error) {
|
|
47
|
+
if (!condition) {
|
|
48
|
+
throw new Constructor(message);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function assertString(value, message) {
|
|
52
|
+
assert(isString(value), message ?? `expected a "string", got "${typeof value}"`);
|
|
53
|
+
}
|
|
54
|
+
function assertNumber(value, message) {
|
|
55
|
+
assert(isNumber(value), message ?? `expected a "number", got "${typeof value}"`);
|
|
56
|
+
}
|
|
57
|
+
function assertBigInt(value, message) {
|
|
58
|
+
assert(isBigInt(value), message ?? `expected a "bigint", got "${typeof value}"`);
|
|
59
|
+
}
|
|
60
|
+
function assertBoolean(value, message) {
|
|
61
|
+
assert(isBoolean(value), message ?? `expected a "boolean", got "${typeof value}"`);
|
|
62
|
+
}
|
|
63
|
+
function assertSymbol(value, message) {
|
|
64
|
+
assert(isSymbol(value), message ?? `expected a "symbol", got "${typeof value}"`);
|
|
65
|
+
}
|
|
66
|
+
function assertObject(value, message) {
|
|
67
|
+
assert(isObject(value), message ?? `expected an "object", got "${typeof value}"`);
|
|
68
|
+
}
|
|
69
|
+
function assertUndefined(value, message) {
|
|
70
|
+
assert(isUndefined(value), message ?? `expected "undefined", got "${typeof value}"`);
|
|
71
|
+
}
|
|
72
|
+
var unreachable = (x, message = "unreachable") => {
|
|
73
|
+
throw new Error(message);
|
|
74
|
+
};
|
|
75
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
76
|
+
0 && (module.exports = {
|
|
77
|
+
assert,
|
|
78
|
+
assertBigInt,
|
|
79
|
+
assertBoolean,
|
|
80
|
+
assertNumber,
|
|
81
|
+
assertObject,
|
|
82
|
+
assertString,
|
|
83
|
+
assertSymbol,
|
|
84
|
+
assertUndefined,
|
|
85
|
+
unreachable
|
|
86
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare function assert(condition: unknown, message?: string, Constructor?: ErrorConstructor): asserts condition;
|
|
2
|
+
declare function assertString(value: unknown, message?: string): asserts value is string;
|
|
3
|
+
declare function assertNumber(value: unknown, message?: string): asserts value is number;
|
|
4
|
+
declare function assertBigInt(value: unknown, message?: string): asserts value is bigint;
|
|
5
|
+
declare function assertBoolean(value: unknown, message?: string): asserts value is boolean;
|
|
6
|
+
declare function assertSymbol(value: unknown, message?: string): asserts value is symbol;
|
|
7
|
+
declare function assertObject(value: unknown, message?: string): asserts value is object;
|
|
8
|
+
declare function assertUndefined(value: unknown, message?: string): asserts value is undefined;
|
|
9
|
+
declare const unreachable: (x: never, message?: string) => never;
|
|
10
|
+
|
|
11
|
+
export { assert, assertBigInt, assertBoolean, assertNumber, assertObject, assertString, assertSymbol, assertUndefined, unreachable };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare function assert(condition: unknown, message?: string, Constructor?: ErrorConstructor): asserts condition;
|
|
2
|
+
declare function assertString(value: unknown, message?: string): asserts value is string;
|
|
3
|
+
declare function assertNumber(value: unknown, message?: string): asserts value is number;
|
|
4
|
+
declare function assertBigInt(value: unknown, message?: string): asserts value is bigint;
|
|
5
|
+
declare function assertBoolean(value: unknown, message?: string): asserts value is boolean;
|
|
6
|
+
declare function assertSymbol(value: unknown, message?: string): asserts value is symbol;
|
|
7
|
+
declare function assertObject(value: unknown, message?: string): asserts value is object;
|
|
8
|
+
declare function assertUndefined(value: unknown, message?: string): asserts value is undefined;
|
|
9
|
+
declare const unreachable: (x: never, message?: string) => never;
|
|
10
|
+
|
|
11
|
+
export { assert, assertBigInt, assertBoolean, assertNumber, assertObject, assertString, assertSymbol, assertUndefined, unreachable };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assert,
|
|
3
|
+
assertBigInt,
|
|
4
|
+
assertBoolean,
|
|
5
|
+
assertNumber,
|
|
6
|
+
assertObject,
|
|
7
|
+
assertString,
|
|
8
|
+
assertSymbol,
|
|
9
|
+
assertUndefined,
|
|
10
|
+
unreachable
|
|
11
|
+
} from "./chunk-NYJKCZRI.js";
|
|
12
|
+
import "./chunk-CZNX6EUV.js";
|
|
13
|
+
export {
|
|
14
|
+
assert,
|
|
15
|
+
assertBigInt,
|
|
16
|
+
assertBoolean,
|
|
17
|
+
assertNumber,
|
|
18
|
+
assertObject,
|
|
19
|
+
assertString,
|
|
20
|
+
assertSymbol,
|
|
21
|
+
assertUndefined,
|
|
22
|
+
unreachable
|
|
23
|
+
};
|
package/dist/async.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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/async.ts
|
|
21
|
+
var async_exports = {};
|
|
22
|
+
__export(async_exports, {
|
|
23
|
+
Queue: () => Queue,
|
|
24
|
+
RateLimiter: () => RateLimiter,
|
|
25
|
+
deferredPromise: () => deferredPromise,
|
|
26
|
+
sleep: () => sleep
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(async_exports);
|
|
29
|
+
var deferredPromise = () => {
|
|
30
|
+
let resolve;
|
|
31
|
+
let reject;
|
|
32
|
+
const promise = new Promise((res, rej) => {
|
|
33
|
+
resolve = res;
|
|
34
|
+
reject = rej;
|
|
35
|
+
});
|
|
36
|
+
return { promise, resolve, reject };
|
|
37
|
+
};
|
|
38
|
+
var sleep = (ms, { signal } = {}) => {
|
|
39
|
+
const deferred = deferredPromise();
|
|
40
|
+
const timeout = setTimeout(deferred.resolve, ms);
|
|
41
|
+
if (signal) {
|
|
42
|
+
const abort = () => {
|
|
43
|
+
clearTimeout(timeout);
|
|
44
|
+
deferred.resolve();
|
|
45
|
+
};
|
|
46
|
+
signal.addEventListener("abort", abort);
|
|
47
|
+
deferred.promise = deferred.promise.finally(() => {
|
|
48
|
+
signal.removeEventListener("abort", abort);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return deferred.promise;
|
|
52
|
+
};
|
|
53
|
+
var Queue = class {
|
|
54
|
+
constructor(debounce) {
|
|
55
|
+
this.debounce = debounce;
|
|
56
|
+
}
|
|
57
|
+
promise = Promise.resolve();
|
|
58
|
+
enqueue(fn, ...args) {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
this.promise = this.promise.then(async () => {
|
|
61
|
+
const sleepPromise = this.debounce ? sleep(this.debounce) : Promise.resolve();
|
|
62
|
+
await Promise.all([fn(...args).then(resolve, reject), sleepPromise]);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var RateLimiter = class {
|
|
68
|
+
fn;
|
|
69
|
+
debounce;
|
|
70
|
+
queues;
|
|
71
|
+
index = 0;
|
|
72
|
+
constructor(opts) {
|
|
73
|
+
this.fn = opts.fn;
|
|
74
|
+
this.debounce = opts.debounce;
|
|
75
|
+
this.queues = Array.from({ length: opts.maxConcurrency }, () => new Queue(this.debounce));
|
|
76
|
+
}
|
|
77
|
+
request(...args) {
|
|
78
|
+
const nextIndex = this.index;
|
|
79
|
+
this.index = (this.index + 1) % this.queues.length;
|
|
80
|
+
return this.queues[nextIndex].enqueue(this.fn, ...args);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
84
|
+
0 && (module.exports = {
|
|
85
|
+
Queue,
|
|
86
|
+
RateLimiter,
|
|
87
|
+
deferredPromise,
|
|
88
|
+
sleep
|
|
89
|
+
});
|
package/dist/async.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const deferredPromise: <T>() => {
|
|
2
|
+
promise: Promise<T>;
|
|
3
|
+
resolve: (value: T) => void;
|
|
4
|
+
reject: (error: Error) => void;
|
|
5
|
+
};
|
|
6
|
+
type DeferredPromise<T> = ReturnType<typeof deferredPromise<T>>;
|
|
7
|
+
declare const sleep: (ms: number, { signal }?: {
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}) => Promise<void>;
|
|
10
|
+
declare class Queue {
|
|
11
|
+
private readonly debounce?;
|
|
12
|
+
private promise;
|
|
13
|
+
constructor(debounce?: number | undefined);
|
|
14
|
+
enqueue<T, Args extends unknown[]>(fn: (...args: Args) => Promise<T>, ...args: Args): Promise<T>;
|
|
15
|
+
}
|
|
16
|
+
declare class RateLimiter<T, Args extends unknown[]> {
|
|
17
|
+
fn: (...args: Args) => Promise<T>;
|
|
18
|
+
debounce: number;
|
|
19
|
+
queues: Queue[];
|
|
20
|
+
index: number;
|
|
21
|
+
constructor(opts: {
|
|
22
|
+
fn: (...args: Args) => Promise<T>;
|
|
23
|
+
debounce: number;
|
|
24
|
+
maxConcurrency: number;
|
|
25
|
+
});
|
|
26
|
+
request(...args: Args): Promise<T>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { type DeferredPromise, Queue, RateLimiter, deferredPromise, sleep };
|
package/dist/async.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const deferredPromise: <T>() => {
|
|
2
|
+
promise: Promise<T>;
|
|
3
|
+
resolve: (value: T) => void;
|
|
4
|
+
reject: (error: Error) => void;
|
|
5
|
+
};
|
|
6
|
+
type DeferredPromise<T> = ReturnType<typeof deferredPromise<T>>;
|
|
7
|
+
declare const sleep: (ms: number, { signal }?: {
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}) => Promise<void>;
|
|
10
|
+
declare class Queue {
|
|
11
|
+
private readonly debounce?;
|
|
12
|
+
private promise;
|
|
13
|
+
constructor(debounce?: number | undefined);
|
|
14
|
+
enqueue<T, Args extends unknown[]>(fn: (...args: Args) => Promise<T>, ...args: Args): Promise<T>;
|
|
15
|
+
}
|
|
16
|
+
declare class RateLimiter<T, Args extends unknown[]> {
|
|
17
|
+
fn: (...args: Args) => Promise<T>;
|
|
18
|
+
debounce: number;
|
|
19
|
+
queues: Queue[];
|
|
20
|
+
index: number;
|
|
21
|
+
constructor(opts: {
|
|
22
|
+
fn: (...args: Args) => Promise<T>;
|
|
23
|
+
debounce: number;
|
|
24
|
+
maxConcurrency: number;
|
|
25
|
+
});
|
|
26
|
+
request(...args: Args): Promise<T>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { type DeferredPromise, Queue, RateLimiter, deferredPromise, sleep };
|
package/dist/async.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
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 };
|
|
10
|
+
};
|
|
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;
|
|
25
|
+
};
|
|
26
|
+
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
|
+
}
|
|
39
|
+
};
|
|
40
|
+
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
|
+
}
|
|
55
|
+
};
|
|
56
|
+
export {
|
|
57
|
+
Queue,
|
|
58
|
+
RateLimiter,
|
|
59
|
+
deferredPromise,
|
|
60
|
+
sleep
|
|
61
|
+
};
|
package/dist/base58.cjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
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/base58.ts
|
|
21
|
+
var base58_exports = {};
|
|
22
|
+
__export(base58_exports, {
|
|
23
|
+
decode: () => decode,
|
|
24
|
+
encode: () => encode
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(base58_exports);
|
|
27
|
+
|
|
28
|
+
// src/guard.ts
|
|
29
|
+
var createIsGuard = (type) => (value) => typeof value === type;
|
|
30
|
+
var isString = createIsGuard("string");
|
|
31
|
+
var isNumber = createIsGuard("number");
|
|
32
|
+
var isBigInt = createIsGuard("bigint");
|
|
33
|
+
var isBoolean = createIsGuard("boolean");
|
|
34
|
+
var isSymbol = createIsGuard("symbol");
|
|
35
|
+
var isObject = createIsGuard("object");
|
|
36
|
+
var isUndefined = createIsGuard("undefined");
|
|
37
|
+
|
|
38
|
+
// src/bytes.ts
|
|
39
|
+
var convertBase = (bytes, fromBase, toBase) => {
|
|
40
|
+
const result = [];
|
|
41
|
+
for (const byte of bytes) {
|
|
42
|
+
let carry = byte;
|
|
43
|
+
for (let i = 0; i < result.length; i += 1) {
|
|
44
|
+
carry += result[i] * fromBase;
|
|
45
|
+
result[i] = carry % toBase;
|
|
46
|
+
carry = Math.floor(carry / toBase);
|
|
47
|
+
}
|
|
48
|
+
while (carry !== 0) {
|
|
49
|
+
result.push(carry % toBase);
|
|
50
|
+
carry = Math.floor(carry / toBase);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
let leadingZeros = 0;
|
|
54
|
+
while (bytes[leadingZeros] === 0) {
|
|
55
|
+
leadingZeros += 1;
|
|
56
|
+
result.push(0);
|
|
57
|
+
}
|
|
58
|
+
return result.reverse();
|
|
59
|
+
};
|
|
60
|
+
var encodeBytesWithCharset = (bytes, charset2) => convertBase(bytes, 256, charset2.length).map((charCode) => charset2.charAt(charCode)).join("");
|
|
61
|
+
var decodeBytesWithCharset = (input, charset2) => {
|
|
62
|
+
const charMap = Object.fromEntries([...charset2].map((char, index) => [char, index]));
|
|
63
|
+
const bytes = input.split("").map((char) => charMap[char]);
|
|
64
|
+
return convertBase(bytes, charset2.length, 256);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/base58.ts
|
|
68
|
+
var charset = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
69
|
+
var encode = (bytes) => encodeBytesWithCharset(bytes, charset);
|
|
70
|
+
var decode = (input) => decodeBytesWithCharset(input, charset);
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
decode,
|
|
74
|
+
encode
|
|
75
|
+
});
|
package/dist/base58.d.ts
ADDED
package/dist/base58.js
ADDED
package/dist/bytes.cjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(bytes_exports);
|
|
29
|
+
|
|
30
|
+
// src/guard.ts
|
|
31
|
+
var createIsGuard = (type) => (value) => typeof value === type;
|
|
32
|
+
var isString = createIsGuard("string");
|
|
33
|
+
var isNumber = createIsGuard("number");
|
|
34
|
+
var isBigInt = createIsGuard("bigint");
|
|
35
|
+
var isBoolean = createIsGuard("boolean");
|
|
36
|
+
var isSymbol = createIsGuard("symbol");
|
|
37
|
+
var isObject = createIsGuard("object");
|
|
38
|
+
var isUndefined = createIsGuard("undefined");
|
|
39
|
+
|
|
40
|
+
// src/assertion.ts
|
|
41
|
+
function assert(condition, message = "assertion failed", Constructor = Error) {
|
|
42
|
+
if (!condition) {
|
|
43
|
+
throw new Constructor(message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/bytes.ts
|
|
48
|
+
var bytesToHex = (input) => {
|
|
49
|
+
const bytes = new Uint8Array(input);
|
|
50
|
+
return `0x${Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
|
|
51
|
+
};
|
|
52
|
+
var hexToBytes = (input) => {
|
|
53
|
+
assert(/^0x[\da-f]*$/i.test(input) && input.length % 2 === 0, "Invalid hex string");
|
|
54
|
+
const hex = input.slice(2);
|
|
55
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
56
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
57
|
+
bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16);
|
|
58
|
+
}
|
|
59
|
+
return bytes;
|
|
60
|
+
};
|
|
61
|
+
var convertBase = (bytes, fromBase, toBase) => {
|
|
62
|
+
const result = [];
|
|
63
|
+
for (const byte of bytes) {
|
|
64
|
+
let carry = byte;
|
|
65
|
+
for (let i = 0; i < result.length; i += 1) {
|
|
66
|
+
carry += result[i] * fromBase;
|
|
67
|
+
result[i] = carry % toBase;
|
|
68
|
+
carry = Math.floor(carry / toBase);
|
|
69
|
+
}
|
|
70
|
+
while (carry !== 0) {
|
|
71
|
+
result.push(carry % toBase);
|
|
72
|
+
carry = Math.floor(carry / toBase);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
let leadingZeros = 0;
|
|
76
|
+
while (bytes[leadingZeros] === 0) {
|
|
77
|
+
leadingZeros += 1;
|
|
78
|
+
result.push(0);
|
|
79
|
+
}
|
|
80
|
+
return result.reverse();
|
|
81
|
+
};
|
|
82
|
+
var encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
|
|
83
|
+
var decodeBytesWithCharset = (input, charset) => {
|
|
84
|
+
const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
|
|
85
|
+
const bytes = input.split("").map((char) => charMap[char]);
|
|
86
|
+
return convertBase(bytes, charset.length, 256);
|
|
87
|
+
};
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
bytesToHex,
|
|
91
|
+
decodeBytesWithCharset,
|
|
92
|
+
encodeBytesWithCharset,
|
|
93
|
+
hexToBytes
|
|
94
|
+
});
|
package/dist/bytes.d.cts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HexString } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
declare const bytesToHex: (input: Uint8Array | number[]) => `0x${string}`;
|
|
4
|
+
declare const hexToBytes: (input: HexString) => Uint8Array;
|
|
5
|
+
declare const encodeBytesWithCharset: (bytes: Uint8Array | number[], charset: string) => string;
|
|
6
|
+
declare const decodeBytesWithCharset: (input: string, charset: string) => number[];
|
|
7
|
+
|
|
8
|
+
export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes };
|
package/dist/bytes.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HexString } from './types.js';
|
|
2
|
+
|
|
3
|
+
declare const bytesToHex: (input: Uint8Array | number[]) => `0x${string}`;
|
|
4
|
+
declare const hexToBytes: (input: HexString) => Uint8Array;
|
|
5
|
+
declare const encodeBytesWithCharset: (bytes: Uint8Array | number[], charset: string) => string;
|
|
6
|
+
declare const decodeBytesWithCharset: (input: string, charset: string) => number[];
|
|
7
|
+
|
|
8
|
+
export { bytesToHex, decodeBytesWithCharset, encodeBytesWithCharset, hexToBytes };
|
package/dist/bytes.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bytesToHex,
|
|
3
|
+
decodeBytesWithCharset,
|
|
4
|
+
encodeBytesWithCharset,
|
|
5
|
+
hexToBytes
|
|
6
|
+
} from "./chunk-DGVZ5UDU.js";
|
|
7
|
+
import "./chunk-NYJKCZRI.js";
|
|
8
|
+
import "./chunk-CZNX6EUV.js";
|
|
9
|
+
export {
|
|
10
|
+
bytesToHex,
|
|
11
|
+
decodeBytesWithCharset,
|
|
12
|
+
encodeBytesWithCharset,
|
|
13
|
+
hexToBytes
|
|
14
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/guard.ts
|
|
2
|
+
var createIsGuard = (type) => (value) => typeof value === type;
|
|
3
|
+
var isString = createIsGuard("string");
|
|
4
|
+
var isNumber = createIsGuard("number");
|
|
5
|
+
var isBigInt = createIsGuard("bigint");
|
|
6
|
+
var isBoolean = createIsGuard("boolean");
|
|
7
|
+
var isSymbol = createIsGuard("symbol");
|
|
8
|
+
var isObject = createIsGuard("object");
|
|
9
|
+
var isUndefined = createIsGuard("undefined");
|
|
10
|
+
var isNotNullish = (value) => value != null;
|
|
11
|
+
var isNullish = (value) => value == null;
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
isString,
|
|
15
|
+
isNumber,
|
|
16
|
+
isBigInt,
|
|
17
|
+
isBoolean,
|
|
18
|
+
isSymbol,
|
|
19
|
+
isObject,
|
|
20
|
+
isUndefined,
|
|
21
|
+
isNotNullish,
|
|
22
|
+
isNullish
|
|
23
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assert
|
|
3
|
+
} from "./chunk-NYJKCZRI.js";
|
|
4
|
+
|
|
5
|
+
// src/bytes.ts
|
|
6
|
+
var bytesToHex = (input) => {
|
|
7
|
+
const bytes = new Uint8Array(input);
|
|
8
|
+
return `0x${Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
|
|
9
|
+
};
|
|
10
|
+
var hexToBytes = (input) => {
|
|
11
|
+
assert(/^0x[\da-f]*$/i.test(input) && input.length % 2 === 0, "Invalid hex string");
|
|
12
|
+
const hex = input.slice(2);
|
|
13
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
14
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
15
|
+
bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16);
|
|
16
|
+
}
|
|
17
|
+
return bytes;
|
|
18
|
+
};
|
|
19
|
+
var convertBase = (bytes, fromBase, toBase) => {
|
|
20
|
+
const result = [];
|
|
21
|
+
for (const byte of bytes) {
|
|
22
|
+
let carry = byte;
|
|
23
|
+
for (let i = 0; i < result.length; i += 1) {
|
|
24
|
+
carry += result[i] * fromBase;
|
|
25
|
+
result[i] = carry % toBase;
|
|
26
|
+
carry = Math.floor(carry / toBase);
|
|
27
|
+
}
|
|
28
|
+
while (carry !== 0) {
|
|
29
|
+
result.push(carry % toBase);
|
|
30
|
+
carry = Math.floor(carry / toBase);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
let leadingZeros = 0;
|
|
34
|
+
while (bytes[leadingZeros] === 0) {
|
|
35
|
+
leadingZeros += 1;
|
|
36
|
+
result.push(0);
|
|
37
|
+
}
|
|
38
|
+
return result.reverse();
|
|
39
|
+
};
|
|
40
|
+
var encodeBytesWithCharset = (bytes, charset) => convertBase(bytes, 256, charset.length).map((charCode) => charset.charAt(charCode)).join("");
|
|
41
|
+
var decodeBytesWithCharset = (input, charset) => {
|
|
42
|
+
const charMap = Object.fromEntries([...charset].map((char, index) => [char, index]));
|
|
43
|
+
const bytes = input.split("").map((char) => charMap[char]);
|
|
44
|
+
return convertBase(bytes, charset.length, 256);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
bytesToHex,
|
|
49
|
+
hexToBytes,
|
|
50
|
+
encodeBytesWithCharset,
|
|
51
|
+
decodeBytesWithCharset
|
|
52
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeBytesWithCharset,
|
|
3
|
+
encodeBytesWithCharset
|
|
4
|
+
} from "./chunk-DGVZ5UDU.js";
|
|
5
|
+
|
|
6
|
+
// src/base58.ts
|
|
7
|
+
var charset = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
8
|
+
var encode = (bytes) => encodeBytesWithCharset(bytes, charset);
|
|
9
|
+
var decode = (input) => decodeBytesWithCharset(input, charset);
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
encode,
|
|
13
|
+
decode
|
|
14
|
+
};
|