@layerzerolabs/common-encoding-utils 0.2.113 → 0.2.115
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/index.d.ts +6 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -49
- package/dist/index.js.map +1 -1
- package/package.json +6 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
+
import type { HexString } from '@layerzerolabs/typescript-utils';
|
|
1
2
|
export * from './byte-codec';
|
|
2
3
|
export * from './uuid';
|
|
3
|
-
export type HexString = `0x${string}`;
|
|
4
4
|
export declare function isBase58(address: string): boolean;
|
|
5
5
|
export declare function isBase64(address: string): boolean;
|
|
6
|
-
export declare function isHexString(value: any, length?: number):
|
|
6
|
+
export declare function isHexString(value: any, length?: number): boolean;
|
|
7
7
|
export declare function hexZeroPad(value: string, length: number): HexString;
|
|
8
8
|
export declare function trim0x(str: string): string;
|
|
9
9
|
export declare function ensure0xPrefixed(str: string): HexString;
|
|
10
10
|
export declare function trimLeadingZeros(hexString: string): HexString;
|
|
11
|
-
export declare function hexToUint8Array(hexString: string): Uint8Array;
|
|
12
11
|
export declare function hexToAscii(hex: string): string;
|
|
13
|
-
export declare function stringToUint8Array(str: string): Uint8Array;
|
|
14
12
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
13
|
+
* Bytes from a `0x`-prefixed (or bare) hex string. The single canonical hex decoder.
|
|
14
|
+
* Throws on non-hex and on odd-length input: a dangling half-byte is always a caller bug,
|
|
15
|
+
* never silently padded or truncated.
|
|
18
16
|
*/
|
|
19
17
|
export declare function hexToBytes(hex: string): Uint8Array;
|
|
20
18
|
export declare function bytesToHex(bytes: Uint8Array | ArrayBuffer): string;
|
|
@@ -25,23 +23,19 @@ export declare function bytesToHex(bytes: Uint8Array | ArrayBuffer): string;
|
|
|
25
23
|
export declare function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString;
|
|
26
24
|
/**
|
|
27
25
|
* Big-endian `bigint` from a hex string. Empty hex (`''` / `'0x'`) is `0n`.
|
|
28
|
-
* Replaces the inline `BigInt('0x' + ...)` / `BigInt(\`0x${hex}\`)` idiom.
|
|
29
26
|
*/
|
|
30
27
|
export declare function hexToBigInt(hex: string): bigint;
|
|
31
28
|
/**
|
|
32
29
|
* Big-endian `bigint` from bytes. Empty input is `0n`.
|
|
33
|
-
* Replaces the inline `BigInt('0x' + buf.toString('hex'))` idiom.
|
|
34
30
|
*/
|
|
35
31
|
export declare function bytesToBigInt(bytes: Uint8Array | ArrayBuffer): bigint;
|
|
36
32
|
/**
|
|
37
33
|
* Big-endian bytes from a non-negative `bigint`, left-padded to `byteLength`. The inverse of
|
|
38
|
-
* `bytesToBigInt`. Throws on a negative value or one too wide for `byteLength
|
|
39
|
-
* `hexZeroPad`). Replaces the inline `new BN(v).toBuffer('be', n)` / `writeBigUInt*` idiom.
|
|
34
|
+
* `bytesToBigInt`. Throws on a negative value or one too wide for `byteLength`.
|
|
40
35
|
*/
|
|
41
36
|
export declare function bigIntToBytes(value: bigint, byteLength: number): Uint8Array;
|
|
42
37
|
/**
|
|
43
38
|
* `0x`-hex from a non-negative `bigint`, optionally left-padded to `byteLength` bytes.
|
|
44
|
-
* Replaces the inline `'0x' + v.toString(16)` (± `padStart`) idiom.
|
|
45
39
|
* Without `byteLength` the output is minimal and may be odd-length (e.g. `0xabc`), which
|
|
46
40
|
* `hexToBytes` rejects — use `bigIntToBytes(value, byteLength)` when you need bytes.
|
|
47
41
|
*/
|
|
@@ -52,8 +46,6 @@ export declare function bigIntToHex(value: bigint, byteLength?: number): HexStri
|
|
|
52
46
|
export declare function numberToHex(value: number, byteLength?: number): HexString;
|
|
53
47
|
/**
|
|
54
48
|
* `number` from a hex string. Throws if the value exceeds `Number.MAX_SAFE_INTEGER`.
|
|
55
|
-
* Replaces the inline `parseInt(hex, 16)` / `Number('0x' + hex)` idiom (which silently
|
|
56
|
-
* lose precision past 2^53).
|
|
57
49
|
*/
|
|
58
50
|
export declare function hexToNumber(hex: string): number;
|
|
59
51
|
export declare function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAEjE,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AAQvB,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAEjD;AAED,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAEjD;AAKD,wBAAgB,WAAW,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAUhE;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAoBnE;AAED,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE1C;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAEvD;AAED,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAS7D;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAYlD;AAOD,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,SAAS,CAE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(value, length) {
|
|
|
18
18
|
if (typeof value !== "string" || !value.match(hexStringRegex)) {
|
|
19
19
|
return false;
|
|
20
20
|
}
|
|
21
|
-
if (length && value.length !== 2
|
|
21
|
+
if (length && trim0x(value).length !== 2 * length) {
|
|
22
22
|
return false;
|
|
23
23
|
}
|
|
24
24
|
return true;
|
|
25
25
|
}
|
|
26
26
|
__name(isHexString, "isHexString");
|
|
27
27
|
function hexZeroPad(value, length) {
|
|
28
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
29
|
+
throw new Error("byteLength must be a non-negative safe integer");
|
|
30
|
+
}
|
|
28
31
|
if (!isHexString(value)) {
|
|
29
|
-
throw new Error(`invalid hex string
|
|
32
|
+
throw new Error(`invalid hex string (length ${value.length})`);
|
|
30
33
|
}
|
|
34
|
+
value = ensure0xPrefixed(value);
|
|
31
35
|
if (value.length > 2 * length + 2) {
|
|
32
36
|
throw new Error("value out of range");
|
|
33
37
|
}
|
|
@@ -48,37 +52,19 @@ function trimLeadingZeros(hexString) {
|
|
|
48
52
|
return ensure0xPrefixed(withoutPrefix);
|
|
49
53
|
}
|
|
50
54
|
__name(trimLeadingZeros, "trimLeadingZeros");
|
|
51
|
-
function hexToUint8Array(hexString) {
|
|
52
|
-
return Uint8Array.from(Buffer.from(trim0x(hexString), "hex"));
|
|
53
|
-
}
|
|
54
|
-
__name(hexToUint8Array, "hexToUint8Array");
|
|
55
55
|
function hexToAscii(hex) {
|
|
56
|
-
|
|
57
|
-
throw new Error(`Invalid hex string ${hex}`);
|
|
58
|
-
}
|
|
59
|
-
let str = "";
|
|
60
|
-
let i = 0;
|
|
61
|
-
const l = hex.length;
|
|
62
|
-
if (hex.startsWith("0x")) {
|
|
63
|
-
i = 2;
|
|
64
|
-
}
|
|
65
|
-
for (; i < l; i += 2) {
|
|
66
|
-
const code = parseInt(hex.slice(i, i + 2), 16);
|
|
67
|
-
str += String.fromCharCode(code);
|
|
68
|
-
}
|
|
69
|
-
return str;
|
|
56
|
+
return Array.from(hexToBytes(hex), (b) => String.fromCharCode(b)).join("");
|
|
70
57
|
}
|
|
71
58
|
__name(hexToAscii, "hexToAscii");
|
|
72
|
-
function stringToUint8Array(str) {
|
|
73
|
-
const value = str.replace(/^0x/i, "");
|
|
74
|
-
const len = value.length + 1 - (value.length + 1) % 2;
|
|
75
|
-
return Uint8Array.from(Buffer.from(value.padStart(len, "0"), "hex"));
|
|
76
|
-
}
|
|
77
|
-
__name(stringToUint8Array, "stringToUint8Array");
|
|
78
59
|
function hexToBytes(hex) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
60
|
+
if (!isHexString(hex)) {
|
|
61
|
+
throw new Error(`invalid hex string (length ${hex.length})`);
|
|
62
|
+
}
|
|
63
|
+
const body = trim0x(hex);
|
|
64
|
+
if (body.length % 2 !== 0) {
|
|
65
|
+
throw new Error(`hex string has an odd length (${body.length})`);
|
|
66
|
+
}
|
|
67
|
+
return Uint8Array.from(Buffer.from(body, "hex"));
|
|
82
68
|
}
|
|
83
69
|
__name(hexToBytes, "hexToBytes");
|
|
84
70
|
function asUint8Array(bytes) {
|
|
@@ -86,7 +72,7 @@ function asUint8Array(bytes) {
|
|
|
86
72
|
}
|
|
87
73
|
__name(asUint8Array, "asUint8Array");
|
|
88
74
|
function bytesToHex(bytes) {
|
|
89
|
-
return Buffer.from(
|
|
75
|
+
return Buffer.from(asUint8Array(bytes)).toString("hex");
|
|
90
76
|
}
|
|
91
77
|
__name(bytesToHex, "bytesToHex");
|
|
92
78
|
function bytesToHexPrefixed(bytes) {
|
|
@@ -134,34 +120,22 @@ function hexToNumber(hex) {
|
|
|
134
120
|
}
|
|
135
121
|
__name(hexToNumber, "hexToNumber");
|
|
136
122
|
function bytesToBase64(bytes) {
|
|
137
|
-
return Buffer.from(
|
|
123
|
+
return Buffer.from(asUint8Array(bytes)).toString("base64");
|
|
138
124
|
}
|
|
139
125
|
__name(bytesToBase64, "bytesToBase64");
|
|
140
126
|
function base64ToBytes(base64) {
|
|
127
|
+
if (!isBase64(base64) || base64.length % 4 !== 0) {
|
|
128
|
+
throw new Error(`invalid base64 string (length ${base64.length})`);
|
|
129
|
+
}
|
|
141
130
|
return Uint8Array.from(Buffer.from(base64, "base64"));
|
|
142
131
|
}
|
|
143
132
|
__name(base64ToBytes, "base64ToBytes");
|
|
144
133
|
function hexToBase64(hexString) {
|
|
145
|
-
|
|
146
|
-
if (!/^[0-9a-fA-F]+$/.test(hex)) {
|
|
147
|
-
throw new Error(`Invalid hex string: ${hexString}`);
|
|
148
|
-
}
|
|
149
|
-
if (hex.length % 2 !== 0) {
|
|
150
|
-
throw new Error("Hex string must have an even length");
|
|
151
|
-
}
|
|
152
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
153
|
-
for (let i = 0; i < hex.length; i += 2) {
|
|
154
|
-
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
155
|
-
}
|
|
156
|
-
const base64String = Buffer.from(bytes).toString("base64");
|
|
157
|
-
return base64String;
|
|
134
|
+
return bytesToBase64(hexToBytes(hexString));
|
|
158
135
|
}
|
|
159
136
|
__name(hexToBase64, "hexToBase64");
|
|
160
137
|
function base64ToHex(base64String) {
|
|
161
|
-
|
|
162
|
-
throw new Error(`Invalid base64 string: ${base64String}`);
|
|
163
|
-
}
|
|
164
|
-
return `0x${Buffer.from(base64String, "base64").toString("hex")}`;
|
|
138
|
+
return bytesToHexPrefixed(base64ToBytes(base64String));
|
|
165
139
|
}
|
|
166
140
|
__name(base64ToHex, "base64ToHex");
|
|
167
141
|
function base58ToBytes(value) {
|
|
@@ -209,6 +183,6 @@ function padAlignHex(str, byteSize = 8, padding = "0") {
|
|
|
209
183
|
}
|
|
210
184
|
__name(padAlignHex, "padAlignHex");
|
|
211
185
|
|
|
212
|
-
export { base58ToBytes, base58ToHex, base64ToBytes, base64ToHex, bigIntToBytes, bigIntToHex, bytesToBase58, bytesToBase64, bytesToBigInt, bytesToHex, bytesToHexPrefixed, ensure0xPrefixed, hexToAscii, hexToBase58, hexToBase64, hexToBigInt, hexToBytes, hexToNumber,
|
|
186
|
+
export { base58ToBytes, base58ToHex, base64ToBytes, base64ToHex, bigIntToBytes, bigIntToHex, bytesToBase58, bytesToBase64, bytesToBigInt, bytesToHex, bytesToHexPrefixed, ensure0xPrefixed, hexToAscii, hexToBase58, hexToBase64, hexToBigInt, hexToBytes, hexToNumber, hexZeroPad, isBase58, isBase64, isHexString, numberToHex, padAlignHex, trim0x, trimLeadingZeros };
|
|
213
187
|
//# sourceMappingURL=index.js.map
|
|
214
188
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["base58regex","base64regex","hexStringRegex","isBase58","address","test","isBase64","isHexString","value","length","match","hexZeroPad","Error","trim0x","padStart","str","replace","ensure0xPrefixed","trimLeadingZeros","hexString","withoutPrefix","hexToUint8Array","Uint8Array","from","Buffer","hexToAscii","hex","i","l","startsWith","code","parseInt","slice","String","fromCharCode","stringToUint8Array","len","hexToBytes","asUint8Array","bytes","ArrayBuffer","bytesToHex","toString","bytesToHexPrefixed","hexToBigInt","body","BigInt","bytesToBigInt","bigIntToBytes","byteLength","bigIntToHex","undefined","numberToHex","Number","isSafeInteger","hexToNumber","MAX_SAFE_INTEGER","bytesToBase64","base64ToBytes","base64","hexToBase64","substr","base64String","base64ToHex","base58ToBytes","bs58","decode","bytesToBase58","encode","base58ToHex","hexToBase58","padString","left","padding","diff","result","pad","repeat","padLeft","calcByteLength","byteSize","remainder","padAlignHex","trimmed"],"mappings":";;;;;AAOA,IAAMA,WAAAA,GAAc,yBAAA;AACpB,IAAMC,WAAAA,GAAc,wBAAA;AACpB,IAAMC,cAAAA,GAAiB,qBAAA;AAEhB,SAASC,SAASC,OAAAA,EAAe;AACpC,EAAA,OAAOJ,WAAAA,CAAYK,KAAKD,OAAAA,CAAAA;AAC5B;AAFgBD,MAAAA,CAAAA,QAAAA,EAAAA,UAAAA,CA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,IAAaD,QAAAA,GAAYA,WAAWA,QAAAA,GAAWvE,MAAAA;AACjF;AAJSsE,MAAAA,CAAAA,cAAAA,EAAAA,gBAAAA,CAAAA;AAcF,SAASG,WAAAA,CAAYnE,GAAAA,EAAaiE,QAAAA,GAAW,CAAA,EAAGP,UAAU,GAAA,EAAG;AAChE,EAAA,MAAMU,OAAAA,GAAUtE,OAAOE,GAAAA,CAAAA;AACvB,EAAA,OAAO+D,QAAQK,OAAAA,EAASJ,cAAAA,CAAeI,OAAAA,EAASH,QAAAA,GAAWP,OAAAA,CAAAA;AAC/D;AAHgBS,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA","file":"index.js","sourcesContent":["import bs58 from 'bs58';\n\nexport * from './byte-codec';\nexport * from './uuid';\n\nexport type HexString = `0x${string}`;\n\nconst base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;\nconst base64regex = /^[A-Za-z0-9+/]*={0,2}$/;\nconst hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;\n\nexport function isBase58(address: string): boolean {\n return base58regex.test(address);\n}\n\nexport function isBase64(address: string): boolean {\n return base64regex.test(address);\n}\n\nexport function isHexString(value: any, length?: number): value is HexString {\n if (typeof value !== 'string' || !value.match(hexStringRegex)) {\n return false;\n }\n if (length && value.length !== 2 + 2 * length) {\n return false;\n }\n return true;\n}\n\nexport function hexZeroPad(value: string, length: number): HexString {\n if (!isHexString(value)) {\n throw new Error(`invalid hex string: ${value}`);\n }\n\n if (value.length > 2 * length + 2) {\n throw new Error('value out of range');\n }\n\n return `0x${trim0x(value).padStart(2 * length, '0')}`;\n}\n\nexport function trim0x(str: string): string {\n return str.replace(/^0x/, '');\n}\n\nexport function ensure0xPrefixed(str: string): HexString {\n return `0x${trim0x(str)}`;\n}\n\nexport function trimLeadingZeros(hexString: string): HexString {\n // Remove the '0x' prefix\n let withoutPrefix = trim0x(hexString);\n\n // Trim leading zeros\n withoutPrefix = withoutPrefix.replace(/^0+/, '');\n\n // Add back the '0x' prefix\n return ensure0xPrefixed(withoutPrefix);\n}\n\nexport function hexToUint8Array(hexString: string): Uint8Array {\n return Uint8Array.from(Buffer.from(trim0x(hexString), 'hex'));\n}\n\nexport function hexToAscii(hex: string): string {\n if (!isHexString(hex)) {\n throw new Error(`Invalid hex string ${hex}`);\n }\n\n let str = '';\n let i = 0;\n const l = hex.length;\n if (hex.startsWith('0x')) {\n i = 2;\n }\n for (; i < l; i += 2) {\n const code = parseInt(hex.slice(i, i + 2), 16);\n str += String.fromCharCode(code);\n }\n\n return str;\n}\n\nexport function stringToUint8Array(str: string): Uint8Array {\n const value = str.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\n/**\n * A function to convert hex string to Uint8Array (not prefixed)\n * @param hex hex string\n * @returns Uint8Array\n */\nexport function hexToBytes(hex: string): Uint8Array {\n const value = hex.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\n// Buffer.from copies a Uint8Array but reinterprets an ArrayBuffer's bytes; normalize to bytes first.\nfunction asUint8Array(bytes: Uint8Array | ArrayBuffer): Uint8Array {\n return bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes;\n}\n\nexport function bytesToHex(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(\n 'hex',\n );\n}\n\n/**\n * 0x prefixed hex string from Uint8Array\n * @param bytes\n */\nexport function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString {\n return ensure0xPrefixed(bytesToHex(bytes));\n}\n\n/**\n * Big-endian `bigint` from a hex string. Empty hex (`''` / `'0x'`) is `0n`.\n * Replaces the inline `BigInt('0x' + ...)` / `BigInt(\\`0x${hex}\\`)` idiom.\n */\nexport function hexToBigInt(hex: string): bigint {\n if (!isHexString(hex)) {\n throw new Error(`invalid hex string (length ${hex.length})`);\n }\n const body = trim0x(hex);\n return body === '' ? 0n : BigInt(`0x${body}`);\n}\n\n/**\n * Big-endian `bigint` from bytes. Empty input is `0n`.\n * Replaces the inline `BigInt('0x' + buf.toString('hex'))` idiom.\n */\nexport function bytesToBigInt(bytes: Uint8Array | ArrayBuffer): bigint {\n const hex = bytesToHex(bytes);\n return hex === '' ? 0n : BigInt(`0x${hex}`);\n}\n\n/**\n * Big-endian bytes from a non-negative `bigint`, left-padded to `byteLength`. The inverse of\n * `bytesToBigInt`. Throws on a negative value or one too wide for `byteLength` (via `bigIntToHex` /\n * `hexZeroPad`). Replaces the inline `new BN(v).toBuffer('be', n)` / `writeBigUInt*` idiom.\n */\nexport function bigIntToBytes(value: bigint, byteLength: number): Uint8Array {\n return hexToBytes(bigIntToHex(value, byteLength));\n}\n\n/**\n * `0x`-hex from a non-negative `bigint`, optionally left-padded to `byteLength` bytes.\n * Replaces the inline `'0x' + v.toString(16)` (± `padStart`) idiom.\n * Without `byteLength` the output is minimal and may be odd-length (e.g. `0xabc`), which\n * `hexToBytes` rejects — use `bigIntToBytes(value, byteLength)` when you need bytes.\n */\nexport function bigIntToHex(value: bigint, byteLength?: number): HexString {\n if (value < 0n) {\n throw new Error('cannot hex-encode a negative value');\n }\n const hex = ensure0xPrefixed(value.toString(16));\n return byteLength === undefined ? hex : hexZeroPad(hex, byteLength);\n}\n\n/**\n * `0x`-hex from a non-negative integer, optionally left-padded to `byteLength` bytes.\n */\nexport function numberToHex(value: number, byteLength?: number): HexString {\n // isSafeInteger (not isInteger): reject unsafe ints rather than encode a rounded value.\n if (!Number.isSafeInteger(value) || value < 0) {\n throw new Error('cannot hex-encode value: expected a non-negative safe integer');\n }\n return bigIntToHex(BigInt(value), byteLength);\n}\n\n/**\n * `number` from a hex string. Throws if the value exceeds `Number.MAX_SAFE_INTEGER`.\n * Replaces the inline `parseInt(hex, 16)` / `Number('0x' + hex)` idiom (which silently\n * lose precision past 2^53).\n */\nexport function hexToNumber(hex: string): number {\n const value = hexToBigInt(hex);\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new Error(`hex value exceeds Number.MAX_SAFE_INTEGER (length ${hex.length})`);\n }\n return Number(value);\n}\n\nexport function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(\n 'base64',\n );\n}\n\nexport function base64ToBytes(base64: string): Uint8Array {\n return Uint8Array.from(Buffer.from(base64, 'base64'));\n}\n\nexport function hexToBase64(hexString: string): string {\n const hex = trim0x(hexString);\n // Validate that the input is a valid hex string\n if (!/^[0-9a-fA-F]+$/.test(hex)) {\n throw new Error(`Invalid hex string: ${hexString}`);\n }\n\n // Ensure the hex string has an even length\n if (hex.length % 2 !== 0) {\n throw new Error('Hex string must have an even length');\n }\n\n // Convert hex string to a byte array\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < hex.length; i += 2) {\n bytes[i / 2] = parseInt(hex.substr(i, 2), 16);\n }\n\n // Convert the byte array to a base64 string\n const base64String = Buffer.from(bytes).toString('base64');\n\n return base64String;\n}\n\nexport function base64ToHex(base64String: string): HexString {\n if (!/^[A-Za-z0-9+/=]*$/.test(base64String)) {\n throw new Error(`Invalid base64 string: ${base64String}`);\n }\n return `0x${Buffer.from(base64String, 'base64').toString('hex')}`;\n}\n\nexport function base58ToBytes(value: string): Uint8Array {\n if (!isBase58(value)) {\n // Length only, never the value (consistent with the hex decoders).\n throw new Error(`invalid base58 string (length ${value.length})`);\n }\n return bs58.decode(value);\n}\n\nexport function bytesToBase58(bytes: Uint8Array | ArrayBuffer): string {\n return bs58.encode(asUint8Array(bytes));\n}\n\nexport function base58ToHex(value: string): HexString {\n return bytesToHexPrefixed(base58ToBytes(value));\n}\n\nexport function hexToBase58(hex: string): string {\n return bytesToBase58(hexToBytes(hex));\n}\n\nfunction padString(str: string, length: number, left: boolean, padding = '0') {\n const diff = length - str.length;\n let result = str;\n if (diff > 0) {\n const pad = padding.repeat(diff);\n result = left ? pad + str : str + pad;\n }\n return result;\n}\n\nfunction padLeft(str: string, length: number, padding = '0') {\n return padString(str, length, true, padding);\n}\nfunction calcByteLength(str: string, byteSize = 8) {\n const { length } = str;\n const remainder = length % byteSize;\n return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;\n}\n\n/**\n * Pads a hex string on the left so its length is a multiple of `byteSize`.\n * Used to align hex strings.\n * @param str Hex string.\n * @param byteSize Group size to align to, in hex chars (default: 8).\n * @param padding Left-pad character (default: '0').\n * @returns Hex string left-padded to a multiple of `byteSize`.\n */\nexport function padAlignHex(str: string, byteSize = 8, padding = '0') {\n const trimmed = trim0x(str);\n return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["base58regex","base64regex","hexStringRegex","isBase58","address","test","isBase64","isHexString","value","length","match","trim0x","hexZeroPad","Number","isSafeInteger","Error","ensure0xPrefixed","padStart","str","replace","trimLeadingZeros","hexString","withoutPrefix","hexToAscii","hex","Array","from","hexToBytes","b","String","fromCharCode","join","body","Uint8Array","Buffer","asUint8Array","bytes","ArrayBuffer","bytesToHex","toString","bytesToHexPrefixed","hexToBigInt","BigInt","bytesToBigInt","bigIntToBytes","byteLength","bigIntToHex","undefined","numberToHex","hexToNumber","MAX_SAFE_INTEGER","bytesToBase64","base64ToBytes","base64","hexToBase64","base64ToHex","base64String","base58ToBytes","bs58","decode","bytesToBase58","encode","base58ToHex","hexToBase58","padString","left","padding","diff","result","pad","repeat","padLeft","calcByteLength","byteSize","remainder","padAlignHex","trimmed"],"mappings":";;;;;AAOA,IAAMA,WAAAA,GAAc,yBAAA;AACpB,IAAMC,WAAAA,GAAc,wBAAA;AAGpB,IAAMC,cAAAA,GAAiB,qBAAA;AAEhB,SAASC,SAASC,OAAAA,EAAe;AACpC,EAAA,OAAOJ,WAAAA,CAAYK,KAAKD,OA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A,WAAAA,EAAAA,aAAAA,CAAAA","file":"index.js","sourcesContent":["import bs58 from 'bs58';\n\nimport type { HexString } from '@layerzerolabs/typescript-utils';\n\nexport * from './byte-codec';\nexport * from './uuid';\n\nconst base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;\nconst base64regex = /^[A-Za-z0-9+/]*={0,2}$/;\n// Lowercase `0x` prefix only: an uppercase `0X` is a manual-input error, not something any encoder\n// emits, so it's rejected rather than silently accepted. (The body charset matches either case.)\nconst hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;\n\nexport function isBase58(address: string): boolean {\n return base58regex.test(address);\n}\n\nexport function isBase64(address: string): boolean {\n return base64regex.test(address);\n}\n\n// Returns a plain boolean, not a `value is HexString` guard: the regex accepts unprefixed input\n// (e.g. 'deadbeef'), so a true result does not prove the `0x` prefix HexString requires. Callers\n// that need a HexString must run the value through ensure0xPrefixed.\nexport function isHexString(value: any, length?: number): boolean {\n if (typeof value !== 'string' || !value.match(hexStringRegex)) {\n return false;\n }\n // Measure the body, not the raw string: isHexString accepts unprefixed input, so a raw-length\n // check would assume a 2-char `0x` and reject e.g. isHexString('deadbeef', 4).\n if (length && trim0x(value).length !== 2 * length) {\n return false;\n }\n return true;\n}\n\nexport function hexZeroPad(value: string, length: number): HexString {\n if (!Number.isSafeInteger(length) || length < 0) {\n // A fractional width (e.g. 1.5) produces odd-length output that hexToBytes would reject,\n // so reject it here where bigIntToHex / bigIntToBytes / numberToHex all funnel through.\n throw new Error('byteLength must be a non-negative safe integer');\n }\n if (!isHexString(value)) {\n // Length only, never the value (can be sensitive).\n throw new Error(`invalid hex string (length ${value.length})`);\n }\n\n // Normalize first: isHexString accepts unprefixed input, for which the `2 * length + 2`\n // check below (which assumes a 2-char `0x`) would under-count by a byte and pass overflow.\n value = ensure0xPrefixed(value);\n\n if (value.length > 2 * length + 2) {\n throw new Error('value out of range');\n }\n\n return `0x${trim0x(value).padStart(2 * length, '0')}`;\n}\n\nexport function trim0x(str: string): string {\n return str.replace(/^0x/, '');\n}\n\nexport function ensure0xPrefixed(str: string): HexString {\n return `0x${trim0x(str)}`;\n}\n\nexport function trimLeadingZeros(hexString: string): HexString {\n // Remove the '0x' prefix\n let withoutPrefix = trim0x(hexString);\n\n // Trim leading zeros\n withoutPrefix = withoutPrefix.replace(/^0+/, '');\n\n // Add back the '0x' prefix\n return ensure0xPrefixed(withoutPrefix);\n}\n\nexport function hexToAscii(hex: string): string {\n return Array.from(hexToBytes(hex), (b) => String.fromCharCode(b)).join('');\n}\n\n/**\n * Bytes from a `0x`-prefixed (or bare) hex string. The single canonical hex decoder.\n * Throws on non-hex and on odd-length input: a dangling half-byte is always a caller bug,\n * never silently padded or truncated.\n */\nexport function hexToBytes(hex: string): Uint8Array {\n if (!isHexString(hex)) {\n // Length only, never the value (sometimes a private key).\n throw new Error(`invalid hex string (length ${hex.length})`);\n }\n const body = trim0x(hex);\n if (body.length % 2 !== 0) {\n throw new Error(`hex string has an odd length (${body.length})`);\n }\n // The isHexString guard above is load-bearing: Buffer.from(_, 'hex') silently drops non-hex\n // characters, so the pure-hex check is what makes this call safe.\n return Uint8Array.from(Buffer.from(body, 'hex'));\n}\n\n// Buffer.from copies a Uint8Array but reinterprets an ArrayBuffer's bytes; normalize to bytes first.\nfunction asUint8Array(bytes: Uint8Array | ArrayBuffer): Uint8Array {\n return bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes;\n}\n\nexport function bytesToHex(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(asUint8Array(bytes)).toString('hex');\n}\n\n/**\n * 0x prefixed hex string from Uint8Array\n * @param bytes\n */\nexport function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString {\n return ensure0xPrefixed(bytesToHex(bytes));\n}\n\n/**\n * Big-endian `bigint` from a hex string. Empty hex (`''` / `'0x'`) is `0n`.\n */\nexport function hexToBigInt(hex: string): bigint {\n if (!isHexString(hex)) {\n throw new Error(`invalid hex string (length ${hex.length})`);\n }\n const body = trim0x(hex);\n return body === '' ? 0n : BigInt(`0x${body}`);\n}\n\n/**\n * Big-endian `bigint` from bytes. Empty input is `0n`.\n */\nexport function bytesToBigInt(bytes: Uint8Array | ArrayBuffer): bigint {\n const hex = bytesToHex(bytes);\n return hex === '' ? 0n : BigInt(`0x${hex}`);\n}\n\n/**\n * Big-endian bytes from a non-negative `bigint`, left-padded to `byteLength`. The inverse of\n * `bytesToBigInt`. Throws on a negative value or one too wide for `byteLength`.\n */\nexport function bigIntToBytes(value: bigint, byteLength: number): Uint8Array {\n return hexToBytes(bigIntToHex(value, byteLength));\n}\n\n/**\n * `0x`-hex from a non-negative `bigint`, optionally left-padded to `byteLength` bytes.\n * Without `byteLength` the output is minimal and may be odd-length (e.g. `0xabc`), which\n * `hexToBytes` rejects — use `bigIntToBytes(value, byteLength)` when you need bytes.\n */\nexport function bigIntToHex(value: bigint, byteLength?: number): HexString {\n if (value < 0n) {\n throw new Error('cannot hex-encode a negative value');\n }\n const hex = ensure0xPrefixed(value.toString(16));\n return byteLength === undefined ? hex : hexZeroPad(hex, byteLength);\n}\n\n/**\n * `0x`-hex from a non-negative integer, optionally left-padded to `byteLength` bytes.\n */\nexport function numberToHex(value: number, byteLength?: number): HexString {\n // isSafeInteger (not isInteger): reject unsafe ints rather than encode a rounded value.\n if (!Number.isSafeInteger(value) || value < 0) {\n throw new Error('cannot hex-encode value: expected a non-negative safe integer');\n }\n return bigIntToHex(BigInt(value), byteLength);\n}\n\n/**\n * `number` from a hex string. Throws if the value exceeds `Number.MAX_SAFE_INTEGER`.\n */\nexport function hexToNumber(hex: string): number {\n const value = hexToBigInt(hex);\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new Error(`hex value exceeds Number.MAX_SAFE_INTEGER (length ${hex.length})`);\n }\n return Number(value);\n}\n\nexport function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(asUint8Array(bytes)).toString('base64');\n}\n\nexport function base64ToBytes(base64: string): Uint8Array {\n // isBase64 checks the alphabet/padding; the length must also be a multiple of 4, or Buffer.from\n // silently drops the trailing partial group. Length only, never the value.\n if (!isBase64(base64) || base64.length % 4 !== 0) {\n throw new Error(`invalid base64 string (length ${base64.length})`);\n }\n return Uint8Array.from(Buffer.from(base64, 'base64'));\n}\n\nexport function hexToBase64(hexString: string): string {\n // hexToBytes validates the input, so no separate hex check here.\n return bytesToBase64(hexToBytes(hexString));\n}\n\nexport function base64ToHex(base64String: string): HexString {\n return bytesToHexPrefixed(base64ToBytes(base64String));\n}\n\nexport function base58ToBytes(value: string): Uint8Array {\n if (!isBase58(value)) {\n // Length only, never the value (consistent with the hex decoders).\n throw new Error(`invalid base58 string (length ${value.length})`);\n }\n return bs58.decode(value);\n}\n\nexport function bytesToBase58(bytes: Uint8Array | ArrayBuffer): string {\n return bs58.encode(asUint8Array(bytes));\n}\n\nexport function base58ToHex(value: string): HexString {\n return bytesToHexPrefixed(base58ToBytes(value));\n}\n\nexport function hexToBase58(hex: string): string {\n return bytesToBase58(hexToBytes(hex));\n}\n\nfunction padString(str: string, length: number, left: boolean, padding = '0') {\n const diff = length - str.length;\n let result = str;\n if (diff > 0) {\n const pad = padding.repeat(diff);\n result = left ? pad + str : str + pad;\n }\n return result;\n}\n\nfunction padLeft(str: string, length: number, padding = '0') {\n return padString(str, length, true, padding);\n}\nfunction calcByteLength(str: string, byteSize = 8) {\n const { length } = str;\n const remainder = length % byteSize;\n return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;\n}\n\n/**\n * Pads a hex string on the left so its length is a multiple of `byteSize`.\n * Used to align hex strings.\n * @param str Hex string.\n * @param byteSize Group size to align to, in hex chars (default: 8).\n * @param padding Left-pad character (default: '0').\n * @returns Hex string left-padded to a multiple of `byteSize`.\n */\nexport function padAlignHex(str: string, byteSize = 8, padding = '0') {\n const trimmed = trim0x(str);\n return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);\n}\n"]}
|
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.115",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,13 +16,14 @@
|
|
|
16
16
|
"dist/**/*"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"bs58": "^5.0.0"
|
|
19
|
+
"bs58": "^5.0.0",
|
|
20
|
+
"@layerzerolabs/typescript-utils": "0.2.115"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
23
|
"tsup": "^8.4.0",
|
|
23
24
|
"vitest": "^3.2.6",
|
|
24
|
-
"@layerzerolabs/
|
|
25
|
-
"@layerzerolabs/
|
|
25
|
+
"@layerzerolabs/typescript-configuration": "0.2.115",
|
|
26
|
+
"@layerzerolabs/tsup-configuration": "0.2.115"
|
|
26
27
|
},
|
|
27
28
|
"publishConfig": {
|
|
28
29
|
"access": "public",
|
|
@@ -33,6 +34,7 @@
|
|
|
33
34
|
"audit-external",
|
|
34
35
|
"console-pen-test",
|
|
35
36
|
"layerzero-ver",
|
|
37
|
+
"monorepo-external",
|
|
36
38
|
"onesig-client"
|
|
37
39
|
]
|
|
38
40
|
},
|