@layerzerolabs/common-encoding-utils 0.2.67 → 0.2.69
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/package.json +11 -3
- package/.turbo/turbo-build.log +0 -27
- package/.turbo/turbo-lint.log +0 -8
- package/.turbo/turbo-test.log +0 -16
- package/src/byte-codec.ts +0 -265
- package/src/index.ts +0 -183
- package/tsconfig.json +0 -20
- package/tsup.config.ts +0 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@layerzerolabs/common-encoding-utils",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.69",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -13,16 +13,24 @@
|
|
|
13
13
|
"main": "./dist/index.cjs",
|
|
14
14
|
"module": "./dist/index.js",
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*"
|
|
18
|
+
],
|
|
16
19
|
"devDependencies": {
|
|
17
20
|
"tsup": "^8.4.0",
|
|
18
21
|
"vitest": "^3.2.3",
|
|
19
|
-
"@layerzerolabs/tsup-configuration": "0.2.
|
|
20
|
-
"@layerzerolabs/typescript-configuration": "0.2.
|
|
22
|
+
"@layerzerolabs/tsup-configuration": "0.2.69",
|
|
23
|
+
"@layerzerolabs/typescript-configuration": "0.2.69"
|
|
21
24
|
},
|
|
22
25
|
"publishConfig": {
|
|
23
26
|
"access": "public",
|
|
24
27
|
"registry": "https://registry.npmjs.org/"
|
|
25
28
|
},
|
|
29
|
+
"externalRepoConfig": {
|
|
30
|
+
"targets": [
|
|
31
|
+
"onesig-client"
|
|
32
|
+
]
|
|
33
|
+
},
|
|
26
34
|
"scripts": {
|
|
27
35
|
"build": "tsup",
|
|
28
36
|
"lint": "eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)",
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
WARN Issue while reading "/home/runner/work/monorepo-internal/monorepo-internal/.npmrc". Failed to replace env in config: ${NPM_TOKEN}
|
|
2
|
-
|
|
3
|
-
> @layerzerolabs/common-encoding-utils@0.0.0 build /home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils
|
|
4
|
-
> tsup
|
|
5
|
-
|
|
6
|
-
[34mCLI[39m Building entry: src/byte-codec.ts, src/index.ts
|
|
7
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
|
-
[34mCLI[39m tsup v8.5.1
|
|
9
|
-
[34mCLI[39m Using tsup config: /home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils/tsup.config.ts
|
|
10
|
-
[34mCLI[39m Target: ES2023
|
|
11
|
-
[34mCLI[39m Cleaning output folder
|
|
12
|
-
[34mCJS[39m Build start
|
|
13
|
-
[34mESM[39m Build start
|
|
14
|
-
[32mCJS[39m [1mdist/byte-codec.cjs [22m[32m267.00 B[39m
|
|
15
|
-
[32mCJS[39m [1mdist/DF2CBFKF.cjs [22m[32m7.67 KB[39m
|
|
16
|
-
[32mCJS[39m [1mdist/index.cjs [22m[32m5.61 KB[39m
|
|
17
|
-
[32mCJS[39m [1mdist/byte-codec.cjs.map [22m[32m75.00 B[39m
|
|
18
|
-
[32mCJS[39m [1mdist/DF2CBFKF.cjs.map [22m[32m19.07 KB[39m
|
|
19
|
-
[32mCJS[39m [1mdist/index.cjs.map [22m[32m12.44 KB[39m
|
|
20
|
-
[32mCJS[39m ⚡️ Build success in 178ms
|
|
21
|
-
[32mESM[39m [1mdist/2BTCXNB3.js [22m[32m7.63 KB[39m
|
|
22
|
-
[32mESM[39m [1mdist/index.js [22m[32m4.85 KB[39m
|
|
23
|
-
[32mESM[39m [1mdist/byte-codec.js [22m[32m120.00 B[39m
|
|
24
|
-
[32mESM[39m [1mdist/2BTCXNB3.js.map [22m[32m19.06 KB[39m
|
|
25
|
-
[32mESM[39m [1mdist/index.js.map [22m[32m12.42 KB[39m
|
|
26
|
-
[32mESM[39m [1mdist/byte-codec.js.map [22m[32m74.00 B[39m
|
|
27
|
-
[32mESM[39m ⚡️ Build success in 178ms
|
package/.turbo/turbo-lint.log
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @layerzerolabs/common-encoding-utils@0.0.0 lint /home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils
|
|
3
|
-
> eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)
|
|
4
|
-
|
|
5
|
-
(node:65531) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/monorepo-internal/monorepo-internal/eslint.config.js?mtime=1775770562286 is not specified and it doesn't parse as CommonJS.
|
|
6
|
-
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
|
|
7
|
-
To eliminate this warning, add "type": "module" to /home/runner/work/monorepo-internal/monorepo-internal/package.json.
|
|
8
|
-
(Use `node --trace-warnings ...` to show where the warning was created)
|
package/.turbo/turbo-test.log
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @layerzerolabs/common-encoding-utils@0.0.0 test /home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils
|
|
3
|
-
> vitest --run --pass-with-no-tests --typecheck
|
|
4
|
-
|
|
5
|
-
[33mTesting types with tsc and vue-tsc is an experimental feature.
|
|
6
|
-
Breaking changes might not follow SemVer, please pin Vitest's version when using it.[39m
|
|
7
|
-
|
|
8
|
-
[1m[46m RUN [49m[22m [36mv3.2.4 [39m[90m/home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils[39m
|
|
9
|
-
|
|
10
|
-
No test files found, exiting with code 0
|
|
11
|
-
|
|
12
|
-
[2minclude: [22m[33m**/*.{test,spec}.?(c|m)[jt]s?(x)[39m
|
|
13
|
-
[2mexclude: [22m[33m**/node_modules/**[2m, [22m**/dist/**[2m, [22m**/cypress/**[2m, [22m**/.{idea,git,cache,output,temp}/**[2m, [22m**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*[39m
|
|
14
|
-
[2mtypecheck include: [22m[33m**/*.{test,spec}-d.?(c|m)[jt]s?(x)[39m
|
|
15
|
-
[2mtypecheck exclude: [22m[33m**/node_modules/**[2m, [22m**/dist/**[2m, [22m**/cypress/**[2m, [22m**/.{idea,git,cache,output,temp}/**[2m, [22m**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*[39m
|
|
16
|
-
|
package/src/byte-codec.ts
DELETED
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Growable byte codec backed by `ArrayBuffer` + `DataView`.
|
|
3
|
-
*
|
|
4
|
-
* Low-level building block for constructing byte payloads deterministically.
|
|
5
|
-
* Higher-level concerns (e.g. hashing, ABI encoding, function signatures) should
|
|
6
|
-
* live in wrappers/subclasses.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
function uMax(bits: number): bigint {
|
|
10
|
-
if (!Number.isInteger(bits) || bits < 0) {
|
|
11
|
-
throw new RangeError(`ByteCodec: invalid bit width: ${bits}`);
|
|
12
|
-
}
|
|
13
|
-
// (2^bits) - 1, as bigint
|
|
14
|
-
return bits === 0 ? 0n : (1n << BigInt(bits)) - 1n;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// TODO: add tests for this class — currently has zero test coverage
|
|
18
|
-
export class ByteCodec {
|
|
19
|
-
// Start with a small-ish initial capacity to avoid frequent reallocations for
|
|
20
|
-
// common short payloads (e.g. function selector + a few fixed-width fields),
|
|
21
|
-
// while still staying tiny in memory terms. Buffer grows by doubling as needed.
|
|
22
|
-
#buf = new ArrayBuffer(128);
|
|
23
|
-
#view = new DataView(this.#buf);
|
|
24
|
-
#cursor = 0;
|
|
25
|
-
|
|
26
|
-
static readonly #ZERO = 0n;
|
|
27
|
-
static readonly #U8_MAX = uMax(8);
|
|
28
|
-
static readonly #U16_MAX = uMax(16);
|
|
29
|
-
static readonly #U32_MAX = uMax(32);
|
|
30
|
-
static readonly #U64_MAX = uMax(64);
|
|
31
|
-
static readonly #U128_MAX = uMax(128);
|
|
32
|
-
static readonly #U256_MAX = uMax(256);
|
|
33
|
-
|
|
34
|
-
static #outOfRange(targetLength: number, value: bigint): RangeError {
|
|
35
|
-
return new RangeError(`ByteCodec: value out of range for u${targetLength * 8}: ${value}`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Read helpers (big-endian). These are intentionally small/low-level so other
|
|
40
|
-
* packages can share a single implementation for parsing encoded payloads.
|
|
41
|
-
*/
|
|
42
|
-
static readU16be(buf: Uint8Array, offset: number): number {
|
|
43
|
-
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
44
|
-
return view.getUint16(offset, false);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
static readU8(buf: Uint8Array, offset: number): number {
|
|
48
|
-
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
49
|
-
return view.getUint8(offset);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
static readU32be(buf: Uint8Array, offset: number): number {
|
|
53
|
-
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
54
|
-
return view.getUint32(offset, false);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
static readU128be(buf: Uint8Array, offset: number): bigint {
|
|
58
|
-
return ByteCodec.readUNbe(buf, offset, 16);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
static readUNbe(buf: Uint8Array, offset: number, length: number): bigint {
|
|
62
|
-
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
63
|
-
|
|
64
|
-
let out = 0n;
|
|
65
|
-
const end = offset + length;
|
|
66
|
-
|
|
67
|
-
// Fast path: read 8 bytes at a time
|
|
68
|
-
let cursor = offset;
|
|
69
|
-
while (cursor + 8 <= end) {
|
|
70
|
-
out = (out << 64n) | view.getBigUint64(cursor, false);
|
|
71
|
-
cursor += 8;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Tail: 0..7 bytes
|
|
75
|
-
while (cursor < end) {
|
|
76
|
-
out = (out << 8n) | BigInt(view.getUint8(cursor));
|
|
77
|
-
cursor += 1;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return out;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
static readBytes32(buf: Uint8Array, offset: number): Uint8Array {
|
|
84
|
-
if (offset + 32 > buf.length) throw new RangeError(`ByteCodec: out of bounds at ${offset}`);
|
|
85
|
-
return buf.slice(offset, offset + 32);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Left-pad `bytes` to `targetLength` using `padByte` (default 0x00).
|
|
90
|
-
*
|
|
91
|
-
* Commonly used to mimic Solidity's `bytes32(bytesN)`-style left zero padding.
|
|
92
|
-
*/
|
|
93
|
-
static leftPad(bytes: Uint8Array, targetLength: number, padByte = 0): Uint8Array {
|
|
94
|
-
if (!Number.isInteger(targetLength) || targetLength < 0) {
|
|
95
|
-
throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
|
|
96
|
-
}
|
|
97
|
-
if (!Number.isInteger(padByte) || padByte < 0 || padByte > 255) {
|
|
98
|
-
throw new RangeError(`ByteCodec: invalid pad byte: ${padByte}`);
|
|
99
|
-
}
|
|
100
|
-
if (bytes.length > targetLength) {
|
|
101
|
-
throw new RangeError(
|
|
102
|
-
`ByteCodec: bytes length ${bytes.length} exceeds target ${targetLength}`,
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const out = new Uint8Array(targetLength);
|
|
107
|
-
if (padByte !== 0) out.fill(padByte);
|
|
108
|
-
out.set(bytes, targetLength - bytes.length);
|
|
109
|
-
return out;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Cast an unsigned big-endian integer in `bytes` into a fixed-width uint (by bytes),
|
|
114
|
-
* reverting on overflow.
|
|
115
|
-
*
|
|
116
|
-
* This mirrors Solidity-style safe casts like `SafeCast.toUint128(uint256)`:
|
|
117
|
-
* - if `bytes.length > targetLength`, the high (bytes.length - targetLength) bytes must be all zero
|
|
118
|
-
* - returns the low `targetLength` bytes as the result
|
|
119
|
-
*
|
|
120
|
-
* Examples:
|
|
121
|
-
* - cast bytes32 -> u128: castUNbe(bytes32, 16)
|
|
122
|
-
* - cast bytesN -> u64: castUNbe(bytesN, 8)
|
|
123
|
-
*/
|
|
124
|
-
static castUNbe(bytes: Uint8Array, targetLength: number): bigint {
|
|
125
|
-
if (!Number.isInteger(targetLength) || targetLength < 0) {
|
|
126
|
-
throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const len = bytes.length;
|
|
130
|
-
if (len <= targetLength) return ByteCodec.readUNbe(bytes, 0, len);
|
|
131
|
-
|
|
132
|
-
// Overflow check: any excess high bytes must be 0x00.
|
|
133
|
-
const excess = len - targetLength;
|
|
134
|
-
for (let i = 0; i < excess; i++) {
|
|
135
|
-
if (bytes[i] !== 0)
|
|
136
|
-
throw ByteCodec.#outOfRange(targetLength, ByteCodec.readUNbe(bytes, 0, len));
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return ByteCodec.readUNbe(bytes, excess, targetLength);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
static castU8be(bytes: Uint8Array): bigint {
|
|
143
|
-
return ByteCodec.castUNbe(bytes, 1);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
static castU16be(bytes: Uint8Array): bigint {
|
|
147
|
-
return ByteCodec.castUNbe(bytes, 2);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
static castU32be(bytes: Uint8Array): bigint {
|
|
151
|
-
return ByteCodec.castUNbe(bytes, 4);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
static castU64be(bytes: Uint8Array): bigint {
|
|
155
|
-
return ByteCodec.castUNbe(bytes, 8);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
static castU128be(bytes: Uint8Array): bigint {
|
|
159
|
-
return ByteCodec.castUNbe(bytes, 16);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
protected ensureCapacity(additionalBytes: number): void {
|
|
163
|
-
const needed = this.#cursor + additionalBytes;
|
|
164
|
-
if (needed <= this.#buf.byteLength) return;
|
|
165
|
-
|
|
166
|
-
let nextCap = this.#buf.byteLength;
|
|
167
|
-
while (nextCap < needed) nextCap *= 2;
|
|
168
|
-
|
|
169
|
-
// Grow by allocating a new ArrayBuffer and copying the written prefix
|
|
170
|
-
// [0..cursor) into it. We then swap the backing buffer+view.
|
|
171
|
-
const next = new ArrayBuffer(nextCap);
|
|
172
|
-
new Uint8Array(next).set(new Uint8Array(this.#buf, 0, this.#cursor));
|
|
173
|
-
this.#buf = next;
|
|
174
|
-
this.#view = new DataView(this.#buf);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
bytes(b: Uint8Array): this {
|
|
178
|
-
this.ensureCapacity(b.length);
|
|
179
|
-
new Uint8Array(this.#buf, this.#cursor, b.length).set(b);
|
|
180
|
-
this.#cursor += b.length;
|
|
181
|
-
return this;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
bytes32(b: Uint8Array): this {
|
|
185
|
-
return this.bytes(ByteCodec.leftPad(b, 32));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
bool(b: boolean): this {
|
|
189
|
-
return this.u8(b ? 1n : ByteCodec.#ZERO);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
u8(v: bigint): this {
|
|
193
|
-
if (v < ByteCodec.#ZERO || v > ByteCodec.#U8_MAX)
|
|
194
|
-
throw new RangeError(`ByteCodec: value out of range for u8: ${v}`);
|
|
195
|
-
this.ensureCapacity(1);
|
|
196
|
-
this.#view.setUint8(this.#cursor, Number(v));
|
|
197
|
-
this.#cursor += 1;
|
|
198
|
-
return this;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
u16be(v: bigint): this {
|
|
202
|
-
if (v < ByteCodec.#ZERO || v > ByteCodec.#U16_MAX)
|
|
203
|
-
throw new RangeError(`ByteCodec: value out of range for u16: ${v}`);
|
|
204
|
-
this.ensureCapacity(2);
|
|
205
|
-
this.#view.setUint16(this.#cursor, Number(v), false);
|
|
206
|
-
this.#cursor += 2;
|
|
207
|
-
return this;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
u32be(v: bigint): this {
|
|
211
|
-
if (v < ByteCodec.#ZERO || v > ByteCodec.#U32_MAX) {
|
|
212
|
-
throw new RangeError(`ByteCodec: value out of range for u32: ${v}`);
|
|
213
|
-
}
|
|
214
|
-
this.ensureCapacity(4);
|
|
215
|
-
this.#view.setUint32(this.#cursor, Number(v), false);
|
|
216
|
-
this.#cursor += 4;
|
|
217
|
-
return this;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
u64be(v: bigint): this {
|
|
221
|
-
if (v < ByteCodec.#ZERO || v > ByteCodec.#U64_MAX) {
|
|
222
|
-
throw new RangeError(`ByteCodec: value out of range for u64: ${v}`);
|
|
223
|
-
}
|
|
224
|
-
this.ensureCapacity(8);
|
|
225
|
-
this.#view.setBigUint64(this.#cursor, v, false);
|
|
226
|
-
this.#cursor += 8;
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
u128be(v: bigint): this {
|
|
231
|
-
if (v < ByteCodec.#ZERO || v > ByteCodec.#U128_MAX) {
|
|
232
|
-
throw new RangeError(`ByteCodec: value out of range for u128: ${v}`);
|
|
233
|
-
}
|
|
234
|
-
this.ensureCapacity(16);
|
|
235
|
-
const hi = (v >> 64n) & ByteCodec.#U64_MAX;
|
|
236
|
-
const lo = v & ByteCodec.#U64_MAX;
|
|
237
|
-
this.#view.setBigUint64(this.#cursor, hi, false);
|
|
238
|
-
this.#view.setBigUint64(this.#cursor + 8, lo, false);
|
|
239
|
-
this.#cursor += 16;
|
|
240
|
-
return this;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
u256be(v: bigint): this {
|
|
244
|
-
if (v < ByteCodec.#ZERO || v > ByteCodec.#U256_MAX) {
|
|
245
|
-
throw new RangeError(`ByteCodec: value out of range for u256: ${v}`);
|
|
246
|
-
}
|
|
247
|
-
this.ensureCapacity(32);
|
|
248
|
-
const w0 = (v >> 192n) & ByteCodec.#U64_MAX;
|
|
249
|
-
const w1 = (v >> 128n) & ByteCodec.#U64_MAX;
|
|
250
|
-
const w2 = (v >> 64n) & ByteCodec.#U64_MAX;
|
|
251
|
-
const w3 = v & ByteCodec.#U64_MAX;
|
|
252
|
-
this.#view.setBigUint64(this.#cursor, w0, false);
|
|
253
|
-
this.#view.setBigUint64(this.#cursor + 8, w1, false);
|
|
254
|
-
this.#view.setBigUint64(this.#cursor + 16, w2, false);
|
|
255
|
-
this.#view.setBigUint64(this.#cursor + 24, w3, false);
|
|
256
|
-
this.#cursor += 32;
|
|
257
|
-
return this;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Returns a copy of the accumulated bytes (no shared backing buffer).
|
|
261
|
-
*/
|
|
262
|
-
toBytes(): Uint8Array {
|
|
263
|
-
return new Uint8Array(this.#buf, 0, this.#cursor).slice();
|
|
264
|
-
}
|
|
265
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
export * from './byte-codec';
|
|
2
|
-
|
|
3
|
-
export type HexString = `0x${string}`;
|
|
4
|
-
|
|
5
|
-
const base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;
|
|
6
|
-
const base64regex = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
7
|
-
const hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;
|
|
8
|
-
|
|
9
|
-
export function isBase58(address: string): boolean {
|
|
10
|
-
return base58regex.test(address);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function isBase64(address: string): boolean {
|
|
14
|
-
return base64regex.test(address);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function isHexString(value: any, length?: number): value is HexString {
|
|
18
|
-
if (typeof value !== 'string' || !value.match(hexStringRegex)) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
if (length && value.length !== 2 + 2 * length) {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function hexZeroPad(value: string, length: number): HexString {
|
|
28
|
-
if (!isHexString(value)) {
|
|
29
|
-
throw new Error(`invalid hex string: ${value}`);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (value.length > 2 * length + 2) {
|
|
33
|
-
throw new Error('value out of range');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return `0x${trim0x(value).padStart(2 * length, '0')}`;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function trim0x(str: string): string {
|
|
40
|
-
return str.replace(/^0x/, '');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function ensure0xPrefixed(str: string): HexString {
|
|
44
|
-
return `0x${trim0x(str)}`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function trimLeadingZeros(hexString: string): HexString {
|
|
48
|
-
// Remove the '0x' prefix
|
|
49
|
-
let withoutPrefix = trim0x(hexString);
|
|
50
|
-
|
|
51
|
-
// Trim leading zeros
|
|
52
|
-
withoutPrefix = withoutPrefix.replace(/^0+/, '');
|
|
53
|
-
|
|
54
|
-
// Add back the '0x' prefix
|
|
55
|
-
return ensure0xPrefixed(withoutPrefix);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function hexToUint8Array(hexString: string): Uint8Array {
|
|
59
|
-
return Uint8Array.from(Buffer.from(trim0x(hexString), 'hex'));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function hexToAscii(hex: string): string {
|
|
63
|
-
if (!isHexString(hex)) {
|
|
64
|
-
throw new Error(`Invalid hex string ${hex}`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
let str = '';
|
|
68
|
-
let i = 0;
|
|
69
|
-
const l = hex.length;
|
|
70
|
-
if (hex.startsWith('0x')) {
|
|
71
|
-
i = 2;
|
|
72
|
-
}
|
|
73
|
-
for (; i < l; i += 2) {
|
|
74
|
-
const code = parseInt(hex.slice(i, i + 2), 16);
|
|
75
|
-
str += String.fromCharCode(code);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return str;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function stringToUint8Array(str: string): Uint8Array {
|
|
82
|
-
const value = str.replace(/^0x/i, '');
|
|
83
|
-
const len = value.length + 1 - ((value.length + 1) % 2);
|
|
84
|
-
return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* A function to convert hex string to Uint8Array (not prefixed)
|
|
89
|
-
* @param hex hex string
|
|
90
|
-
* @returns Uint8Array
|
|
91
|
-
*/
|
|
92
|
-
export function hexToBytes(hex: string): Uint8Array {
|
|
93
|
-
const value = hex.replace(/^0x/i, '');
|
|
94
|
-
const len = value.length + 1 - ((value.length + 1) % 2);
|
|
95
|
-
return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function bytesToHex(bytes: Uint8Array | ArrayBuffer): string {
|
|
99
|
-
return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(
|
|
100
|
-
'hex',
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* 0x prefixed hex string from Uint8Array
|
|
106
|
-
* @param bytes
|
|
107
|
-
*/
|
|
108
|
-
export function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString {
|
|
109
|
-
return ensure0xPrefixed(bytesToHex(bytes));
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string {
|
|
113
|
-
return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(
|
|
114
|
-
'base64',
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function base64ToBytes(base64: string): Uint8Array {
|
|
119
|
-
return Uint8Array.from(Buffer.from(base64, 'base64'));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function hexToBase64(hexString: string): string {
|
|
123
|
-
const hex = trim0x(hexString);
|
|
124
|
-
// Validate that the input is a valid hex string
|
|
125
|
-
if (!/^[0-9a-fA-F]+$/.test(hex)) {
|
|
126
|
-
throw new Error(`Invalid hex string: ${hexString}`);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Ensure the hex string has an even length
|
|
130
|
-
if (hex.length % 2 !== 0) {
|
|
131
|
-
throw new Error('Hex string must have an even length');
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Convert hex string to a byte array
|
|
135
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
136
|
-
for (let i = 0; i < hex.length; i += 2) {
|
|
137
|
-
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Convert the byte array to a base64 string
|
|
141
|
-
const base64String = Buffer.from(bytes).toString('base64');
|
|
142
|
-
|
|
143
|
-
return base64String;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function base64ToHex(base64String: string): HexString {
|
|
147
|
-
if (!/^[A-Za-z0-9+/=]*$/.test(base64String)) {
|
|
148
|
-
throw new Error(`Invalid base64 string: ${base64String}`);
|
|
149
|
-
}
|
|
150
|
-
return `0x${Buffer.from(base64String, 'base64').toString('hex')}`;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function padString(str: string, length: number, left: boolean, padding = '0') {
|
|
154
|
-
const diff = length - str.length;
|
|
155
|
-
let result = str;
|
|
156
|
-
if (diff > 0) {
|
|
157
|
-
const pad = padding.repeat(diff);
|
|
158
|
-
result = left ? pad + str : str + pad;
|
|
159
|
-
}
|
|
160
|
-
return result;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function padLeft(str: string, length: number, padding = '0') {
|
|
164
|
-
return padString(str, length, true, padding);
|
|
165
|
-
}
|
|
166
|
-
function calcByteLength(str: string, byteSize = 8) {
|
|
167
|
-
const { length } = str;
|
|
168
|
-
const remainder = length % byteSize;
|
|
169
|
-
return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Pads a hex string on the left so its length is a multiple of `byteSize`.
|
|
174
|
-
* Used to align hex strings.
|
|
175
|
-
* @param str Hex string.
|
|
176
|
-
* @param byteSize Group size to align to, in hex chars (default: 8).
|
|
177
|
-
* @param padding Left-pad character (default: '0').
|
|
178
|
-
* @returns Hex string left-padded to a multiple of `byteSize`.
|
|
179
|
-
*/
|
|
180
|
-
export function padAlignHex(str: string, byteSize = 8, padding = '0') {
|
|
181
|
-
const trimmed = trim0x(str);
|
|
182
|
-
return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);
|
|
183
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "@layerzerolabs/typescript-configuration/tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "./src",
|
|
5
|
-
"outDir": "./dist",
|
|
6
|
-
"strictPropertyInitialization": false,
|
|
7
|
-
"noUnusedLocals": false,
|
|
8
|
-
"noUnusedParameters": false,
|
|
9
|
-
"jsx": "react-jsx"
|
|
10
|
-
},
|
|
11
|
-
"exclude": [
|
|
12
|
-
"node_modules",
|
|
13
|
-
"**/__mocks__/*",
|
|
14
|
-
"**/__tests__/*",
|
|
15
|
-
"**/*.spec.ts",
|
|
16
|
-
"**/*.test.ts",
|
|
17
|
-
"dist"
|
|
18
|
-
],
|
|
19
|
-
"include": ["src/**/*"]
|
|
20
|
-
}
|