@qevm/bytes 5.7.1

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Richard Moore
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ Byte Manipulation
2
+ =================
3
+
4
+ This sub-module is part of the [ethers project](https://github.com/ethers-io/ethers.js).
5
+
6
+ It is responsible for manipulating binary data.
7
+
8
+ For more information, see the [documentation](https://docs.ethers.io/v5/api/utils/bytes/).
9
+
10
+
11
+ Importing
12
+ ---------
13
+
14
+ Most users will prefer to use the [umbrella package](https://www.npmjs.com/package/ethers),
15
+ but for those with more specific needs, individual components can be imported.
16
+
17
+ ```javascript
18
+ const {
19
+
20
+ isBytesLike,
21
+ isBytes,
22
+
23
+ arrayify,
24
+
25
+ concat,
26
+
27
+ stripZeros,
28
+ zeroPad,
29
+
30
+ isHexString,
31
+ hexlify,
32
+
33
+ hexDataLength,
34
+ hexDataSlice,
35
+ hexConcat,
36
+
37
+ hexValue,
38
+
39
+ hexStripZeros,
40
+ hexZeroPad,
41
+
42
+ splitSignature,
43
+ joinSignature,
44
+
45
+ // Types
46
+
47
+ Bytes,
48
+ BytesLike,
49
+
50
+ DataOptions,
51
+
52
+ Hexable,
53
+
54
+ SignatureLike,
55
+ Signature
56
+
57
+ } = require("@ethersproject/bytes");
58
+ ```
59
+
60
+
61
+ License
62
+ -------
63
+
64
+ MIT License
@@ -0,0 +1,2 @@
1
+ export declare const version = "bytes/5.7.0";
2
+ //# sourceMappingURL=_version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_version.d.ts","sourceRoot":"","sources":["../src.ts/_version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,gBAAgB,CAAC"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.version = void 0;
4
+ exports.version = "bytes/5.7.0";
5
+ //# sourceMappingURL=_version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_version.js","sourceRoot":"","sources":["../src.ts/_version.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,aAAa,CAAC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ export declare type Bytes = ArrayLike<number>;
2
+ export declare type BytesLike = Bytes | string;
3
+ export declare type DataOptions = {
4
+ allowMissingPrefix?: boolean;
5
+ hexPad?: "left" | "right" | null;
6
+ };
7
+ export interface Hexable {
8
+ toHexString(): string;
9
+ }
10
+ export declare type SignatureLike = {
11
+ signature: string;
12
+ publicKey: string;
13
+ } | BytesLike;
14
+ export interface Signature {
15
+ signature: string;
16
+ publicKey: string;
17
+ }
18
+ export declare function isBytesLike(value: any): value is BytesLike;
19
+ export declare function isBytes(value: any): value is Bytes;
20
+ export declare function arrayify(value: BytesLike | Hexable | number, options?: DataOptions): Uint8Array;
21
+ export declare function concat(items: ReadonlyArray<BytesLike>): Uint8Array;
22
+ export declare function stripZeros(value: BytesLike): Uint8Array;
23
+ export declare function zeroPad(value: BytesLike, length: number): Uint8Array;
24
+ export declare function isHexString(value: any, length?: number): boolean;
25
+ export declare function hexlify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): string;
26
+ export declare function hexDataLength(data: BytesLike): number;
27
+ export declare function hexDataSlice(data: BytesLike, offset: number, endOffset?: number): string;
28
+ export declare function hexConcat(items: ReadonlyArray<BytesLike>): string;
29
+ export declare function hexValue(value: BytesLike | Hexable | number | bigint): string;
30
+ export declare function hexStripZeros(value: BytesLike): string;
31
+ export declare function hexZeroPad(value: BytesLike, length: number): string;
32
+ export declare function splitSignature(signature: SignatureLike): Signature;
33
+ export declare function joinSignature(signature: SignatureLike): string;
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src.ts/index.ts"],"names":[],"mappings":"AASA,oBAAY,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;AAEtC,oBAAY,SAAS,GAAG,KAAK,GAAG,MAAM,CAAC;AAEvC,oBAAY,WAAW,GAAG;IACtB,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;CACpC,CAAC;AAEF,MAAM,WAAW,OAAO;IACpB,WAAW,IAAI,MAAM,CAAC;CACzB;AAED,oBAAY,aAAa,GAAG;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACrB,GAAG,SAAS,CAAC;AAEd,MAAM,WAAW,SAAS;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACrB;AAoBD,wBAAgB,WAAW,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI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wBAAgB,aAAa,CAAC,SAAS,EAAE,aAAa,GAAG,MAAM,CAO9D"}
package/lib/index.js ADDED
@@ -0,0 +1,307 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.joinSignature = exports.splitSignature = exports.hexZeroPad = exports.hexStripZeros = exports.hexValue = exports.hexConcat = exports.hexDataSlice = exports.hexDataLength = exports.hexlify = exports.isHexString = exports.zeroPad = exports.stripZeros = exports.concat = exports.arrayify = exports.isBytes = exports.isBytesLike = void 0;
4
+ var logger_1 = require("@ethersproject/logger");
5
+ var _version_1 = require("./_version");
6
+ var logger = new logger_1.Logger(_version_1.version);
7
+ ///////////////////////////////
8
+ function isHexable(value) {
9
+ return !!(value.toHexString);
10
+ }
11
+ function addSlice(array) {
12
+ if (array.slice) {
13
+ return array;
14
+ }
15
+ array.slice = function () {
16
+ var args = Array.prototype.slice.call(arguments);
17
+ return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args)));
18
+ };
19
+ return array;
20
+ }
21
+ function isBytesLike(value) {
22
+ return ((isHexString(value) && !(value.length % 2)) || isBytes(value));
23
+ }
24
+ exports.isBytesLike = isBytesLike;
25
+ function isInteger(value) {
26
+ return (typeof (value) === "number" && value == value && (value % 1) === 0);
27
+ }
28
+ function isBytes(value) {
29
+ if (value == null) {
30
+ return false;
31
+ }
32
+ if (value.constructor === Uint8Array) {
33
+ return true;
34
+ }
35
+ if (typeof (value) === "string") {
36
+ return false;
37
+ }
38
+ if (!isInteger(value.length) || value.length < 0) {
39
+ return false;
40
+ }
41
+ for (var i = 0; i < value.length; i++) {
42
+ var v = value[i];
43
+ if (!isInteger(v) || v < 0 || v >= 256) {
44
+ return false;
45
+ }
46
+ }
47
+ return true;
48
+ }
49
+ exports.isBytes = isBytes;
50
+ function arrayify(value, options) {
51
+ if (!options) {
52
+ options = {};
53
+ }
54
+ if (typeof (value) === "number") {
55
+ logger.checkSafeUint53(value, "invalid arrayify value");
56
+ var result = [];
57
+ while (value) {
58
+ result.unshift(value & 0xff);
59
+ value = parseInt(String(value / 256));
60
+ }
61
+ if (result.length === 0) {
62
+ result.push(0);
63
+ }
64
+ return addSlice(new Uint8Array(result));
65
+ }
66
+ if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
67
+ value = "0x" + value;
68
+ }
69
+ if (isHexable(value)) {
70
+ value = value.toHexString();
71
+ }
72
+ if (isHexString(value)) {
73
+ var hex = value.substring(2);
74
+ if (hex.length % 2) {
75
+ if (options.hexPad === "left") {
76
+ hex = "0" + hex;
77
+ }
78
+ else if (options.hexPad === "right") {
79
+ hex += "0";
80
+ }
81
+ else {
82
+ logger.throwArgumentError("hex data is odd-length", "value", value);
83
+ }
84
+ }
85
+ var result = [];
86
+ for (var i = 0; i < hex.length; i += 2) {
87
+ result.push(parseInt(hex.substring(i, i + 2), 16));
88
+ }
89
+ return addSlice(new Uint8Array(result));
90
+ }
91
+ if (isBytes(value)) {
92
+ return addSlice(new Uint8Array(value));
93
+ }
94
+ return logger.throwArgumentError("invalid arrayify value", "value", value);
95
+ }
96
+ exports.arrayify = arrayify;
97
+ function concat(items) {
98
+ var objects = items.map(function (item) { return arrayify(item); });
99
+ var length = objects.reduce(function (accum, item) { return (accum + item.length); }, 0);
100
+ var result = new Uint8Array(length);
101
+ objects.reduce(function (offset, object) {
102
+ result.set(object, offset);
103
+ return offset + object.length;
104
+ }, 0);
105
+ return addSlice(result);
106
+ }
107
+ exports.concat = concat;
108
+ function stripZeros(value) {
109
+ var result = arrayify(value);
110
+ if (result.length === 0) {
111
+ return result;
112
+ }
113
+ // Find the first non-zero entry
114
+ var start = 0;
115
+ while (start < result.length && result[start] === 0) {
116
+ start++;
117
+ }
118
+ // If we started with zeros, strip them
119
+ if (start) {
120
+ result = result.slice(start);
121
+ }
122
+ return result;
123
+ }
124
+ exports.stripZeros = stripZeros;
125
+ function zeroPad(value, length) {
126
+ value = arrayify(value);
127
+ if (value.length > length) {
128
+ logger.throwArgumentError("value out of range", "value", arguments[0]);
129
+ }
130
+ var result = new Uint8Array(length);
131
+ result.set(value, length - value.length);
132
+ return addSlice(result);
133
+ }
134
+ exports.zeroPad = zeroPad;
135
+ function isHexString(value, length) {
136
+ if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
137
+ return false;
138
+ }
139
+ if (length && value.length !== 2 + 2 * length) {
140
+ return false;
141
+ }
142
+ return true;
143
+ }
144
+ exports.isHexString = isHexString;
145
+ var HexCharacters = "0123456789abcdef";
146
+ function hexlify(value, options) {
147
+ if (!options) {
148
+ options = {};
149
+ }
150
+ if (typeof (value) === "number") {
151
+ logger.checkSafeUint53(value, "invalid hexlify value");
152
+ var hex = "";
153
+ while (value) {
154
+ hex = HexCharacters[value & 0xf] + hex;
155
+ value = Math.floor(value / 16);
156
+ }
157
+ if (hex.length) {
158
+ if (hex.length % 2) {
159
+ hex = "0" + hex;
160
+ }
161
+ return "0x" + hex;
162
+ }
163
+ return "0x00";
164
+ }
165
+ if (typeof (value) === "bigint") {
166
+ value = value.toString(16);
167
+ if (value.length % 2) {
168
+ return ("0x0" + value);
169
+ }
170
+ return "0x" + value;
171
+ }
172
+ if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
173
+ value = "0x" + value;
174
+ }
175
+ if (isHexable(value)) {
176
+ return value.toHexString();
177
+ }
178
+ if (isHexString(value)) {
179
+ if (value.length % 2) {
180
+ if (options.hexPad === "left") {
181
+ value = "0x0" + value.substring(2);
182
+ }
183
+ else if (options.hexPad === "right") {
184
+ value += "0";
185
+ }
186
+ else {
187
+ logger.throwArgumentError("hex data is odd-length", "value", value);
188
+ }
189
+ }
190
+ return value.toLowerCase();
191
+ }
192
+ if (isBytes(value)) {
193
+ var result = "0x";
194
+ for (var i = 0; i < value.length; i++) {
195
+ var v = value[i];
196
+ result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
197
+ }
198
+ return result;
199
+ }
200
+ return logger.throwArgumentError("invalid hexlify value", "value", value);
201
+ }
202
+ exports.hexlify = hexlify;
203
+ /*
204
+ function unoddify(value: BytesLike | Hexable | number): BytesLike | Hexable | number {
205
+ if (typeof(value) === "string" && value.length % 2 && value.substring(0, 2) === "0x") {
206
+ return "0x0" + value.substring(2);
207
+ }
208
+ return value;
209
+ }
210
+ */
211
+ function hexDataLength(data) {
212
+ if (typeof (data) !== "string") {
213
+ data = hexlify(data);
214
+ }
215
+ else if (!isHexString(data) || (data.length % 2)) {
216
+ return null;
217
+ }
218
+ return (data.length - 2) / 2;
219
+ }
220
+ exports.hexDataLength = hexDataLength;
221
+ function hexDataSlice(data, offset, endOffset) {
222
+ if (typeof (data) !== "string") {
223
+ data = hexlify(data);
224
+ }
225
+ else if (!isHexString(data) || (data.length % 2)) {
226
+ logger.throwArgumentError("invalid hexData", "value", data);
227
+ }
228
+ offset = 2 + 2 * offset;
229
+ if (endOffset != null) {
230
+ return "0x" + data.substring(offset, 2 + 2 * endOffset);
231
+ }
232
+ return "0x" + data.substring(offset);
233
+ }
234
+ exports.hexDataSlice = hexDataSlice;
235
+ function hexConcat(items) {
236
+ var result = "0x";
237
+ items.forEach(function (item) {
238
+ result += hexlify(item).substring(2);
239
+ });
240
+ return result;
241
+ }
242
+ exports.hexConcat = hexConcat;
243
+ function hexValue(value) {
244
+ var trimmed = hexStripZeros(hexlify(value, { hexPad: "left" }));
245
+ if (trimmed === "0x") {
246
+ return "0x0";
247
+ }
248
+ return trimmed;
249
+ }
250
+ exports.hexValue = hexValue;
251
+ function hexStripZeros(value) {
252
+ if (typeof (value) !== "string") {
253
+ value = hexlify(value);
254
+ }
255
+ if (!isHexString(value)) {
256
+ logger.throwArgumentError("invalid hex string", "value", value);
257
+ }
258
+ value = value.substring(2);
259
+ var offset = 0;
260
+ while (offset < value.length && value[offset] === "0") {
261
+ offset++;
262
+ }
263
+ return "0x" + value.substring(offset);
264
+ }
265
+ exports.hexStripZeros = hexStripZeros;
266
+ function hexZeroPad(value, length) {
267
+ if (typeof (value) !== "string") {
268
+ value = hexlify(value);
269
+ }
270
+ else if (!isHexString(value)) {
271
+ logger.throwArgumentError("invalid hex string", "value", value);
272
+ }
273
+ if (value.length > 2 * length + 2) {
274
+ logger.throwArgumentError("value out of range", "value", arguments[1]);
275
+ }
276
+ while (value.length < 2 * length + 2) {
277
+ value = "0x0" + value.substring(2);
278
+ }
279
+ return value;
280
+ }
281
+ exports.hexZeroPad = hexZeroPad;
282
+ function splitSignature(signature) {
283
+ var result = {
284
+ signature: "",
285
+ publicKey: "",
286
+ };
287
+ if (isBytesLike(signature)) {
288
+ var bytes = arrayify(signature);
289
+ result.publicKey = hexlify(bytes.slice(0, 1184));
290
+ result.signature = hexlify(bytes.slice(1184));
291
+ }
292
+ else {
293
+ result.publicKey = signature.publicKey;
294
+ result.signature = signature.signature;
295
+ }
296
+ return result;
297
+ }
298
+ exports.splitSignature = splitSignature;
299
+ function joinSignature(signature) {
300
+ signature = splitSignature(signature);
301
+ return hexlify(concat([
302
+ signature.publicKey,
303
+ signature.signature
304
+ ]));
305
+ }
306
+ exports.joinSignature = joinSignature;
307
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src.ts/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AAEb,gDAA+C;AAC/C,uCAAqC;AACrC,IAAM,MAAM,GAAG,IAAI,eAAM,CAAC,kBAAO,CAAC,CAAC;AA4BnC,+BAA+B;AAG/B,SAAS,SAAS,CAAC,KAAU;IACzB,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACjC,CAAC;AAED,SAAS,QAAQ,CAAC,KAAiB;IAC/B,IAAI,KAAK,CAAC,KAAK,EAAE;QAAE,OAAO,KAAK,CAAC;KAAE;IAElC,KAAK,CAAC,KAAK,GAAG;QACV,IAAM,IAAI,GAAG,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,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AoB,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;KAC1E;IAED,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE;QAClC,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KACtC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC;AAhBD,gCAgBC;AAED,SAAgB,cAAc,CAAC,SAAwB;IACnD,IAAM,MAAM,GAAa;QACrB,SAAS,EAAE,EAAE;QACb,SAAS,EAAE,EAAE;KAChB,CAAC;IAEF,IAAI,WAAW,CAAC,SAAS,CAAC,EAAE;QACxB,IAAI,KAAK,GAAe,QAAQ,CAAC,SAAS,CAAC,CAAC;QAC5C,MAAM,CAAC,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAC,IAAI,CAAC,CAAC,CAAC;QAChD,MAAM,CAAC,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;KAEjD;SAAM;QACH,MAAM,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC;QACvC,MAAM,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC;KAC1C;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAjBD,wCAiBC;AAED,SAAgB,aAAa,CAAC,SAAwB;IAClD,SAAS,GAAG,cAAc,CAAC,SAAS,CAAC,CAAC;IAEtC,OAAO,OAAO,CAAC,MAAM,CAAC;QAClB,SAAS,CAAC,SAAS;QACnB,SAAS,CAAC,SAAS;KACtB,CAAC,CAAC,CAAC;AACR,CAAC;AAPD,sCAOC"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "author": "Eugene Kuleshov",
3
+ "dependencies": {
4
+ "@ethersproject/logger": "^5.7.0"
5
+ },
6
+ "description": "Bytes utility functions for qethers.",
7
+ "keywords": [
8
+ "QuantumEVM",
9
+ "qethers"
10
+ ],
11
+ "license": "MIT",
12
+ "main": "./lib/index.js",
13
+ "module": "./lib.esm/index.js",
14
+ "name": "@qevm/bytes",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "scripts": {
19
+ "auto-build": "npm run build -- -w",
20
+ "build": "tsc -p ./tsconfig.json",
21
+ "test": "echo \"Error: no test specified\" && exit 1"
22
+ },
23
+ "sideEffects": false,
24
+ "tarballHash": "0xd063b80f586d9923d11307949ef6b65855c5a58a836fb36b9e4a71f987001ecf",
25
+ "types": "./lib/index.d.ts",
26
+ "version": "5.7.1"
27
+ }
@@ -0,0 +1 @@
1
+ export const version = "bytes/5.7.0";
@@ -0,0 +1,338 @@
1
+ "use strict";
2
+
3
+ import { Logger } from "@ethersproject/logger";
4
+ import { version } from "./_version";
5
+ const logger = new Logger(version);
6
+
7
+ ///////////////////////////////
8
+ // Exported Types
9
+
10
+ export type Bytes = ArrayLike<number>;
11
+
12
+ export type BytesLike = Bytes | string;
13
+
14
+ export type DataOptions = {
15
+ allowMissingPrefix?: boolean;
16
+ hexPad?: "left" | "right" | null;
17
+ };
18
+
19
+ export interface Hexable {
20
+ toHexString(): string;
21
+ }
22
+
23
+ export type SignatureLike = {
24
+ signature: string,
25
+ publicKey: string,
26
+ } | BytesLike;
27
+
28
+ export interface Signature {
29
+ signature: string,
30
+ publicKey: string,
31
+ }
32
+
33
+ ///////////////////////////////
34
+
35
+
36
+ function isHexable(value: any): value is Hexable {
37
+ return !!(value.toHexString);
38
+ }
39
+
40
+ function addSlice(array: Uint8Array): Uint8Array {
41
+ if (array.slice) { return array; }
42
+
43
+ array.slice = function() {
44
+ const args = Array.prototype.slice.call(arguments);
45
+ return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args)));
46
+ }
47
+
48
+ return array;
49
+ }
50
+
51
+ export function isBytesLike(value: any): value is BytesLike {
52
+ return ((isHexString(value) && !(value.length % 2)) || isBytes(value));
53
+ }
54
+
55
+ function isInteger(value: number) {
56
+ return (typeof(value) === "number" && value == value && (value % 1) === 0);
57
+ }
58
+
59
+ export function isBytes(value: any): value is Bytes {
60
+ if (value == null) { return false; }
61
+
62
+ if (value.constructor === Uint8Array) { return true; }
63
+ if (typeof(value) === "string") { return false; }
64
+ if (!isInteger(value.length) || value.length < 0) { return false; }
65
+
66
+ for (let i = 0; i < value.length; i++) {
67
+ const v = value[i];
68
+ if (!isInteger(v) || v < 0 || v >= 256) { return false; }
69
+ }
70
+ return true;
71
+ }
72
+
73
+
74
+ export function arrayify(value: BytesLike | Hexable | number, options?: DataOptions): Uint8Array {
75
+ if (!options) { options = { }; }
76
+
77
+ if (typeof(value) === "number") {
78
+ logger.checkSafeUint53(value, "invalid arrayify value");
79
+
80
+ const result = [];
81
+ while (value) {
82
+ result.unshift(value & 0xff);
83
+ value = parseInt(String(value / 256));
84
+ }
85
+ if (result.length === 0) { result.push(0); }
86
+
87
+ return addSlice(new Uint8Array(result));
88
+ }
89
+
90
+ if (options.allowMissingPrefix && typeof(value) === "string" && value.substring(0, 2) !== "0x") {
91
+ value = "0x" + value;
92
+ }
93
+
94
+ if (isHexable(value)) { value = value.toHexString(); }
95
+
96
+ if (isHexString(value)) {
97
+ let hex = (<string>value).substring(2);
98
+ if (hex.length % 2) {
99
+ if (options.hexPad === "left") {
100
+ hex = "0" + hex;
101
+ } else if (options.hexPad === "right") {
102
+ hex += "0";
103
+ } else {
104
+ logger.throwArgumentError("hex data is odd-length", "value", value);
105
+ }
106
+ }
107
+
108
+ const result = [];
109
+ for (let i = 0; i < hex.length; i += 2) {
110
+ result.push(parseInt(hex.substring(i, i + 2), 16));
111
+ }
112
+
113
+ return addSlice(new Uint8Array(result));
114
+ }
115
+
116
+ if (isBytes(value)) {
117
+ return addSlice(new Uint8Array(value));
118
+ }
119
+
120
+ return logger.throwArgumentError("invalid arrayify value", "value", value);
121
+ }
122
+
123
+ export function concat(items: ReadonlyArray<BytesLike>): Uint8Array {
124
+ const objects = items.map(item => arrayify(item));
125
+ const length = objects.reduce((accum, item) => (accum + item.length), 0);
126
+
127
+ const result = new Uint8Array(length);
128
+
129
+ objects.reduce((offset, object) => {
130
+ result.set(object, offset);
131
+ return offset + object.length;
132
+ }, 0);
133
+
134
+ return addSlice(result);
135
+ }
136
+
137
+ export function stripZeros(value: BytesLike): Uint8Array {
138
+ let result: Uint8Array = arrayify(value);
139
+
140
+ if (result.length === 0) { return result; }
141
+
142
+ // Find the first non-zero entry
143
+ let start = 0;
144
+ while (start < result.length && result[start] === 0) { start++ }
145
+
146
+ // If we started with zeros, strip them
147
+ if (start) {
148
+ result = result.slice(start);
149
+ }
150
+
151
+ return result;
152
+ }
153
+
154
+ export function zeroPad(value: BytesLike, length: number): Uint8Array {
155
+ value = arrayify(value);
156
+
157
+ if (value.length > length) {
158
+ logger.throwArgumentError("value out of range", "value", arguments[0]);
159
+ }
160
+
161
+ const result = new Uint8Array(length);
162
+ result.set(value, length - value.length);
163
+ return addSlice(result);
164
+ }
165
+
166
+
167
+ export function isHexString(value: any, length?: number): boolean {
168
+ if (typeof(value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
169
+ return false
170
+ }
171
+ if (length && value.length !== 2 + 2 * length) { return false; }
172
+ return true;
173
+ }
174
+
175
+ const HexCharacters: string = "0123456789abcdef";
176
+
177
+ export function hexlify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): string {
178
+ if (!options) { options = { }; }
179
+
180
+ if (typeof(value) === "number") {
181
+ logger.checkSafeUint53(value, "invalid hexlify value");
182
+
183
+ let hex = "";
184
+ while (value) {
185
+ hex = HexCharacters[value & 0xf] + hex;
186
+ value = Math.floor(value / 16);
187
+ }
188
+
189
+ if (hex.length) {
190
+ if (hex.length % 2) { hex = "0" + hex; }
191
+ return "0x" + hex;
192
+ }
193
+
194
+ return "0x00";
195
+ }
196
+
197
+ if (typeof(value) === "bigint") {
198
+ value = value.toString(16);
199
+ if (value.length % 2) { return ("0x0" + value); }
200
+ return "0x" + value;
201
+ }
202
+
203
+ if (options.allowMissingPrefix && typeof(value) === "string" && value.substring(0, 2) !== "0x") {
204
+ value = "0x" + value;
205
+ }
206
+
207
+ if (isHexable(value)) { return value.toHexString(); }
208
+
209
+ if (isHexString(value)) {
210
+ if ((<string>value).length % 2) {
211
+ if (options.hexPad === "left") {
212
+ value = "0x0" + (<string>value).substring(2);
213
+ } else if (options.hexPad === "right") {
214
+ value += "0";
215
+ } else {
216
+ logger.throwArgumentError("hex data is odd-length", "value", value);
217
+ }
218
+ }
219
+ return (<string>value).toLowerCase();
220
+ }
221
+
222
+ if (isBytes(value)) {
223
+ let result = "0x";
224
+ for (let i = 0; i < value.length; i++) {
225
+ let v = value[i];
226
+ result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
227
+ }
228
+ return result;
229
+ }
230
+
231
+ return logger.throwArgumentError("invalid hexlify value", "value", value);
232
+ }
233
+
234
+ /*
235
+ function unoddify(value: BytesLike | Hexable | number): BytesLike | Hexable | number {
236
+ if (typeof(value) === "string" && value.length % 2 && value.substring(0, 2) === "0x") {
237
+ return "0x0" + value.substring(2);
238
+ }
239
+ return value;
240
+ }
241
+ */
242
+ export function hexDataLength(data: BytesLike) {
243
+ if (typeof(data) !== "string") {
244
+ data = hexlify(data);
245
+ } else if (!isHexString(data) || (data.length % 2)) {
246
+ return null;
247
+ }
248
+
249
+ return (data.length - 2) / 2;
250
+ }
251
+
252
+ export function hexDataSlice(data: BytesLike, offset: number, endOffset?: number): string {
253
+ if (typeof(data) !== "string") {
254
+ data = hexlify(data);
255
+ } else if (!isHexString(data) || (data.length % 2)) {
256
+ logger.throwArgumentError("invalid hexData", "value", data );
257
+ }
258
+
259
+ offset = 2 + 2 * offset;
260
+
261
+ if (endOffset != null) {
262
+ return "0x" + data.substring(offset, 2 + 2 * endOffset);
263
+ }
264
+
265
+ return "0x" + data.substring(offset);
266
+ }
267
+
268
+ export function hexConcat(items: ReadonlyArray<BytesLike>): string {
269
+ let result = "0x";
270
+ items.forEach((item) => {
271
+ result += hexlify(item).substring(2);
272
+ });
273
+ return result;
274
+ }
275
+
276
+ export function hexValue(value: BytesLike | Hexable | number | bigint): string {
277
+ const trimmed = hexStripZeros(hexlify(value, { hexPad: "left" }));
278
+ if (trimmed === "0x") { return "0x0"; }
279
+ return trimmed;
280
+ }
281
+
282
+ export function hexStripZeros(value: BytesLike): string {
283
+ if (typeof(value) !== "string") { value = hexlify(value); }
284
+
285
+ if (!isHexString(value)) {
286
+ logger.throwArgumentError("invalid hex string", "value", value);
287
+ }
288
+ value = value.substring(2);
289
+ let offset = 0;
290
+ while (offset < value.length && value[offset] === "0") { offset++; }
291
+ return "0x" + value.substring(offset);
292
+ }
293
+
294
+ export function hexZeroPad(value: BytesLike, length: number): string {
295
+ if (typeof(value) !== "string") {
296
+ value = hexlify(value);
297
+ } else if (!isHexString(value)) {
298
+ logger.throwArgumentError("invalid hex string", "value", value);
299
+ }
300
+
301
+ if (value.length > 2 * length + 2) {
302
+ logger.throwArgumentError("value out of range", "value", arguments[1]);
303
+ }
304
+
305
+ while (value.length < 2 * length + 2) {
306
+ value = "0x0" + value.substring(2);
307
+ }
308
+
309
+ return value;
310
+ }
311
+
312
+ export function splitSignature(signature: SignatureLike): Signature {
313
+ const result: Signature ={
314
+ signature: "",
315
+ publicKey: "",
316
+ };
317
+
318
+ if (isBytesLike(signature)) {
319
+ let bytes: Uint8Array = arrayify(signature);
320
+ result.publicKey = hexlify(bytes.slice(0,1184));
321
+ result.signature = hexlify(bytes.slice(1184));
322
+
323
+ } else {
324
+ result.publicKey = signature.publicKey;
325
+ result.signature = signature.signature;
326
+ }
327
+
328
+ return result;
329
+ }
330
+
331
+ export function joinSignature(signature: SignatureLike): string {
332
+ signature = splitSignature(signature);
333
+
334
+ return hexlify(concat([
335
+ signature.publicKey,
336
+ signature.signature
337
+ ]));
338
+ }