@semaphore-protocol/identity 4.0.0-beta.2 → 4.0.0-beta.4
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.cjs +2369 -0
- package/dist/index.js +2367 -0
- package/dist/types/identity.d.ts +15 -12
- package/package.json +13 -19
- package/src/identity.ts +44 -22
- package/dist/index.browser.js +0 -1075
- package/dist/index.node.cjs +0 -233
- package/dist/index.node.js +0 -231
- package/dist/types/bytes-to-bigint.d.ts +0 -1
- package/dist/types/random-number.browser.d.ts +0 -1
- package/dist/types/random-number.node.d.ts +0 -1
- package/src/bytes-to-bigint.ts +0 -9
- package/src/random-number.browser.ts +0 -8
- package/src/random-number.node.ts +0 -9
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @semaphore-protocol/identity
|
|
3
|
+
* @version 4.0.0-beta.4
|
|
4
|
+
* @file A library to create Semaphore identities.
|
|
5
|
+
* @copyright Ethereum Foundation 2024
|
|
6
|
+
* @license MIT
|
|
7
|
+
* @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/identity}
|
|
8
|
+
*/
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
var eddsaPoseidon = require('@zk-kit/eddsa-poseidon');
|
|
12
|
+
|
|
13
|
+
function getDefaultExportFromCjs (x) {
|
|
14
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var buffer = {};
|
|
18
|
+
|
|
19
|
+
var base64Js = {};
|
|
20
|
+
|
|
21
|
+
base64Js.byteLength = byteLength;
|
|
22
|
+
base64Js.toByteArray = toByteArray;
|
|
23
|
+
base64Js.fromByteArray = fromByteArray;
|
|
24
|
+
var lookup = [];
|
|
25
|
+
var revLookup = [];
|
|
26
|
+
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
|
|
27
|
+
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
28
|
+
for (var i = 0, len = code.length; i < len; ++i) {
|
|
29
|
+
lookup[i] = code[i];
|
|
30
|
+
revLookup[code.charCodeAt(i)] = i;
|
|
31
|
+
}
|
|
32
|
+
revLookup['-'.charCodeAt(0)] = 62;
|
|
33
|
+
revLookup['_'.charCodeAt(0)] = 63;
|
|
34
|
+
function getLens (b64) {
|
|
35
|
+
var len = b64.length;
|
|
36
|
+
if (len % 4 > 0) {
|
|
37
|
+
throw new Error('Invalid string. Length must be a multiple of 4')
|
|
38
|
+
}
|
|
39
|
+
var validLen = b64.indexOf('=');
|
|
40
|
+
if (validLen === -1) validLen = len;
|
|
41
|
+
var placeHoldersLen = validLen === len
|
|
42
|
+
? 0
|
|
43
|
+
: 4 - (validLen % 4);
|
|
44
|
+
return [validLen, placeHoldersLen]
|
|
45
|
+
}
|
|
46
|
+
function byteLength (b64) {
|
|
47
|
+
var lens = getLens(b64);
|
|
48
|
+
var validLen = lens[0];
|
|
49
|
+
var placeHoldersLen = lens[1];
|
|
50
|
+
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
|
|
51
|
+
}
|
|
52
|
+
function _byteLength (b64, validLen, placeHoldersLen) {
|
|
53
|
+
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
|
|
54
|
+
}
|
|
55
|
+
function toByteArray (b64) {
|
|
56
|
+
var tmp;
|
|
57
|
+
var lens = getLens(b64);
|
|
58
|
+
var validLen = lens[0];
|
|
59
|
+
var placeHoldersLen = lens[1];
|
|
60
|
+
var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen));
|
|
61
|
+
var curByte = 0;
|
|
62
|
+
var len = placeHoldersLen > 0
|
|
63
|
+
? validLen - 4
|
|
64
|
+
: validLen;
|
|
65
|
+
var i;
|
|
66
|
+
for (i = 0; i < len; i += 4) {
|
|
67
|
+
tmp =
|
|
68
|
+
(revLookup[b64.charCodeAt(i)] << 18) |
|
|
69
|
+
(revLookup[b64.charCodeAt(i + 1)] << 12) |
|
|
70
|
+
(revLookup[b64.charCodeAt(i + 2)] << 6) |
|
|
71
|
+
revLookup[b64.charCodeAt(i + 3)];
|
|
72
|
+
arr[curByte++] = (tmp >> 16) & 0xFF;
|
|
73
|
+
arr[curByte++] = (tmp >> 8) & 0xFF;
|
|
74
|
+
arr[curByte++] = tmp & 0xFF;
|
|
75
|
+
}
|
|
76
|
+
if (placeHoldersLen === 2) {
|
|
77
|
+
tmp =
|
|
78
|
+
(revLookup[b64.charCodeAt(i)] << 2) |
|
|
79
|
+
(revLookup[b64.charCodeAt(i + 1)] >> 4);
|
|
80
|
+
arr[curByte++] = tmp & 0xFF;
|
|
81
|
+
}
|
|
82
|
+
if (placeHoldersLen === 1) {
|
|
83
|
+
tmp =
|
|
84
|
+
(revLookup[b64.charCodeAt(i)] << 10) |
|
|
85
|
+
(revLookup[b64.charCodeAt(i + 1)] << 4) |
|
|
86
|
+
(revLookup[b64.charCodeAt(i + 2)] >> 2);
|
|
87
|
+
arr[curByte++] = (tmp >> 8) & 0xFF;
|
|
88
|
+
arr[curByte++] = tmp & 0xFF;
|
|
89
|
+
}
|
|
90
|
+
return arr
|
|
91
|
+
}
|
|
92
|
+
function tripletToBase64 (num) {
|
|
93
|
+
return lookup[num >> 18 & 0x3F] +
|
|
94
|
+
lookup[num >> 12 & 0x3F] +
|
|
95
|
+
lookup[num >> 6 & 0x3F] +
|
|
96
|
+
lookup[num & 0x3F]
|
|
97
|
+
}
|
|
98
|
+
function encodeChunk (uint8, start, end) {
|
|
99
|
+
var tmp;
|
|
100
|
+
var output = [];
|
|
101
|
+
for (var i = start; i < end; i += 3) {
|
|
102
|
+
tmp =
|
|
103
|
+
((uint8[i] << 16) & 0xFF0000) +
|
|
104
|
+
((uint8[i + 1] << 8) & 0xFF00) +
|
|
105
|
+
(uint8[i + 2] & 0xFF);
|
|
106
|
+
output.push(tripletToBase64(tmp));
|
|
107
|
+
}
|
|
108
|
+
return output.join('')
|
|
109
|
+
}
|
|
110
|
+
function fromByteArray (uint8) {
|
|
111
|
+
var tmp;
|
|
112
|
+
var len = uint8.length;
|
|
113
|
+
var extraBytes = len % 3;
|
|
114
|
+
var parts = [];
|
|
115
|
+
var maxChunkLength = 16383;
|
|
116
|
+
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
|
117
|
+
parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
|
|
118
|
+
}
|
|
119
|
+
if (extraBytes === 1) {
|
|
120
|
+
tmp = uint8[len - 1];
|
|
121
|
+
parts.push(
|
|
122
|
+
lookup[tmp >> 2] +
|
|
123
|
+
lookup[(tmp << 4) & 0x3F] +
|
|
124
|
+
'=='
|
|
125
|
+
);
|
|
126
|
+
} else if (extraBytes === 2) {
|
|
127
|
+
tmp = (uint8[len - 2] << 8) + uint8[len - 1];
|
|
128
|
+
parts.push(
|
|
129
|
+
lookup[tmp >> 10] +
|
|
130
|
+
lookup[(tmp >> 4) & 0x3F] +
|
|
131
|
+
lookup[(tmp << 2) & 0x3F] +
|
|
132
|
+
'='
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return parts.join('')
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
var ieee754 = {};
|
|
139
|
+
|
|
140
|
+
ieee754.read = function (buffer, offset, isLE, mLen, nBytes) {
|
|
141
|
+
var e, m;
|
|
142
|
+
var eLen = (nBytes * 8) - mLen - 1;
|
|
143
|
+
var eMax = (1 << eLen) - 1;
|
|
144
|
+
var eBias = eMax >> 1;
|
|
145
|
+
var nBits = -7;
|
|
146
|
+
var i = isLE ? (nBytes - 1) : 0;
|
|
147
|
+
var d = isLE ? -1 : 1;
|
|
148
|
+
var s = buffer[offset + i];
|
|
149
|
+
i += d;
|
|
150
|
+
e = s & ((1 << (-nBits)) - 1);
|
|
151
|
+
s >>= (-nBits);
|
|
152
|
+
nBits += eLen;
|
|
153
|
+
for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
|
|
154
|
+
m = e & ((1 << (-nBits)) - 1);
|
|
155
|
+
e >>= (-nBits);
|
|
156
|
+
nBits += mLen;
|
|
157
|
+
for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
|
|
158
|
+
if (e === 0) {
|
|
159
|
+
e = 1 - eBias;
|
|
160
|
+
} else if (e === eMax) {
|
|
161
|
+
return m ? NaN : ((s ? -1 : 1) * Infinity)
|
|
162
|
+
} else {
|
|
163
|
+
m = m + Math.pow(2, mLen);
|
|
164
|
+
e = e - eBias;
|
|
165
|
+
}
|
|
166
|
+
return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
|
|
167
|
+
};
|
|
168
|
+
ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
|
|
169
|
+
var e, m, c;
|
|
170
|
+
var eLen = (nBytes * 8) - mLen - 1;
|
|
171
|
+
var eMax = (1 << eLen) - 1;
|
|
172
|
+
var eBias = eMax >> 1;
|
|
173
|
+
var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
|
|
174
|
+
var i = isLE ? 0 : (nBytes - 1);
|
|
175
|
+
var d = isLE ? 1 : -1;
|
|
176
|
+
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
177
|
+
value = Math.abs(value);
|
|
178
|
+
if (isNaN(value) || value === Infinity) {
|
|
179
|
+
m = isNaN(value) ? 1 : 0;
|
|
180
|
+
e = eMax;
|
|
181
|
+
} else {
|
|
182
|
+
e = Math.floor(Math.log(value) / Math.LN2);
|
|
183
|
+
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
184
|
+
e--;
|
|
185
|
+
c *= 2;
|
|
186
|
+
}
|
|
187
|
+
if (e + eBias >= 1) {
|
|
188
|
+
value += rt / c;
|
|
189
|
+
} else {
|
|
190
|
+
value += rt * Math.pow(2, 1 - eBias);
|
|
191
|
+
}
|
|
192
|
+
if (value * c >= 2) {
|
|
193
|
+
e++;
|
|
194
|
+
c /= 2;
|
|
195
|
+
}
|
|
196
|
+
if (e + eBias >= eMax) {
|
|
197
|
+
m = 0;
|
|
198
|
+
e = eMax;
|
|
199
|
+
} else if (e + eBias >= 1) {
|
|
200
|
+
m = ((value * c) - 1) * Math.pow(2, mLen);
|
|
201
|
+
e = e + eBias;
|
|
202
|
+
} else {
|
|
203
|
+
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
204
|
+
e = 0;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
|
|
208
|
+
e = (e << mLen) | m;
|
|
209
|
+
eLen += mLen;
|
|
210
|
+
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
|
|
211
|
+
buffer[offset + i - d] |= s * 128;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
(function (exports) {
|
|
215
|
+
const base64 = base64Js;
|
|
216
|
+
const ieee754$1 = ieee754;
|
|
217
|
+
const customInspectSymbol =
|
|
218
|
+
(typeof Symbol === 'function' && typeof Symbol['for'] === 'function')
|
|
219
|
+
? Symbol['for']('nodejs.util.inspect.custom')
|
|
220
|
+
: null;
|
|
221
|
+
exports.Buffer = Buffer;
|
|
222
|
+
exports.SlowBuffer = SlowBuffer;
|
|
223
|
+
exports.INSPECT_MAX_BYTES = 50;
|
|
224
|
+
const K_MAX_LENGTH = 0x7fffffff;
|
|
225
|
+
exports.kMaxLength = K_MAX_LENGTH;
|
|
226
|
+
Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport();
|
|
227
|
+
if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
|
|
228
|
+
typeof console.error === 'function') {
|
|
229
|
+
console.error(
|
|
230
|
+
'This browser lacks typed array (Uint8Array) support which is required by ' +
|
|
231
|
+
'`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
function typedArraySupport () {
|
|
235
|
+
try {
|
|
236
|
+
const arr = new Uint8Array(1);
|
|
237
|
+
const proto = { foo: function () { return 42 } };
|
|
238
|
+
Object.setPrototypeOf(proto, Uint8Array.prototype);
|
|
239
|
+
Object.setPrototypeOf(arr, proto);
|
|
240
|
+
return arr.foo() === 42
|
|
241
|
+
} catch (e) {
|
|
242
|
+
return false
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
Object.defineProperty(Buffer.prototype, 'parent', {
|
|
246
|
+
enumerable: true,
|
|
247
|
+
get: function () {
|
|
248
|
+
if (!Buffer.isBuffer(this)) return undefined
|
|
249
|
+
return this.buffer
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
Object.defineProperty(Buffer.prototype, 'offset', {
|
|
253
|
+
enumerable: true,
|
|
254
|
+
get: function () {
|
|
255
|
+
if (!Buffer.isBuffer(this)) return undefined
|
|
256
|
+
return this.byteOffset
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
function createBuffer (length) {
|
|
260
|
+
if (length > K_MAX_LENGTH) {
|
|
261
|
+
throw new RangeError('The value "' + length + '" is invalid for option "size"')
|
|
262
|
+
}
|
|
263
|
+
const buf = new Uint8Array(length);
|
|
264
|
+
Object.setPrototypeOf(buf, Buffer.prototype);
|
|
265
|
+
return buf
|
|
266
|
+
}
|
|
267
|
+
function Buffer (arg, encodingOrOffset, length) {
|
|
268
|
+
if (typeof arg === 'number') {
|
|
269
|
+
if (typeof encodingOrOffset === 'string') {
|
|
270
|
+
throw new TypeError(
|
|
271
|
+
'The "string" argument must be of type string. Received type number'
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
return allocUnsafe(arg)
|
|
275
|
+
}
|
|
276
|
+
return from(arg, encodingOrOffset, length)
|
|
277
|
+
}
|
|
278
|
+
Buffer.poolSize = 8192;
|
|
279
|
+
function from (value, encodingOrOffset, length) {
|
|
280
|
+
if (typeof value === 'string') {
|
|
281
|
+
return fromString(value, encodingOrOffset)
|
|
282
|
+
}
|
|
283
|
+
if (ArrayBuffer.isView(value)) {
|
|
284
|
+
return fromArrayView(value)
|
|
285
|
+
}
|
|
286
|
+
if (value == null) {
|
|
287
|
+
throw new TypeError(
|
|
288
|
+
'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
|
|
289
|
+
'or Array-like Object. Received type ' + (typeof value)
|
|
290
|
+
)
|
|
291
|
+
}
|
|
292
|
+
if (isInstance(value, ArrayBuffer) ||
|
|
293
|
+
(value && isInstance(value.buffer, ArrayBuffer))) {
|
|
294
|
+
return fromArrayBuffer(value, encodingOrOffset, length)
|
|
295
|
+
}
|
|
296
|
+
if (typeof SharedArrayBuffer !== 'undefined' &&
|
|
297
|
+
(isInstance(value, SharedArrayBuffer) ||
|
|
298
|
+
(value && isInstance(value.buffer, SharedArrayBuffer)))) {
|
|
299
|
+
return fromArrayBuffer(value, encodingOrOffset, length)
|
|
300
|
+
}
|
|
301
|
+
if (typeof value === 'number') {
|
|
302
|
+
throw new TypeError(
|
|
303
|
+
'The "value" argument must not be of type number. Received type number'
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
const valueOf = value.valueOf && value.valueOf();
|
|
307
|
+
if (valueOf != null && valueOf !== value) {
|
|
308
|
+
return Buffer.from(valueOf, encodingOrOffset, length)
|
|
309
|
+
}
|
|
310
|
+
const b = fromObject(value);
|
|
311
|
+
if (b) return b
|
|
312
|
+
if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
|
|
313
|
+
typeof value[Symbol.toPrimitive] === 'function') {
|
|
314
|
+
return Buffer.from(value[Symbol.toPrimitive]('string'), encodingOrOffset, length)
|
|
315
|
+
}
|
|
316
|
+
throw new TypeError(
|
|
317
|
+
'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
|
|
318
|
+
'or Array-like Object. Received type ' + (typeof value)
|
|
319
|
+
)
|
|
320
|
+
}
|
|
321
|
+
Buffer.from = function (value, encodingOrOffset, length) {
|
|
322
|
+
return from(value, encodingOrOffset, length)
|
|
323
|
+
};
|
|
324
|
+
Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
|
|
325
|
+
Object.setPrototypeOf(Buffer, Uint8Array);
|
|
326
|
+
function assertSize (size) {
|
|
327
|
+
if (typeof size !== 'number') {
|
|
328
|
+
throw new TypeError('"size" argument must be of type number')
|
|
329
|
+
} else if (size < 0) {
|
|
330
|
+
throw new RangeError('The value "' + size + '" is invalid for option "size"')
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function alloc (size, fill, encoding) {
|
|
334
|
+
assertSize(size);
|
|
335
|
+
if (size <= 0) {
|
|
336
|
+
return createBuffer(size)
|
|
337
|
+
}
|
|
338
|
+
if (fill !== undefined) {
|
|
339
|
+
return typeof encoding === 'string'
|
|
340
|
+
? createBuffer(size).fill(fill, encoding)
|
|
341
|
+
: createBuffer(size).fill(fill)
|
|
342
|
+
}
|
|
343
|
+
return createBuffer(size)
|
|
344
|
+
}
|
|
345
|
+
Buffer.alloc = function (size, fill, encoding) {
|
|
346
|
+
return alloc(size, fill, encoding)
|
|
347
|
+
};
|
|
348
|
+
function allocUnsafe (size) {
|
|
349
|
+
assertSize(size);
|
|
350
|
+
return createBuffer(size < 0 ? 0 : checked(size) | 0)
|
|
351
|
+
}
|
|
352
|
+
Buffer.allocUnsafe = function (size) {
|
|
353
|
+
return allocUnsafe(size)
|
|
354
|
+
};
|
|
355
|
+
Buffer.allocUnsafeSlow = function (size) {
|
|
356
|
+
return allocUnsafe(size)
|
|
357
|
+
};
|
|
358
|
+
function fromString (string, encoding) {
|
|
359
|
+
if (typeof encoding !== 'string' || encoding === '') {
|
|
360
|
+
encoding = 'utf8';
|
|
361
|
+
}
|
|
362
|
+
if (!Buffer.isEncoding(encoding)) {
|
|
363
|
+
throw new TypeError('Unknown encoding: ' + encoding)
|
|
364
|
+
}
|
|
365
|
+
const length = byteLength(string, encoding) | 0;
|
|
366
|
+
let buf = createBuffer(length);
|
|
367
|
+
const actual = buf.write(string, encoding);
|
|
368
|
+
if (actual !== length) {
|
|
369
|
+
buf = buf.slice(0, actual);
|
|
370
|
+
}
|
|
371
|
+
return buf
|
|
372
|
+
}
|
|
373
|
+
function fromArrayLike (array) {
|
|
374
|
+
const length = array.length < 0 ? 0 : checked(array.length) | 0;
|
|
375
|
+
const buf = createBuffer(length);
|
|
376
|
+
for (let i = 0; i < length; i += 1) {
|
|
377
|
+
buf[i] = array[i] & 255;
|
|
378
|
+
}
|
|
379
|
+
return buf
|
|
380
|
+
}
|
|
381
|
+
function fromArrayView (arrayView) {
|
|
382
|
+
if (isInstance(arrayView, Uint8Array)) {
|
|
383
|
+
const copy = new Uint8Array(arrayView);
|
|
384
|
+
return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
|
|
385
|
+
}
|
|
386
|
+
return fromArrayLike(arrayView)
|
|
387
|
+
}
|
|
388
|
+
function fromArrayBuffer (array, byteOffset, length) {
|
|
389
|
+
if (byteOffset < 0 || array.byteLength < byteOffset) {
|
|
390
|
+
throw new RangeError('"offset" is outside of buffer bounds')
|
|
391
|
+
}
|
|
392
|
+
if (array.byteLength < byteOffset + (length || 0)) {
|
|
393
|
+
throw new RangeError('"length" is outside of buffer bounds')
|
|
394
|
+
}
|
|
395
|
+
let buf;
|
|
396
|
+
if (byteOffset === undefined && length === undefined) {
|
|
397
|
+
buf = new Uint8Array(array);
|
|
398
|
+
} else if (length === undefined) {
|
|
399
|
+
buf = new Uint8Array(array, byteOffset);
|
|
400
|
+
} else {
|
|
401
|
+
buf = new Uint8Array(array, byteOffset, length);
|
|
402
|
+
}
|
|
403
|
+
Object.setPrototypeOf(buf, Buffer.prototype);
|
|
404
|
+
return buf
|
|
405
|
+
}
|
|
406
|
+
function fromObject (obj) {
|
|
407
|
+
if (Buffer.isBuffer(obj)) {
|
|
408
|
+
const len = checked(obj.length) | 0;
|
|
409
|
+
const buf = createBuffer(len);
|
|
410
|
+
if (buf.length === 0) {
|
|
411
|
+
return buf
|
|
412
|
+
}
|
|
413
|
+
obj.copy(buf, 0, 0, len);
|
|
414
|
+
return buf
|
|
415
|
+
}
|
|
416
|
+
if (obj.length !== undefined) {
|
|
417
|
+
if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
|
|
418
|
+
return createBuffer(0)
|
|
419
|
+
}
|
|
420
|
+
return fromArrayLike(obj)
|
|
421
|
+
}
|
|
422
|
+
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
|
|
423
|
+
return fromArrayLike(obj.data)
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
function checked (length) {
|
|
427
|
+
if (length >= K_MAX_LENGTH) {
|
|
428
|
+
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
|
|
429
|
+
'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
|
|
430
|
+
}
|
|
431
|
+
return length | 0
|
|
432
|
+
}
|
|
433
|
+
function SlowBuffer (length) {
|
|
434
|
+
if (+length != length) {
|
|
435
|
+
length = 0;
|
|
436
|
+
}
|
|
437
|
+
return Buffer.alloc(+length)
|
|
438
|
+
}
|
|
439
|
+
Buffer.isBuffer = function isBuffer (b) {
|
|
440
|
+
return b != null && b._isBuffer === true &&
|
|
441
|
+
b !== Buffer.prototype
|
|
442
|
+
};
|
|
443
|
+
Buffer.compare = function compare (a, b) {
|
|
444
|
+
if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength);
|
|
445
|
+
if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength);
|
|
446
|
+
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
|
|
447
|
+
throw new TypeError(
|
|
448
|
+
'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
|
|
449
|
+
)
|
|
450
|
+
}
|
|
451
|
+
if (a === b) return 0
|
|
452
|
+
let x = a.length;
|
|
453
|
+
let y = b.length;
|
|
454
|
+
for (let i = 0, len = Math.min(x, y); i < len; ++i) {
|
|
455
|
+
if (a[i] !== b[i]) {
|
|
456
|
+
x = a[i];
|
|
457
|
+
y = b[i];
|
|
458
|
+
break
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (x < y) return -1
|
|
462
|
+
if (y < x) return 1
|
|
463
|
+
return 0
|
|
464
|
+
};
|
|
465
|
+
Buffer.isEncoding = function isEncoding (encoding) {
|
|
466
|
+
switch (String(encoding).toLowerCase()) {
|
|
467
|
+
case 'hex':
|
|
468
|
+
case 'utf8':
|
|
469
|
+
case 'utf-8':
|
|
470
|
+
case 'ascii':
|
|
471
|
+
case 'latin1':
|
|
472
|
+
case 'binary':
|
|
473
|
+
case 'base64':
|
|
474
|
+
case 'ucs2':
|
|
475
|
+
case 'ucs-2':
|
|
476
|
+
case 'utf16le':
|
|
477
|
+
case 'utf-16le':
|
|
478
|
+
return true
|
|
479
|
+
default:
|
|
480
|
+
return false
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
Buffer.concat = function concat (list, length) {
|
|
484
|
+
if (!Array.isArray(list)) {
|
|
485
|
+
throw new TypeError('"list" argument must be an Array of Buffers')
|
|
486
|
+
}
|
|
487
|
+
if (list.length === 0) {
|
|
488
|
+
return Buffer.alloc(0)
|
|
489
|
+
}
|
|
490
|
+
let i;
|
|
491
|
+
if (length === undefined) {
|
|
492
|
+
length = 0;
|
|
493
|
+
for (i = 0; i < list.length; ++i) {
|
|
494
|
+
length += list[i].length;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
498
|
+
let pos = 0;
|
|
499
|
+
for (i = 0; i < list.length; ++i) {
|
|
500
|
+
let buf = list[i];
|
|
501
|
+
if (isInstance(buf, Uint8Array)) {
|
|
502
|
+
if (pos + buf.length > buffer.length) {
|
|
503
|
+
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
|
|
504
|
+
buf.copy(buffer, pos);
|
|
505
|
+
} else {
|
|
506
|
+
Uint8Array.prototype.set.call(
|
|
507
|
+
buffer,
|
|
508
|
+
buf,
|
|
509
|
+
pos
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
} else if (!Buffer.isBuffer(buf)) {
|
|
513
|
+
throw new TypeError('"list" argument must be an Array of Buffers')
|
|
514
|
+
} else {
|
|
515
|
+
buf.copy(buffer, pos);
|
|
516
|
+
}
|
|
517
|
+
pos += buf.length;
|
|
518
|
+
}
|
|
519
|
+
return buffer
|
|
520
|
+
};
|
|
521
|
+
function byteLength (string, encoding) {
|
|
522
|
+
if (Buffer.isBuffer(string)) {
|
|
523
|
+
return string.length
|
|
524
|
+
}
|
|
525
|
+
if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
|
|
526
|
+
return string.byteLength
|
|
527
|
+
}
|
|
528
|
+
if (typeof string !== 'string') {
|
|
529
|
+
throw new TypeError(
|
|
530
|
+
'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
|
|
531
|
+
'Received type ' + typeof string
|
|
532
|
+
)
|
|
533
|
+
}
|
|
534
|
+
const len = string.length;
|
|
535
|
+
const mustMatch = (arguments.length > 2 && arguments[2] === true);
|
|
536
|
+
if (!mustMatch && len === 0) return 0
|
|
537
|
+
let loweredCase = false;
|
|
538
|
+
for (;;) {
|
|
539
|
+
switch (encoding) {
|
|
540
|
+
case 'ascii':
|
|
541
|
+
case 'latin1':
|
|
542
|
+
case 'binary':
|
|
543
|
+
return len
|
|
544
|
+
case 'utf8':
|
|
545
|
+
case 'utf-8':
|
|
546
|
+
return utf8ToBytes(string).length
|
|
547
|
+
case 'ucs2':
|
|
548
|
+
case 'ucs-2':
|
|
549
|
+
case 'utf16le':
|
|
550
|
+
case 'utf-16le':
|
|
551
|
+
return len * 2
|
|
552
|
+
case 'hex':
|
|
553
|
+
return len >>> 1
|
|
554
|
+
case 'base64':
|
|
555
|
+
return base64ToBytes(string).length
|
|
556
|
+
default:
|
|
557
|
+
if (loweredCase) {
|
|
558
|
+
return mustMatch ? -1 : utf8ToBytes(string).length
|
|
559
|
+
}
|
|
560
|
+
encoding = ('' + encoding).toLowerCase();
|
|
561
|
+
loweredCase = true;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
Buffer.byteLength = byteLength;
|
|
566
|
+
function slowToString (encoding, start, end) {
|
|
567
|
+
let loweredCase = false;
|
|
568
|
+
if (start === undefined || start < 0) {
|
|
569
|
+
start = 0;
|
|
570
|
+
}
|
|
571
|
+
if (start > this.length) {
|
|
572
|
+
return ''
|
|
573
|
+
}
|
|
574
|
+
if (end === undefined || end > this.length) {
|
|
575
|
+
end = this.length;
|
|
576
|
+
}
|
|
577
|
+
if (end <= 0) {
|
|
578
|
+
return ''
|
|
579
|
+
}
|
|
580
|
+
end >>>= 0;
|
|
581
|
+
start >>>= 0;
|
|
582
|
+
if (end <= start) {
|
|
583
|
+
return ''
|
|
584
|
+
}
|
|
585
|
+
if (!encoding) encoding = 'utf8';
|
|
586
|
+
while (true) {
|
|
587
|
+
switch (encoding) {
|
|
588
|
+
case 'hex':
|
|
589
|
+
return hexSlice(this, start, end)
|
|
590
|
+
case 'utf8':
|
|
591
|
+
case 'utf-8':
|
|
592
|
+
return utf8Slice(this, start, end)
|
|
593
|
+
case 'ascii':
|
|
594
|
+
return asciiSlice(this, start, end)
|
|
595
|
+
case 'latin1':
|
|
596
|
+
case 'binary':
|
|
597
|
+
return latin1Slice(this, start, end)
|
|
598
|
+
case 'base64':
|
|
599
|
+
return base64Slice(this, start, end)
|
|
600
|
+
case 'ucs2':
|
|
601
|
+
case 'ucs-2':
|
|
602
|
+
case 'utf16le':
|
|
603
|
+
case 'utf-16le':
|
|
604
|
+
return utf16leSlice(this, start, end)
|
|
605
|
+
default:
|
|
606
|
+
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
|
607
|
+
encoding = (encoding + '').toLowerCase();
|
|
608
|
+
loweredCase = true;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
Buffer.prototype._isBuffer = true;
|
|
613
|
+
function swap (b, n, m) {
|
|
614
|
+
const i = b[n];
|
|
615
|
+
b[n] = b[m];
|
|
616
|
+
b[m] = i;
|
|
617
|
+
}
|
|
618
|
+
Buffer.prototype.swap16 = function swap16 () {
|
|
619
|
+
const len = this.length;
|
|
620
|
+
if (len % 2 !== 0) {
|
|
621
|
+
throw new RangeError('Buffer size must be a multiple of 16-bits')
|
|
622
|
+
}
|
|
623
|
+
for (let i = 0; i < len; i += 2) {
|
|
624
|
+
swap(this, i, i + 1);
|
|
625
|
+
}
|
|
626
|
+
return this
|
|
627
|
+
};
|
|
628
|
+
Buffer.prototype.swap32 = function swap32 () {
|
|
629
|
+
const len = this.length;
|
|
630
|
+
if (len % 4 !== 0) {
|
|
631
|
+
throw new RangeError('Buffer size must be a multiple of 32-bits')
|
|
632
|
+
}
|
|
633
|
+
for (let i = 0; i < len; i += 4) {
|
|
634
|
+
swap(this, i, i + 3);
|
|
635
|
+
swap(this, i + 1, i + 2);
|
|
636
|
+
}
|
|
637
|
+
return this
|
|
638
|
+
};
|
|
639
|
+
Buffer.prototype.swap64 = function swap64 () {
|
|
640
|
+
const len = this.length;
|
|
641
|
+
if (len % 8 !== 0) {
|
|
642
|
+
throw new RangeError('Buffer size must be a multiple of 64-bits')
|
|
643
|
+
}
|
|
644
|
+
for (let i = 0; i < len; i += 8) {
|
|
645
|
+
swap(this, i, i + 7);
|
|
646
|
+
swap(this, i + 1, i + 6);
|
|
647
|
+
swap(this, i + 2, i + 5);
|
|
648
|
+
swap(this, i + 3, i + 4);
|
|
649
|
+
}
|
|
650
|
+
return this
|
|
651
|
+
};
|
|
652
|
+
Buffer.prototype.toString = function toString () {
|
|
653
|
+
const length = this.length;
|
|
654
|
+
if (length === 0) return ''
|
|
655
|
+
if (arguments.length === 0) return utf8Slice(this, 0, length)
|
|
656
|
+
return slowToString.apply(this, arguments)
|
|
657
|
+
};
|
|
658
|
+
Buffer.prototype.toLocaleString = Buffer.prototype.toString;
|
|
659
|
+
Buffer.prototype.equals = function equals (b) {
|
|
660
|
+
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
|
|
661
|
+
if (this === b) return true
|
|
662
|
+
return Buffer.compare(this, b) === 0
|
|
663
|
+
};
|
|
664
|
+
Buffer.prototype.inspect = function inspect () {
|
|
665
|
+
let str = '';
|
|
666
|
+
const max = exports.INSPECT_MAX_BYTES;
|
|
667
|
+
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
|
|
668
|
+
if (this.length > max) str += ' ... ';
|
|
669
|
+
return '<Buffer ' + str + '>'
|
|
670
|
+
};
|
|
671
|
+
if (customInspectSymbol) {
|
|
672
|
+
Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
|
|
673
|
+
}
|
|
674
|
+
Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
|
|
675
|
+
if (isInstance(target, Uint8Array)) {
|
|
676
|
+
target = Buffer.from(target, target.offset, target.byteLength);
|
|
677
|
+
}
|
|
678
|
+
if (!Buffer.isBuffer(target)) {
|
|
679
|
+
throw new TypeError(
|
|
680
|
+
'The "target" argument must be one of type Buffer or Uint8Array. ' +
|
|
681
|
+
'Received type ' + (typeof target)
|
|
682
|
+
)
|
|
683
|
+
}
|
|
684
|
+
if (start === undefined) {
|
|
685
|
+
start = 0;
|
|
686
|
+
}
|
|
687
|
+
if (end === undefined) {
|
|
688
|
+
end = target ? target.length : 0;
|
|
689
|
+
}
|
|
690
|
+
if (thisStart === undefined) {
|
|
691
|
+
thisStart = 0;
|
|
692
|
+
}
|
|
693
|
+
if (thisEnd === undefined) {
|
|
694
|
+
thisEnd = this.length;
|
|
695
|
+
}
|
|
696
|
+
if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
|
|
697
|
+
throw new RangeError('out of range index')
|
|
698
|
+
}
|
|
699
|
+
if (thisStart >= thisEnd && start >= end) {
|
|
700
|
+
return 0
|
|
701
|
+
}
|
|
702
|
+
if (thisStart >= thisEnd) {
|
|
703
|
+
return -1
|
|
704
|
+
}
|
|
705
|
+
if (start >= end) {
|
|
706
|
+
return 1
|
|
707
|
+
}
|
|
708
|
+
start >>>= 0;
|
|
709
|
+
end >>>= 0;
|
|
710
|
+
thisStart >>>= 0;
|
|
711
|
+
thisEnd >>>= 0;
|
|
712
|
+
if (this === target) return 0
|
|
713
|
+
let x = thisEnd - thisStart;
|
|
714
|
+
let y = end - start;
|
|
715
|
+
const len = Math.min(x, y);
|
|
716
|
+
const thisCopy = this.slice(thisStart, thisEnd);
|
|
717
|
+
const targetCopy = target.slice(start, end);
|
|
718
|
+
for (let i = 0; i < len; ++i) {
|
|
719
|
+
if (thisCopy[i] !== targetCopy[i]) {
|
|
720
|
+
x = thisCopy[i];
|
|
721
|
+
y = targetCopy[i];
|
|
722
|
+
break
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
if (x < y) return -1
|
|
726
|
+
if (y < x) return 1
|
|
727
|
+
return 0
|
|
728
|
+
};
|
|
729
|
+
function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
|
|
730
|
+
if (buffer.length === 0) return -1
|
|
731
|
+
if (typeof byteOffset === 'string') {
|
|
732
|
+
encoding = byteOffset;
|
|
733
|
+
byteOffset = 0;
|
|
734
|
+
} else if (byteOffset > 0x7fffffff) {
|
|
735
|
+
byteOffset = 0x7fffffff;
|
|
736
|
+
} else if (byteOffset < -0x80000000) {
|
|
737
|
+
byteOffset = -0x80000000;
|
|
738
|
+
}
|
|
739
|
+
byteOffset = +byteOffset;
|
|
740
|
+
if (numberIsNaN(byteOffset)) {
|
|
741
|
+
byteOffset = dir ? 0 : (buffer.length - 1);
|
|
742
|
+
}
|
|
743
|
+
if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
|
|
744
|
+
if (byteOffset >= buffer.length) {
|
|
745
|
+
if (dir) return -1
|
|
746
|
+
else byteOffset = buffer.length - 1;
|
|
747
|
+
} else if (byteOffset < 0) {
|
|
748
|
+
if (dir) byteOffset = 0;
|
|
749
|
+
else return -1
|
|
750
|
+
}
|
|
751
|
+
if (typeof val === 'string') {
|
|
752
|
+
val = Buffer.from(val, encoding);
|
|
753
|
+
}
|
|
754
|
+
if (Buffer.isBuffer(val)) {
|
|
755
|
+
if (val.length === 0) {
|
|
756
|
+
return -1
|
|
757
|
+
}
|
|
758
|
+
return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
|
|
759
|
+
} else if (typeof val === 'number') {
|
|
760
|
+
val = val & 0xFF;
|
|
761
|
+
if (typeof Uint8Array.prototype.indexOf === 'function') {
|
|
762
|
+
if (dir) {
|
|
763
|
+
return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
|
|
764
|
+
} else {
|
|
765
|
+
return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
|
|
769
|
+
}
|
|
770
|
+
throw new TypeError('val must be string, number or Buffer')
|
|
771
|
+
}
|
|
772
|
+
function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
|
|
773
|
+
let indexSize = 1;
|
|
774
|
+
let arrLength = arr.length;
|
|
775
|
+
let valLength = val.length;
|
|
776
|
+
if (encoding !== undefined) {
|
|
777
|
+
encoding = String(encoding).toLowerCase();
|
|
778
|
+
if (encoding === 'ucs2' || encoding === 'ucs-2' ||
|
|
779
|
+
encoding === 'utf16le' || encoding === 'utf-16le') {
|
|
780
|
+
if (arr.length < 2 || val.length < 2) {
|
|
781
|
+
return -1
|
|
782
|
+
}
|
|
783
|
+
indexSize = 2;
|
|
784
|
+
arrLength /= 2;
|
|
785
|
+
valLength /= 2;
|
|
786
|
+
byteOffset /= 2;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
function read (buf, i) {
|
|
790
|
+
if (indexSize === 1) {
|
|
791
|
+
return buf[i]
|
|
792
|
+
} else {
|
|
793
|
+
return buf.readUInt16BE(i * indexSize)
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
let i;
|
|
797
|
+
if (dir) {
|
|
798
|
+
let foundIndex = -1;
|
|
799
|
+
for (i = byteOffset; i < arrLength; i++) {
|
|
800
|
+
if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
|
|
801
|
+
if (foundIndex === -1) foundIndex = i;
|
|
802
|
+
if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
|
|
803
|
+
} else {
|
|
804
|
+
if (foundIndex !== -1) i -= i - foundIndex;
|
|
805
|
+
foundIndex = -1;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
} else {
|
|
809
|
+
if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
|
|
810
|
+
for (i = byteOffset; i >= 0; i--) {
|
|
811
|
+
let found = true;
|
|
812
|
+
for (let j = 0; j < valLength; j++) {
|
|
813
|
+
if (read(arr, i + j) !== read(val, j)) {
|
|
814
|
+
found = false;
|
|
815
|
+
break
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
if (found) return i
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
return -1
|
|
822
|
+
}
|
|
823
|
+
Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
|
|
824
|
+
return this.indexOf(val, byteOffset, encoding) !== -1
|
|
825
|
+
};
|
|
826
|
+
Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
|
|
827
|
+
return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
|
|
828
|
+
};
|
|
829
|
+
Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
|
|
830
|
+
return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
|
|
831
|
+
};
|
|
832
|
+
function hexWrite (buf, string, offset, length) {
|
|
833
|
+
offset = Number(offset) || 0;
|
|
834
|
+
const remaining = buf.length - offset;
|
|
835
|
+
if (!length) {
|
|
836
|
+
length = remaining;
|
|
837
|
+
} else {
|
|
838
|
+
length = Number(length);
|
|
839
|
+
if (length > remaining) {
|
|
840
|
+
length = remaining;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
const strLen = string.length;
|
|
844
|
+
if (length > strLen / 2) {
|
|
845
|
+
length = strLen / 2;
|
|
846
|
+
}
|
|
847
|
+
let i;
|
|
848
|
+
for (i = 0; i < length; ++i) {
|
|
849
|
+
const parsed = parseInt(string.substr(i * 2, 2), 16);
|
|
850
|
+
if (numberIsNaN(parsed)) return i
|
|
851
|
+
buf[offset + i] = parsed;
|
|
852
|
+
}
|
|
853
|
+
return i
|
|
854
|
+
}
|
|
855
|
+
function utf8Write (buf, string, offset, length) {
|
|
856
|
+
return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
|
|
857
|
+
}
|
|
858
|
+
function asciiWrite (buf, string, offset, length) {
|
|
859
|
+
return blitBuffer(asciiToBytes(string), buf, offset, length)
|
|
860
|
+
}
|
|
861
|
+
function base64Write (buf, string, offset, length) {
|
|
862
|
+
return blitBuffer(base64ToBytes(string), buf, offset, length)
|
|
863
|
+
}
|
|
864
|
+
function ucs2Write (buf, string, offset, length) {
|
|
865
|
+
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
|
|
866
|
+
}
|
|
867
|
+
Buffer.prototype.write = function write (string, offset, length, encoding) {
|
|
868
|
+
if (offset === undefined) {
|
|
869
|
+
encoding = 'utf8';
|
|
870
|
+
length = this.length;
|
|
871
|
+
offset = 0;
|
|
872
|
+
} else if (length === undefined && typeof offset === 'string') {
|
|
873
|
+
encoding = offset;
|
|
874
|
+
length = this.length;
|
|
875
|
+
offset = 0;
|
|
876
|
+
} else if (isFinite(offset)) {
|
|
877
|
+
offset = offset >>> 0;
|
|
878
|
+
if (isFinite(length)) {
|
|
879
|
+
length = length >>> 0;
|
|
880
|
+
if (encoding === undefined) encoding = 'utf8';
|
|
881
|
+
} else {
|
|
882
|
+
encoding = length;
|
|
883
|
+
length = undefined;
|
|
884
|
+
}
|
|
885
|
+
} else {
|
|
886
|
+
throw new Error(
|
|
887
|
+
'Buffer.write(string, encoding, offset[, length]) is no longer supported'
|
|
888
|
+
)
|
|
889
|
+
}
|
|
890
|
+
const remaining = this.length - offset;
|
|
891
|
+
if (length === undefined || length > remaining) length = remaining;
|
|
892
|
+
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
|
|
893
|
+
throw new RangeError('Attempt to write outside buffer bounds')
|
|
894
|
+
}
|
|
895
|
+
if (!encoding) encoding = 'utf8';
|
|
896
|
+
let loweredCase = false;
|
|
897
|
+
for (;;) {
|
|
898
|
+
switch (encoding) {
|
|
899
|
+
case 'hex':
|
|
900
|
+
return hexWrite(this, string, offset, length)
|
|
901
|
+
case 'utf8':
|
|
902
|
+
case 'utf-8':
|
|
903
|
+
return utf8Write(this, string, offset, length)
|
|
904
|
+
case 'ascii':
|
|
905
|
+
case 'latin1':
|
|
906
|
+
case 'binary':
|
|
907
|
+
return asciiWrite(this, string, offset, length)
|
|
908
|
+
case 'base64':
|
|
909
|
+
return base64Write(this, string, offset, length)
|
|
910
|
+
case 'ucs2':
|
|
911
|
+
case 'ucs-2':
|
|
912
|
+
case 'utf16le':
|
|
913
|
+
case 'utf-16le':
|
|
914
|
+
return ucs2Write(this, string, offset, length)
|
|
915
|
+
default:
|
|
916
|
+
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
|
917
|
+
encoding = ('' + encoding).toLowerCase();
|
|
918
|
+
loweredCase = true;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
};
|
|
922
|
+
Buffer.prototype.toJSON = function toJSON () {
|
|
923
|
+
return {
|
|
924
|
+
type: 'Buffer',
|
|
925
|
+
data: Array.prototype.slice.call(this._arr || this, 0)
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
function base64Slice (buf, start, end) {
|
|
929
|
+
if (start === 0 && end === buf.length) {
|
|
930
|
+
return base64.fromByteArray(buf)
|
|
931
|
+
} else {
|
|
932
|
+
return base64.fromByteArray(buf.slice(start, end))
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
function utf8Slice (buf, start, end) {
|
|
936
|
+
end = Math.min(buf.length, end);
|
|
937
|
+
const res = [];
|
|
938
|
+
let i = start;
|
|
939
|
+
while (i < end) {
|
|
940
|
+
const firstByte = buf[i];
|
|
941
|
+
let codePoint = null;
|
|
942
|
+
let bytesPerSequence = (firstByte > 0xEF)
|
|
943
|
+
? 4
|
|
944
|
+
: (firstByte > 0xDF)
|
|
945
|
+
? 3
|
|
946
|
+
: (firstByte > 0xBF)
|
|
947
|
+
? 2
|
|
948
|
+
: 1;
|
|
949
|
+
if (i + bytesPerSequence <= end) {
|
|
950
|
+
let secondByte, thirdByte, fourthByte, tempCodePoint;
|
|
951
|
+
switch (bytesPerSequence) {
|
|
952
|
+
case 1:
|
|
953
|
+
if (firstByte < 0x80) {
|
|
954
|
+
codePoint = firstByte;
|
|
955
|
+
}
|
|
956
|
+
break
|
|
957
|
+
case 2:
|
|
958
|
+
secondByte = buf[i + 1];
|
|
959
|
+
if ((secondByte & 0xC0) === 0x80) {
|
|
960
|
+
tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
|
|
961
|
+
if (tempCodePoint > 0x7F) {
|
|
962
|
+
codePoint = tempCodePoint;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
break
|
|
966
|
+
case 3:
|
|
967
|
+
secondByte = buf[i + 1];
|
|
968
|
+
thirdByte = buf[i + 2];
|
|
969
|
+
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
|
|
970
|
+
tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
|
|
971
|
+
if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
|
|
972
|
+
codePoint = tempCodePoint;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
break
|
|
976
|
+
case 4:
|
|
977
|
+
secondByte = buf[i + 1];
|
|
978
|
+
thirdByte = buf[i + 2];
|
|
979
|
+
fourthByte = buf[i + 3];
|
|
980
|
+
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
|
|
981
|
+
tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
|
|
982
|
+
if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
|
|
983
|
+
codePoint = tempCodePoint;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
if (codePoint === null) {
|
|
989
|
+
codePoint = 0xFFFD;
|
|
990
|
+
bytesPerSequence = 1;
|
|
991
|
+
} else if (codePoint > 0xFFFF) {
|
|
992
|
+
codePoint -= 0x10000;
|
|
993
|
+
res.push(codePoint >>> 10 & 0x3FF | 0xD800);
|
|
994
|
+
codePoint = 0xDC00 | codePoint & 0x3FF;
|
|
995
|
+
}
|
|
996
|
+
res.push(codePoint);
|
|
997
|
+
i += bytesPerSequence;
|
|
998
|
+
}
|
|
999
|
+
return decodeCodePointsArray(res)
|
|
1000
|
+
}
|
|
1001
|
+
const MAX_ARGUMENTS_LENGTH = 0x1000;
|
|
1002
|
+
function decodeCodePointsArray (codePoints) {
|
|
1003
|
+
const len = codePoints.length;
|
|
1004
|
+
if (len <= MAX_ARGUMENTS_LENGTH) {
|
|
1005
|
+
return String.fromCharCode.apply(String, codePoints)
|
|
1006
|
+
}
|
|
1007
|
+
let res = '';
|
|
1008
|
+
let i = 0;
|
|
1009
|
+
while (i < len) {
|
|
1010
|
+
res += String.fromCharCode.apply(
|
|
1011
|
+
String,
|
|
1012
|
+
codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
|
|
1013
|
+
);
|
|
1014
|
+
}
|
|
1015
|
+
return res
|
|
1016
|
+
}
|
|
1017
|
+
function asciiSlice (buf, start, end) {
|
|
1018
|
+
let ret = '';
|
|
1019
|
+
end = Math.min(buf.length, end);
|
|
1020
|
+
for (let i = start; i < end; ++i) {
|
|
1021
|
+
ret += String.fromCharCode(buf[i] & 0x7F);
|
|
1022
|
+
}
|
|
1023
|
+
return ret
|
|
1024
|
+
}
|
|
1025
|
+
function latin1Slice (buf, start, end) {
|
|
1026
|
+
let ret = '';
|
|
1027
|
+
end = Math.min(buf.length, end);
|
|
1028
|
+
for (let i = start; i < end; ++i) {
|
|
1029
|
+
ret += String.fromCharCode(buf[i]);
|
|
1030
|
+
}
|
|
1031
|
+
return ret
|
|
1032
|
+
}
|
|
1033
|
+
function hexSlice (buf, start, end) {
|
|
1034
|
+
const len = buf.length;
|
|
1035
|
+
if (!start || start < 0) start = 0;
|
|
1036
|
+
if (!end || end < 0 || end > len) end = len;
|
|
1037
|
+
let out = '';
|
|
1038
|
+
for (let i = start; i < end; ++i) {
|
|
1039
|
+
out += hexSliceLookupTable[buf[i]];
|
|
1040
|
+
}
|
|
1041
|
+
return out
|
|
1042
|
+
}
|
|
1043
|
+
function utf16leSlice (buf, start, end) {
|
|
1044
|
+
const bytes = buf.slice(start, end);
|
|
1045
|
+
let res = '';
|
|
1046
|
+
for (let i = 0; i < bytes.length - 1; i += 2) {
|
|
1047
|
+
res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256));
|
|
1048
|
+
}
|
|
1049
|
+
return res
|
|
1050
|
+
}
|
|
1051
|
+
Buffer.prototype.slice = function slice (start, end) {
|
|
1052
|
+
const len = this.length;
|
|
1053
|
+
start = ~~start;
|
|
1054
|
+
end = end === undefined ? len : ~~end;
|
|
1055
|
+
if (start < 0) {
|
|
1056
|
+
start += len;
|
|
1057
|
+
if (start < 0) start = 0;
|
|
1058
|
+
} else if (start > len) {
|
|
1059
|
+
start = len;
|
|
1060
|
+
}
|
|
1061
|
+
if (end < 0) {
|
|
1062
|
+
end += len;
|
|
1063
|
+
if (end < 0) end = 0;
|
|
1064
|
+
} else if (end > len) {
|
|
1065
|
+
end = len;
|
|
1066
|
+
}
|
|
1067
|
+
if (end < start) end = start;
|
|
1068
|
+
const newBuf = this.subarray(start, end);
|
|
1069
|
+
Object.setPrototypeOf(newBuf, Buffer.prototype);
|
|
1070
|
+
return newBuf
|
|
1071
|
+
};
|
|
1072
|
+
function checkOffset (offset, ext, length) {
|
|
1073
|
+
if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
|
|
1074
|
+
if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
|
|
1075
|
+
}
|
|
1076
|
+
Buffer.prototype.readUintLE =
|
|
1077
|
+
Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
|
|
1078
|
+
offset = offset >>> 0;
|
|
1079
|
+
byteLength = byteLength >>> 0;
|
|
1080
|
+
if (!noAssert) checkOffset(offset, byteLength, this.length);
|
|
1081
|
+
let val = this[offset];
|
|
1082
|
+
let mul = 1;
|
|
1083
|
+
let i = 0;
|
|
1084
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1085
|
+
val += this[offset + i] * mul;
|
|
1086
|
+
}
|
|
1087
|
+
return val
|
|
1088
|
+
};
|
|
1089
|
+
Buffer.prototype.readUintBE =
|
|
1090
|
+
Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
|
|
1091
|
+
offset = offset >>> 0;
|
|
1092
|
+
byteLength = byteLength >>> 0;
|
|
1093
|
+
if (!noAssert) {
|
|
1094
|
+
checkOffset(offset, byteLength, this.length);
|
|
1095
|
+
}
|
|
1096
|
+
let val = this[offset + --byteLength];
|
|
1097
|
+
let mul = 1;
|
|
1098
|
+
while (byteLength > 0 && (mul *= 0x100)) {
|
|
1099
|
+
val += this[offset + --byteLength] * mul;
|
|
1100
|
+
}
|
|
1101
|
+
return val
|
|
1102
|
+
};
|
|
1103
|
+
Buffer.prototype.readUint8 =
|
|
1104
|
+
Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
|
|
1105
|
+
offset = offset >>> 0;
|
|
1106
|
+
if (!noAssert) checkOffset(offset, 1, this.length);
|
|
1107
|
+
return this[offset]
|
|
1108
|
+
};
|
|
1109
|
+
Buffer.prototype.readUint16LE =
|
|
1110
|
+
Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
|
|
1111
|
+
offset = offset >>> 0;
|
|
1112
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1113
|
+
return this[offset] | (this[offset + 1] << 8)
|
|
1114
|
+
};
|
|
1115
|
+
Buffer.prototype.readUint16BE =
|
|
1116
|
+
Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
|
|
1117
|
+
offset = offset >>> 0;
|
|
1118
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1119
|
+
return (this[offset] << 8) | this[offset + 1]
|
|
1120
|
+
};
|
|
1121
|
+
Buffer.prototype.readUint32LE =
|
|
1122
|
+
Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
|
|
1123
|
+
offset = offset >>> 0;
|
|
1124
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1125
|
+
return ((this[offset]) |
|
|
1126
|
+
(this[offset + 1] << 8) |
|
|
1127
|
+
(this[offset + 2] << 16)) +
|
|
1128
|
+
(this[offset + 3] * 0x1000000)
|
|
1129
|
+
};
|
|
1130
|
+
Buffer.prototype.readUint32BE =
|
|
1131
|
+
Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
|
|
1132
|
+
offset = offset >>> 0;
|
|
1133
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1134
|
+
return (this[offset] * 0x1000000) +
|
|
1135
|
+
((this[offset + 1] << 16) |
|
|
1136
|
+
(this[offset + 2] << 8) |
|
|
1137
|
+
this[offset + 3])
|
|
1138
|
+
};
|
|
1139
|
+
Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE (offset) {
|
|
1140
|
+
offset = offset >>> 0;
|
|
1141
|
+
validateNumber(offset, 'offset');
|
|
1142
|
+
const first = this[offset];
|
|
1143
|
+
const last = this[offset + 7];
|
|
1144
|
+
if (first === undefined || last === undefined) {
|
|
1145
|
+
boundsError(offset, this.length - 8);
|
|
1146
|
+
}
|
|
1147
|
+
const lo = first +
|
|
1148
|
+
this[++offset] * 2 ** 8 +
|
|
1149
|
+
this[++offset] * 2 ** 16 +
|
|
1150
|
+
this[++offset] * 2 ** 24;
|
|
1151
|
+
const hi = this[++offset] +
|
|
1152
|
+
this[++offset] * 2 ** 8 +
|
|
1153
|
+
this[++offset] * 2 ** 16 +
|
|
1154
|
+
last * 2 ** 24;
|
|
1155
|
+
return BigInt(lo) + (BigInt(hi) << BigInt(32))
|
|
1156
|
+
});
|
|
1157
|
+
Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE (offset) {
|
|
1158
|
+
offset = offset >>> 0;
|
|
1159
|
+
validateNumber(offset, 'offset');
|
|
1160
|
+
const first = this[offset];
|
|
1161
|
+
const last = this[offset + 7];
|
|
1162
|
+
if (first === undefined || last === undefined) {
|
|
1163
|
+
boundsError(offset, this.length - 8);
|
|
1164
|
+
}
|
|
1165
|
+
const hi = first * 2 ** 24 +
|
|
1166
|
+
this[++offset] * 2 ** 16 +
|
|
1167
|
+
this[++offset] * 2 ** 8 +
|
|
1168
|
+
this[++offset];
|
|
1169
|
+
const lo = this[++offset] * 2 ** 24 +
|
|
1170
|
+
this[++offset] * 2 ** 16 +
|
|
1171
|
+
this[++offset] * 2 ** 8 +
|
|
1172
|
+
last;
|
|
1173
|
+
return (BigInt(hi) << BigInt(32)) + BigInt(lo)
|
|
1174
|
+
});
|
|
1175
|
+
Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
|
|
1176
|
+
offset = offset >>> 0;
|
|
1177
|
+
byteLength = byteLength >>> 0;
|
|
1178
|
+
if (!noAssert) checkOffset(offset, byteLength, this.length);
|
|
1179
|
+
let val = this[offset];
|
|
1180
|
+
let mul = 1;
|
|
1181
|
+
let i = 0;
|
|
1182
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1183
|
+
val += this[offset + i] * mul;
|
|
1184
|
+
}
|
|
1185
|
+
mul *= 0x80;
|
|
1186
|
+
if (val >= mul) val -= Math.pow(2, 8 * byteLength);
|
|
1187
|
+
return val
|
|
1188
|
+
};
|
|
1189
|
+
Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
|
|
1190
|
+
offset = offset >>> 0;
|
|
1191
|
+
byteLength = byteLength >>> 0;
|
|
1192
|
+
if (!noAssert) checkOffset(offset, byteLength, this.length);
|
|
1193
|
+
let i = byteLength;
|
|
1194
|
+
let mul = 1;
|
|
1195
|
+
let val = this[offset + --i];
|
|
1196
|
+
while (i > 0 && (mul *= 0x100)) {
|
|
1197
|
+
val += this[offset + --i] * mul;
|
|
1198
|
+
}
|
|
1199
|
+
mul *= 0x80;
|
|
1200
|
+
if (val >= mul) val -= Math.pow(2, 8 * byteLength);
|
|
1201
|
+
return val
|
|
1202
|
+
};
|
|
1203
|
+
Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
|
|
1204
|
+
offset = offset >>> 0;
|
|
1205
|
+
if (!noAssert) checkOffset(offset, 1, this.length);
|
|
1206
|
+
if (!(this[offset] & 0x80)) return (this[offset])
|
|
1207
|
+
return ((0xff - this[offset] + 1) * -1)
|
|
1208
|
+
};
|
|
1209
|
+
Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
|
|
1210
|
+
offset = offset >>> 0;
|
|
1211
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1212
|
+
const val = this[offset] | (this[offset + 1] << 8);
|
|
1213
|
+
return (val & 0x8000) ? val | 0xFFFF0000 : val
|
|
1214
|
+
};
|
|
1215
|
+
Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
|
|
1216
|
+
offset = offset >>> 0;
|
|
1217
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1218
|
+
const val = this[offset + 1] | (this[offset] << 8);
|
|
1219
|
+
return (val & 0x8000) ? val | 0xFFFF0000 : val
|
|
1220
|
+
};
|
|
1221
|
+
Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
|
|
1222
|
+
offset = offset >>> 0;
|
|
1223
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1224
|
+
return (this[offset]) |
|
|
1225
|
+
(this[offset + 1] << 8) |
|
|
1226
|
+
(this[offset + 2] << 16) |
|
|
1227
|
+
(this[offset + 3] << 24)
|
|
1228
|
+
};
|
|
1229
|
+
Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
|
|
1230
|
+
offset = offset >>> 0;
|
|
1231
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1232
|
+
return (this[offset] << 24) |
|
|
1233
|
+
(this[offset + 1] << 16) |
|
|
1234
|
+
(this[offset + 2] << 8) |
|
|
1235
|
+
(this[offset + 3])
|
|
1236
|
+
};
|
|
1237
|
+
Buffer.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE (offset) {
|
|
1238
|
+
offset = offset >>> 0;
|
|
1239
|
+
validateNumber(offset, 'offset');
|
|
1240
|
+
const first = this[offset];
|
|
1241
|
+
const last = this[offset + 7];
|
|
1242
|
+
if (first === undefined || last === undefined) {
|
|
1243
|
+
boundsError(offset, this.length - 8);
|
|
1244
|
+
}
|
|
1245
|
+
const val = this[offset + 4] +
|
|
1246
|
+
this[offset + 5] * 2 ** 8 +
|
|
1247
|
+
this[offset + 6] * 2 ** 16 +
|
|
1248
|
+
(last << 24);
|
|
1249
|
+
return (BigInt(val) << BigInt(32)) +
|
|
1250
|
+
BigInt(first +
|
|
1251
|
+
this[++offset] * 2 ** 8 +
|
|
1252
|
+
this[++offset] * 2 ** 16 +
|
|
1253
|
+
this[++offset] * 2 ** 24)
|
|
1254
|
+
});
|
|
1255
|
+
Buffer.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE (offset) {
|
|
1256
|
+
offset = offset >>> 0;
|
|
1257
|
+
validateNumber(offset, 'offset');
|
|
1258
|
+
const first = this[offset];
|
|
1259
|
+
const last = this[offset + 7];
|
|
1260
|
+
if (first === undefined || last === undefined) {
|
|
1261
|
+
boundsError(offset, this.length - 8);
|
|
1262
|
+
}
|
|
1263
|
+
const val = (first << 24) +
|
|
1264
|
+
this[++offset] * 2 ** 16 +
|
|
1265
|
+
this[++offset] * 2 ** 8 +
|
|
1266
|
+
this[++offset];
|
|
1267
|
+
return (BigInt(val) << BigInt(32)) +
|
|
1268
|
+
BigInt(this[++offset] * 2 ** 24 +
|
|
1269
|
+
this[++offset] * 2 ** 16 +
|
|
1270
|
+
this[++offset] * 2 ** 8 +
|
|
1271
|
+
last)
|
|
1272
|
+
});
|
|
1273
|
+
Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
|
|
1274
|
+
offset = offset >>> 0;
|
|
1275
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1276
|
+
return ieee754$1.read(this, offset, true, 23, 4)
|
|
1277
|
+
};
|
|
1278
|
+
Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
|
|
1279
|
+
offset = offset >>> 0;
|
|
1280
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1281
|
+
return ieee754$1.read(this, offset, false, 23, 4)
|
|
1282
|
+
};
|
|
1283
|
+
Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
|
|
1284
|
+
offset = offset >>> 0;
|
|
1285
|
+
if (!noAssert) checkOffset(offset, 8, this.length);
|
|
1286
|
+
return ieee754$1.read(this, offset, true, 52, 8)
|
|
1287
|
+
};
|
|
1288
|
+
Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
|
|
1289
|
+
offset = offset >>> 0;
|
|
1290
|
+
if (!noAssert) checkOffset(offset, 8, this.length);
|
|
1291
|
+
return ieee754$1.read(this, offset, false, 52, 8)
|
|
1292
|
+
};
|
|
1293
|
+
function checkInt (buf, value, offset, ext, max, min) {
|
|
1294
|
+
if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
|
|
1295
|
+
if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
|
|
1296
|
+
if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
|
1297
|
+
}
|
|
1298
|
+
Buffer.prototype.writeUintLE =
|
|
1299
|
+
Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
|
|
1300
|
+
value = +value;
|
|
1301
|
+
offset = offset >>> 0;
|
|
1302
|
+
byteLength = byteLength >>> 0;
|
|
1303
|
+
if (!noAssert) {
|
|
1304
|
+
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
|
1305
|
+
checkInt(this, value, offset, byteLength, maxBytes, 0);
|
|
1306
|
+
}
|
|
1307
|
+
let mul = 1;
|
|
1308
|
+
let i = 0;
|
|
1309
|
+
this[offset] = value & 0xFF;
|
|
1310
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1311
|
+
this[offset + i] = (value / mul) & 0xFF;
|
|
1312
|
+
}
|
|
1313
|
+
return offset + byteLength
|
|
1314
|
+
};
|
|
1315
|
+
Buffer.prototype.writeUintBE =
|
|
1316
|
+
Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
|
|
1317
|
+
value = +value;
|
|
1318
|
+
offset = offset >>> 0;
|
|
1319
|
+
byteLength = byteLength >>> 0;
|
|
1320
|
+
if (!noAssert) {
|
|
1321
|
+
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
|
1322
|
+
checkInt(this, value, offset, byteLength, maxBytes, 0);
|
|
1323
|
+
}
|
|
1324
|
+
let i = byteLength - 1;
|
|
1325
|
+
let mul = 1;
|
|
1326
|
+
this[offset + i] = value & 0xFF;
|
|
1327
|
+
while (--i >= 0 && (mul *= 0x100)) {
|
|
1328
|
+
this[offset + i] = (value / mul) & 0xFF;
|
|
1329
|
+
}
|
|
1330
|
+
return offset + byteLength
|
|
1331
|
+
};
|
|
1332
|
+
Buffer.prototype.writeUint8 =
|
|
1333
|
+
Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
|
|
1334
|
+
value = +value;
|
|
1335
|
+
offset = offset >>> 0;
|
|
1336
|
+
if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
|
|
1337
|
+
this[offset] = (value & 0xff);
|
|
1338
|
+
return offset + 1
|
|
1339
|
+
};
|
|
1340
|
+
Buffer.prototype.writeUint16LE =
|
|
1341
|
+
Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
|
|
1342
|
+
value = +value;
|
|
1343
|
+
offset = offset >>> 0;
|
|
1344
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
|
|
1345
|
+
this[offset] = (value & 0xff);
|
|
1346
|
+
this[offset + 1] = (value >>> 8);
|
|
1347
|
+
return offset + 2
|
|
1348
|
+
};
|
|
1349
|
+
Buffer.prototype.writeUint16BE =
|
|
1350
|
+
Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
|
|
1351
|
+
value = +value;
|
|
1352
|
+
offset = offset >>> 0;
|
|
1353
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
|
|
1354
|
+
this[offset] = (value >>> 8);
|
|
1355
|
+
this[offset + 1] = (value & 0xff);
|
|
1356
|
+
return offset + 2
|
|
1357
|
+
};
|
|
1358
|
+
Buffer.prototype.writeUint32LE =
|
|
1359
|
+
Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
|
|
1360
|
+
value = +value;
|
|
1361
|
+
offset = offset >>> 0;
|
|
1362
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
|
|
1363
|
+
this[offset + 3] = (value >>> 24);
|
|
1364
|
+
this[offset + 2] = (value >>> 16);
|
|
1365
|
+
this[offset + 1] = (value >>> 8);
|
|
1366
|
+
this[offset] = (value & 0xff);
|
|
1367
|
+
return offset + 4
|
|
1368
|
+
};
|
|
1369
|
+
Buffer.prototype.writeUint32BE =
|
|
1370
|
+
Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
|
|
1371
|
+
value = +value;
|
|
1372
|
+
offset = offset >>> 0;
|
|
1373
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
|
|
1374
|
+
this[offset] = (value >>> 24);
|
|
1375
|
+
this[offset + 1] = (value >>> 16);
|
|
1376
|
+
this[offset + 2] = (value >>> 8);
|
|
1377
|
+
this[offset + 3] = (value & 0xff);
|
|
1378
|
+
return offset + 4
|
|
1379
|
+
};
|
|
1380
|
+
function wrtBigUInt64LE (buf, value, offset, min, max) {
|
|
1381
|
+
checkIntBI(value, min, max, buf, offset, 7);
|
|
1382
|
+
let lo = Number(value & BigInt(0xffffffff));
|
|
1383
|
+
buf[offset++] = lo;
|
|
1384
|
+
lo = lo >> 8;
|
|
1385
|
+
buf[offset++] = lo;
|
|
1386
|
+
lo = lo >> 8;
|
|
1387
|
+
buf[offset++] = lo;
|
|
1388
|
+
lo = lo >> 8;
|
|
1389
|
+
buf[offset++] = lo;
|
|
1390
|
+
let hi = Number(value >> BigInt(32) & BigInt(0xffffffff));
|
|
1391
|
+
buf[offset++] = hi;
|
|
1392
|
+
hi = hi >> 8;
|
|
1393
|
+
buf[offset++] = hi;
|
|
1394
|
+
hi = hi >> 8;
|
|
1395
|
+
buf[offset++] = hi;
|
|
1396
|
+
hi = hi >> 8;
|
|
1397
|
+
buf[offset++] = hi;
|
|
1398
|
+
return offset
|
|
1399
|
+
}
|
|
1400
|
+
function wrtBigUInt64BE (buf, value, offset, min, max) {
|
|
1401
|
+
checkIntBI(value, min, max, buf, offset, 7);
|
|
1402
|
+
let lo = Number(value & BigInt(0xffffffff));
|
|
1403
|
+
buf[offset + 7] = lo;
|
|
1404
|
+
lo = lo >> 8;
|
|
1405
|
+
buf[offset + 6] = lo;
|
|
1406
|
+
lo = lo >> 8;
|
|
1407
|
+
buf[offset + 5] = lo;
|
|
1408
|
+
lo = lo >> 8;
|
|
1409
|
+
buf[offset + 4] = lo;
|
|
1410
|
+
let hi = Number(value >> BigInt(32) & BigInt(0xffffffff));
|
|
1411
|
+
buf[offset + 3] = hi;
|
|
1412
|
+
hi = hi >> 8;
|
|
1413
|
+
buf[offset + 2] = hi;
|
|
1414
|
+
hi = hi >> 8;
|
|
1415
|
+
buf[offset + 1] = hi;
|
|
1416
|
+
hi = hi >> 8;
|
|
1417
|
+
buf[offset] = hi;
|
|
1418
|
+
return offset + 8
|
|
1419
|
+
}
|
|
1420
|
+
Buffer.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE (value, offset = 0) {
|
|
1421
|
+
return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
|
|
1422
|
+
});
|
|
1423
|
+
Buffer.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE (value, offset = 0) {
|
|
1424
|
+
return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
|
|
1425
|
+
});
|
|
1426
|
+
Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
|
|
1427
|
+
value = +value;
|
|
1428
|
+
offset = offset >>> 0;
|
|
1429
|
+
if (!noAssert) {
|
|
1430
|
+
const limit = Math.pow(2, (8 * byteLength) - 1);
|
|
1431
|
+
checkInt(this, value, offset, byteLength, limit - 1, -limit);
|
|
1432
|
+
}
|
|
1433
|
+
let i = 0;
|
|
1434
|
+
let mul = 1;
|
|
1435
|
+
let sub = 0;
|
|
1436
|
+
this[offset] = value & 0xFF;
|
|
1437
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1438
|
+
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
|
|
1439
|
+
sub = 1;
|
|
1440
|
+
}
|
|
1441
|
+
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
|
|
1442
|
+
}
|
|
1443
|
+
return offset + byteLength
|
|
1444
|
+
};
|
|
1445
|
+
Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
|
|
1446
|
+
value = +value;
|
|
1447
|
+
offset = offset >>> 0;
|
|
1448
|
+
if (!noAssert) {
|
|
1449
|
+
const limit = Math.pow(2, (8 * byteLength) - 1);
|
|
1450
|
+
checkInt(this, value, offset, byteLength, limit - 1, -limit);
|
|
1451
|
+
}
|
|
1452
|
+
let i = byteLength - 1;
|
|
1453
|
+
let mul = 1;
|
|
1454
|
+
let sub = 0;
|
|
1455
|
+
this[offset + i] = value & 0xFF;
|
|
1456
|
+
while (--i >= 0 && (mul *= 0x100)) {
|
|
1457
|
+
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
|
|
1458
|
+
sub = 1;
|
|
1459
|
+
}
|
|
1460
|
+
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
|
|
1461
|
+
}
|
|
1462
|
+
return offset + byteLength
|
|
1463
|
+
};
|
|
1464
|
+
Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
|
|
1465
|
+
value = +value;
|
|
1466
|
+
offset = offset >>> 0;
|
|
1467
|
+
if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80);
|
|
1468
|
+
if (value < 0) value = 0xff + value + 1;
|
|
1469
|
+
this[offset] = (value & 0xff);
|
|
1470
|
+
return offset + 1
|
|
1471
|
+
};
|
|
1472
|
+
Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
|
|
1473
|
+
value = +value;
|
|
1474
|
+
offset = offset >>> 0;
|
|
1475
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
|
|
1476
|
+
this[offset] = (value & 0xff);
|
|
1477
|
+
this[offset + 1] = (value >>> 8);
|
|
1478
|
+
return offset + 2
|
|
1479
|
+
};
|
|
1480
|
+
Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
|
|
1481
|
+
value = +value;
|
|
1482
|
+
offset = offset >>> 0;
|
|
1483
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
|
|
1484
|
+
this[offset] = (value >>> 8);
|
|
1485
|
+
this[offset + 1] = (value & 0xff);
|
|
1486
|
+
return offset + 2
|
|
1487
|
+
};
|
|
1488
|
+
Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
|
|
1489
|
+
value = +value;
|
|
1490
|
+
offset = offset >>> 0;
|
|
1491
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
|
|
1492
|
+
this[offset] = (value & 0xff);
|
|
1493
|
+
this[offset + 1] = (value >>> 8);
|
|
1494
|
+
this[offset + 2] = (value >>> 16);
|
|
1495
|
+
this[offset + 3] = (value >>> 24);
|
|
1496
|
+
return offset + 4
|
|
1497
|
+
};
|
|
1498
|
+
Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
|
|
1499
|
+
value = +value;
|
|
1500
|
+
offset = offset >>> 0;
|
|
1501
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
|
|
1502
|
+
if (value < 0) value = 0xffffffff + value + 1;
|
|
1503
|
+
this[offset] = (value >>> 24);
|
|
1504
|
+
this[offset + 1] = (value >>> 16);
|
|
1505
|
+
this[offset + 2] = (value >>> 8);
|
|
1506
|
+
this[offset + 3] = (value & 0xff);
|
|
1507
|
+
return offset + 4
|
|
1508
|
+
};
|
|
1509
|
+
Buffer.prototype.writeBigInt64LE = defineBigIntMethod(function writeBigInt64LE (value, offset = 0) {
|
|
1510
|
+
return wrtBigUInt64LE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
|
|
1511
|
+
});
|
|
1512
|
+
Buffer.prototype.writeBigInt64BE = defineBigIntMethod(function writeBigInt64BE (value, offset = 0) {
|
|
1513
|
+
return wrtBigUInt64BE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
|
|
1514
|
+
});
|
|
1515
|
+
function checkIEEE754 (buf, value, offset, ext, max, min) {
|
|
1516
|
+
if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
|
1517
|
+
if (offset < 0) throw new RangeError('Index out of range')
|
|
1518
|
+
}
|
|
1519
|
+
function writeFloat (buf, value, offset, littleEndian, noAssert) {
|
|
1520
|
+
value = +value;
|
|
1521
|
+
offset = offset >>> 0;
|
|
1522
|
+
if (!noAssert) {
|
|
1523
|
+
checkIEEE754(buf, value, offset, 4);
|
|
1524
|
+
}
|
|
1525
|
+
ieee754$1.write(buf, value, offset, littleEndian, 23, 4);
|
|
1526
|
+
return offset + 4
|
|
1527
|
+
}
|
|
1528
|
+
Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
|
|
1529
|
+
return writeFloat(this, value, offset, true, noAssert)
|
|
1530
|
+
};
|
|
1531
|
+
Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
|
|
1532
|
+
return writeFloat(this, value, offset, false, noAssert)
|
|
1533
|
+
};
|
|
1534
|
+
function writeDouble (buf, value, offset, littleEndian, noAssert) {
|
|
1535
|
+
value = +value;
|
|
1536
|
+
offset = offset >>> 0;
|
|
1537
|
+
if (!noAssert) {
|
|
1538
|
+
checkIEEE754(buf, value, offset, 8);
|
|
1539
|
+
}
|
|
1540
|
+
ieee754$1.write(buf, value, offset, littleEndian, 52, 8);
|
|
1541
|
+
return offset + 8
|
|
1542
|
+
}
|
|
1543
|
+
Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
|
|
1544
|
+
return writeDouble(this, value, offset, true, noAssert)
|
|
1545
|
+
};
|
|
1546
|
+
Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
|
|
1547
|
+
return writeDouble(this, value, offset, false, noAssert)
|
|
1548
|
+
};
|
|
1549
|
+
Buffer.prototype.copy = function copy (target, targetStart, start, end) {
|
|
1550
|
+
if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
|
|
1551
|
+
if (!start) start = 0;
|
|
1552
|
+
if (!end && end !== 0) end = this.length;
|
|
1553
|
+
if (targetStart >= target.length) targetStart = target.length;
|
|
1554
|
+
if (!targetStart) targetStart = 0;
|
|
1555
|
+
if (end > 0 && end < start) end = start;
|
|
1556
|
+
if (end === start) return 0
|
|
1557
|
+
if (target.length === 0 || this.length === 0) return 0
|
|
1558
|
+
if (targetStart < 0) {
|
|
1559
|
+
throw new RangeError('targetStart out of bounds')
|
|
1560
|
+
}
|
|
1561
|
+
if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
|
|
1562
|
+
if (end < 0) throw new RangeError('sourceEnd out of bounds')
|
|
1563
|
+
if (end > this.length) end = this.length;
|
|
1564
|
+
if (target.length - targetStart < end - start) {
|
|
1565
|
+
end = target.length - targetStart + start;
|
|
1566
|
+
}
|
|
1567
|
+
const len = end - start;
|
|
1568
|
+
if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
|
|
1569
|
+
this.copyWithin(targetStart, start, end);
|
|
1570
|
+
} else {
|
|
1571
|
+
Uint8Array.prototype.set.call(
|
|
1572
|
+
target,
|
|
1573
|
+
this.subarray(start, end),
|
|
1574
|
+
targetStart
|
|
1575
|
+
);
|
|
1576
|
+
}
|
|
1577
|
+
return len
|
|
1578
|
+
};
|
|
1579
|
+
Buffer.prototype.fill = function fill (val, start, end, encoding) {
|
|
1580
|
+
if (typeof val === 'string') {
|
|
1581
|
+
if (typeof start === 'string') {
|
|
1582
|
+
encoding = start;
|
|
1583
|
+
start = 0;
|
|
1584
|
+
end = this.length;
|
|
1585
|
+
} else if (typeof end === 'string') {
|
|
1586
|
+
encoding = end;
|
|
1587
|
+
end = this.length;
|
|
1588
|
+
}
|
|
1589
|
+
if (encoding !== undefined && typeof encoding !== 'string') {
|
|
1590
|
+
throw new TypeError('encoding must be a string')
|
|
1591
|
+
}
|
|
1592
|
+
if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
|
|
1593
|
+
throw new TypeError('Unknown encoding: ' + encoding)
|
|
1594
|
+
}
|
|
1595
|
+
if (val.length === 1) {
|
|
1596
|
+
const code = val.charCodeAt(0);
|
|
1597
|
+
if ((encoding === 'utf8' && code < 128) ||
|
|
1598
|
+
encoding === 'latin1') {
|
|
1599
|
+
val = code;
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
} else if (typeof val === 'number') {
|
|
1603
|
+
val = val & 255;
|
|
1604
|
+
} else if (typeof val === 'boolean') {
|
|
1605
|
+
val = Number(val);
|
|
1606
|
+
}
|
|
1607
|
+
if (start < 0 || this.length < start || this.length < end) {
|
|
1608
|
+
throw new RangeError('Out of range index')
|
|
1609
|
+
}
|
|
1610
|
+
if (end <= start) {
|
|
1611
|
+
return this
|
|
1612
|
+
}
|
|
1613
|
+
start = start >>> 0;
|
|
1614
|
+
end = end === undefined ? this.length : end >>> 0;
|
|
1615
|
+
if (!val) val = 0;
|
|
1616
|
+
let i;
|
|
1617
|
+
if (typeof val === 'number') {
|
|
1618
|
+
for (i = start; i < end; ++i) {
|
|
1619
|
+
this[i] = val;
|
|
1620
|
+
}
|
|
1621
|
+
} else {
|
|
1622
|
+
const bytes = Buffer.isBuffer(val)
|
|
1623
|
+
? val
|
|
1624
|
+
: Buffer.from(val, encoding);
|
|
1625
|
+
const len = bytes.length;
|
|
1626
|
+
if (len === 0) {
|
|
1627
|
+
throw new TypeError('The value "' + val +
|
|
1628
|
+
'" is invalid for argument "value"')
|
|
1629
|
+
}
|
|
1630
|
+
for (i = 0; i < end - start; ++i) {
|
|
1631
|
+
this[i + start] = bytes[i % len];
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
return this
|
|
1635
|
+
};
|
|
1636
|
+
const errors = {};
|
|
1637
|
+
function E (sym, getMessage, Base) {
|
|
1638
|
+
errors[sym] = class NodeError extends Base {
|
|
1639
|
+
constructor () {
|
|
1640
|
+
super();
|
|
1641
|
+
Object.defineProperty(this, 'message', {
|
|
1642
|
+
value: getMessage.apply(this, arguments),
|
|
1643
|
+
writable: true,
|
|
1644
|
+
configurable: true
|
|
1645
|
+
});
|
|
1646
|
+
this.name = `${this.name} [${sym}]`;
|
|
1647
|
+
this.stack;
|
|
1648
|
+
delete this.name;
|
|
1649
|
+
}
|
|
1650
|
+
get code () {
|
|
1651
|
+
return sym
|
|
1652
|
+
}
|
|
1653
|
+
set code (value) {
|
|
1654
|
+
Object.defineProperty(this, 'code', {
|
|
1655
|
+
configurable: true,
|
|
1656
|
+
enumerable: true,
|
|
1657
|
+
value,
|
|
1658
|
+
writable: true
|
|
1659
|
+
});
|
|
1660
|
+
}
|
|
1661
|
+
toString () {
|
|
1662
|
+
return `${this.name} [${sym}]: ${this.message}`
|
|
1663
|
+
}
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
E('ERR_BUFFER_OUT_OF_BOUNDS',
|
|
1667
|
+
function (name) {
|
|
1668
|
+
if (name) {
|
|
1669
|
+
return `${name} is outside of buffer bounds`
|
|
1670
|
+
}
|
|
1671
|
+
return 'Attempt to access memory outside buffer bounds'
|
|
1672
|
+
}, RangeError);
|
|
1673
|
+
E('ERR_INVALID_ARG_TYPE',
|
|
1674
|
+
function (name, actual) {
|
|
1675
|
+
return `The "${name}" argument must be of type number. Received type ${typeof actual}`
|
|
1676
|
+
}, TypeError);
|
|
1677
|
+
E('ERR_OUT_OF_RANGE',
|
|
1678
|
+
function (str, range, input) {
|
|
1679
|
+
let msg = `The value of "${str}" is out of range.`;
|
|
1680
|
+
let received = input;
|
|
1681
|
+
if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
|
|
1682
|
+
received = addNumericalSeparator(String(input));
|
|
1683
|
+
} else if (typeof input === 'bigint') {
|
|
1684
|
+
received = String(input);
|
|
1685
|
+
if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) {
|
|
1686
|
+
received = addNumericalSeparator(received);
|
|
1687
|
+
}
|
|
1688
|
+
received += 'n';
|
|
1689
|
+
}
|
|
1690
|
+
msg += ` It must be ${range}. Received ${received}`;
|
|
1691
|
+
return msg
|
|
1692
|
+
}, RangeError);
|
|
1693
|
+
function addNumericalSeparator (val) {
|
|
1694
|
+
let res = '';
|
|
1695
|
+
let i = val.length;
|
|
1696
|
+
const start = val[0] === '-' ? 1 : 0;
|
|
1697
|
+
for (; i >= start + 4; i -= 3) {
|
|
1698
|
+
res = `_${val.slice(i - 3, i)}${res}`;
|
|
1699
|
+
}
|
|
1700
|
+
return `${val.slice(0, i)}${res}`
|
|
1701
|
+
}
|
|
1702
|
+
function checkBounds (buf, offset, byteLength) {
|
|
1703
|
+
validateNumber(offset, 'offset');
|
|
1704
|
+
if (buf[offset] === undefined || buf[offset + byteLength] === undefined) {
|
|
1705
|
+
boundsError(offset, buf.length - (byteLength + 1));
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
function checkIntBI (value, min, max, buf, offset, byteLength) {
|
|
1709
|
+
if (value > max || value < min) {
|
|
1710
|
+
const n = typeof min === 'bigint' ? 'n' : '';
|
|
1711
|
+
let range;
|
|
1712
|
+
if (byteLength > 3) {
|
|
1713
|
+
if (min === 0 || min === BigInt(0)) {
|
|
1714
|
+
range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`;
|
|
1715
|
+
} else {
|
|
1716
|
+
range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ` +
|
|
1717
|
+
`${(byteLength + 1) * 8 - 1}${n}`;
|
|
1718
|
+
}
|
|
1719
|
+
} else {
|
|
1720
|
+
range = `>= ${min}${n} and <= ${max}${n}`;
|
|
1721
|
+
}
|
|
1722
|
+
throw new errors.ERR_OUT_OF_RANGE('value', range, value)
|
|
1723
|
+
}
|
|
1724
|
+
checkBounds(buf, offset, byteLength);
|
|
1725
|
+
}
|
|
1726
|
+
function validateNumber (value, name) {
|
|
1727
|
+
if (typeof value !== 'number') {
|
|
1728
|
+
throw new errors.ERR_INVALID_ARG_TYPE(name, 'number', value)
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
function boundsError (value, length, type) {
|
|
1732
|
+
if (Math.floor(value) !== value) {
|
|
1733
|
+
validateNumber(value, type);
|
|
1734
|
+
throw new errors.ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value)
|
|
1735
|
+
}
|
|
1736
|
+
if (length < 0) {
|
|
1737
|
+
throw new errors.ERR_BUFFER_OUT_OF_BOUNDS()
|
|
1738
|
+
}
|
|
1739
|
+
throw new errors.ERR_OUT_OF_RANGE(type || 'offset',
|
|
1740
|
+
`>= ${type ? 1 : 0} and <= ${length}`,
|
|
1741
|
+
value)
|
|
1742
|
+
}
|
|
1743
|
+
const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
|
|
1744
|
+
function base64clean (str) {
|
|
1745
|
+
str = str.split('=')[0];
|
|
1746
|
+
str = str.trim().replace(INVALID_BASE64_RE, '');
|
|
1747
|
+
if (str.length < 2) return ''
|
|
1748
|
+
while (str.length % 4 !== 0) {
|
|
1749
|
+
str = str + '=';
|
|
1750
|
+
}
|
|
1751
|
+
return str
|
|
1752
|
+
}
|
|
1753
|
+
function utf8ToBytes (string, units) {
|
|
1754
|
+
units = units || Infinity;
|
|
1755
|
+
let codePoint;
|
|
1756
|
+
const length = string.length;
|
|
1757
|
+
let leadSurrogate = null;
|
|
1758
|
+
const bytes = [];
|
|
1759
|
+
for (let i = 0; i < length; ++i) {
|
|
1760
|
+
codePoint = string.charCodeAt(i);
|
|
1761
|
+
if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
|
1762
|
+
if (!leadSurrogate) {
|
|
1763
|
+
if (codePoint > 0xDBFF) {
|
|
1764
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
1765
|
+
continue
|
|
1766
|
+
} else if (i + 1 === length) {
|
|
1767
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
1768
|
+
continue
|
|
1769
|
+
}
|
|
1770
|
+
leadSurrogate = codePoint;
|
|
1771
|
+
continue
|
|
1772
|
+
}
|
|
1773
|
+
if (codePoint < 0xDC00) {
|
|
1774
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
1775
|
+
leadSurrogate = codePoint;
|
|
1776
|
+
continue
|
|
1777
|
+
}
|
|
1778
|
+
codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
|
|
1779
|
+
} else if (leadSurrogate) {
|
|
1780
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
1781
|
+
}
|
|
1782
|
+
leadSurrogate = null;
|
|
1783
|
+
if (codePoint < 0x80) {
|
|
1784
|
+
if ((units -= 1) < 0) break
|
|
1785
|
+
bytes.push(codePoint);
|
|
1786
|
+
} else if (codePoint < 0x800) {
|
|
1787
|
+
if ((units -= 2) < 0) break
|
|
1788
|
+
bytes.push(
|
|
1789
|
+
codePoint >> 0x6 | 0xC0,
|
|
1790
|
+
codePoint & 0x3F | 0x80
|
|
1791
|
+
);
|
|
1792
|
+
} else if (codePoint < 0x10000) {
|
|
1793
|
+
if ((units -= 3) < 0) break
|
|
1794
|
+
bytes.push(
|
|
1795
|
+
codePoint >> 0xC | 0xE0,
|
|
1796
|
+
codePoint >> 0x6 & 0x3F | 0x80,
|
|
1797
|
+
codePoint & 0x3F | 0x80
|
|
1798
|
+
);
|
|
1799
|
+
} else if (codePoint < 0x110000) {
|
|
1800
|
+
if ((units -= 4) < 0) break
|
|
1801
|
+
bytes.push(
|
|
1802
|
+
codePoint >> 0x12 | 0xF0,
|
|
1803
|
+
codePoint >> 0xC & 0x3F | 0x80,
|
|
1804
|
+
codePoint >> 0x6 & 0x3F | 0x80,
|
|
1805
|
+
codePoint & 0x3F | 0x80
|
|
1806
|
+
);
|
|
1807
|
+
} else {
|
|
1808
|
+
throw new Error('Invalid code point')
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
return bytes
|
|
1812
|
+
}
|
|
1813
|
+
function asciiToBytes (str) {
|
|
1814
|
+
const byteArray = [];
|
|
1815
|
+
for (let i = 0; i < str.length; ++i) {
|
|
1816
|
+
byteArray.push(str.charCodeAt(i) & 0xFF);
|
|
1817
|
+
}
|
|
1818
|
+
return byteArray
|
|
1819
|
+
}
|
|
1820
|
+
function utf16leToBytes (str, units) {
|
|
1821
|
+
let c, hi, lo;
|
|
1822
|
+
const byteArray = [];
|
|
1823
|
+
for (let i = 0; i < str.length; ++i) {
|
|
1824
|
+
if ((units -= 2) < 0) break
|
|
1825
|
+
c = str.charCodeAt(i);
|
|
1826
|
+
hi = c >> 8;
|
|
1827
|
+
lo = c % 256;
|
|
1828
|
+
byteArray.push(lo);
|
|
1829
|
+
byteArray.push(hi);
|
|
1830
|
+
}
|
|
1831
|
+
return byteArray
|
|
1832
|
+
}
|
|
1833
|
+
function base64ToBytes (str) {
|
|
1834
|
+
return base64.toByteArray(base64clean(str))
|
|
1835
|
+
}
|
|
1836
|
+
function blitBuffer (src, dst, offset, length) {
|
|
1837
|
+
let i;
|
|
1838
|
+
for (i = 0; i < length; ++i) {
|
|
1839
|
+
if ((i + offset >= dst.length) || (i >= src.length)) break
|
|
1840
|
+
dst[i + offset] = src[i];
|
|
1841
|
+
}
|
|
1842
|
+
return i
|
|
1843
|
+
}
|
|
1844
|
+
function isInstance (obj, type) {
|
|
1845
|
+
return obj instanceof type ||
|
|
1846
|
+
(obj != null && obj.constructor != null && obj.constructor.name != null &&
|
|
1847
|
+
obj.constructor.name === type.name)
|
|
1848
|
+
}
|
|
1849
|
+
function numberIsNaN (obj) {
|
|
1850
|
+
return obj !== obj
|
|
1851
|
+
}
|
|
1852
|
+
const hexSliceLookupTable = (function () {
|
|
1853
|
+
const alphabet = '0123456789abcdef';
|
|
1854
|
+
const table = new Array(256);
|
|
1855
|
+
for (let i = 0; i < 16; ++i) {
|
|
1856
|
+
const i16 = i * 16;
|
|
1857
|
+
for (let j = 0; j < 16; ++j) {
|
|
1858
|
+
table[i16 + j] = alphabet[i] + alphabet[j];
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
return table
|
|
1862
|
+
})();
|
|
1863
|
+
function defineBigIntMethod (fn) {
|
|
1864
|
+
return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn
|
|
1865
|
+
}
|
|
1866
|
+
function BufferBigIntNotDefined () {
|
|
1867
|
+
throw new Error('BigInt not supported')
|
|
1868
|
+
}
|
|
1869
|
+
} (buffer));
|
|
1870
|
+
getDefaultExportFromCjs(buffer);
|
|
1871
|
+
|
|
1872
|
+
/**
|
|
1873
|
+
* @module @zk-kit/utils
|
|
1874
|
+
* @version 0.8.1
|
|
1875
|
+
* @file Essential zero-knowledge utility library for JavaScript developers.
|
|
1876
|
+
* @copyright Ethereum Foundation 2024
|
|
1877
|
+
* @license MIT
|
|
1878
|
+
* @see [Github]{@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/utils}
|
|
1879
|
+
*/
|
|
1880
|
+
/**
|
|
1881
|
+
* @module TypeChecks
|
|
1882
|
+
* This module provides utility functions to check data types.
|
|
1883
|
+
* It defines a set of supported types and includes functions to check if
|
|
1884
|
+
* a value is defined and if it matches a supported type. These functions
|
|
1885
|
+
* are useful for type checking and validation in the other libraries,
|
|
1886
|
+
* enhancing code robustness and reliability.
|
|
1887
|
+
*/
|
|
1888
|
+
const supportedTypes = [
|
|
1889
|
+
"number",
|
|
1890
|
+
"string",
|
|
1891
|
+
"function",
|
|
1892
|
+
"Array",
|
|
1893
|
+
"Uint8Array",
|
|
1894
|
+
"Buffer",
|
|
1895
|
+
"object",
|
|
1896
|
+
"bigint",
|
|
1897
|
+
"stringified-bigint",
|
|
1898
|
+
"hexadecimal",
|
|
1899
|
+
"bignumber",
|
|
1900
|
+
"bignumberish"
|
|
1901
|
+
];
|
|
1902
|
+
/**
|
|
1903
|
+
* Returns true if the value is a number, false otherwise.
|
|
1904
|
+
* @param value The value to be checked.
|
|
1905
|
+
*/
|
|
1906
|
+
function isNumber(value) {
|
|
1907
|
+
return typeof value === "number";
|
|
1908
|
+
}
|
|
1909
|
+
/**
|
|
1910
|
+
* Returns true if the value is a string, false otherwise.
|
|
1911
|
+
* @param value The value to be checked.
|
|
1912
|
+
*/
|
|
1913
|
+
function isString(value) {
|
|
1914
|
+
return typeof value === "string";
|
|
1915
|
+
}
|
|
1916
|
+
/**
|
|
1917
|
+
* Returns true if the value is a function, false otherwise.
|
|
1918
|
+
* @param value The value to be checked.
|
|
1919
|
+
*/
|
|
1920
|
+
function isFunction(value) {
|
|
1921
|
+
return typeof value === "function";
|
|
1922
|
+
}
|
|
1923
|
+
/**
|
|
1924
|
+
* Returns true if the value is an object, false otherwise.
|
|
1925
|
+
* Please, note that arrays are also objects in JavaScript.
|
|
1926
|
+
* @param value The value to be checked.
|
|
1927
|
+
*/
|
|
1928
|
+
function isObject(value) {
|
|
1929
|
+
return typeof value === "object";
|
|
1930
|
+
}
|
|
1931
|
+
/**
|
|
1932
|
+
* Returns true if the value is an Array instance, false otherwise.
|
|
1933
|
+
* @param value The value to be checked.
|
|
1934
|
+
*/
|
|
1935
|
+
function isArray(value) {
|
|
1936
|
+
return isObject(value) && Array.isArray(value);
|
|
1937
|
+
}
|
|
1938
|
+
/**
|
|
1939
|
+
* Returns true if the value is a Uint8Array instance, false otherwise.
|
|
1940
|
+
* @param value The value to be checked.
|
|
1941
|
+
*/
|
|
1942
|
+
function isUint8Array(value) {
|
|
1943
|
+
return value instanceof Uint8Array;
|
|
1944
|
+
}
|
|
1945
|
+
/**
|
|
1946
|
+
* Returns true if the value is a Buffer instance, false otherwise.
|
|
1947
|
+
* @param value The value to be checked.
|
|
1948
|
+
*/
|
|
1949
|
+
function isBuffer(value) {
|
|
1950
|
+
return buffer.Buffer.isBuffer(value);
|
|
1951
|
+
}
|
|
1952
|
+
/**
|
|
1953
|
+
* Returns true if the value is a bigint, false otherwise.
|
|
1954
|
+
* @param value The value to be checked.
|
|
1955
|
+
*/
|
|
1956
|
+
function isBigInt(value) {
|
|
1957
|
+
return typeof value === "bigint";
|
|
1958
|
+
}
|
|
1959
|
+
/**
|
|
1960
|
+
* Checks if the given value is a string that represents a valid bigint.
|
|
1961
|
+
* @param value The value to be checked if it's a stringified bigint.
|
|
1962
|
+
*/
|
|
1963
|
+
function isStringifiedBigInt(value) {
|
|
1964
|
+
if (!isString(value)) {
|
|
1965
|
+
return false;
|
|
1966
|
+
}
|
|
1967
|
+
try {
|
|
1968
|
+
BigInt(value);
|
|
1969
|
+
return true;
|
|
1970
|
+
}
|
|
1971
|
+
catch (_a) {
|
|
1972
|
+
return false;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
/**
|
|
1976
|
+
* Checks if a string is a valid hexadecimal string representation.
|
|
1977
|
+
* If 'prefix' is 'true', the string must start with '0x' or '0X' followed by one or more
|
|
1978
|
+
* hexadecimal digits (0-9, a-f, A-F), otherwise no prefix is expected. 'prefix' is optional and
|
|
1979
|
+
* if its value it is not explicitly defined it will be set to 'true' by default.
|
|
1980
|
+
* @param value The string to be tested.
|
|
1981
|
+
* @param prefix A boolean to include or not a '0x' or '0X' prefix.
|
|
1982
|
+
*/
|
|
1983
|
+
function isHexadecimal(value, prefix = true) {
|
|
1984
|
+
if (!isString(value)) {
|
|
1985
|
+
return false;
|
|
1986
|
+
}
|
|
1987
|
+
if (prefix) {
|
|
1988
|
+
return /^(0x|0X)[0-9a-fA-F]+$/.test(value);
|
|
1989
|
+
}
|
|
1990
|
+
return /^[0-9a-fA-F]+$/.test(value);
|
|
1991
|
+
}
|
|
1992
|
+
/**
|
|
1993
|
+
* Checks if the given value can be considered as BigNumber.
|
|
1994
|
+
* A value is considered a BigNumber if it is a bigint or a string
|
|
1995
|
+
* that can be converted to a bigint (via `Bigint(s)`).
|
|
1996
|
+
* @param value The value to check.
|
|
1997
|
+
*/
|
|
1998
|
+
function isBigNumber(value) {
|
|
1999
|
+
return isBigInt(value) || isStringifiedBigInt(value);
|
|
2000
|
+
}
|
|
2001
|
+
/**
|
|
2002
|
+
* Checks if the given value can be considered as BigNumberish.
|
|
2003
|
+
* A value is considered BigNumberish if it meets
|
|
2004
|
+
* any of the following conditions: it's a number, a bigint, a string
|
|
2005
|
+
* that can be converted to a bigint, a hexadecimal
|
|
2006
|
+
* string, or a Buffer object.
|
|
2007
|
+
* @param value The value to check.
|
|
2008
|
+
*/
|
|
2009
|
+
function isBigNumberish(value) {
|
|
2010
|
+
return (isNumber(value) ||
|
|
2011
|
+
isBigInt(value) ||
|
|
2012
|
+
isStringifiedBigInt(value) ||
|
|
2013
|
+
isHexadecimal(value) ||
|
|
2014
|
+
isBuffer(value) ||
|
|
2015
|
+
isUint8Array(value));
|
|
2016
|
+
}
|
|
2017
|
+
/**
|
|
2018
|
+
* Returns true if the value type is the same as the type passed
|
|
2019
|
+
* as the second parameter, false otherwise.
|
|
2020
|
+
* @param type The expected type.
|
|
2021
|
+
*/
|
|
2022
|
+
function isType(value, type) {
|
|
2023
|
+
switch (type) {
|
|
2024
|
+
case "number":
|
|
2025
|
+
return isNumber(value);
|
|
2026
|
+
case "string":
|
|
2027
|
+
return isString(value);
|
|
2028
|
+
case "function":
|
|
2029
|
+
return isFunction(value);
|
|
2030
|
+
case "Array":
|
|
2031
|
+
return isArray(value);
|
|
2032
|
+
case "Uint8Array":
|
|
2033
|
+
return isUint8Array(value);
|
|
2034
|
+
case "Buffer":
|
|
2035
|
+
return isBuffer(value);
|
|
2036
|
+
case "object":
|
|
2037
|
+
return isObject(value);
|
|
2038
|
+
case "bigint":
|
|
2039
|
+
return isBigInt(value);
|
|
2040
|
+
case "stringified-bigint":
|
|
2041
|
+
return isStringifiedBigInt(value);
|
|
2042
|
+
case "hexadecimal":
|
|
2043
|
+
return isHexadecimal(value);
|
|
2044
|
+
case "bignumber":
|
|
2045
|
+
return isBigNumber(value);
|
|
2046
|
+
case "bignumberish":
|
|
2047
|
+
return isBigNumberish(value);
|
|
2048
|
+
default:
|
|
2049
|
+
return false;
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
/**
|
|
2053
|
+
* Returns true if the type is being supported by this utility
|
|
2054
|
+
* functions, false otherwise.
|
|
2055
|
+
* @param type The type to be checked.
|
|
2056
|
+
*/
|
|
2057
|
+
function isSupportedType(type) {
|
|
2058
|
+
return supportedTypes.includes(type);
|
|
2059
|
+
}
|
|
2060
|
+
|
|
2061
|
+
/**
|
|
2062
|
+
* @module @zk-kit/utils
|
|
2063
|
+
* @version 0.8.1
|
|
2064
|
+
* @file Essential zero-knowledge utility library for JavaScript developers.
|
|
2065
|
+
* @copyright Ethereum Foundation 2024
|
|
2066
|
+
* @license MIT
|
|
2067
|
+
* @see [Github]{@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/utils}
|
|
2068
|
+
*/
|
|
2069
|
+
/**
|
|
2070
|
+
* @throws Throws a type error if the parameter value is not a string.
|
|
2071
|
+
* @param parameterValue The parameter value.
|
|
2072
|
+
* @param parameterName The parameter name.
|
|
2073
|
+
*/
|
|
2074
|
+
function requireString(parameterValue, parameterName) {
|
|
2075
|
+
if (!isString(parameterValue)) {
|
|
2076
|
+
throw new TypeError(`Parameter '${parameterName}' is not a string, received type: ${typeof parameterValue}`);
|
|
2077
|
+
}
|
|
2078
|
+
}
|
|
2079
|
+
/**
|
|
2080
|
+
* @throws Throws a type error if the parameter value is not a hexadecimal string.
|
|
2081
|
+
* If 'prefix' is 'true', the string must start with '0x' or '0X' followed by one or more
|
|
2082
|
+
* hexadecimal digits (0-9, a-f, A-F), otherwise no prefix is expected. 'prefix' is optional and
|
|
2083
|
+
* if its value it is not explicitly defined it will be set to 'true' by default.
|
|
2084
|
+
* @param parameterValue The parameter value.
|
|
2085
|
+
* @param parameterName The parameter name.
|
|
2086
|
+
* @param prefix A boolean to include or not a '0x' or '0X' prefix.
|
|
2087
|
+
*/
|
|
2088
|
+
function requireHexadecimal(parameterValue, parameterName, prefix = true) {
|
|
2089
|
+
if (!isHexadecimal(parameterValue, prefix)) {
|
|
2090
|
+
throw new TypeError(`Parameter '${parameterName}' is not a hexadecimal string`);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
/**
|
|
2094
|
+
* @throws Throws a type error if the parameter value type is not part of the list of types.
|
|
2095
|
+
* @param parameterValue The parameter value.
|
|
2096
|
+
* @param parameterName The parameter name.
|
|
2097
|
+
*/
|
|
2098
|
+
function requireTypes(parameterValue, parameterName, types) {
|
|
2099
|
+
for (const type of types) {
|
|
2100
|
+
if (!isSupportedType(type)) {
|
|
2101
|
+
throw new Error(`Type '${type}' is not supported`);
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
for (const type of types) {
|
|
2105
|
+
if (isType(parameterValue, type)) {
|
|
2106
|
+
return;
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
throw new TypeError(`Parameter '${parameterName}' is none of the following types: ${types.join(", ")}`);
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
/**
|
|
2113
|
+
* @module @zk-kit/utils
|
|
2114
|
+
* @version 0.8.1
|
|
2115
|
+
* @file Essential zero-knowledge utility library for JavaScript developers.
|
|
2116
|
+
* @copyright Ethereum Foundation 2024
|
|
2117
|
+
* @license MIT
|
|
2118
|
+
* @see [Github]{@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/utils}
|
|
2119
|
+
*/
|
|
2120
|
+
/**
|
|
2121
|
+
* Converts an hexadecimal string to a buffer. The hexadecimal string
|
|
2122
|
+
* should not start with '0x' or '0X'. It keeps the bytes in the same order.
|
|
2123
|
+
* @param value The hexadecimal string to convert.
|
|
2124
|
+
* @returns The buffer representation of the hexadecimal string.
|
|
2125
|
+
*/
|
|
2126
|
+
function hexadecimalToBuffer(value) {
|
|
2127
|
+
requireHexadecimal(value, "value", false);
|
|
2128
|
+
if (value.length % 2 !== 0) {
|
|
2129
|
+
value = `0${value}`;
|
|
2130
|
+
}
|
|
2131
|
+
return buffer.Buffer.from(value, "hex");
|
|
2132
|
+
}
|
|
2133
|
+
/**
|
|
2134
|
+
* Converts a buffer to a hexadecimal string. It accepts 'Buffer' or 'Uint8Array'.
|
|
2135
|
+
* The hexadecimal string will not start with '0x' or '0X'. It keeps the bytes in the same order.
|
|
2136
|
+
* @param value The buffer to convert.
|
|
2137
|
+
* @returns The converted hexadecimal string.
|
|
2138
|
+
*/
|
|
2139
|
+
function bufferToHexadecimal(value) {
|
|
2140
|
+
requireTypes(value, "value", ["Buffer", "Uint8Array"]);
|
|
2141
|
+
return buffer.Buffer.from(value).toString("hex");
|
|
2142
|
+
}
|
|
2143
|
+
|
|
2144
|
+
var poseidon2$1 = {};
|
|
2145
|
+
|
|
2146
|
+
const F = BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617');
|
|
2147
|
+
const N_ROUNDS_F = 8;
|
|
2148
|
+
const N_ROUNDS_P = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68];
|
|
2149
|
+
const pow5 = v => {
|
|
2150
|
+
let o = v * v;
|
|
2151
|
+
return v * o * o % F;
|
|
2152
|
+
};
|
|
2153
|
+
function mix(state, M) {
|
|
2154
|
+
const out = [];
|
|
2155
|
+
for (let x = 0; x < state.length; x++) {
|
|
2156
|
+
let o = 0n;
|
|
2157
|
+
for (let y = 0; y < state.length; y++) {
|
|
2158
|
+
o = o + M[x][y] * state[y];
|
|
2159
|
+
}
|
|
2160
|
+
out.push(o % F);
|
|
2161
|
+
}
|
|
2162
|
+
return out;
|
|
2163
|
+
}
|
|
2164
|
+
function poseidon(_inputs, opt) {
|
|
2165
|
+
const inputs = _inputs.map(i => BigInt(i));
|
|
2166
|
+
if (inputs.length <= 0) {
|
|
2167
|
+
throw new Error('poseidon-lite: Not enough inputs');
|
|
2168
|
+
}
|
|
2169
|
+
if (inputs.length > N_ROUNDS_P.length) {
|
|
2170
|
+
throw new Error('poseidon-lite: Too many inputs');
|
|
2171
|
+
}
|
|
2172
|
+
const t = inputs.length + 1;
|
|
2173
|
+
const nRoundsF = N_ROUNDS_F;
|
|
2174
|
+
const nRoundsP = N_ROUNDS_P[t - 2];
|
|
2175
|
+
const {
|
|
2176
|
+
C,
|
|
2177
|
+
M
|
|
2178
|
+
} = opt;
|
|
2179
|
+
if (M.length !== t) {
|
|
2180
|
+
throw new Error(`poseidon-lite: Incorrect M length, expected ${t} got ${M.length}`);
|
|
2181
|
+
}
|
|
2182
|
+
let state = [0n, ...inputs];
|
|
2183
|
+
for (let x = 0; x < nRoundsF + nRoundsP; x++) {
|
|
2184
|
+
for (let y = 0; y < state.length; y++) {
|
|
2185
|
+
state[y] = state[y] + C[x * t + y];
|
|
2186
|
+
if (x < nRoundsF / 2 || x >= nRoundsF / 2 + nRoundsP) state[y] = pow5(state[y]);else if (y === 0) state[y] = pow5(state[y]);
|
|
2187
|
+
}
|
|
2188
|
+
state = mix(state, M);
|
|
2189
|
+
}
|
|
2190
|
+
return state[0];
|
|
2191
|
+
}
|
|
2192
|
+
var poseidon_1 = poseidon;
|
|
2193
|
+
getDefaultExportFromCjs(poseidon_1);
|
|
2194
|
+
|
|
2195
|
+
var unstringify = {};
|
|
2196
|
+
|
|
2197
|
+
Object.defineProperty(unstringify, "__esModule", {
|
|
2198
|
+
value: true
|
|
2199
|
+
});
|
|
2200
|
+
unstringify.default = unstringifyBigInts;
|
|
2201
|
+
function unstringifyBigInts(o) {
|
|
2202
|
+
if (Array.isArray(o)) {
|
|
2203
|
+
return o.map(unstringifyBigInts);
|
|
2204
|
+
} else if (typeof o == 'object') {
|
|
2205
|
+
const res = {};
|
|
2206
|
+
for (const [key, val] of Object.entries(o)) {
|
|
2207
|
+
res[key] = unstringifyBigInts(val);
|
|
2208
|
+
}
|
|
2209
|
+
return res;
|
|
2210
|
+
}
|
|
2211
|
+
const byteArray = Uint8Array.from(atob(o), c => c.charCodeAt(0));
|
|
2212
|
+
const hex = [...byteArray].map(x => x.toString(16).padStart(2, '0')).join('');
|
|
2213
|
+
return BigInt(`0x${hex}`);
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
var _2 = {};
|
|
2217
|
+
|
|
2218
|
+
Object.defineProperty(_2, "__esModule", {
|
|
2219
|
+
value: true
|
|
2220
|
+
});
|
|
2221
|
+
_2.default = void 0;
|
|
2222
|
+
var _default = {
|
|
2223
|
+
C: ['DumlkrqalRjQWYbWVvQMIRTEmTwRuymTjSHUcwTNjm4=', 'APFEUjXyFIxZhlhxafwbzYh7CNTQCGjfVpb/9AlW6GQ=', 'CN/zSH6KyZ4fKaBY0PqAuTDHKHMLerNs6HnziQ7Pc/U=', 'Lye+aQ/a7kbDzij3UysTyFbDU0LIS9puIJZjEPrcAdA=', 'KyrhrPaLe40kFr6/PU9iNLdj/gS4BD7ki4MnvryhbPI=', 'AxnQYgcr737MperAb5fU1VlSwXWrawPq5ktEx9vxHPo=', 'KIE9yuuuqoKKN234evSmO8i3vyetScYpjvezh78oUm0=', 'JydnOyzLyQPxgb844cHUDSAzhlIAw1K8FQkord35y3g=', 'I07EXKJ3J8LnSr0rKhSUzW771D40BYfWuPueMeZcxjI=', 'FbUlNAMa4Y9/hiyyz3z3YKsQqBUKM3sczZn/boeX1Cg=', 'Dcj61tnks19e2aPRhrec444Oio0bWLEy1wHU7s9o0fY=', 'G82V/8IR+8pgD3BfrT+1Z+pOs3j2Lh/sl4BVGKR+TZw=', 'EFILCrchyt/p7/gbAW/DTcdto2wleJN4F8uXjQad5Vk=', 'H21IFJuOf32bJX2O1fu69CkySYB1/tCs6IqeuB9WJ/Y=', 'HZZV9lIwkBTSngDvNaIIm//43ByBbw3JyjS9tUYMhwU=', 'BN9aVv+VvK+wUfexzUOpm6cx/2fkcDIFj+PUGFaXzH0=', 'BnLZlfj/9kAVGz0pDO2vFIaQoQqMhCSn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C5w2x2Q3nbyizI/dFBXD3e1ilAvN4L13E=', 'LiQZ+ewC7DlMmHHIMpY9wbiddDyMe5ZAKbIxFoex/iM=', 'EBBx8AMjebaXMVh2aQ8FPRSNThCfX7BlyKrMVaD4m/o='], ['FDAh7GhqPzMNX55lRjgGXObNeeKMWzdTMmJE7mWhsac=', 'F2zAKWla0CWCpw7/CKb9mdBX4S5Y59e2sWzfq8juKRE=', 'GaP8ClZwK/QXun/uOAJZP6ZERwMHBD93cyec1x0l1eA=']]
|
|
2225
|
+
};
|
|
2226
|
+
_2.default = _default;
|
|
2227
|
+
|
|
2228
|
+
Object.defineProperty(poseidon2$1, "__esModule", {
|
|
2229
|
+
value: true
|
|
2230
|
+
});
|
|
2231
|
+
var poseidon2_2 = poseidon2$1.poseidon2 = poseidon2;
|
|
2232
|
+
var _poseidon = _interopRequireDefault(poseidon_1);
|
|
2233
|
+
var _unstringify = _interopRequireDefault(unstringify);
|
|
2234
|
+
var _ = _interopRequireDefault(_2);
|
|
2235
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2236
|
+
const c = (0, _unstringify.default)(_.default);
|
|
2237
|
+
function poseidon2(inputs) {
|
|
2238
|
+
return (0, _poseidon.default)(inputs, c);
|
|
2239
|
+
}
|
|
2240
|
+
|
|
2241
|
+
/**
|
|
2242
|
+
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
|
|
2243
|
+
* public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
|
|
2244
|
+
* in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
|
|
2245
|
+
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
|
|
2246
|
+
* In addition, the commitment, i.e. the hash of the public key, is used to represent
|
|
2247
|
+
* Semaphore identities in groups, adding an additional layer of privacy and security.
|
|
2248
|
+
* The private key of the identity is stored as a hexadecimal string or text.
|
|
2249
|
+
* The other attributes are stored as stringified bigint.
|
|
2250
|
+
*/
|
|
2251
|
+
var Identity = /** @class */ (function () {
|
|
2252
|
+
/**
|
|
2253
|
+
* Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
|
|
2254
|
+
* Hexadecimal strings must not start with '0x' or '0X'.
|
|
2255
|
+
* If the private key is not passed as a parameter, a random hexadecimal key will be generated.
|
|
2256
|
+
* The EdDSAPoseidon class is used to generate the secret scalar and the public key.
|
|
2257
|
+
* Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
|
|
2258
|
+
*
|
|
2259
|
+
* @example
|
|
2260
|
+
* // Generates an identity.
|
|
2261
|
+
* const { privateKey, publicKey, commitment } = new Identity("private-key")
|
|
2262
|
+
* @example
|
|
2263
|
+
* // Generates an identity with a random private key.
|
|
2264
|
+
* const { privateKey, publicKey, commitment } = new Identity()
|
|
2265
|
+
*
|
|
2266
|
+
* @param privateKey The private key used to derive the public key (hexadecimal or string).
|
|
2267
|
+
*/
|
|
2268
|
+
function Identity(privateKey) {
|
|
2269
|
+
var eddsa;
|
|
2270
|
+
if (privateKey) {
|
|
2271
|
+
requireString(privateKey, "privateKey");
|
|
2272
|
+
this._privateKey = privateKey;
|
|
2273
|
+
if (isHexadecimal(privateKey, false)) {
|
|
2274
|
+
eddsa = new eddsaPoseidon.EdDSAPoseidon(hexadecimalToBuffer(privateKey));
|
|
2275
|
+
}
|
|
2276
|
+
else {
|
|
2277
|
+
eddsa = new eddsaPoseidon.EdDSAPoseidon(privateKey);
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
else {
|
|
2281
|
+
eddsa = new eddsaPoseidon.EdDSAPoseidon();
|
|
2282
|
+
this._privateKey = bufferToHexadecimal(eddsa.privateKey);
|
|
2283
|
+
}
|
|
2284
|
+
this._secretScalar = eddsa.secretScalar;
|
|
2285
|
+
this._publicKey = eddsa.publicKey;
|
|
2286
|
+
this._commitment = poseidon2_2(this._publicKey);
|
|
2287
|
+
}
|
|
2288
|
+
Object.defineProperty(Identity.prototype, "privateKey", {
|
|
2289
|
+
/**
|
|
2290
|
+
* Returns the private key.
|
|
2291
|
+
* @returns The private key as a string (hexadecimal or text).
|
|
2292
|
+
*/
|
|
2293
|
+
get: function () {
|
|
2294
|
+
return this._privateKey;
|
|
2295
|
+
},
|
|
2296
|
+
enumerable: false,
|
|
2297
|
+
configurable: true
|
|
2298
|
+
});
|
|
2299
|
+
Object.defineProperty(Identity.prototype, "secretScalar", {
|
|
2300
|
+
/**
|
|
2301
|
+
* Returns the secret scalar.
|
|
2302
|
+
* @returns The secret scalar as a string.
|
|
2303
|
+
*/
|
|
2304
|
+
get: function () {
|
|
2305
|
+
return this._secretScalar;
|
|
2306
|
+
},
|
|
2307
|
+
enumerable: false,
|
|
2308
|
+
configurable: true
|
|
2309
|
+
});
|
|
2310
|
+
Object.defineProperty(Identity.prototype, "publicKey", {
|
|
2311
|
+
/**
|
|
2312
|
+
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
|
|
2313
|
+
* @returns The public key as a point.
|
|
2314
|
+
*/
|
|
2315
|
+
get: function () {
|
|
2316
|
+
return this._publicKey;
|
|
2317
|
+
},
|
|
2318
|
+
enumerable: false,
|
|
2319
|
+
configurable: true
|
|
2320
|
+
});
|
|
2321
|
+
Object.defineProperty(Identity.prototype, "commitment", {
|
|
2322
|
+
/**
|
|
2323
|
+
* Returns the commitment hash of the public key.
|
|
2324
|
+
* @returns The commitment as a string.
|
|
2325
|
+
*/
|
|
2326
|
+
get: function () {
|
|
2327
|
+
return this._commitment;
|
|
2328
|
+
},
|
|
2329
|
+
enumerable: false,
|
|
2330
|
+
configurable: true
|
|
2331
|
+
});
|
|
2332
|
+
/**
|
|
2333
|
+
* Generates a signature for a given message using the private key.
|
|
2334
|
+
* This method demonstrates how to sign a message and could be used
|
|
2335
|
+
* for authentication or data integrity.
|
|
2336
|
+
*
|
|
2337
|
+
* @example
|
|
2338
|
+
* const identity = new Identity()
|
|
2339
|
+
* const signature = identity.signMessage("message")
|
|
2340
|
+
*
|
|
2341
|
+
* @param message The message to be signed.
|
|
2342
|
+
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
|
|
2343
|
+
*/
|
|
2344
|
+
Identity.prototype.signMessage = function (message) {
|
|
2345
|
+
return eddsaPoseidon.signMessage(this.privateKey, message);
|
|
2346
|
+
};
|
|
2347
|
+
/**
|
|
2348
|
+
* Verifies a signature against a given message and public key.
|
|
2349
|
+
* This static method allows for the verification of signatures without needing
|
|
2350
|
+
* an instance of the Identity class. It's useful for cases where you only have
|
|
2351
|
+
* the public key, the message and a signature, and need to verify if they match.
|
|
2352
|
+
*
|
|
2353
|
+
* @example
|
|
2354
|
+
* const identity = new Identity()
|
|
2355
|
+
* const signature = identity.signMessage("message")
|
|
2356
|
+
* Identity.verifySignature("message", signature, identity.publicKey)
|
|
2357
|
+
*
|
|
2358
|
+
* @param message The message that was signed.
|
|
2359
|
+
* @param signature The signature to verify.
|
|
2360
|
+
* @param publicKey The public key to use for verification.
|
|
2361
|
+
* @returns A boolean indicating whether the signature is valid.
|
|
2362
|
+
*/
|
|
2363
|
+
Identity.verifySignature = function (message, signature, publicKey) {
|
|
2364
|
+
return eddsaPoseidon.verifySignature(message, signature, publicKey);
|
|
2365
|
+
};
|
|
2366
|
+
return Identity;
|
|
2367
|
+
}());
|
|
2368
|
+
|
|
2369
|
+
exports.Identity = Identity;
|