@taquito/utils 23.0.3 → 24.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/constants.js +1 -128
- package/dist/lib/encoding.js +603 -0
- package/dist/lib/taquito-utils.js +16 -710
- package/dist/lib/validators.js +10 -27
- package/dist/lib/verify-signature.js +20 -35
- package/dist/lib/version.js +2 -2
- package/dist/taquito-utils.es6.js +2690 -958
- package/dist/taquito-utils.es6.js.map +1 -1
- package/dist/taquito-utils.umd.js +2578 -858
- package/dist/taquito-utils.umd.js.map +1 -1
- package/dist/types/constants.d.ts +0 -101
- package/dist/types/encoding.d.ts +184 -0
- package/dist/types/taquito-utils.d.ts +1 -234
- package/dist/types/validators.d.ts +1 -6
- package/dist/types/verify-signature.d.ts +0 -8
- package/package.json +8 -8
|
@@ -1,770 +1,2196 @@
|
|
|
1
|
-
import { Buffer } from 'buffer';
|
|
2
1
|
import { hash } from '@stablelib/blake2b';
|
|
3
2
|
import bs58check from 'bs58check';
|
|
4
3
|
import BigNumber from 'bignumber.js';
|
|
5
4
|
import toBuffer from 'typedarray-to-buffer';
|
|
6
|
-
import {
|
|
5
|
+
import { ParameterValidationError, ValidationResult, InvalidKeyError, InvalidAddressError, InvalidPublicKeyError, InvalidHexStringError, InvalidSignatureError, InvalidMessageError, UnsupportedActionError } from '@taquito/core';
|
|
7
6
|
export { DeprecationError, InvalidAddressError, InvalidBlockHashError, InvalidChainIdError, InvalidContractAddressError, InvalidHexStringError, InvalidKeyError, InvalidKeyHashError, InvalidMessageError, InvalidOperationHashError, InvalidOperationKindError, InvalidPublicKeyError, InvalidSignatureError, ProhibitedActionError, ValidationResult } from '@taquito/core';
|
|
8
7
|
import { verify } from '@stablelib/ed25519';
|
|
9
|
-
import
|
|
8
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
9
|
+
import { p256 } from '@noble/curves/nist';
|
|
10
10
|
import { bls12_381 } from '@noble/curves/bls12-381';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
var global$1 = (typeof global !== "undefined" ? global :
|
|
13
|
+
typeof self !== "undefined" ? self :
|
|
14
|
+
typeof window !== "undefined" ? window : {});
|
|
15
|
+
|
|
16
|
+
var lookup = [];
|
|
17
|
+
var revLookup = [];
|
|
18
|
+
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
|
|
19
|
+
var inited = false;
|
|
20
|
+
function init () {
|
|
21
|
+
inited = true;
|
|
22
|
+
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
23
|
+
for (var i = 0, len = code.length; i < len; ++i) {
|
|
24
|
+
lookup[i] = code[i];
|
|
25
|
+
revLookup[code.charCodeAt(i)] = i;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
revLookup['-'.charCodeAt(0)] = 62;
|
|
29
|
+
revLookup['_'.charCodeAt(0)] = 63;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function toByteArray (b64) {
|
|
33
|
+
if (!inited) {
|
|
34
|
+
init();
|
|
35
|
+
}
|
|
36
|
+
var i, j, l, tmp, placeHolders, arr;
|
|
37
|
+
var len = b64.length;
|
|
38
|
+
|
|
39
|
+
if (len % 4 > 0) {
|
|
40
|
+
throw new Error('Invalid string. Length must be a multiple of 4')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// the number of equal signs (place holders)
|
|
44
|
+
// if there are two placeholders, than the two characters before it
|
|
45
|
+
// represent one byte
|
|
46
|
+
// if there is only one, then the three characters before it represent 2 bytes
|
|
47
|
+
// this is just a cheap hack to not do indexOf twice
|
|
48
|
+
placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0;
|
|
49
|
+
|
|
50
|
+
// base64 is 4/3 + up to two characters of the original data
|
|
51
|
+
arr = new Arr(len * 3 / 4 - placeHolders);
|
|
52
|
+
|
|
53
|
+
// if there are placeholders, only get up to the last complete 4 chars
|
|
54
|
+
l = placeHolders > 0 ? len - 4 : len;
|
|
55
|
+
|
|
56
|
+
var L = 0;
|
|
57
|
+
|
|
58
|
+
for (i = 0, j = 0; i < l; i += 4, j += 3) {
|
|
59
|
+
tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)];
|
|
60
|
+
arr[L++] = (tmp >> 16) & 0xFF;
|
|
61
|
+
arr[L++] = (tmp >> 8) & 0xFF;
|
|
62
|
+
arr[L++] = tmp & 0xFF;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (placeHolders === 2) {
|
|
66
|
+
tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4);
|
|
67
|
+
arr[L++] = tmp & 0xFF;
|
|
68
|
+
} else if (placeHolders === 1) {
|
|
69
|
+
tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2);
|
|
70
|
+
arr[L++] = (tmp >> 8) & 0xFF;
|
|
71
|
+
arr[L++] = tmp & 0xFF;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return arr
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function tripletToBase64 (num) {
|
|
78
|
+
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function encodeChunk (uint8, start, end) {
|
|
82
|
+
var tmp;
|
|
83
|
+
var output = [];
|
|
84
|
+
for (var i = start; i < end; i += 3) {
|
|
85
|
+
tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
|
|
86
|
+
output.push(tripletToBase64(tmp));
|
|
87
|
+
}
|
|
88
|
+
return output.join('')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function fromByteArray (uint8) {
|
|
92
|
+
if (!inited) {
|
|
93
|
+
init();
|
|
94
|
+
}
|
|
95
|
+
var tmp;
|
|
96
|
+
var len = uint8.length;
|
|
97
|
+
var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
|
|
98
|
+
var output = '';
|
|
99
|
+
var parts = [];
|
|
100
|
+
var maxChunkLength = 16383; // must be multiple of 3
|
|
101
|
+
|
|
102
|
+
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
103
|
+
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
|
104
|
+
parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
108
|
+
if (extraBytes === 1) {
|
|
109
|
+
tmp = uint8[len - 1];
|
|
110
|
+
output += lookup[tmp >> 2];
|
|
111
|
+
output += lookup[(tmp << 4) & 0x3F];
|
|
112
|
+
output += '==';
|
|
113
|
+
} else if (extraBytes === 2) {
|
|
114
|
+
tmp = (uint8[len - 2] << 8) + (uint8[len - 1]);
|
|
115
|
+
output += lookup[tmp >> 10];
|
|
116
|
+
output += lookup[(tmp >> 4) & 0x3F];
|
|
117
|
+
output += lookup[(tmp << 2) & 0x3F];
|
|
118
|
+
output += '=';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
parts.push(output);
|
|
122
|
+
|
|
123
|
+
return parts.join('')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function read (buffer, offset, isLE, mLen, nBytes) {
|
|
127
|
+
var e, m;
|
|
128
|
+
var eLen = nBytes * 8 - mLen - 1;
|
|
129
|
+
var eMax = (1 << eLen) - 1;
|
|
130
|
+
var eBias = eMax >> 1;
|
|
131
|
+
var nBits = -7;
|
|
132
|
+
var i = isLE ? (nBytes - 1) : 0;
|
|
133
|
+
var d = isLE ? -1 : 1;
|
|
134
|
+
var s = buffer[offset + i];
|
|
135
|
+
|
|
136
|
+
i += d;
|
|
137
|
+
|
|
138
|
+
e = s & ((1 << (-nBits)) - 1);
|
|
139
|
+
s >>= (-nBits);
|
|
140
|
+
nBits += eLen;
|
|
141
|
+
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
|
|
142
|
+
|
|
143
|
+
m = e & ((1 << (-nBits)) - 1);
|
|
144
|
+
e >>= (-nBits);
|
|
145
|
+
nBits += mLen;
|
|
146
|
+
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
|
|
147
|
+
|
|
148
|
+
if (e === 0) {
|
|
149
|
+
e = 1 - eBias;
|
|
150
|
+
} else if (e === eMax) {
|
|
151
|
+
return m ? NaN : ((s ? -1 : 1) * Infinity)
|
|
152
|
+
} else {
|
|
153
|
+
m = m + Math.pow(2, mLen);
|
|
154
|
+
e = e - eBias;
|
|
155
|
+
}
|
|
156
|
+
return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function write (buffer, value, offset, isLE, mLen, nBytes) {
|
|
160
|
+
var e, m, c;
|
|
161
|
+
var eLen = nBytes * 8 - mLen - 1;
|
|
162
|
+
var eMax = (1 << eLen) - 1;
|
|
163
|
+
var eBias = eMax >> 1;
|
|
164
|
+
var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
|
|
165
|
+
var i = isLE ? 0 : (nBytes - 1);
|
|
166
|
+
var d = isLE ? 1 : -1;
|
|
167
|
+
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
168
|
+
|
|
169
|
+
value = Math.abs(value);
|
|
170
|
+
|
|
171
|
+
if (isNaN(value) || value === Infinity) {
|
|
172
|
+
m = isNaN(value) ? 1 : 0;
|
|
173
|
+
e = eMax;
|
|
174
|
+
} else {
|
|
175
|
+
e = Math.floor(Math.log(value) / Math.LN2);
|
|
176
|
+
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
177
|
+
e--;
|
|
178
|
+
c *= 2;
|
|
179
|
+
}
|
|
180
|
+
if (e + eBias >= 1) {
|
|
181
|
+
value += rt / c;
|
|
182
|
+
} else {
|
|
183
|
+
value += rt * Math.pow(2, 1 - eBias);
|
|
184
|
+
}
|
|
185
|
+
if (value * c >= 2) {
|
|
186
|
+
e++;
|
|
187
|
+
c /= 2;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (e + eBias >= eMax) {
|
|
191
|
+
m = 0;
|
|
192
|
+
e = eMax;
|
|
193
|
+
} else if (e + eBias >= 1) {
|
|
194
|
+
m = (value * c - 1) * Math.pow(2, mLen);
|
|
195
|
+
e = e + eBias;
|
|
196
|
+
} else {
|
|
197
|
+
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
198
|
+
e = 0;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
|
|
203
|
+
|
|
204
|
+
e = (e << mLen) | m;
|
|
205
|
+
eLen += mLen;
|
|
206
|
+
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
|
|
207
|
+
|
|
208
|
+
buffer[offset + i - d] |= s * 128;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
var toString = {}.toString;
|
|
212
|
+
|
|
213
|
+
var isArray = Array.isArray || function (arr) {
|
|
214
|
+
return toString.call(arr) == '[object Array]';
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/*!
|
|
218
|
+
* The buffer module from node.js, for the browser.
|
|
219
|
+
*
|
|
220
|
+
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
|
|
221
|
+
* @license MIT
|
|
222
|
+
*/
|
|
223
|
+
/* eslint-disable no-proto */
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
var INSPECT_MAX_BYTES = 50;
|
|
227
|
+
|
|
13
228
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
229
|
+
* If `Buffer.TYPED_ARRAY_SUPPORT`:
|
|
230
|
+
* === true Use Uint8Array implementation (fastest)
|
|
231
|
+
* === false Use Object implementation (most compatible, even IE6)
|
|
232
|
+
*
|
|
233
|
+
* Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
|
|
234
|
+
* Opera 11.6+, iOS 4.2+.
|
|
235
|
+
*
|
|
236
|
+
* Due to various browser bugs, sometimes the Object implementation will be used even
|
|
237
|
+
* when the browser supports typed arrays.
|
|
238
|
+
*
|
|
239
|
+
* Note:
|
|
240
|
+
*
|
|
241
|
+
* - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
|
|
242
|
+
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
|
|
243
|
+
*
|
|
244
|
+
* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
|
|
245
|
+
*
|
|
246
|
+
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
|
|
247
|
+
* incorrect length in some situations.
|
|
248
|
+
|
|
249
|
+
* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
|
|
250
|
+
* get the Object implementation, which is slower but behaves correctly.
|
|
251
|
+
*/
|
|
252
|
+
Buffer.TYPED_ARRAY_SUPPORT = global$1.TYPED_ARRAY_SUPPORT !== undefined
|
|
253
|
+
? global$1.TYPED_ARRAY_SUPPORT
|
|
254
|
+
: true;
|
|
255
|
+
|
|
256
|
+
/*
|
|
257
|
+
* Export kMaxLength after typed array support is determined.
|
|
16
258
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
Prefix["NCE"] = "nce";
|
|
45
|
-
Prefix["B"] = "B";
|
|
46
|
-
Prefix["O"] = "o";
|
|
47
|
-
Prefix["LO"] = "Lo";
|
|
48
|
-
Prefix["LLO"] = "LLo";
|
|
49
|
-
Prefix["P"] = "P";
|
|
50
|
-
Prefix["CO"] = "Co";
|
|
51
|
-
Prefix["ID"] = "id";
|
|
52
|
-
Prefix["EXPR"] = "expr";
|
|
53
|
-
Prefix["TZ"] = "TZ";
|
|
54
|
-
Prefix["VH"] = "vh";
|
|
55
|
-
Prefix["SASK"] = "sask";
|
|
56
|
-
Prefix["ZET1"] = "zet1";
|
|
57
|
-
Prefix["SR1"] = "sr1";
|
|
58
|
-
Prefix["SRC1"] = "src1";
|
|
59
|
-
Prefix["SH"] = "sh";
|
|
60
|
-
})(Prefix || (Prefix = {}));
|
|
259
|
+
kMaxLength();
|
|
260
|
+
|
|
261
|
+
function kMaxLength () {
|
|
262
|
+
return Buffer.TYPED_ARRAY_SUPPORT
|
|
263
|
+
? 0x7fffffff
|
|
264
|
+
: 0x3fffffff
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function createBuffer (that, length) {
|
|
268
|
+
if (kMaxLength() < length) {
|
|
269
|
+
throw new RangeError('Invalid typed array length')
|
|
270
|
+
}
|
|
271
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
272
|
+
// Return an augmented `Uint8Array` instance, for best performance
|
|
273
|
+
that = new Uint8Array(length);
|
|
274
|
+
that.__proto__ = Buffer.prototype;
|
|
275
|
+
} else {
|
|
276
|
+
// Fallback: Return an object instance of the Buffer class
|
|
277
|
+
if (that === null) {
|
|
278
|
+
that = new Buffer(length);
|
|
279
|
+
}
|
|
280
|
+
that.length = length;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return that
|
|
284
|
+
}
|
|
285
|
+
|
|
61
286
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
287
|
+
* The Buffer constructor returns instances of `Uint8Array` that have their
|
|
288
|
+
* prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
|
|
289
|
+
* `Uint8Array`, so the returned instances will have all the node `Buffer` methods
|
|
290
|
+
* and the `Uint8Array` methods. Square bracket notation works as expected -- it
|
|
291
|
+
* returns a single octet.
|
|
292
|
+
*
|
|
293
|
+
* The `Uint8Array` prototype remains unmodified.
|
|
64
294
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
[Prefix.SIG]: new Uint8Array([4, 130, 43]),
|
|
90
|
-
[Prefix.NET]: new Uint8Array([87, 82, 0]),
|
|
91
|
-
[Prefix.NCE]: new Uint8Array([69, 220, 169]),
|
|
92
|
-
[Prefix.B]: new Uint8Array([1, 52]),
|
|
93
|
-
[Prefix.O]: new Uint8Array([5, 116]),
|
|
94
|
-
[Prefix.LO]: new Uint8Array([133, 233]),
|
|
95
|
-
[Prefix.LLO]: new Uint8Array([29, 159, 109]),
|
|
96
|
-
[Prefix.P]: new Uint8Array([2, 170]),
|
|
97
|
-
[Prefix.CO]: new Uint8Array([79, 179]),
|
|
98
|
-
[Prefix.ID]: new Uint8Array([153, 103]),
|
|
99
|
-
[Prefix.EXPR]: new Uint8Array([13, 44, 64, 27]),
|
|
100
|
-
// Legacy prefix
|
|
101
|
-
[Prefix.TZ]: new Uint8Array([2, 90, 121]),
|
|
102
|
-
[Prefix.VH]: new Uint8Array([1, 106, 242]),
|
|
103
|
-
[Prefix.SASK]: new Uint8Array([11, 237, 20, 92]),
|
|
104
|
-
[Prefix.ZET1]: new Uint8Array([18, 71, 40, 223]),
|
|
105
|
-
[Prefix.SR1]: new Uint8Array([6, 124, 117]),
|
|
106
|
-
[Prefix.SRC1]: new Uint8Array([17, 165, 134, 138]),
|
|
107
|
-
[Prefix.SH]: new Uint8Array([2, 116, 180]),
|
|
295
|
+
|
|
296
|
+
function Buffer (arg, encodingOrOffset, length) {
|
|
297
|
+
if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
|
|
298
|
+
return new Buffer(arg, encodingOrOffset, length)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Common case.
|
|
302
|
+
if (typeof arg === 'number') {
|
|
303
|
+
if (typeof encodingOrOffset === 'string') {
|
|
304
|
+
throw new Error(
|
|
305
|
+
'If encoding is specified then the first argument must be a string'
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
return allocUnsafe(this, arg)
|
|
309
|
+
}
|
|
310
|
+
return from(this, arg, encodingOrOffset, length)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
Buffer.poolSize = 8192; // not used by this implementation
|
|
314
|
+
|
|
315
|
+
// TODO: Legacy, not needed anymore. Remove in next major version.
|
|
316
|
+
Buffer._augment = function (arr) {
|
|
317
|
+
arr.__proto__ = Buffer.prototype;
|
|
318
|
+
return arr
|
|
108
319
|
};
|
|
320
|
+
|
|
321
|
+
function from (that, value, encodingOrOffset, length) {
|
|
322
|
+
if (typeof value === 'number') {
|
|
323
|
+
throw new TypeError('"value" argument must not be a number')
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
|
|
327
|
+
return fromArrayBuffer(that, value, encodingOrOffset, length)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (typeof value === 'string') {
|
|
331
|
+
return fromString(that, value, encodingOrOffset)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return fromObject(that, value)
|
|
335
|
+
}
|
|
336
|
+
|
|
109
337
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
[Prefix.KT1]: 20,
|
|
120
|
-
[Prefix.EDPK]: 32,
|
|
121
|
-
[Prefix.SPPK]: 33,
|
|
122
|
-
[Prefix.P2PK]: 33,
|
|
123
|
-
[Prefix.BLPK]: 48,
|
|
124
|
-
[Prefix.EDSIG]: 64,
|
|
125
|
-
[Prefix.SPSIG]: 64,
|
|
126
|
-
[Prefix.P2SIG]: 64,
|
|
127
|
-
[Prefix.BLSIG]: 96,
|
|
128
|
-
[Prefix.SIG]: 64,
|
|
129
|
-
[Prefix.NET]: 4,
|
|
130
|
-
[Prefix.B]: 32,
|
|
131
|
-
[Prefix.P]: 32,
|
|
132
|
-
[Prefix.O]: 32,
|
|
133
|
-
[Prefix.VH]: 32,
|
|
134
|
-
[Prefix.SASK]: 169,
|
|
135
|
-
[Prefix.ZET1]: 43,
|
|
136
|
-
[Prefix.SR1]: 20,
|
|
137
|
-
[Prefix.SRC1]: 32,
|
|
138
|
-
[Prefix.SH]: 48,
|
|
338
|
+
* Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
|
|
339
|
+
* if value is a number.
|
|
340
|
+
* Buffer.from(str[, encoding])
|
|
341
|
+
* Buffer.from(array)
|
|
342
|
+
* Buffer.from(buffer)
|
|
343
|
+
* Buffer.from(arrayBuffer[, byteOffset[, length]])
|
|
344
|
+
**/
|
|
345
|
+
Buffer.from = function (value, encodingOrOffset, length) {
|
|
346
|
+
return from(null, value, encodingOrOffset, length)
|
|
139
347
|
};
|
|
348
|
+
|
|
349
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
350
|
+
Buffer.prototype.__proto__ = Uint8Array.prototype;
|
|
351
|
+
Buffer.__proto__ = Uint8Array;
|
|
352
|
+
if (typeof Symbol !== 'undefined' && Symbol.species &&
|
|
353
|
+
Buffer[Symbol.species] === Buffer) ;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function assertSize (size) {
|
|
357
|
+
if (typeof size !== 'number') {
|
|
358
|
+
throw new TypeError('"size" argument must be a number')
|
|
359
|
+
} else if (size < 0) {
|
|
360
|
+
throw new RangeError('"size" argument must not be negative')
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function alloc (that, size, fill, encoding) {
|
|
365
|
+
assertSize(size);
|
|
366
|
+
if (size <= 0) {
|
|
367
|
+
return createBuffer(that, size)
|
|
368
|
+
}
|
|
369
|
+
if (fill !== undefined) {
|
|
370
|
+
// Only pay attention to encoding if it's a string. This
|
|
371
|
+
// prevents accidentally sending in a number that would
|
|
372
|
+
// be interpretted as a start offset.
|
|
373
|
+
return typeof encoding === 'string'
|
|
374
|
+
? createBuffer(that, size).fill(fill, encoding)
|
|
375
|
+
: createBuffer(that, size).fill(fill)
|
|
376
|
+
}
|
|
377
|
+
return createBuffer(that, size)
|
|
378
|
+
}
|
|
379
|
+
|
|
140
380
|
/**
|
|
141
|
-
*
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
PrefixV2["BlindedPublicKeyHash"] = "btz1";
|
|
160
|
-
PrefixV2["BLS12_381PublicKeyHash"] = "tz4";
|
|
161
|
-
PrefixV2["TXRollupAddress"] = "txr1";
|
|
162
|
-
PrefixV2["ZkRollupHash"] = "epx1";
|
|
163
|
-
PrefixV2["ScRollupHash"] = "scr1";
|
|
164
|
-
PrefixV2["SmartRollupHash"] = "sr1";
|
|
165
|
-
PrefixV2["CryptoboxPublicKeyHash"] = "id";
|
|
166
|
-
PrefixV2["Ed25519Seed"] = "edsk";
|
|
167
|
-
PrefixV2["Ed25519PublicKey"] = "edpk";
|
|
168
|
-
PrefixV2["Secp256k1SecretKey"] = "spsk";
|
|
169
|
-
PrefixV2["P256SecretKey"] = "p2sk";
|
|
170
|
-
PrefixV2["BLS12_381SecretKey"] = "BLsk";
|
|
171
|
-
PrefixV2["ValueHash"] = "vh";
|
|
172
|
-
PrefixV2["CycleNonce"] = "nce";
|
|
173
|
-
PrefixV2["ScriptExpr"] = "expr";
|
|
174
|
-
PrefixV2["InboxHash"] = "txi";
|
|
175
|
-
PrefixV2["MessageHash"] = "txm";
|
|
176
|
-
PrefixV2["CommitmentHash"] = "txc";
|
|
177
|
-
PrefixV2["MessageResultHash"] = "txmr";
|
|
178
|
-
PrefixV2["MessageResultListHash"] = "txM";
|
|
179
|
-
PrefixV2["WithdrawListHash"] = "txw";
|
|
180
|
-
PrefixV2["ScRollupStateHash"] = "scs1";
|
|
181
|
-
PrefixV2["ScRollupCommitmentHash"] = "scc1";
|
|
182
|
-
PrefixV2["SmartRollupStateHash"] = "srs1";
|
|
183
|
-
PrefixV2["SmartRollupCommitmentHash"] = "src1";
|
|
184
|
-
PrefixV2["Ed25519EncryptedSeed"] = "edesk";
|
|
185
|
-
PrefixV2["Secp256k1EncryptedSecretKey"] = "spesk";
|
|
186
|
-
PrefixV2["P256EncryptedSecretKey"] = "p2esk";
|
|
187
|
-
PrefixV2["BLS12_381EncryptedSecretKey"] = "BLesk";
|
|
188
|
-
PrefixV2["Secp256k1EncryptedScalar"] = "seesk";
|
|
189
|
-
PrefixV2["Secp256k1PublicKey"] = "sppk";
|
|
190
|
-
PrefixV2["P256PublicKey"] = "p2pk";
|
|
191
|
-
PrefixV2["Secp256k1Scalar"] = "SSp";
|
|
192
|
-
PrefixV2["Secp256k1Element"] = "GSp";
|
|
193
|
-
PrefixV2["Ed25519SecretKey"] = "_edsk";
|
|
194
|
-
PrefixV2["Ed25519Signature"] = "edsig";
|
|
195
|
-
PrefixV2["Secp256k1Signature"] = "spsig1";
|
|
196
|
-
PrefixV2["P256Signature"] = "p2sig";
|
|
197
|
-
PrefixV2["GenericSignature"] = "sig";
|
|
198
|
-
PrefixV2["ChainID"] = "Net";
|
|
199
|
-
PrefixV2["SaplingSpendingKey"] = "sask";
|
|
200
|
-
PrefixV2["EncryptedSaplingSpendingKey"] = "_sask";
|
|
201
|
-
PrefixV2["SaplingAddress"] = "zet1";
|
|
202
|
-
PrefixV2["GenericAggregateSignature"] = "asig";
|
|
203
|
-
PrefixV2["BLS12_381Signature"] = "BLsig";
|
|
204
|
-
PrefixV2["BLS12_381PublicKey"] = "BLpk";
|
|
205
|
-
PrefixV2["SlotHeader"] = "sh";
|
|
206
|
-
})(PrefixV2 || (PrefixV2 = {}));
|
|
381
|
+
* Creates a new filled Buffer instance.
|
|
382
|
+
* alloc(size[, fill[, encoding]])
|
|
383
|
+
**/
|
|
384
|
+
Buffer.alloc = function (size, fill, encoding) {
|
|
385
|
+
return alloc(null, size, fill, encoding)
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
function allocUnsafe (that, size) {
|
|
389
|
+
assertSize(size);
|
|
390
|
+
that = createBuffer(that, size < 0 ? 0 : checked(size) | 0);
|
|
391
|
+
if (!Buffer.TYPED_ARRAY_SUPPORT) {
|
|
392
|
+
for (var i = 0; i < size; ++i) {
|
|
393
|
+
that[i] = 0;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return that
|
|
397
|
+
}
|
|
398
|
+
|
|
207
399
|
/**
|
|
208
|
-
*
|
|
209
|
-
*/
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
[PrefixV2.OperationHash]: new Uint8Array([5, 116]),
|
|
213
|
-
[PrefixV2.OperationListHash]: new Uint8Array([133, 233]),
|
|
214
|
-
[PrefixV2.OperationListListHash]: new Uint8Array([29, 159, 109]),
|
|
215
|
-
[PrefixV2.ProtocolHash]: new Uint8Array([2, 170]),
|
|
216
|
-
[PrefixV2.ContextHash]: new Uint8Array([79, 199]),
|
|
217
|
-
[PrefixV2.BlockMetadataHash]: new Uint8Array([234, 249]),
|
|
218
|
-
[PrefixV2.OperationMetadataHash]: new Uint8Array([5, 183]),
|
|
219
|
-
[PrefixV2.OperationMetadataListHash]: new Uint8Array([134, 39]),
|
|
220
|
-
[PrefixV2.OperationMetadataListListHash]: new Uint8Array([29, 159, 182]),
|
|
221
|
-
[PrefixV2.Ed25519PublicKeyHash]: new Uint8Array([6, 161, 159]),
|
|
222
|
-
[PrefixV2.Secp256k1PublicKeyHash]: new Uint8Array([6, 161, 161]),
|
|
223
|
-
[PrefixV2.P256PublicKeyHash]: new Uint8Array([6, 161, 164]),
|
|
224
|
-
[PrefixV2.ContractHash]: new Uint8Array([2, 90, 121]),
|
|
225
|
-
[PrefixV2.BlindedPublicKeyHash]: new Uint8Array([1, 2, 49, 223]),
|
|
226
|
-
[PrefixV2.BLS12_381PublicKeyHash]: new Uint8Array([6, 161, 166]),
|
|
227
|
-
[PrefixV2.TXRollupAddress]: new Uint8Array([1, 128, 120, 31]),
|
|
228
|
-
[PrefixV2.ZkRollupHash]: new Uint8Array([1, 23, 224, 125]),
|
|
229
|
-
[PrefixV2.ScRollupHash]: new Uint8Array([1, 118, 132, 217]),
|
|
230
|
-
[PrefixV2.SmartRollupHash]: new Uint8Array([6, 124, 117]),
|
|
231
|
-
[PrefixV2.CryptoboxPublicKeyHash]: new Uint8Array([153, 103]),
|
|
232
|
-
[PrefixV2.Ed25519Seed]: new Uint8Array([13, 15, 58, 7]),
|
|
233
|
-
[PrefixV2.Ed25519PublicKey]: new Uint8Array([13, 15, 37, 217]),
|
|
234
|
-
[PrefixV2.Secp256k1SecretKey]: new Uint8Array([17, 162, 224, 201]),
|
|
235
|
-
[PrefixV2.P256SecretKey]: new Uint8Array([16, 81, 238, 189]),
|
|
236
|
-
[PrefixV2.BLS12_381SecretKey]: new Uint8Array([3, 150, 192, 40]),
|
|
237
|
-
[PrefixV2.ValueHash]: new Uint8Array([1, 106, 242]),
|
|
238
|
-
[PrefixV2.CycleNonce]: new Uint8Array([69, 220, 169]),
|
|
239
|
-
[PrefixV2.ScriptExpr]: new Uint8Array([13, 44, 64, 27]),
|
|
240
|
-
[PrefixV2.InboxHash]: new Uint8Array([79, 148, 196]),
|
|
241
|
-
[PrefixV2.MessageHash]: new Uint8Array([79, 149, 30]),
|
|
242
|
-
[PrefixV2.CommitmentHash]: new Uint8Array([79, 148, 17]),
|
|
243
|
-
[PrefixV2.MessageResultHash]: new Uint8Array([18, 7, 206, 87]),
|
|
244
|
-
[PrefixV2.MessageResultListHash]: new Uint8Array([79, 146, 82]),
|
|
245
|
-
[PrefixV2.WithdrawListHash]: new Uint8Array([79, 150, 72]),
|
|
246
|
-
[PrefixV2.ScRollupStateHash]: new Uint8Array([17, 144, 122, 202]),
|
|
247
|
-
[PrefixV2.ScRollupCommitmentHash]: new Uint8Array([17, 144, 21, 100]),
|
|
248
|
-
[PrefixV2.SmartRollupStateHash]: new Uint8Array([17, 165, 235, 240]),
|
|
249
|
-
[PrefixV2.SmartRollupCommitmentHash]: new Uint8Array([17, 165, 134, 138]),
|
|
250
|
-
[PrefixV2.Ed25519EncryptedSeed]: new Uint8Array([7, 90, 60, 179, 41]),
|
|
251
|
-
[PrefixV2.Secp256k1EncryptedSecretKey]: new Uint8Array([9, 237, 241, 174, 150]),
|
|
252
|
-
[PrefixV2.P256EncryptedSecretKey]: new Uint8Array([9, 48, 57, 115, 171]),
|
|
253
|
-
[PrefixV2.BLS12_381EncryptedSecretKey]: new Uint8Array([2, 5, 30, 53, 25]),
|
|
254
|
-
[PrefixV2.Secp256k1EncryptedScalar]: new Uint8Array([1, 131, 36, 86, 248]),
|
|
255
|
-
[PrefixV2.Secp256k1PublicKey]: new Uint8Array([3, 254, 226, 86]),
|
|
256
|
-
[PrefixV2.P256PublicKey]: new Uint8Array([3, 178, 139, 127]),
|
|
257
|
-
[PrefixV2.Secp256k1Scalar]: new Uint8Array([38, 248, 136]),
|
|
258
|
-
[PrefixV2.Secp256k1Element]: new Uint8Array([5, 92, 0]),
|
|
259
|
-
[PrefixV2.Ed25519SecretKey]: new Uint8Array([43, 246, 78, 7]),
|
|
260
|
-
[PrefixV2.Ed25519Signature]: new Uint8Array([9, 245, 205, 134, 18]),
|
|
261
|
-
[PrefixV2.Secp256k1Signature]: new Uint8Array([13, 115, 101, 19, 63]),
|
|
262
|
-
[PrefixV2.P256Signature]: new Uint8Array([54, 240, 44, 52]),
|
|
263
|
-
[PrefixV2.GenericSignature]: new Uint8Array([4, 130, 43]),
|
|
264
|
-
[PrefixV2.ChainID]: new Uint8Array([87, 82, 0]),
|
|
265
|
-
[PrefixV2.SaplingSpendingKey]: new Uint8Array([11, 237, 20, 92]),
|
|
266
|
-
[PrefixV2.EncryptedSaplingSpendingKey]: new Uint8Array([11, 237, 20, 92]),
|
|
267
|
-
[PrefixV2.SaplingAddress]: new Uint8Array([18, 71, 40, 223]),
|
|
268
|
-
[PrefixV2.GenericAggregateSignature]: new Uint8Array([2, 75, 234, 101]),
|
|
269
|
-
[PrefixV2.BLS12_381Signature]: new Uint8Array([40, 171, 64, 207]),
|
|
270
|
-
[PrefixV2.BLS12_381PublicKey]: new Uint8Array([6, 149, 135, 204]),
|
|
271
|
-
[PrefixV2.SlotHeader]: new Uint8Array([2, 116, 180]),
|
|
400
|
+
* Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
|
|
401
|
+
* */
|
|
402
|
+
Buffer.allocUnsafe = function (size) {
|
|
403
|
+
return allocUnsafe(null, size)
|
|
272
404
|
};
|
|
273
405
|
/**
|
|
274
|
-
*
|
|
406
|
+
* Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
|
|
275
407
|
*/
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
[PrefixV2.OperationHash]: 32,
|
|
279
|
-
[PrefixV2.OperationListHash]: 32,
|
|
280
|
-
[PrefixV2.OperationListListHash]: 32,
|
|
281
|
-
[PrefixV2.ProtocolHash]: 32,
|
|
282
|
-
[PrefixV2.ContextHash]: 32,
|
|
283
|
-
[PrefixV2.BlockMetadataHash]: 32,
|
|
284
|
-
[PrefixV2.OperationMetadataHash]: 32,
|
|
285
|
-
[PrefixV2.OperationMetadataListHash]: 32,
|
|
286
|
-
[PrefixV2.OperationMetadataListListHash]: 32,
|
|
287
|
-
[PrefixV2.Ed25519PublicKeyHash]: 20,
|
|
288
|
-
[PrefixV2.Secp256k1PublicKeyHash]: 20,
|
|
289
|
-
[PrefixV2.P256PublicKeyHash]: 20,
|
|
290
|
-
[PrefixV2.ContractHash]: 20,
|
|
291
|
-
[PrefixV2.BlindedPublicKeyHash]: 20,
|
|
292
|
-
[PrefixV2.BLS12_381PublicKeyHash]: 20,
|
|
293
|
-
[PrefixV2.TXRollupAddress]: 20,
|
|
294
|
-
[PrefixV2.ZkRollupHash]: 20,
|
|
295
|
-
[PrefixV2.ScRollupHash]: 20,
|
|
296
|
-
[PrefixV2.SmartRollupHash]: 20,
|
|
297
|
-
[PrefixV2.CryptoboxPublicKeyHash]: 16,
|
|
298
|
-
[PrefixV2.Ed25519Seed]: 32,
|
|
299
|
-
[PrefixV2.Ed25519PublicKey]: 32,
|
|
300
|
-
[PrefixV2.Secp256k1SecretKey]: 32,
|
|
301
|
-
[PrefixV2.P256SecretKey]: 32,
|
|
302
|
-
[PrefixV2.BLS12_381SecretKey]: 32,
|
|
303
|
-
[PrefixV2.ValueHash]: 32,
|
|
304
|
-
[PrefixV2.CycleNonce]: 32,
|
|
305
|
-
[PrefixV2.ScriptExpr]: 32,
|
|
306
|
-
[PrefixV2.InboxHash]: 32,
|
|
307
|
-
[PrefixV2.MessageHash]: 32,
|
|
308
|
-
[PrefixV2.CommitmentHash]: 32,
|
|
309
|
-
[PrefixV2.MessageResultHash]: 32,
|
|
310
|
-
[PrefixV2.MessageResultListHash]: 32,
|
|
311
|
-
[PrefixV2.WithdrawListHash]: 32,
|
|
312
|
-
[PrefixV2.ScRollupStateHash]: 32,
|
|
313
|
-
[PrefixV2.ScRollupCommitmentHash]: 32,
|
|
314
|
-
[PrefixV2.SmartRollupStateHash]: 32,
|
|
315
|
-
[PrefixV2.SmartRollupCommitmentHash]: 32,
|
|
316
|
-
[PrefixV2.Ed25519EncryptedSeed]: 56,
|
|
317
|
-
[PrefixV2.Secp256k1EncryptedSecretKey]: 56,
|
|
318
|
-
[PrefixV2.P256EncryptedSecretKey]: 56,
|
|
319
|
-
[PrefixV2.BLS12_381EncryptedSecretKey]: 56,
|
|
320
|
-
[PrefixV2.Secp256k1EncryptedScalar]: 60,
|
|
321
|
-
[PrefixV2.Secp256k1PublicKey]: 33,
|
|
322
|
-
[PrefixV2.P256PublicKey]: 33,
|
|
323
|
-
[PrefixV2.Secp256k1Scalar]: 33,
|
|
324
|
-
[PrefixV2.Secp256k1Element]: 33,
|
|
325
|
-
[PrefixV2.Ed25519SecretKey]: 64,
|
|
326
|
-
[PrefixV2.Ed25519Signature]: 64,
|
|
327
|
-
[PrefixV2.Secp256k1Signature]: 64,
|
|
328
|
-
[PrefixV2.P256Signature]: 64,
|
|
329
|
-
[PrefixV2.GenericSignature]: 64,
|
|
330
|
-
[PrefixV2.ChainID]: 4,
|
|
331
|
-
[PrefixV2.SaplingSpendingKey]: 169,
|
|
332
|
-
[PrefixV2.EncryptedSaplingSpendingKey]: 193,
|
|
333
|
-
[PrefixV2.SaplingAddress]: 43,
|
|
334
|
-
[PrefixV2.GenericAggregateSignature]: 96,
|
|
335
|
-
[PrefixV2.BLS12_381Signature]: 96,
|
|
336
|
-
[PrefixV2.BLS12_381PublicKey]: 48,
|
|
337
|
-
[PrefixV2.SlotHeader]: 48,
|
|
408
|
+
Buffer.allocUnsafeSlow = function (size) {
|
|
409
|
+
return allocUnsafe(null, size)
|
|
338
410
|
};
|
|
339
411
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
412
|
+
function fromString (that, string, encoding) {
|
|
413
|
+
if (typeof encoding !== 'string' || encoding === '') {
|
|
414
|
+
encoding = 'utf8';
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (!Buffer.isEncoding(encoding)) {
|
|
418
|
+
throw new TypeError('"encoding" must be a valid string encoding')
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
var length = byteLength(string, encoding) | 0;
|
|
422
|
+
that = createBuffer(that, length);
|
|
423
|
+
|
|
424
|
+
var actual = that.write(string, encoding);
|
|
425
|
+
|
|
426
|
+
if (actual !== length) {
|
|
427
|
+
// Writing a hex string, for example, that contains invalid characters will
|
|
428
|
+
// cause everything after the first invalid character to be ignored. (e.g.
|
|
429
|
+
// 'abxxcd' will be treated as 'ab')
|
|
430
|
+
that = that.slice(0, actual);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return that
|
|
353
434
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
435
|
+
|
|
436
|
+
function fromArrayLike (that, array) {
|
|
437
|
+
var length = array.length < 0 ? 0 : checked(array.length) | 0;
|
|
438
|
+
that = createBuffer(that, length);
|
|
439
|
+
for (var i = 0; i < length; i += 1) {
|
|
440
|
+
that[i] = array[i] & 255;
|
|
441
|
+
}
|
|
442
|
+
return that
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function fromArrayBuffer (that, array, byteOffset, length) {
|
|
446
|
+
array.byteLength; // this throws if `array` is not a valid ArrayBuffer
|
|
447
|
+
|
|
448
|
+
if (byteOffset < 0 || array.byteLength < byteOffset) {
|
|
449
|
+
throw new RangeError('\'offset\' is out of bounds')
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (array.byteLength < byteOffset + (length || 0)) {
|
|
453
|
+
throw new RangeError('\'length\' is out of bounds')
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (byteOffset === undefined && length === undefined) {
|
|
457
|
+
array = new Uint8Array(array);
|
|
458
|
+
} else if (length === undefined) {
|
|
459
|
+
array = new Uint8Array(array, byteOffset);
|
|
460
|
+
} else {
|
|
461
|
+
array = new Uint8Array(array, byteOffset, length);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
465
|
+
// Return an augmented `Uint8Array` instance, for best performance
|
|
466
|
+
that = array;
|
|
467
|
+
that.__proto__ = Buffer.prototype;
|
|
468
|
+
} else {
|
|
469
|
+
// Fallback: Return an object instance of the Buffer class
|
|
470
|
+
that = fromArrayLike(that, array);
|
|
471
|
+
}
|
|
472
|
+
return that
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function fromObject (that, obj) {
|
|
476
|
+
if (internalIsBuffer(obj)) {
|
|
477
|
+
var len = checked(obj.length) | 0;
|
|
478
|
+
that = createBuffer(that, len);
|
|
479
|
+
|
|
480
|
+
if (that.length === 0) {
|
|
481
|
+
return that
|
|
372
482
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
483
|
+
|
|
484
|
+
obj.copy(that, 0, 0, len);
|
|
485
|
+
return that
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (obj) {
|
|
489
|
+
if ((typeof ArrayBuffer !== 'undefined' &&
|
|
490
|
+
obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
|
|
491
|
+
if (typeof obj.length !== 'number' || isnan(obj.length)) {
|
|
492
|
+
return createBuffer(that, 0)
|
|
493
|
+
}
|
|
494
|
+
return fromArrayLike(that, obj)
|
|
378
495
|
}
|
|
379
|
-
|
|
496
|
+
|
|
497
|
+
if (obj.type === 'Buffer' && isArray(obj.data)) {
|
|
498
|
+
return fromArrayLike(that, obj.data)
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
|
|
380
503
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
* const pkh = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx'
|
|
391
|
-
* const validation = validateAddress(pkh)
|
|
392
|
-
* console.log(validation)
|
|
393
|
-
* // This example return 3 which correspond to VALID
|
|
394
|
-
* ```
|
|
395
|
-
*/
|
|
396
|
-
function validateAddress(value) {
|
|
397
|
-
const [addr] = splitAddress(value !== null && value !== void 0 ? value : '');
|
|
398
|
-
return validatePrefixedValue(addr, addressPrefixes);
|
|
504
|
+
|
|
505
|
+
function checked (length) {
|
|
506
|
+
// Note: cannot use `length < kMaxLength()` here because that fails when
|
|
507
|
+
// length is NaN (which is otherwise coerced to zero.)
|
|
508
|
+
if (length >= kMaxLength()) {
|
|
509
|
+
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
|
|
510
|
+
'size: 0x' + kMaxLength().toString(16) + ' bytes')
|
|
511
|
+
}
|
|
512
|
+
return length | 0
|
|
399
513
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
* @returns
|
|
404
|
-
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
405
|
-
*
|
|
406
|
-
* @example
|
|
407
|
-
* ```
|
|
408
|
-
* import { validateChain } from '@taquito/utils';
|
|
409
|
-
* const chainId = 'NetXdQprcVkpaWU'
|
|
410
|
-
* const validation = validateChain(chainId)
|
|
411
|
-
* console.log(validation)
|
|
412
|
-
* // This example return 3 which correspond to VALID
|
|
413
|
-
* ```
|
|
414
|
-
*/
|
|
415
|
-
function validateChain(value) {
|
|
416
|
-
return validatePrefixedValue(value, [PrefixV2.ChainID]);
|
|
514
|
+
Buffer.isBuffer = isBuffer;
|
|
515
|
+
function internalIsBuffer (b) {
|
|
516
|
+
return !!(b != null && b._isBuffer)
|
|
417
517
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
518
|
+
|
|
519
|
+
Buffer.compare = function compare (a, b) {
|
|
520
|
+
if (!internalIsBuffer(a) || !internalIsBuffer(b)) {
|
|
521
|
+
throw new TypeError('Arguments must be Buffers')
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (a === b) return 0
|
|
525
|
+
|
|
526
|
+
var x = a.length;
|
|
527
|
+
var y = b.length;
|
|
528
|
+
|
|
529
|
+
for (var i = 0, len = Math.min(x, y); i < len; ++i) {
|
|
530
|
+
if (a[i] !== b[i]) {
|
|
531
|
+
x = a[i];
|
|
532
|
+
y = b[i];
|
|
533
|
+
break
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (x < y) return -1
|
|
538
|
+
if (y < x) return 1
|
|
539
|
+
return 0
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
Buffer.isEncoding = function isEncoding (encoding) {
|
|
543
|
+
switch (String(encoding).toLowerCase()) {
|
|
544
|
+
case 'hex':
|
|
545
|
+
case 'utf8':
|
|
546
|
+
case 'utf-8':
|
|
547
|
+
case 'ascii':
|
|
548
|
+
case 'latin1':
|
|
549
|
+
case 'binary':
|
|
550
|
+
case 'base64':
|
|
551
|
+
case 'ucs2':
|
|
552
|
+
case 'ucs-2':
|
|
553
|
+
case 'utf16le':
|
|
554
|
+
case 'utf-16le':
|
|
555
|
+
return true
|
|
556
|
+
default:
|
|
557
|
+
return false
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
Buffer.concat = function concat (list, length) {
|
|
562
|
+
if (!isArray(list)) {
|
|
563
|
+
throw new TypeError('"list" argument must be an Array of Buffers')
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if (list.length === 0) {
|
|
567
|
+
return Buffer.alloc(0)
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
var i;
|
|
571
|
+
if (length === undefined) {
|
|
572
|
+
length = 0;
|
|
573
|
+
for (i = 0; i < list.length; ++i) {
|
|
574
|
+
length += list[i].length;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
var buffer = Buffer.allocUnsafe(length);
|
|
579
|
+
var pos = 0;
|
|
580
|
+
for (i = 0; i < list.length; ++i) {
|
|
581
|
+
var buf = list[i];
|
|
582
|
+
if (!internalIsBuffer(buf)) {
|
|
583
|
+
throw new TypeError('"list" argument must be an Array of Buffers')
|
|
584
|
+
}
|
|
585
|
+
buf.copy(buffer, pos);
|
|
586
|
+
pos += buf.length;
|
|
587
|
+
}
|
|
588
|
+
return buffer
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
function byteLength (string, encoding) {
|
|
592
|
+
if (internalIsBuffer(string)) {
|
|
593
|
+
return string.length
|
|
594
|
+
}
|
|
595
|
+
if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
|
|
596
|
+
(ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
|
|
597
|
+
return string.byteLength
|
|
598
|
+
}
|
|
599
|
+
if (typeof string !== 'string') {
|
|
600
|
+
string = '' + string;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
var len = string.length;
|
|
604
|
+
if (len === 0) return 0
|
|
605
|
+
|
|
606
|
+
// Use a for loop to avoid recursion
|
|
607
|
+
var loweredCase = false;
|
|
608
|
+
for (;;) {
|
|
609
|
+
switch (encoding) {
|
|
610
|
+
case 'ascii':
|
|
611
|
+
case 'latin1':
|
|
612
|
+
case 'binary':
|
|
613
|
+
return len
|
|
614
|
+
case 'utf8':
|
|
615
|
+
case 'utf-8':
|
|
616
|
+
case undefined:
|
|
617
|
+
return utf8ToBytes(string).length
|
|
618
|
+
case 'ucs2':
|
|
619
|
+
case 'ucs-2':
|
|
620
|
+
case 'utf16le':
|
|
621
|
+
case 'utf-16le':
|
|
622
|
+
return len * 2
|
|
623
|
+
case 'hex':
|
|
624
|
+
return len >>> 1
|
|
625
|
+
case 'base64':
|
|
626
|
+
return base64ToBytes(string).length
|
|
627
|
+
default:
|
|
628
|
+
if (loweredCase) return utf8ToBytes(string).length // assume utf8
|
|
629
|
+
encoding = ('' + encoding).toLowerCase();
|
|
630
|
+
loweredCase = true;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
435
633
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
634
|
+
Buffer.byteLength = byteLength;
|
|
635
|
+
|
|
636
|
+
function slowToString (encoding, start, end) {
|
|
637
|
+
var loweredCase = false;
|
|
638
|
+
|
|
639
|
+
// No need to verify that "this.length <= MAX_UINT32" since it's a read-only
|
|
640
|
+
// property of a typed array.
|
|
641
|
+
|
|
642
|
+
// This behaves neither like String nor Uint8Array in that we set start/end
|
|
643
|
+
// to their upper/lower bounds if the value passed is out of range.
|
|
644
|
+
// undefined is handled specially as per ECMA-262 6th Edition,
|
|
645
|
+
// Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
|
|
646
|
+
if (start === undefined || start < 0) {
|
|
647
|
+
start = 0;
|
|
648
|
+
}
|
|
649
|
+
// Return early if start > this.length. Done here to prevent potential uint32
|
|
650
|
+
// coercion fail below.
|
|
651
|
+
if (start > this.length) {
|
|
652
|
+
return ''
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (end === undefined || end > this.length) {
|
|
656
|
+
end = this.length;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (end <= 0) {
|
|
660
|
+
return ''
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// Force coersion to uint32. This will also coerce falsey/NaN values to 0.
|
|
664
|
+
end >>>= 0;
|
|
665
|
+
start >>>= 0;
|
|
666
|
+
|
|
667
|
+
if (end <= start) {
|
|
668
|
+
return ''
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (!encoding) encoding = 'utf8';
|
|
672
|
+
|
|
673
|
+
while (true) {
|
|
674
|
+
switch (encoding) {
|
|
675
|
+
case 'hex':
|
|
676
|
+
return hexSlice(this, start, end)
|
|
677
|
+
|
|
678
|
+
case 'utf8':
|
|
679
|
+
case 'utf-8':
|
|
680
|
+
return utf8Slice(this, start, end)
|
|
681
|
+
|
|
682
|
+
case 'ascii':
|
|
683
|
+
return asciiSlice(this, start, end)
|
|
684
|
+
|
|
685
|
+
case 'latin1':
|
|
686
|
+
case 'binary':
|
|
687
|
+
return latin1Slice(this, start, end)
|
|
688
|
+
|
|
689
|
+
case 'base64':
|
|
690
|
+
return base64Slice(this, start, end)
|
|
691
|
+
|
|
692
|
+
case 'ucs2':
|
|
693
|
+
case 'ucs-2':
|
|
694
|
+
case 'utf16le':
|
|
695
|
+
case 'utf-16le':
|
|
696
|
+
return utf16leSlice(this, start, end)
|
|
697
|
+
|
|
698
|
+
default:
|
|
699
|
+
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
|
700
|
+
encoding = (encoding + '').toLowerCase();
|
|
701
|
+
loweredCase = true;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
453
704
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
* const signature = 'edsigtkpiSSschcaCt9pUVrpNPf7TTcgvgDEDD6NCEHMy8NNQJCGnMfLZzYoQj74yLjo9wx6MPVV29CvVzgi7qEcEUok3k7AuMg'
|
|
464
|
-
* const validation = validateSignature(signature)
|
|
465
|
-
* console.log(validation)
|
|
466
|
-
* // This example return 3 which correspond to VALID
|
|
467
|
-
* ```
|
|
468
|
-
*/
|
|
469
|
-
function validateSignature(value) {
|
|
470
|
-
return validatePrefixedValue(value, signaturePrefixes);
|
|
705
|
+
|
|
706
|
+
// The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
|
|
707
|
+
// Buffer instances.
|
|
708
|
+
Buffer.prototype._isBuffer = true;
|
|
709
|
+
|
|
710
|
+
function swap (b, n, m) {
|
|
711
|
+
var i = b[n];
|
|
712
|
+
b[n] = b[m];
|
|
713
|
+
b[m] = i;
|
|
471
714
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
715
|
+
|
|
716
|
+
Buffer.prototype.swap16 = function swap16 () {
|
|
717
|
+
var len = this.length;
|
|
718
|
+
if (len % 2 !== 0) {
|
|
719
|
+
throw new RangeError('Buffer size must be a multiple of 16-bits')
|
|
720
|
+
}
|
|
721
|
+
for (var i = 0; i < len; i += 2) {
|
|
722
|
+
swap(this, i, i + 1);
|
|
723
|
+
}
|
|
724
|
+
return this
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
Buffer.prototype.swap32 = function swap32 () {
|
|
728
|
+
var len = this.length;
|
|
729
|
+
if (len % 4 !== 0) {
|
|
730
|
+
throw new RangeError('Buffer size must be a multiple of 32-bits')
|
|
731
|
+
}
|
|
732
|
+
for (var i = 0; i < len; i += 4) {
|
|
733
|
+
swap(this, i, i + 3);
|
|
734
|
+
swap(this, i + 1, i + 2);
|
|
735
|
+
}
|
|
736
|
+
return this
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
Buffer.prototype.swap64 = function swap64 () {
|
|
740
|
+
var len = this.length;
|
|
741
|
+
if (len % 8 !== 0) {
|
|
742
|
+
throw new RangeError('Buffer size must be a multiple of 64-bits')
|
|
743
|
+
}
|
|
744
|
+
for (var i = 0; i < len; i += 8) {
|
|
745
|
+
swap(this, i, i + 7);
|
|
746
|
+
swap(this, i + 1, i + 6);
|
|
747
|
+
swap(this, i + 2, i + 5);
|
|
748
|
+
swap(this, i + 3, i + 4);
|
|
749
|
+
}
|
|
750
|
+
return this
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
Buffer.prototype.toString = function toString () {
|
|
754
|
+
var length = this.length | 0;
|
|
755
|
+
if (length === 0) return ''
|
|
756
|
+
if (arguments.length === 0) return utf8Slice(this, 0, length)
|
|
757
|
+
return slowToString.apply(this, arguments)
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
Buffer.prototype.equals = function equals (b) {
|
|
761
|
+
if (!internalIsBuffer(b)) throw new TypeError('Argument must be a Buffer')
|
|
762
|
+
if (this === b) return true
|
|
763
|
+
return Buffer.compare(this, b) === 0
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
Buffer.prototype.inspect = function inspect () {
|
|
767
|
+
var str = '';
|
|
768
|
+
var max = INSPECT_MAX_BYTES;
|
|
769
|
+
if (this.length > 0) {
|
|
770
|
+
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
|
|
771
|
+
if (this.length > max) str += ' ... ';
|
|
772
|
+
}
|
|
773
|
+
return '<Buffer ' + str + '>'
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
|
|
777
|
+
if (!internalIsBuffer(target)) {
|
|
778
|
+
throw new TypeError('Argument must be a Buffer')
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if (start === undefined) {
|
|
782
|
+
start = 0;
|
|
783
|
+
}
|
|
784
|
+
if (end === undefined) {
|
|
785
|
+
end = target ? target.length : 0;
|
|
786
|
+
}
|
|
787
|
+
if (thisStart === undefined) {
|
|
788
|
+
thisStart = 0;
|
|
789
|
+
}
|
|
790
|
+
if (thisEnd === undefined) {
|
|
791
|
+
thisEnd = this.length;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
|
|
795
|
+
throw new RangeError('out of range index')
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
if (thisStart >= thisEnd && start >= end) {
|
|
799
|
+
return 0
|
|
800
|
+
}
|
|
801
|
+
if (thisStart >= thisEnd) {
|
|
802
|
+
return -1
|
|
803
|
+
}
|
|
804
|
+
if (start >= end) {
|
|
805
|
+
return 1
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
start >>>= 0;
|
|
809
|
+
end >>>= 0;
|
|
810
|
+
thisStart >>>= 0;
|
|
811
|
+
thisEnd >>>= 0;
|
|
812
|
+
|
|
813
|
+
if (this === target) return 0
|
|
814
|
+
|
|
815
|
+
var x = thisEnd - thisStart;
|
|
816
|
+
var y = end - start;
|
|
817
|
+
var len = Math.min(x, y);
|
|
818
|
+
|
|
819
|
+
var thisCopy = this.slice(thisStart, thisEnd);
|
|
820
|
+
var targetCopy = target.slice(start, end);
|
|
821
|
+
|
|
822
|
+
for (var i = 0; i < len; ++i) {
|
|
823
|
+
if (thisCopy[i] !== targetCopy[i]) {
|
|
824
|
+
x = thisCopy[i];
|
|
825
|
+
y = targetCopy[i];
|
|
826
|
+
break
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
if (x < y) return -1
|
|
831
|
+
if (y < x) return 1
|
|
832
|
+
return 0
|
|
833
|
+
};
|
|
834
|
+
|
|
835
|
+
// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
|
|
836
|
+
// OR the last index of `val` in `buffer` at offset <= `byteOffset`.
|
|
837
|
+
//
|
|
838
|
+
// Arguments:
|
|
839
|
+
// - buffer - a Buffer to search
|
|
840
|
+
// - val - a string, Buffer, or number
|
|
841
|
+
// - byteOffset - an index into `buffer`; will be clamped to an int32
|
|
842
|
+
// - encoding - an optional encoding, relevant is val is a string
|
|
843
|
+
// - dir - true for indexOf, false for lastIndexOf
|
|
844
|
+
function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
|
|
845
|
+
// Empty buffer means no match
|
|
846
|
+
if (buffer.length === 0) return -1
|
|
847
|
+
|
|
848
|
+
// Normalize byteOffset
|
|
849
|
+
if (typeof byteOffset === 'string') {
|
|
850
|
+
encoding = byteOffset;
|
|
851
|
+
byteOffset = 0;
|
|
852
|
+
} else if (byteOffset > 0x7fffffff) {
|
|
853
|
+
byteOffset = 0x7fffffff;
|
|
854
|
+
} else if (byteOffset < -2147483648) {
|
|
855
|
+
byteOffset = -2147483648;
|
|
856
|
+
}
|
|
857
|
+
byteOffset = +byteOffset; // Coerce to Number.
|
|
858
|
+
if (isNaN(byteOffset)) {
|
|
859
|
+
// byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
|
|
860
|
+
byteOffset = dir ? 0 : (buffer.length - 1);
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// Normalize byteOffset: negative offsets start from the end of the buffer
|
|
864
|
+
if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
|
|
865
|
+
if (byteOffset >= buffer.length) {
|
|
866
|
+
if (dir) return -1
|
|
867
|
+
else byteOffset = buffer.length - 1;
|
|
868
|
+
} else if (byteOffset < 0) {
|
|
869
|
+
if (dir) byteOffset = 0;
|
|
870
|
+
else return -1
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// Normalize val
|
|
874
|
+
if (typeof val === 'string') {
|
|
875
|
+
val = Buffer.from(val, encoding);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
// Finally, search either indexOf (if dir is true) or lastIndexOf
|
|
879
|
+
if (internalIsBuffer(val)) {
|
|
880
|
+
// Special case: looking for empty string/buffer always fails
|
|
881
|
+
if (val.length === 0) {
|
|
882
|
+
return -1
|
|
883
|
+
}
|
|
884
|
+
return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
|
|
885
|
+
} else if (typeof val === 'number') {
|
|
886
|
+
val = val & 0xFF; // Search for a byte value [0-255]
|
|
887
|
+
if (Buffer.TYPED_ARRAY_SUPPORT &&
|
|
888
|
+
typeof Uint8Array.prototype.indexOf === 'function') {
|
|
889
|
+
if (dir) {
|
|
890
|
+
return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
|
|
891
|
+
} else {
|
|
892
|
+
return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
throw new TypeError('val must be string, number or Buffer')
|
|
489
899
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
900
|
+
|
|
901
|
+
function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
|
|
902
|
+
var indexSize = 1;
|
|
903
|
+
var arrLength = arr.length;
|
|
904
|
+
var valLength = val.length;
|
|
905
|
+
|
|
906
|
+
if (encoding !== undefined) {
|
|
907
|
+
encoding = String(encoding).toLowerCase();
|
|
908
|
+
if (encoding === 'ucs2' || encoding === 'ucs-2' ||
|
|
909
|
+
encoding === 'utf16le' || encoding === 'utf-16le') {
|
|
910
|
+
if (arr.length < 2 || val.length < 2) {
|
|
911
|
+
return -1
|
|
912
|
+
}
|
|
913
|
+
indexSize = 2;
|
|
914
|
+
arrLength /= 2;
|
|
915
|
+
valLength /= 2;
|
|
916
|
+
byteOffset /= 2;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function read (buf, i) {
|
|
921
|
+
if (indexSize === 1) {
|
|
922
|
+
return buf[i]
|
|
923
|
+
} else {
|
|
924
|
+
return buf.readUInt16BE(i * indexSize)
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
var i;
|
|
929
|
+
if (dir) {
|
|
930
|
+
var foundIndex = -1;
|
|
931
|
+
for (i = byteOffset; i < arrLength; i++) {
|
|
932
|
+
if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
|
|
933
|
+
if (foundIndex === -1) foundIndex = i;
|
|
934
|
+
if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
|
|
935
|
+
} else {
|
|
936
|
+
if (foundIndex !== -1) i -= i - foundIndex;
|
|
937
|
+
foundIndex = -1;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
} else {
|
|
941
|
+
if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
|
|
942
|
+
for (i = byteOffset; i >= 0; i--) {
|
|
943
|
+
var found = true;
|
|
944
|
+
for (var j = 0; j < valLength; j++) {
|
|
945
|
+
if (read(arr, i + j) !== read(val, j)) {
|
|
946
|
+
found = false;
|
|
947
|
+
break
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
if (found) return i
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
return -1
|
|
507
955
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
956
|
+
|
|
957
|
+
Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
|
|
958
|
+
return this.indexOf(val, byteOffset, encoding) !== -1
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
|
|
962
|
+
return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
|
|
963
|
+
};
|
|
964
|
+
|
|
965
|
+
Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
|
|
966
|
+
return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
function hexWrite (buf, string, offset, length) {
|
|
970
|
+
offset = Number(offset) || 0;
|
|
971
|
+
var remaining = buf.length - offset;
|
|
972
|
+
if (!length) {
|
|
973
|
+
length = remaining;
|
|
974
|
+
} else {
|
|
975
|
+
length = Number(length);
|
|
976
|
+
if (length > remaining) {
|
|
977
|
+
length = remaining;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// must be an even number of digits
|
|
982
|
+
var strLen = string.length;
|
|
983
|
+
if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
|
|
984
|
+
|
|
985
|
+
if (length > strLen / 2) {
|
|
986
|
+
length = strLen / 2;
|
|
987
|
+
}
|
|
988
|
+
for (var i = 0; i < length; ++i) {
|
|
989
|
+
var parsed = parseInt(string.substr(i * 2, 2), 16);
|
|
990
|
+
if (isNaN(parsed)) return i
|
|
991
|
+
buf[offset + i] = parsed;
|
|
992
|
+
}
|
|
993
|
+
return i
|
|
525
994
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
* @returns
|
|
530
|
-
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
531
|
-
*
|
|
532
|
-
* @example
|
|
533
|
-
* ```
|
|
534
|
-
* import { validateBlock } from '@taquito/utils';
|
|
535
|
-
* const blockHash = 'PtHangz2aRngywmSRGGvrcTyMbbdpWdpFKuS4uMWxg2RaH9i1qx'
|
|
536
|
-
* const validation = validateBlock(blockHash)
|
|
537
|
-
* console.log(validation)
|
|
538
|
-
* // This example return 3 which correspond to VALID
|
|
539
|
-
* ```
|
|
540
|
-
*/
|
|
541
|
-
function validateBlock(value) {
|
|
542
|
-
return validatePrefixedValue(value, [PrefixV2.BlockHash]);
|
|
995
|
+
|
|
996
|
+
function utf8Write (buf, string, offset, length) {
|
|
997
|
+
return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
|
|
543
998
|
}
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
*/
|
|
548
|
-
function invalidDetail(validation) {
|
|
549
|
-
switch (validation) {
|
|
550
|
-
case ValidationResult.NO_PREFIX_MATCHED:
|
|
551
|
-
return 'with unsupported prefix';
|
|
552
|
-
case ValidationResult.INVALID_CHECKSUM:
|
|
553
|
-
return 'failed checksum';
|
|
554
|
-
case ValidationResult.INVALID_LENGTH:
|
|
555
|
-
return 'with incorrect length';
|
|
556
|
-
default:
|
|
557
|
-
return '';
|
|
558
|
-
}
|
|
999
|
+
|
|
1000
|
+
function asciiWrite (buf, string, offset, length) {
|
|
1001
|
+
return blitBuffer(asciiToBytes(string), buf, offset, length)
|
|
559
1002
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
564
|
-
*
|
|
565
|
-
*/
|
|
566
|
-
function validateSpendingKey(value) {
|
|
567
|
-
return validatePrefixedValue(value, [PrefixV2.SaplingSpendingKey]);
|
|
1003
|
+
|
|
1004
|
+
function latin1Write (buf, string, offset, length) {
|
|
1005
|
+
return asciiWrite(buf, string, offset, length)
|
|
568
1006
|
}
|
|
569
|
-
|
|
570
|
-
|
|
1007
|
+
|
|
1008
|
+
function base64Write (buf, string, offset, length) {
|
|
1009
|
+
return blitBuffer(base64ToBytes(string), buf, offset, length)
|
|
571
1010
|
}
|
|
572
1011
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
1012
|
+
function ucs2Write (buf, string, offset, length) {
|
|
1013
|
+
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
Buffer.prototype.write = function write (string, offset, length, encoding) {
|
|
1017
|
+
// Buffer#write(string)
|
|
1018
|
+
if (offset === undefined) {
|
|
1019
|
+
encoding = 'utf8';
|
|
1020
|
+
length = this.length;
|
|
1021
|
+
offset = 0;
|
|
1022
|
+
// Buffer#write(string, encoding)
|
|
1023
|
+
} else if (length === undefined && typeof offset === 'string') {
|
|
1024
|
+
encoding = offset;
|
|
1025
|
+
length = this.length;
|
|
1026
|
+
offset = 0;
|
|
1027
|
+
// Buffer#write(string, offset[, length][, encoding])
|
|
1028
|
+
} else if (isFinite(offset)) {
|
|
1029
|
+
offset = offset | 0;
|
|
1030
|
+
if (isFinite(length)) {
|
|
1031
|
+
length = length | 0;
|
|
1032
|
+
if (encoding === undefined) encoding = 'utf8';
|
|
1033
|
+
} else {
|
|
1034
|
+
encoding = length;
|
|
1035
|
+
length = undefined;
|
|
1036
|
+
}
|
|
1037
|
+
// legacy write(string, encoding, offset, length) - remove in v0.13
|
|
1038
|
+
} else {
|
|
1039
|
+
throw new Error(
|
|
1040
|
+
'Buffer.write(string, encoding, offset[, length]) is no longer supported'
|
|
1041
|
+
)
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
var remaining = this.length - offset;
|
|
1045
|
+
if (length === undefined || length > remaining) length = remaining;
|
|
1046
|
+
|
|
1047
|
+
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
|
|
1048
|
+
throw new RangeError('Attempt to write outside buffer bounds')
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
if (!encoding) encoding = 'utf8';
|
|
1052
|
+
|
|
1053
|
+
var loweredCase = false;
|
|
1054
|
+
for (;;) {
|
|
1055
|
+
switch (encoding) {
|
|
1056
|
+
case 'hex':
|
|
1057
|
+
return hexWrite(this, string, offset, length)
|
|
1058
|
+
|
|
1059
|
+
case 'utf8':
|
|
1060
|
+
case 'utf-8':
|
|
1061
|
+
return utf8Write(this, string, offset, length)
|
|
1062
|
+
|
|
1063
|
+
case 'ascii':
|
|
1064
|
+
return asciiWrite(this, string, offset, length)
|
|
1065
|
+
|
|
1066
|
+
case 'latin1':
|
|
1067
|
+
case 'binary':
|
|
1068
|
+
return latin1Write(this, string, offset, length)
|
|
1069
|
+
|
|
1070
|
+
case 'base64':
|
|
1071
|
+
// Warning: maxLength not taken into account in base64Write
|
|
1072
|
+
return base64Write(this, string, offset, length)
|
|
1073
|
+
|
|
1074
|
+
case 'ucs2':
|
|
1075
|
+
case 'ucs-2':
|
|
1076
|
+
case 'utf16le':
|
|
1077
|
+
case 'utf-16le':
|
|
1078
|
+
return ucs2Write(this, string, offset, length)
|
|
1079
|
+
|
|
1080
|
+
default:
|
|
1081
|
+
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
|
1082
|
+
encoding = ('' + encoding).toLowerCase();
|
|
1083
|
+
loweredCase = true;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
577
1086
|
};
|
|
578
1087
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
1088
|
+
Buffer.prototype.toJSON = function toJSON () {
|
|
1089
|
+
return {
|
|
1090
|
+
type: 'Buffer',
|
|
1091
|
+
data: Array.prototype.slice.call(this._arr || this, 0)
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1094
|
+
|
|
1095
|
+
function base64Slice (buf, start, end) {
|
|
1096
|
+
if (start === 0 && end === buf.length) {
|
|
1097
|
+
return fromByteArray(buf)
|
|
1098
|
+
} else {
|
|
1099
|
+
return fromByteArray(buf.slice(start, end))
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
function utf8Slice (buf, start, end) {
|
|
1104
|
+
end = Math.min(buf.length, end);
|
|
1105
|
+
var res = [];
|
|
1106
|
+
|
|
1107
|
+
var i = start;
|
|
1108
|
+
while (i < end) {
|
|
1109
|
+
var firstByte = buf[i];
|
|
1110
|
+
var codePoint = null;
|
|
1111
|
+
var bytesPerSequence = (firstByte > 0xEF) ? 4
|
|
1112
|
+
: (firstByte > 0xDF) ? 3
|
|
1113
|
+
: (firstByte > 0xBF) ? 2
|
|
1114
|
+
: 1;
|
|
1115
|
+
|
|
1116
|
+
if (i + bytesPerSequence <= end) {
|
|
1117
|
+
var secondByte, thirdByte, fourthByte, tempCodePoint;
|
|
1118
|
+
|
|
1119
|
+
switch (bytesPerSequence) {
|
|
1120
|
+
case 1:
|
|
1121
|
+
if (firstByte < 0x80) {
|
|
1122
|
+
codePoint = firstByte;
|
|
1123
|
+
}
|
|
1124
|
+
break
|
|
1125
|
+
case 2:
|
|
1126
|
+
secondByte = buf[i + 1];
|
|
1127
|
+
if ((secondByte & 0xC0) === 0x80) {
|
|
1128
|
+
tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
|
|
1129
|
+
if (tempCodePoint > 0x7F) {
|
|
1130
|
+
codePoint = tempCodePoint;
|
|
611
1131
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
throw new InvalidSignatureError(signature, err.result);
|
|
1132
|
+
}
|
|
1133
|
+
break
|
|
1134
|
+
case 3:
|
|
1135
|
+
secondByte = buf[i + 1];
|
|
1136
|
+
thirdByte = buf[i + 2];
|
|
1137
|
+
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
|
|
1138
|
+
tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
|
|
1139
|
+
if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
|
|
1140
|
+
codePoint = tempCodePoint;
|
|
622
1141
|
}
|
|
623
|
-
|
|
624
|
-
|
|
1142
|
+
}
|
|
1143
|
+
break
|
|
1144
|
+
case 4:
|
|
1145
|
+
secondByte = buf[i + 1];
|
|
1146
|
+
thirdByte = buf[i + 2];
|
|
1147
|
+
fourthByte = buf[i + 3];
|
|
1148
|
+
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
|
|
1149
|
+
tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
|
|
1150
|
+
if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
|
|
1151
|
+
codePoint = tempCodePoint;
|
|
625
1152
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
let msg;
|
|
629
|
-
if (typeof message === 'string') {
|
|
630
|
-
msg = hex2buf(message);
|
|
631
|
-
}
|
|
632
|
-
else {
|
|
633
|
-
msg = message;
|
|
634
|
-
}
|
|
635
|
-
if (msg.length === 0) {
|
|
636
|
-
throw new InvalidMessageError(buf2hex(msg), `can't be empty`);
|
|
637
|
-
}
|
|
638
|
-
if (typeof watermark !== 'undefined') {
|
|
639
|
-
msg = mergebuf(watermark, msg);
|
|
640
|
-
}
|
|
641
|
-
if (pop) {
|
|
642
|
-
return verifyBLSPopSignature(sig, msg, pk);
|
|
643
|
-
}
|
|
644
|
-
else {
|
|
645
|
-
switch (pre) {
|
|
646
|
-
case PrefixV2.P256PublicKey:
|
|
647
|
-
return verifyP2Signature(sig, msg, pk);
|
|
648
|
-
case PrefixV2.Secp256k1PublicKey:
|
|
649
|
-
return verifySpSignature(sig, msg, pk);
|
|
650
|
-
case PrefixV2.Ed25519PublicKey:
|
|
651
|
-
return verifyEdSignature(sig, msg, pk);
|
|
652
|
-
default:
|
|
653
|
-
return verifyBLSSignature(sig, msg, pk);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
/**
|
|
658
|
-
* @deprecated use b58DecodeAndCheckPrefix instead, this function will be removed in the next minor release
|
|
659
|
-
* @description validates a public key and extracts the prefix
|
|
660
|
-
*/
|
|
661
|
-
function validatePkAndExtractPrefix(publicKey) {
|
|
662
|
-
if (publicKey === '') {
|
|
663
|
-
throw new InvalidPublicKeyError(publicKey, `can't be empty`);
|
|
664
|
-
}
|
|
665
|
-
const pkPrefix = publicKey.substring(0, 4);
|
|
666
|
-
const publicKeyValidation = validatePublicKey(publicKey);
|
|
667
|
-
if (publicKeyValidation !== ValidationResult.VALID) {
|
|
668
|
-
throw new InvalidPublicKeyError(publicKey, invalidDetail(publicKeyValidation));
|
|
669
|
-
}
|
|
670
|
-
return pkPrefix;
|
|
671
|
-
}
|
|
672
|
-
function verifyEdSignature(sig, msg, publicKey) {
|
|
673
|
-
const hash$1 = hash(msg, 32);
|
|
674
|
-
try {
|
|
675
|
-
return verify(publicKey, hash$1, sig);
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
676
1155
|
}
|
|
677
|
-
|
|
678
|
-
|
|
1156
|
+
|
|
1157
|
+
if (codePoint === null) {
|
|
1158
|
+
// we did not generate a valid codePoint so insert a
|
|
1159
|
+
// replacement char (U+FFFD) and advance only 1 byte
|
|
1160
|
+
codePoint = 0xFFFD;
|
|
1161
|
+
bytesPerSequence = 1;
|
|
1162
|
+
} else if (codePoint > 0xFFFF) {
|
|
1163
|
+
// encode to utf16 (surrogate pair dance)
|
|
1164
|
+
codePoint -= 0x10000;
|
|
1165
|
+
res.push(codePoint >>> 10 & 0x3FF | 0xD800);
|
|
1166
|
+
codePoint = 0xDC00 | codePoint & 0x3FF;
|
|
679
1167
|
}
|
|
1168
|
+
|
|
1169
|
+
res.push(codePoint);
|
|
1170
|
+
i += bytesPerSequence;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
return decodeCodePointsArray(res)
|
|
680
1174
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
1175
|
+
|
|
1176
|
+
// Based on http://stackoverflow.com/a/22747272/680742, the browser with
|
|
1177
|
+
// the lowest limit is Chrome, with 0x10000 args.
|
|
1178
|
+
// We go 1 magnitude less, for safety
|
|
1179
|
+
var MAX_ARGUMENTS_LENGTH = 0x1000;
|
|
1180
|
+
|
|
1181
|
+
function decodeCodePointsArray (codePoints) {
|
|
1182
|
+
var len = codePoints.length;
|
|
1183
|
+
if (len <= MAX_ARGUMENTS_LENGTH) {
|
|
1184
|
+
return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
// Decode in chunks to avoid "call stack size exceeded".
|
|
1188
|
+
var res = '';
|
|
1189
|
+
var i = 0;
|
|
1190
|
+
while (i < len) {
|
|
1191
|
+
res += String.fromCharCode.apply(
|
|
1192
|
+
String,
|
|
1193
|
+
codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
return res
|
|
684
1197
|
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
1198
|
+
|
|
1199
|
+
function asciiSlice (buf, start, end) {
|
|
1200
|
+
var ret = '';
|
|
1201
|
+
end = Math.min(buf.length, end);
|
|
1202
|
+
|
|
1203
|
+
for (var i = start; i < end; ++i) {
|
|
1204
|
+
ret += String.fromCharCode(buf[i] & 0x7F);
|
|
1205
|
+
}
|
|
1206
|
+
return ret
|
|
688
1207
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
}
|
|
1208
|
+
|
|
1209
|
+
function latin1Slice (buf, start, end) {
|
|
1210
|
+
var ret = '';
|
|
1211
|
+
end = Math.min(buf.length, end);
|
|
1212
|
+
|
|
1213
|
+
for (var i = start; i < end; ++i) {
|
|
1214
|
+
ret += String.fromCharCode(buf[i]);
|
|
1215
|
+
}
|
|
1216
|
+
return ret
|
|
699
1217
|
}
|
|
700
|
-
|
|
701
|
-
function
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
1218
|
+
|
|
1219
|
+
function hexSlice (buf, start, end) {
|
|
1220
|
+
var len = buf.length;
|
|
1221
|
+
|
|
1222
|
+
if (!start || start < 0) start = 0;
|
|
1223
|
+
if (!end || end < 0 || end > len) end = len;
|
|
1224
|
+
|
|
1225
|
+
var out = '';
|
|
1226
|
+
for (var i = start; i < end; ++i) {
|
|
1227
|
+
out += toHex(buf[i]);
|
|
1228
|
+
}
|
|
1229
|
+
return out
|
|
709
1230
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
1231
|
+
|
|
1232
|
+
function utf16leSlice (buf, start, end) {
|
|
1233
|
+
var bytes = buf.slice(start, end);
|
|
1234
|
+
var res = '';
|
|
1235
|
+
for (var i = 0; i < bytes.length; i += 2) {
|
|
1236
|
+
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
|
|
1237
|
+
}
|
|
1238
|
+
return res
|
|
718
1239
|
}
|
|
719
1240
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
1241
|
+
Buffer.prototype.slice = function slice (start, end) {
|
|
1242
|
+
var len = this.length;
|
|
1243
|
+
start = ~~start;
|
|
1244
|
+
end = end === undefined ? len : ~~end;
|
|
1245
|
+
|
|
1246
|
+
if (start < 0) {
|
|
1247
|
+
start += len;
|
|
1248
|
+
if (start < 0) start = 0;
|
|
1249
|
+
} else if (start > len) {
|
|
1250
|
+
start = len;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
if (end < 0) {
|
|
1254
|
+
end += len;
|
|
1255
|
+
if (end < 0) end = 0;
|
|
1256
|
+
} else if (end > len) {
|
|
1257
|
+
end = len;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
if (end < start) end = start;
|
|
1261
|
+
|
|
1262
|
+
var newBuf;
|
|
1263
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1264
|
+
newBuf = this.subarray(start, end);
|
|
1265
|
+
newBuf.__proto__ = Buffer.prototype;
|
|
1266
|
+
} else {
|
|
1267
|
+
var sliceLen = end - start;
|
|
1268
|
+
newBuf = new Buffer(sliceLen, undefined);
|
|
1269
|
+
for (var i = 0; i < sliceLen; ++i) {
|
|
1270
|
+
newBuf[i] = this[i + start];
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
return newBuf
|
|
1275
|
+
};
|
|
1276
|
+
|
|
1277
|
+
/*
|
|
1278
|
+
* Need to make sure that buffer isn't trying to write out of bounds.
|
|
723
1279
|
*/
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
this.protocolHash = protocolHash;
|
|
728
|
-
this.name = 'InvalidProtocolHashError';
|
|
729
|
-
this.name = this.constructor.name;
|
|
730
|
-
}
|
|
1280
|
+
function checkOffset (offset, ext, length) {
|
|
1281
|
+
if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
|
|
1282
|
+
if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
|
|
731
1283
|
}
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
1284
|
+
|
|
1285
|
+
Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
|
|
1286
|
+
offset = offset | 0;
|
|
1287
|
+
byteLength = byteLength | 0;
|
|
1288
|
+
if (!noAssert) checkOffset(offset, byteLength, this.length);
|
|
1289
|
+
|
|
1290
|
+
var val = this[offset];
|
|
1291
|
+
var mul = 1;
|
|
1292
|
+
var i = 0;
|
|
1293
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1294
|
+
val += this[offset + i] * mul;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
return val
|
|
1298
|
+
};
|
|
1299
|
+
|
|
1300
|
+
Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
|
|
1301
|
+
offset = offset | 0;
|
|
1302
|
+
byteLength = byteLength | 0;
|
|
1303
|
+
if (!noAssert) {
|
|
1304
|
+
checkOffset(offset, byteLength, this.length);
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
var val = this[offset + --byteLength];
|
|
1308
|
+
var mul = 1;
|
|
1309
|
+
while (byteLength > 0 && (mul *= 0x100)) {
|
|
1310
|
+
val += this[offset + --byteLength] * mul;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
return val
|
|
1314
|
+
};
|
|
1315
|
+
|
|
1316
|
+
Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
|
|
1317
|
+
if (!noAssert) checkOffset(offset, 1, this.length);
|
|
1318
|
+
return this[offset]
|
|
1319
|
+
};
|
|
1320
|
+
|
|
1321
|
+
Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
|
|
1322
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1323
|
+
return this[offset] | (this[offset + 1] << 8)
|
|
1324
|
+
};
|
|
1325
|
+
|
|
1326
|
+
Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
|
|
1327
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1328
|
+
return (this[offset] << 8) | this[offset + 1]
|
|
1329
|
+
};
|
|
1330
|
+
|
|
1331
|
+
Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
|
|
1332
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1333
|
+
|
|
1334
|
+
return ((this[offset]) |
|
|
1335
|
+
(this[offset + 1] << 8) |
|
|
1336
|
+
(this[offset + 2] << 16)) +
|
|
1337
|
+
(this[offset + 3] * 0x1000000)
|
|
1338
|
+
};
|
|
1339
|
+
|
|
1340
|
+
Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
|
|
1341
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1342
|
+
|
|
1343
|
+
return (this[offset] * 0x1000000) +
|
|
1344
|
+
((this[offset + 1] << 16) |
|
|
1345
|
+
(this[offset + 2] << 8) |
|
|
1346
|
+
this[offset + 3])
|
|
1347
|
+
};
|
|
1348
|
+
|
|
1349
|
+
Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
|
|
1350
|
+
offset = offset | 0;
|
|
1351
|
+
byteLength = byteLength | 0;
|
|
1352
|
+
if (!noAssert) checkOffset(offset, byteLength, this.length);
|
|
1353
|
+
|
|
1354
|
+
var val = this[offset];
|
|
1355
|
+
var mul = 1;
|
|
1356
|
+
var i = 0;
|
|
1357
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1358
|
+
val += this[offset + i] * mul;
|
|
1359
|
+
}
|
|
1360
|
+
mul *= 0x80;
|
|
1361
|
+
|
|
1362
|
+
if (val >= mul) val -= Math.pow(2, 8 * byteLength);
|
|
1363
|
+
|
|
1364
|
+
return val
|
|
1365
|
+
};
|
|
1366
|
+
|
|
1367
|
+
Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
|
|
1368
|
+
offset = offset | 0;
|
|
1369
|
+
byteLength = byteLength | 0;
|
|
1370
|
+
if (!noAssert) checkOffset(offset, byteLength, this.length);
|
|
1371
|
+
|
|
1372
|
+
var i = byteLength;
|
|
1373
|
+
var mul = 1;
|
|
1374
|
+
var val = this[offset + --i];
|
|
1375
|
+
while (i > 0 && (mul *= 0x100)) {
|
|
1376
|
+
val += this[offset + --i] * mul;
|
|
1377
|
+
}
|
|
1378
|
+
mul *= 0x80;
|
|
1379
|
+
|
|
1380
|
+
if (val >= mul) val -= Math.pow(2, 8 * byteLength);
|
|
1381
|
+
|
|
1382
|
+
return val
|
|
1383
|
+
};
|
|
1384
|
+
|
|
1385
|
+
Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
|
|
1386
|
+
if (!noAssert) checkOffset(offset, 1, this.length);
|
|
1387
|
+
if (!(this[offset] & 0x80)) return (this[offset])
|
|
1388
|
+
return ((0xff - this[offset] + 1) * -1)
|
|
1389
|
+
};
|
|
1390
|
+
|
|
1391
|
+
Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
|
|
1392
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1393
|
+
var val = this[offset] | (this[offset + 1] << 8);
|
|
1394
|
+
return (val & 0x8000) ? val | 0xFFFF0000 : val
|
|
1395
|
+
};
|
|
1396
|
+
|
|
1397
|
+
Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
|
|
1398
|
+
if (!noAssert) checkOffset(offset, 2, this.length);
|
|
1399
|
+
var val = this[offset + 1] | (this[offset] << 8);
|
|
1400
|
+
return (val & 0x8000) ? val | 0xFFFF0000 : val
|
|
1401
|
+
};
|
|
1402
|
+
|
|
1403
|
+
Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
|
|
1404
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1405
|
+
|
|
1406
|
+
return (this[offset]) |
|
|
1407
|
+
(this[offset + 1] << 8) |
|
|
1408
|
+
(this[offset + 2] << 16) |
|
|
1409
|
+
(this[offset + 3] << 24)
|
|
1410
|
+
};
|
|
1411
|
+
|
|
1412
|
+
Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
|
|
1413
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1414
|
+
|
|
1415
|
+
return (this[offset] << 24) |
|
|
1416
|
+
(this[offset + 1] << 16) |
|
|
1417
|
+
(this[offset + 2] << 8) |
|
|
1418
|
+
(this[offset + 3])
|
|
1419
|
+
};
|
|
1420
|
+
|
|
1421
|
+
Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
|
|
1422
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1423
|
+
return read(this, offset, true, 23, 4)
|
|
1424
|
+
};
|
|
1425
|
+
|
|
1426
|
+
Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
|
|
1427
|
+
if (!noAssert) checkOffset(offset, 4, this.length);
|
|
1428
|
+
return read(this, offset, false, 23, 4)
|
|
1429
|
+
};
|
|
1430
|
+
|
|
1431
|
+
Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
|
|
1432
|
+
if (!noAssert) checkOffset(offset, 8, this.length);
|
|
1433
|
+
return read(this, offset, true, 52, 8)
|
|
1434
|
+
};
|
|
1435
|
+
|
|
1436
|
+
Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
|
|
1437
|
+
if (!noAssert) checkOffset(offset, 8, this.length);
|
|
1438
|
+
return read(this, offset, false, 52, 8)
|
|
1439
|
+
};
|
|
1440
|
+
|
|
1441
|
+
function checkInt (buf, value, offset, ext, max, min) {
|
|
1442
|
+
if (!internalIsBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
|
|
1443
|
+
if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
|
|
1444
|
+
if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
|
743
1445
|
}
|
|
744
1446
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
1447
|
+
Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
|
|
1448
|
+
value = +value;
|
|
1449
|
+
offset = offset | 0;
|
|
1450
|
+
byteLength = byteLength | 0;
|
|
1451
|
+
if (!noAssert) {
|
|
1452
|
+
var maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
|
1453
|
+
checkInt(this, value, offset, byteLength, maxBytes, 0);
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
var mul = 1;
|
|
1457
|
+
var i = 0;
|
|
1458
|
+
this[offset] = value & 0xFF;
|
|
1459
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1460
|
+
this[offset + i] = (value / mul) & 0xFF;
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
return offset + byteLength
|
|
1464
|
+
};
|
|
1465
|
+
|
|
1466
|
+
Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
|
|
1467
|
+
value = +value;
|
|
1468
|
+
offset = offset | 0;
|
|
1469
|
+
byteLength = byteLength | 0;
|
|
1470
|
+
if (!noAssert) {
|
|
1471
|
+
var maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
|
1472
|
+
checkInt(this, value, offset, byteLength, maxBytes, 0);
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
var i = byteLength - 1;
|
|
1476
|
+
var mul = 1;
|
|
1477
|
+
this[offset + i] = value & 0xFF;
|
|
1478
|
+
while (--i >= 0 && (mul *= 0x100)) {
|
|
1479
|
+
this[offset + i] = (value / mul) & 0xFF;
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
return offset + byteLength
|
|
1483
|
+
};
|
|
1484
|
+
|
|
1485
|
+
Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
|
|
1486
|
+
value = +value;
|
|
1487
|
+
offset = offset | 0;
|
|
1488
|
+
if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
|
|
1489
|
+
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
|
|
1490
|
+
this[offset] = (value & 0xff);
|
|
1491
|
+
return offset + 1
|
|
1492
|
+
};
|
|
1493
|
+
|
|
1494
|
+
function objectWriteUInt16 (buf, value, offset, littleEndian) {
|
|
1495
|
+
if (value < 0) value = 0xffff + value + 1;
|
|
1496
|
+
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
|
|
1497
|
+
buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
|
|
1498
|
+
(littleEndian ? i : 1 - i) * 8;
|
|
1499
|
+
}
|
|
757
1500
|
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
1501
|
+
|
|
1502
|
+
Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
|
|
1503
|
+
value = +value;
|
|
1504
|
+
offset = offset | 0;
|
|
1505
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
|
|
1506
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1507
|
+
this[offset] = (value & 0xff);
|
|
1508
|
+
this[offset + 1] = (value >>> 8);
|
|
1509
|
+
} else {
|
|
1510
|
+
objectWriteUInt16(this, value, offset, true);
|
|
1511
|
+
}
|
|
1512
|
+
return offset + 2
|
|
1513
|
+
};
|
|
1514
|
+
|
|
1515
|
+
Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
|
|
1516
|
+
value = +value;
|
|
1517
|
+
offset = offset | 0;
|
|
1518
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
|
|
1519
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1520
|
+
this[offset] = (value >>> 8);
|
|
1521
|
+
this[offset + 1] = (value & 0xff);
|
|
1522
|
+
} else {
|
|
1523
|
+
objectWriteUInt16(this, value, offset, false);
|
|
1524
|
+
}
|
|
1525
|
+
return offset + 2
|
|
1526
|
+
};
|
|
1527
|
+
|
|
1528
|
+
function objectWriteUInt32 (buf, value, offset, littleEndian) {
|
|
1529
|
+
if (value < 0) value = 0xffffffff + value + 1;
|
|
1530
|
+
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
|
|
1531
|
+
buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff;
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
|
|
1536
|
+
value = +value;
|
|
1537
|
+
offset = offset | 0;
|
|
1538
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
|
|
1539
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1540
|
+
this[offset + 3] = (value >>> 24);
|
|
1541
|
+
this[offset + 2] = (value >>> 16);
|
|
1542
|
+
this[offset + 1] = (value >>> 8);
|
|
1543
|
+
this[offset] = (value & 0xff);
|
|
1544
|
+
} else {
|
|
1545
|
+
objectWriteUInt32(this, value, offset, true);
|
|
1546
|
+
}
|
|
1547
|
+
return offset + 4
|
|
1548
|
+
};
|
|
1549
|
+
|
|
1550
|
+
Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
|
|
1551
|
+
value = +value;
|
|
1552
|
+
offset = offset | 0;
|
|
1553
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
|
|
1554
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1555
|
+
this[offset] = (value >>> 24);
|
|
1556
|
+
this[offset + 1] = (value >>> 16);
|
|
1557
|
+
this[offset + 2] = (value >>> 8);
|
|
1558
|
+
this[offset + 3] = (value & 0xff);
|
|
1559
|
+
} else {
|
|
1560
|
+
objectWriteUInt32(this, value, offset, false);
|
|
1561
|
+
}
|
|
1562
|
+
return offset + 4
|
|
1563
|
+
};
|
|
1564
|
+
|
|
1565
|
+
Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
|
|
1566
|
+
value = +value;
|
|
1567
|
+
offset = offset | 0;
|
|
1568
|
+
if (!noAssert) {
|
|
1569
|
+
var limit = Math.pow(2, 8 * byteLength - 1);
|
|
1570
|
+
|
|
1571
|
+
checkInt(this, value, offset, byteLength, limit - 1, -limit);
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
var i = 0;
|
|
1575
|
+
var mul = 1;
|
|
1576
|
+
var sub = 0;
|
|
1577
|
+
this[offset] = value & 0xFF;
|
|
1578
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
|
1579
|
+
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
|
|
1580
|
+
sub = 1;
|
|
1581
|
+
}
|
|
1582
|
+
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
return offset + byteLength
|
|
1586
|
+
};
|
|
1587
|
+
|
|
1588
|
+
Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
|
|
1589
|
+
value = +value;
|
|
1590
|
+
offset = offset | 0;
|
|
1591
|
+
if (!noAssert) {
|
|
1592
|
+
var limit = Math.pow(2, 8 * byteLength - 1);
|
|
1593
|
+
|
|
1594
|
+
checkInt(this, value, offset, byteLength, limit - 1, -limit);
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
var i = byteLength - 1;
|
|
1598
|
+
var mul = 1;
|
|
1599
|
+
var sub = 0;
|
|
1600
|
+
this[offset + i] = value & 0xFF;
|
|
1601
|
+
while (--i >= 0 && (mul *= 0x100)) {
|
|
1602
|
+
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
|
|
1603
|
+
sub = 1;
|
|
1604
|
+
}
|
|
1605
|
+
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
return offset + byteLength
|
|
1609
|
+
};
|
|
1610
|
+
|
|
1611
|
+
Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
|
|
1612
|
+
value = +value;
|
|
1613
|
+
offset = offset | 0;
|
|
1614
|
+
if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -128);
|
|
1615
|
+
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
|
|
1616
|
+
if (value < 0) value = 0xff + value + 1;
|
|
1617
|
+
this[offset] = (value & 0xff);
|
|
1618
|
+
return offset + 1
|
|
1619
|
+
};
|
|
1620
|
+
|
|
1621
|
+
Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
|
|
1622
|
+
value = +value;
|
|
1623
|
+
offset = offset | 0;
|
|
1624
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
|
|
1625
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1626
|
+
this[offset] = (value & 0xff);
|
|
1627
|
+
this[offset + 1] = (value >>> 8);
|
|
1628
|
+
} else {
|
|
1629
|
+
objectWriteUInt16(this, value, offset, true);
|
|
1630
|
+
}
|
|
1631
|
+
return offset + 2
|
|
1632
|
+
};
|
|
1633
|
+
|
|
1634
|
+
Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
|
|
1635
|
+
value = +value;
|
|
1636
|
+
offset = offset | 0;
|
|
1637
|
+
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
|
|
1638
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1639
|
+
this[offset] = (value >>> 8);
|
|
1640
|
+
this[offset + 1] = (value & 0xff);
|
|
1641
|
+
} else {
|
|
1642
|
+
objectWriteUInt16(this, value, offset, false);
|
|
1643
|
+
}
|
|
1644
|
+
return offset + 2
|
|
1645
|
+
};
|
|
1646
|
+
|
|
1647
|
+
Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
|
|
1648
|
+
value = +value;
|
|
1649
|
+
offset = offset | 0;
|
|
1650
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
|
|
1651
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1652
|
+
this[offset] = (value & 0xff);
|
|
1653
|
+
this[offset + 1] = (value >>> 8);
|
|
1654
|
+
this[offset + 2] = (value >>> 16);
|
|
1655
|
+
this[offset + 3] = (value >>> 24);
|
|
1656
|
+
} else {
|
|
1657
|
+
objectWriteUInt32(this, value, offset, true);
|
|
1658
|
+
}
|
|
1659
|
+
return offset + 4
|
|
1660
|
+
};
|
|
1661
|
+
|
|
1662
|
+
Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
|
|
1663
|
+
value = +value;
|
|
1664
|
+
offset = offset | 0;
|
|
1665
|
+
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
|
|
1666
|
+
if (value < 0) value = 0xffffffff + value + 1;
|
|
1667
|
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1668
|
+
this[offset] = (value >>> 24);
|
|
1669
|
+
this[offset + 1] = (value >>> 16);
|
|
1670
|
+
this[offset + 2] = (value >>> 8);
|
|
1671
|
+
this[offset + 3] = (value & 0xff);
|
|
1672
|
+
} else {
|
|
1673
|
+
objectWriteUInt32(this, value, offset, false);
|
|
1674
|
+
}
|
|
1675
|
+
return offset + 4
|
|
1676
|
+
};
|
|
1677
|
+
|
|
1678
|
+
function checkIEEE754 (buf, value, offset, ext, max, min) {
|
|
1679
|
+
if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
|
1680
|
+
if (offset < 0) throw new RangeError('Index out of range')
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
function writeFloat (buf, value, offset, littleEndian, noAssert) {
|
|
1684
|
+
if (!noAssert) {
|
|
1685
|
+
checkIEEE754(buf, value, offset, 4);
|
|
1686
|
+
}
|
|
1687
|
+
write(buf, value, offset, littleEndian, 23, 4);
|
|
1688
|
+
return offset + 4
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
|
|
1692
|
+
return writeFloat(this, value, offset, true, noAssert)
|
|
1693
|
+
};
|
|
1694
|
+
|
|
1695
|
+
Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
|
|
1696
|
+
return writeFloat(this, value, offset, false, noAssert)
|
|
1697
|
+
};
|
|
1698
|
+
|
|
1699
|
+
function writeDouble (buf, value, offset, littleEndian, noAssert) {
|
|
1700
|
+
if (!noAssert) {
|
|
1701
|
+
checkIEEE754(buf, value, offset, 8);
|
|
1702
|
+
}
|
|
1703
|
+
write(buf, value, offset, littleEndian, 52, 8);
|
|
1704
|
+
return offset + 8
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
|
|
1708
|
+
return writeDouble(this, value, offset, true, noAssert)
|
|
1709
|
+
};
|
|
1710
|
+
|
|
1711
|
+
Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
|
|
1712
|
+
return writeDouble(this, value, offset, false, noAssert)
|
|
1713
|
+
};
|
|
1714
|
+
|
|
1715
|
+
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
|
|
1716
|
+
Buffer.prototype.copy = function copy (target, targetStart, start, end) {
|
|
1717
|
+
if (!start) start = 0;
|
|
1718
|
+
if (!end && end !== 0) end = this.length;
|
|
1719
|
+
if (targetStart >= target.length) targetStart = target.length;
|
|
1720
|
+
if (!targetStart) targetStart = 0;
|
|
1721
|
+
if (end > 0 && end < start) end = start;
|
|
1722
|
+
|
|
1723
|
+
// Copy 0 bytes; we're done
|
|
1724
|
+
if (end === start) return 0
|
|
1725
|
+
if (target.length === 0 || this.length === 0) return 0
|
|
1726
|
+
|
|
1727
|
+
// Fatal error conditions
|
|
1728
|
+
if (targetStart < 0) {
|
|
1729
|
+
throw new RangeError('targetStart out of bounds')
|
|
1730
|
+
}
|
|
1731
|
+
if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
|
|
1732
|
+
if (end < 0) throw new RangeError('sourceEnd out of bounds')
|
|
1733
|
+
|
|
1734
|
+
// Are we oob?
|
|
1735
|
+
if (end > this.length) end = this.length;
|
|
1736
|
+
if (target.length - targetStart < end - start) {
|
|
1737
|
+
end = target.length - targetStart + start;
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
var len = end - start;
|
|
1741
|
+
var i;
|
|
1742
|
+
|
|
1743
|
+
if (this === target && start < targetStart && targetStart < end) {
|
|
1744
|
+
// descending copy from end
|
|
1745
|
+
for (i = len - 1; i >= 0; --i) {
|
|
1746
|
+
target[i + targetStart] = this[i + start];
|
|
1747
|
+
}
|
|
1748
|
+
} else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
|
|
1749
|
+
// ascending copy from start
|
|
1750
|
+
for (i = 0; i < len; ++i) {
|
|
1751
|
+
target[i + targetStart] = this[i + start];
|
|
1752
|
+
}
|
|
1753
|
+
} else {
|
|
1754
|
+
Uint8Array.prototype.set.call(
|
|
1755
|
+
target,
|
|
1756
|
+
this.subarray(start, start + len),
|
|
1757
|
+
targetStart
|
|
1758
|
+
);
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
return len
|
|
1762
|
+
};
|
|
1763
|
+
|
|
1764
|
+
// Usage:
|
|
1765
|
+
// buffer.fill(number[, offset[, end]])
|
|
1766
|
+
// buffer.fill(buffer[, offset[, end]])
|
|
1767
|
+
// buffer.fill(string[, offset[, end]][, encoding])
|
|
1768
|
+
Buffer.prototype.fill = function fill (val, start, end, encoding) {
|
|
1769
|
+
// Handle string cases:
|
|
1770
|
+
if (typeof val === 'string') {
|
|
1771
|
+
if (typeof start === 'string') {
|
|
1772
|
+
encoding = start;
|
|
1773
|
+
start = 0;
|
|
1774
|
+
end = this.length;
|
|
1775
|
+
} else if (typeof end === 'string') {
|
|
1776
|
+
encoding = end;
|
|
1777
|
+
end = this.length;
|
|
1778
|
+
}
|
|
1779
|
+
if (val.length === 1) {
|
|
1780
|
+
var code = val.charCodeAt(0);
|
|
1781
|
+
if (code < 256) {
|
|
1782
|
+
val = code;
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
if (encoding !== undefined && typeof encoding !== 'string') {
|
|
1786
|
+
throw new TypeError('encoding must be a string')
|
|
1787
|
+
}
|
|
1788
|
+
if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
|
|
1789
|
+
throw new TypeError('Unknown encoding: ' + encoding)
|
|
1790
|
+
}
|
|
1791
|
+
} else if (typeof val === 'number') {
|
|
1792
|
+
val = val & 255;
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
// Invalid ranges are not set to a default, so can range check early.
|
|
1796
|
+
if (start < 0 || this.length < start || this.length < end) {
|
|
1797
|
+
throw new RangeError('Out of range index')
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
if (end <= start) {
|
|
1801
|
+
return this
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
start = start >>> 0;
|
|
1805
|
+
end = end === undefined ? this.length : end >>> 0;
|
|
1806
|
+
|
|
1807
|
+
if (!val) val = 0;
|
|
1808
|
+
|
|
1809
|
+
var i;
|
|
1810
|
+
if (typeof val === 'number') {
|
|
1811
|
+
for (i = start; i < end; ++i) {
|
|
1812
|
+
this[i] = val;
|
|
1813
|
+
}
|
|
1814
|
+
} else {
|
|
1815
|
+
var bytes = internalIsBuffer(val)
|
|
1816
|
+
? val
|
|
1817
|
+
: utf8ToBytes(new Buffer(val, encoding).toString());
|
|
1818
|
+
var len = bytes.length;
|
|
1819
|
+
for (i = 0; i < end - start; ++i) {
|
|
1820
|
+
this[i + start] = bytes[i % len];
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
return this
|
|
1825
|
+
};
|
|
1826
|
+
|
|
1827
|
+
// HELPER FUNCTIONS
|
|
1828
|
+
// ================
|
|
1829
|
+
|
|
1830
|
+
var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g;
|
|
1831
|
+
|
|
1832
|
+
function base64clean (str) {
|
|
1833
|
+
// Node strips out invalid characters like \n and \t from the string, base64-js does not
|
|
1834
|
+
str = stringtrim(str).replace(INVALID_BASE64_RE, '');
|
|
1835
|
+
// Node converts strings with length < 2 to ''
|
|
1836
|
+
if (str.length < 2) return ''
|
|
1837
|
+
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
|
|
1838
|
+
while (str.length % 4 !== 0) {
|
|
1839
|
+
str = str + '=';
|
|
1840
|
+
}
|
|
1841
|
+
return str
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
function stringtrim (str) {
|
|
1845
|
+
if (str.trim) return str.trim()
|
|
1846
|
+
return str.replace(/^\s+|\s+$/g, '')
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
function toHex (n) {
|
|
1850
|
+
if (n < 16) return '0' + n.toString(16)
|
|
1851
|
+
return n.toString(16)
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
function utf8ToBytes (string, units) {
|
|
1855
|
+
units = units || Infinity;
|
|
1856
|
+
var codePoint;
|
|
1857
|
+
var length = string.length;
|
|
1858
|
+
var leadSurrogate = null;
|
|
1859
|
+
var bytes = [];
|
|
1860
|
+
|
|
1861
|
+
for (var i = 0; i < length; ++i) {
|
|
1862
|
+
codePoint = string.charCodeAt(i);
|
|
1863
|
+
|
|
1864
|
+
// is surrogate component
|
|
1865
|
+
if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
|
1866
|
+
// last char was a lead
|
|
1867
|
+
if (!leadSurrogate) {
|
|
1868
|
+
// no lead yet
|
|
1869
|
+
if (codePoint > 0xDBFF) {
|
|
1870
|
+
// unexpected trail
|
|
1871
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
1872
|
+
continue
|
|
1873
|
+
} else if (i + 1 === length) {
|
|
1874
|
+
// unpaired lead
|
|
1875
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
1876
|
+
continue
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
// valid lead
|
|
1880
|
+
leadSurrogate = codePoint;
|
|
1881
|
+
|
|
1882
|
+
continue
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
// 2 leads in a row
|
|
1886
|
+
if (codePoint < 0xDC00) {
|
|
1887
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
1888
|
+
leadSurrogate = codePoint;
|
|
1889
|
+
continue
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
// valid surrogate pair
|
|
1893
|
+
codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
|
|
1894
|
+
} else if (leadSurrogate) {
|
|
1895
|
+
// valid bmp char, but last char was a lead
|
|
1896
|
+
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
|
|
762
1897
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
1898
|
+
|
|
1899
|
+
leadSurrogate = null;
|
|
1900
|
+
|
|
1901
|
+
// encode utf8
|
|
1902
|
+
if (codePoint < 0x80) {
|
|
1903
|
+
if ((units -= 1) < 0) break
|
|
1904
|
+
bytes.push(codePoint);
|
|
1905
|
+
} else if (codePoint < 0x800) {
|
|
1906
|
+
if ((units -= 2) < 0) break
|
|
1907
|
+
bytes.push(
|
|
1908
|
+
codePoint >> 0x6 | 0xC0,
|
|
1909
|
+
codePoint & 0x3F | 0x80
|
|
1910
|
+
);
|
|
1911
|
+
} else if (codePoint < 0x10000) {
|
|
1912
|
+
if ((units -= 3) < 0) break
|
|
1913
|
+
bytes.push(
|
|
1914
|
+
codePoint >> 0xC | 0xE0,
|
|
1915
|
+
codePoint >> 0x6 & 0x3F | 0x80,
|
|
1916
|
+
codePoint & 0x3F | 0x80
|
|
1917
|
+
);
|
|
1918
|
+
} else if (codePoint < 0x110000) {
|
|
1919
|
+
if ((units -= 4) < 0) break
|
|
1920
|
+
bytes.push(
|
|
1921
|
+
codePoint >> 0x12 | 0xF0,
|
|
1922
|
+
codePoint >> 0xC & 0x3F | 0x80,
|
|
1923
|
+
codePoint >> 0x6 & 0x3F | 0x80,
|
|
1924
|
+
codePoint & 0x3F | 0x80
|
|
1925
|
+
);
|
|
1926
|
+
} else {
|
|
1927
|
+
throw new Error('Invalid code point')
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
return bytes
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
function asciiToBytes (str) {
|
|
1935
|
+
var byteArray = [];
|
|
1936
|
+
for (var i = 0; i < str.length; ++i) {
|
|
1937
|
+
// Node's code seems to be doing this and not & 0x7F..
|
|
1938
|
+
byteArray.push(str.charCodeAt(i) & 0xFF);
|
|
1939
|
+
}
|
|
1940
|
+
return byteArray
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
function utf16leToBytes (str, units) {
|
|
1944
|
+
var c, hi, lo;
|
|
1945
|
+
var byteArray = [];
|
|
1946
|
+
for (var i = 0; i < str.length; ++i) {
|
|
1947
|
+
if ((units -= 2) < 0) break
|
|
1948
|
+
|
|
1949
|
+
c = str.charCodeAt(i);
|
|
1950
|
+
hi = c >> 8;
|
|
1951
|
+
lo = c % 256;
|
|
1952
|
+
byteArray.push(lo);
|
|
1953
|
+
byteArray.push(hi);
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
return byteArray
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
|
|
1960
|
+
function base64ToBytes (str) {
|
|
1961
|
+
return toByteArray(base64clean(str))
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
function blitBuffer (src, dst, offset, length) {
|
|
1965
|
+
for (var i = 0; i < length; ++i) {
|
|
1966
|
+
if ((i + offset >= dst.length) || (i >= src.length)) break
|
|
1967
|
+
dst[i + offset] = src[i];
|
|
1968
|
+
}
|
|
1969
|
+
return i
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
function isnan (val) {
|
|
1973
|
+
return val !== val // eslint-disable-line no-self-compare
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
// the following is from is-buffer, also by Feross Aboukhadijeh and with same lisence
|
|
1978
|
+
// The _isBuffer check is for Safari 5-7 support, because it's missing
|
|
1979
|
+
// Object.prototype.constructor. Remove this eventually
|
|
1980
|
+
function isBuffer(obj) {
|
|
1981
|
+
return obj != null && (!!obj._isBuffer || isFastBuffer(obj) || isSlowBuffer(obj))
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1984
|
+
function isFastBuffer (obj) {
|
|
1985
|
+
return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
|
|
766
1986
|
}
|
|
767
1987
|
|
|
1988
|
+
// For Node v0.10 support. Remove this eventually.
|
|
1989
|
+
function isSlowBuffer (obj) {
|
|
1990
|
+
return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isFastBuffer(obj.slice(0, 0))
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
// ref https://gitlab.com/tezos/tezos/-/blob/master/src/lib_crypto/base58.ml
|
|
1994
|
+
/**
|
|
1995
|
+
* @description base58 name to prefix mapping
|
|
1996
|
+
*/
|
|
1997
|
+
var PrefixV2;
|
|
1998
|
+
(function (PrefixV2) {
|
|
1999
|
+
PrefixV2["BlockHash"] = "B";
|
|
2000
|
+
PrefixV2["OperationHash"] = "o";
|
|
2001
|
+
PrefixV2["OperationListHash"] = "Lo";
|
|
2002
|
+
PrefixV2["OperationListListHash"] = "LLo";
|
|
2003
|
+
PrefixV2["ProtocolHash"] = "P";
|
|
2004
|
+
PrefixV2["ContextHash"] = "Co";
|
|
2005
|
+
PrefixV2["BlockMetadataHash"] = "bm";
|
|
2006
|
+
PrefixV2["OperationMetadataHash"] = "r";
|
|
2007
|
+
PrefixV2["OperationMetadataListHash"] = "Lr";
|
|
2008
|
+
PrefixV2["OperationMetadataListListHash"] = "LLr";
|
|
2009
|
+
PrefixV2["Ed25519PublicKeyHash"] = "tz1";
|
|
2010
|
+
PrefixV2["Secp256k1PublicKeyHash"] = "tz2";
|
|
2011
|
+
PrefixV2["P256PublicKeyHash"] = "tz3";
|
|
2012
|
+
PrefixV2["ContractHash"] = "KT1";
|
|
2013
|
+
PrefixV2["BlindedPublicKeyHash"] = "btz1";
|
|
2014
|
+
PrefixV2["BLS12_381PublicKeyHash"] = "tz4";
|
|
2015
|
+
PrefixV2["TXRollupAddress"] = "txr1";
|
|
2016
|
+
PrefixV2["ZkRollupHash"] = "epx1";
|
|
2017
|
+
PrefixV2["ScRollupHash"] = "scr1";
|
|
2018
|
+
PrefixV2["SmartRollupHash"] = "sr1";
|
|
2019
|
+
PrefixV2["CryptoboxPublicKeyHash"] = "id";
|
|
2020
|
+
PrefixV2["Ed25519Seed"] = "edsk";
|
|
2021
|
+
PrefixV2["Ed25519PublicKey"] = "edpk";
|
|
2022
|
+
PrefixV2["Secp256k1SecretKey"] = "spsk";
|
|
2023
|
+
PrefixV2["P256SecretKey"] = "p2sk";
|
|
2024
|
+
PrefixV2["BLS12_381SecretKey"] = "BLsk";
|
|
2025
|
+
PrefixV2["ValueHash"] = "vh";
|
|
2026
|
+
PrefixV2["CycleNonce"] = "nce";
|
|
2027
|
+
PrefixV2["ScriptExpr"] = "expr";
|
|
2028
|
+
PrefixV2["InboxHash"] = "txi";
|
|
2029
|
+
PrefixV2["MessageHash"] = "txm";
|
|
2030
|
+
PrefixV2["CommitmentHash"] = "txc";
|
|
2031
|
+
PrefixV2["MessageResultHash"] = "txmr";
|
|
2032
|
+
PrefixV2["MessageResultListHash"] = "txM";
|
|
2033
|
+
PrefixV2["WithdrawListHash"] = "txw";
|
|
2034
|
+
PrefixV2["ScRollupStateHash"] = "scs1";
|
|
2035
|
+
PrefixV2["ScRollupCommitmentHash"] = "scc1";
|
|
2036
|
+
PrefixV2["SmartRollupStateHash"] = "srs1";
|
|
2037
|
+
PrefixV2["SmartRollupCommitmentHash"] = "src1";
|
|
2038
|
+
PrefixV2["Ed25519EncryptedSeed"] = "edesk";
|
|
2039
|
+
PrefixV2["Secp256k1EncryptedSecretKey"] = "spesk";
|
|
2040
|
+
PrefixV2["P256EncryptedSecretKey"] = "p2esk";
|
|
2041
|
+
PrefixV2["BLS12_381EncryptedSecretKey"] = "BLesk";
|
|
2042
|
+
PrefixV2["Secp256k1EncryptedScalar"] = "seesk";
|
|
2043
|
+
PrefixV2["Secp256k1PublicKey"] = "sppk";
|
|
2044
|
+
PrefixV2["P256PublicKey"] = "p2pk";
|
|
2045
|
+
PrefixV2["Secp256k1Scalar"] = "SSp";
|
|
2046
|
+
PrefixV2["Secp256k1Element"] = "GSp";
|
|
2047
|
+
PrefixV2["Ed25519SecretKey"] = "_edsk";
|
|
2048
|
+
PrefixV2["Ed25519Signature"] = "edsig";
|
|
2049
|
+
PrefixV2["Secp256k1Signature"] = "spsig1";
|
|
2050
|
+
PrefixV2["P256Signature"] = "p2sig";
|
|
2051
|
+
PrefixV2["GenericSignature"] = "sig";
|
|
2052
|
+
PrefixV2["ChainID"] = "Net";
|
|
2053
|
+
PrefixV2["SaplingSpendingKey"] = "sask";
|
|
2054
|
+
PrefixV2["EncryptedSaplingSpendingKey"] = "_sask";
|
|
2055
|
+
PrefixV2["SaplingAddress"] = "zet1";
|
|
2056
|
+
PrefixV2["GenericAggregateSignature"] = "asig";
|
|
2057
|
+
PrefixV2["BLS12_381Signature"] = "BLsig";
|
|
2058
|
+
PrefixV2["BLS12_381PublicKey"] = "BLpk";
|
|
2059
|
+
PrefixV2["SlotHeader"] = "sh";
|
|
2060
|
+
})(PrefixV2 || (PrefixV2 = {}));
|
|
2061
|
+
/**
|
|
2062
|
+
* @description base58 prefix to bytes mapping
|
|
2063
|
+
*/
|
|
2064
|
+
const prefixV2 = {
|
|
2065
|
+
[PrefixV2.BlockHash]: new Uint8Array([1, 52]),
|
|
2066
|
+
[PrefixV2.OperationHash]: new Uint8Array([5, 116]),
|
|
2067
|
+
[PrefixV2.OperationListHash]: new Uint8Array([133, 233]),
|
|
2068
|
+
[PrefixV2.OperationListListHash]: new Uint8Array([29, 159, 109]),
|
|
2069
|
+
[PrefixV2.ProtocolHash]: new Uint8Array([2, 170]),
|
|
2070
|
+
[PrefixV2.ContextHash]: new Uint8Array([79, 199]),
|
|
2071
|
+
[PrefixV2.BlockMetadataHash]: new Uint8Array([234, 249]),
|
|
2072
|
+
[PrefixV2.OperationMetadataHash]: new Uint8Array([5, 183]),
|
|
2073
|
+
[PrefixV2.OperationMetadataListHash]: new Uint8Array([134, 39]),
|
|
2074
|
+
[PrefixV2.OperationMetadataListListHash]: new Uint8Array([29, 159, 182]),
|
|
2075
|
+
[PrefixV2.Ed25519PublicKeyHash]: new Uint8Array([6, 161, 159]),
|
|
2076
|
+
[PrefixV2.Secp256k1PublicKeyHash]: new Uint8Array([6, 161, 161]),
|
|
2077
|
+
[PrefixV2.P256PublicKeyHash]: new Uint8Array([6, 161, 164]),
|
|
2078
|
+
[PrefixV2.ContractHash]: new Uint8Array([2, 90, 121]),
|
|
2079
|
+
[PrefixV2.BlindedPublicKeyHash]: new Uint8Array([1, 2, 49, 223]),
|
|
2080
|
+
[PrefixV2.BLS12_381PublicKeyHash]: new Uint8Array([6, 161, 166]),
|
|
2081
|
+
[PrefixV2.TXRollupAddress]: new Uint8Array([1, 128, 120, 31]),
|
|
2082
|
+
[PrefixV2.ZkRollupHash]: new Uint8Array([1, 23, 224, 125]),
|
|
2083
|
+
[PrefixV2.ScRollupHash]: new Uint8Array([1, 118, 132, 217]),
|
|
2084
|
+
[PrefixV2.SmartRollupHash]: new Uint8Array([6, 124, 117]),
|
|
2085
|
+
[PrefixV2.CryptoboxPublicKeyHash]: new Uint8Array([153, 103]),
|
|
2086
|
+
[PrefixV2.Ed25519Seed]: new Uint8Array([13, 15, 58, 7]),
|
|
2087
|
+
[PrefixV2.Ed25519PublicKey]: new Uint8Array([13, 15, 37, 217]),
|
|
2088
|
+
[PrefixV2.Secp256k1SecretKey]: new Uint8Array([17, 162, 224, 201]),
|
|
2089
|
+
[PrefixV2.P256SecretKey]: new Uint8Array([16, 81, 238, 189]),
|
|
2090
|
+
[PrefixV2.BLS12_381SecretKey]: new Uint8Array([3, 150, 192, 40]),
|
|
2091
|
+
[PrefixV2.ValueHash]: new Uint8Array([1, 106, 242]),
|
|
2092
|
+
[PrefixV2.CycleNonce]: new Uint8Array([69, 220, 169]),
|
|
2093
|
+
[PrefixV2.ScriptExpr]: new Uint8Array([13, 44, 64, 27]),
|
|
2094
|
+
[PrefixV2.InboxHash]: new Uint8Array([79, 148, 196]),
|
|
2095
|
+
[PrefixV2.MessageHash]: new Uint8Array([79, 149, 30]),
|
|
2096
|
+
[PrefixV2.CommitmentHash]: new Uint8Array([79, 148, 17]),
|
|
2097
|
+
[PrefixV2.MessageResultHash]: new Uint8Array([18, 7, 206, 87]),
|
|
2098
|
+
[PrefixV2.MessageResultListHash]: new Uint8Array([79, 146, 82]),
|
|
2099
|
+
[PrefixV2.WithdrawListHash]: new Uint8Array([79, 150, 72]),
|
|
2100
|
+
[PrefixV2.ScRollupStateHash]: new Uint8Array([17, 144, 122, 202]),
|
|
2101
|
+
[PrefixV2.ScRollupCommitmentHash]: new Uint8Array([17, 144, 21, 100]),
|
|
2102
|
+
[PrefixV2.SmartRollupStateHash]: new Uint8Array([17, 165, 235, 240]),
|
|
2103
|
+
[PrefixV2.SmartRollupCommitmentHash]: new Uint8Array([17, 165, 134, 138]),
|
|
2104
|
+
[PrefixV2.Ed25519EncryptedSeed]: new Uint8Array([7, 90, 60, 179, 41]),
|
|
2105
|
+
[PrefixV2.Secp256k1EncryptedSecretKey]: new Uint8Array([9, 237, 241, 174, 150]),
|
|
2106
|
+
[PrefixV2.P256EncryptedSecretKey]: new Uint8Array([9, 48, 57, 115, 171]),
|
|
2107
|
+
[PrefixV2.BLS12_381EncryptedSecretKey]: new Uint8Array([2, 5, 30, 53, 25]),
|
|
2108
|
+
[PrefixV2.Secp256k1EncryptedScalar]: new Uint8Array([1, 131, 36, 86, 248]),
|
|
2109
|
+
[PrefixV2.Secp256k1PublicKey]: new Uint8Array([3, 254, 226, 86]),
|
|
2110
|
+
[PrefixV2.P256PublicKey]: new Uint8Array([3, 178, 139, 127]),
|
|
2111
|
+
[PrefixV2.Secp256k1Scalar]: new Uint8Array([38, 248, 136]),
|
|
2112
|
+
[PrefixV2.Secp256k1Element]: new Uint8Array([5, 92, 0]),
|
|
2113
|
+
[PrefixV2.Ed25519SecretKey]: new Uint8Array([43, 246, 78, 7]),
|
|
2114
|
+
[PrefixV2.Ed25519Signature]: new Uint8Array([9, 245, 205, 134, 18]),
|
|
2115
|
+
[PrefixV2.Secp256k1Signature]: new Uint8Array([13, 115, 101, 19, 63]),
|
|
2116
|
+
[PrefixV2.P256Signature]: new Uint8Array([54, 240, 44, 52]),
|
|
2117
|
+
[PrefixV2.GenericSignature]: new Uint8Array([4, 130, 43]),
|
|
2118
|
+
[PrefixV2.ChainID]: new Uint8Array([87, 82, 0]),
|
|
2119
|
+
[PrefixV2.SaplingSpendingKey]: new Uint8Array([11, 237, 20, 92]),
|
|
2120
|
+
[PrefixV2.EncryptedSaplingSpendingKey]: new Uint8Array([11, 237, 20, 92]),
|
|
2121
|
+
[PrefixV2.SaplingAddress]: new Uint8Array([18, 71, 40, 223]),
|
|
2122
|
+
[PrefixV2.GenericAggregateSignature]: new Uint8Array([2, 75, 234, 101]),
|
|
2123
|
+
[PrefixV2.BLS12_381Signature]: new Uint8Array([40, 171, 64, 207]),
|
|
2124
|
+
[PrefixV2.BLS12_381PublicKey]: new Uint8Array([6, 149, 135, 204]),
|
|
2125
|
+
[PrefixV2.SlotHeader]: new Uint8Array([2, 116, 180]),
|
|
2126
|
+
};
|
|
2127
|
+
/**
|
|
2128
|
+
* @description base58 prefix to payload length mapping
|
|
2129
|
+
*/
|
|
2130
|
+
const payloadLength = {
|
|
2131
|
+
[PrefixV2.BlockHash]: 32,
|
|
2132
|
+
[PrefixV2.OperationHash]: 32,
|
|
2133
|
+
[PrefixV2.OperationListHash]: 32,
|
|
2134
|
+
[PrefixV2.OperationListListHash]: 32,
|
|
2135
|
+
[PrefixV2.ProtocolHash]: 32,
|
|
2136
|
+
[PrefixV2.ContextHash]: 32,
|
|
2137
|
+
[PrefixV2.BlockMetadataHash]: 32,
|
|
2138
|
+
[PrefixV2.OperationMetadataHash]: 32,
|
|
2139
|
+
[PrefixV2.OperationMetadataListHash]: 32,
|
|
2140
|
+
[PrefixV2.OperationMetadataListListHash]: 32,
|
|
2141
|
+
[PrefixV2.Ed25519PublicKeyHash]: 20,
|
|
2142
|
+
[PrefixV2.Secp256k1PublicKeyHash]: 20,
|
|
2143
|
+
[PrefixV2.P256PublicKeyHash]: 20,
|
|
2144
|
+
[PrefixV2.ContractHash]: 20,
|
|
2145
|
+
[PrefixV2.BlindedPublicKeyHash]: 20,
|
|
2146
|
+
[PrefixV2.BLS12_381PublicKeyHash]: 20,
|
|
2147
|
+
[PrefixV2.TXRollupAddress]: 20,
|
|
2148
|
+
[PrefixV2.ZkRollupHash]: 20,
|
|
2149
|
+
[PrefixV2.ScRollupHash]: 20,
|
|
2150
|
+
[PrefixV2.SmartRollupHash]: 20,
|
|
2151
|
+
[PrefixV2.CryptoboxPublicKeyHash]: 16,
|
|
2152
|
+
[PrefixV2.Ed25519Seed]: 32,
|
|
2153
|
+
[PrefixV2.Ed25519PublicKey]: 32,
|
|
2154
|
+
[PrefixV2.Secp256k1SecretKey]: 32,
|
|
2155
|
+
[PrefixV2.P256SecretKey]: 32,
|
|
2156
|
+
[PrefixV2.BLS12_381SecretKey]: 32,
|
|
2157
|
+
[PrefixV2.ValueHash]: 32,
|
|
2158
|
+
[PrefixV2.CycleNonce]: 32,
|
|
2159
|
+
[PrefixV2.ScriptExpr]: 32,
|
|
2160
|
+
[PrefixV2.InboxHash]: 32,
|
|
2161
|
+
[PrefixV2.MessageHash]: 32,
|
|
2162
|
+
[PrefixV2.CommitmentHash]: 32,
|
|
2163
|
+
[PrefixV2.MessageResultHash]: 32,
|
|
2164
|
+
[PrefixV2.MessageResultListHash]: 32,
|
|
2165
|
+
[PrefixV2.WithdrawListHash]: 32,
|
|
2166
|
+
[PrefixV2.ScRollupStateHash]: 32,
|
|
2167
|
+
[PrefixV2.ScRollupCommitmentHash]: 32,
|
|
2168
|
+
[PrefixV2.SmartRollupStateHash]: 32,
|
|
2169
|
+
[PrefixV2.SmartRollupCommitmentHash]: 32,
|
|
2170
|
+
[PrefixV2.Ed25519EncryptedSeed]: 56,
|
|
2171
|
+
[PrefixV2.Secp256k1EncryptedSecretKey]: 56,
|
|
2172
|
+
[PrefixV2.P256EncryptedSecretKey]: 56,
|
|
2173
|
+
[PrefixV2.BLS12_381EncryptedSecretKey]: 56,
|
|
2174
|
+
[PrefixV2.Secp256k1EncryptedScalar]: 60,
|
|
2175
|
+
[PrefixV2.Secp256k1PublicKey]: 33,
|
|
2176
|
+
[PrefixV2.P256PublicKey]: 33,
|
|
2177
|
+
[PrefixV2.Secp256k1Scalar]: 33,
|
|
2178
|
+
[PrefixV2.Secp256k1Element]: 33,
|
|
2179
|
+
[PrefixV2.Ed25519SecretKey]: 64,
|
|
2180
|
+
[PrefixV2.Ed25519Signature]: 64,
|
|
2181
|
+
[PrefixV2.Secp256k1Signature]: 64,
|
|
2182
|
+
[PrefixV2.P256Signature]: 64,
|
|
2183
|
+
[PrefixV2.GenericSignature]: 64,
|
|
2184
|
+
[PrefixV2.ChainID]: 4,
|
|
2185
|
+
[PrefixV2.SaplingSpendingKey]: 169,
|
|
2186
|
+
[PrefixV2.EncryptedSaplingSpendingKey]: 193,
|
|
2187
|
+
[PrefixV2.SaplingAddress]: 43,
|
|
2188
|
+
[PrefixV2.GenericAggregateSignature]: 96,
|
|
2189
|
+
[PrefixV2.BLS12_381Signature]: 96,
|
|
2190
|
+
[PrefixV2.BLS12_381PublicKey]: 48,
|
|
2191
|
+
[PrefixV2.SlotHeader]: 48,
|
|
2192
|
+
};
|
|
2193
|
+
|
|
768
2194
|
/**
|
|
769
2195
|
* @packageDocumentation
|
|
770
2196
|
* @module @taquito/utils
|
|
@@ -1111,317 +2537,623 @@ function encodeAddress(value) {
|
|
|
1111
2537
|
}
|
|
1112
2538
|
}
|
|
1113
2539
|
/**
|
|
1114
|
-
* @description Base58 encode an address without predefined prefix
|
|
1115
|
-
* @param value Address to base58 encode (tz4) hex dec
|
|
1116
|
-
* @returns return address
|
|
1117
|
-
* @example encodeBlsAddress('af2dc3c40667abc0e89c0ef40171d22aed08d5eb') // return 'tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8'
|
|
2540
|
+
* @description Base58 encode an address without predefined prefix
|
|
2541
|
+
* @param value Address to base58 encode (tz4) hex dec
|
|
2542
|
+
* @returns return address
|
|
2543
|
+
* @example encodeBlsAddress('af2dc3c40667abc0e89c0ef40171d22aed08d5eb') // return 'tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8'
|
|
2544
|
+
*/
|
|
2545
|
+
function encodeBlsAddress(value) {
|
|
2546
|
+
return b58Encode(value, PrefixV2.BLS12_381PublicKeyHash);
|
|
2547
|
+
}
|
|
2548
|
+
/**
|
|
2549
|
+
* @description convert a fragment of Michelson code in hex string to an 'expr' prefix + base58 encoded BLAKE2b hash string
|
|
2550
|
+
* @param value a fragment of Michelson code in hex string
|
|
2551
|
+
* @returns return 'expr' prefix + base58 encoded BLAKE2b hash
|
|
2552
|
+
* @example encodeExpr('050a000000160000b2e19a9e74440d86c59f13dab8a18ff873e889ea') // return 'exprv6UsC1sN3Fk2XfgcJCL8NCerP5rCGy1PRESZAqr7L2JdzX55EN'
|
|
2553
|
+
*/
|
|
2554
|
+
function encodeExpr(value) {
|
|
2555
|
+
const blakeHash = hash(hex2buf(value), 32);
|
|
2556
|
+
return b58Encode(blakeHash, PrefixV2.ScriptExpr);
|
|
2557
|
+
}
|
|
2558
|
+
/**
|
|
2559
|
+
* @description convert a signed operation in hex string to an 'op' prefix + base58 encoded BLAKE2b hash string
|
|
2560
|
+
* @param value signed operation in hex string
|
|
2561
|
+
* @returns return 'op' prefix + base58 encoded BLAKE2b hash
|
|
2562
|
+
* @example encodeOpHash('0f185d8a30061e8134c162dbb7a6c3ab8f5fdb153363ccd6149b49a33481156a6c00b2e19a9e74440d86c59f13dab8a18ff873e889eaa304ab05da13000001f1585a7384f36e45fb43dc37e8ce172bced3e05700ff0000000002002110c033f3a990c2e46a3d6054ecc2f74072aae7a34b5ac4d9ce9edc11c2410a97695682108951786f05b361da03b97245dc9897e1955e08b5b8d9e153b0bdeb0d') // return 'opapqvVXmebRTCFd2GQFydr4tJj3V5QocQuTmuhbatcHm4Seo2t'
|
|
2563
|
+
*/
|
|
2564
|
+
function encodeOpHash(value) {
|
|
2565
|
+
const blakeHash = hash(hex2buf(value), 32);
|
|
2566
|
+
return b58Encode(blakeHash, PrefixV2.OperationHash);
|
|
2567
|
+
}
|
|
2568
|
+
/**
|
|
2569
|
+
* @description Convert an hex string to a Uint8Array
|
|
2570
|
+
* @param hex Hex string to convert
|
|
2571
|
+
* @throws {@link ValueConversionError}
|
|
2572
|
+
*/
|
|
2573
|
+
function hex2buf(hex) {
|
|
2574
|
+
hex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
2575
|
+
if (hex.length % 2 !== 0) {
|
|
2576
|
+
throw new InvalidHexStringError(hex, `Expecting even number of characters`);
|
|
2577
|
+
}
|
|
2578
|
+
if (!hex.match(/^([\da-f]{2})*$/gi)) {
|
|
2579
|
+
throw new InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
|
|
2580
|
+
}
|
|
2581
|
+
const res = new Uint8Array(hex.length / 2);
|
|
2582
|
+
let j = 0;
|
|
2583
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
2584
|
+
const ss = hex.slice(i, i + 2);
|
|
2585
|
+
const x = parseInt(ss, 16);
|
|
2586
|
+
if (Number.isNaN(x)) {
|
|
2587
|
+
throw new InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
|
|
2588
|
+
}
|
|
2589
|
+
res[j++] = x;
|
|
2590
|
+
}
|
|
2591
|
+
return res;
|
|
2592
|
+
}
|
|
2593
|
+
/**
|
|
2594
|
+
* @description Merge 2 buffers together
|
|
2595
|
+
* @param b1 First buffer
|
|
2596
|
+
* @param b2 Second buffer
|
|
2597
|
+
*/
|
|
2598
|
+
function mergebuf(b1, b2) {
|
|
2599
|
+
const r = new Uint8Array(b1.length + b2.length);
|
|
2600
|
+
r.set(b1);
|
|
2601
|
+
r.set(b2, b1.length);
|
|
2602
|
+
return r;
|
|
2603
|
+
}
|
|
2604
|
+
/**
|
|
2605
|
+
* @description Flatten a michelson json representation to an array
|
|
2606
|
+
* @param s michelson json
|
|
2607
|
+
*/
|
|
2608
|
+
function mic2arr(s) {
|
|
2609
|
+
let ret = [];
|
|
2610
|
+
if (Object.prototype.hasOwnProperty.call(s, 'prim')) {
|
|
2611
|
+
if (s.prim === 'Pair') {
|
|
2612
|
+
ret.push(mic2arr(s.args[0]));
|
|
2613
|
+
ret = ret.concat(mic2arr(s.args[1]));
|
|
2614
|
+
}
|
|
2615
|
+
else if (s.prim === 'Elt') {
|
|
2616
|
+
ret = {
|
|
2617
|
+
key: mic2arr(s.args[0]),
|
|
2618
|
+
val: mic2arr(s.args[1]),
|
|
2619
|
+
};
|
|
2620
|
+
}
|
|
2621
|
+
else if (s.prim === 'True') {
|
|
2622
|
+
ret = true;
|
|
2623
|
+
}
|
|
2624
|
+
else if (s.prim === 'False') {
|
|
2625
|
+
ret = false;
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
else if (Array.isArray(s)) {
|
|
2629
|
+
const sc = s.length;
|
|
2630
|
+
for (let i = 0; i < sc; i++) {
|
|
2631
|
+
const n = mic2arr(s[i]);
|
|
2632
|
+
if (typeof n.key !== 'undefined') {
|
|
2633
|
+
if (Array.isArray(ret)) {
|
|
2634
|
+
ret = {
|
|
2635
|
+
keys: [],
|
|
2636
|
+
vals: [],
|
|
2637
|
+
};
|
|
2638
|
+
}
|
|
2639
|
+
ret.keys.push(n.key);
|
|
2640
|
+
ret.vals.push(n.val);
|
|
2641
|
+
}
|
|
2642
|
+
else {
|
|
2643
|
+
ret.push(n);
|
|
2644
|
+
}
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
else if (Object.prototype.hasOwnProperty.call(s, 'string')) {
|
|
2648
|
+
ret = s.string;
|
|
2649
|
+
}
|
|
2650
|
+
else if (Object.prototype.hasOwnProperty.call(s, 'int')) {
|
|
2651
|
+
ret = parseInt(s.int, 10);
|
|
2652
|
+
}
|
|
2653
|
+
else {
|
|
2654
|
+
ret = s;
|
|
2655
|
+
}
|
|
2656
|
+
return ret;
|
|
2657
|
+
}
|
|
2658
|
+
/**
|
|
2659
|
+
* @description Convert a Uint8Array to an hex string
|
|
2660
|
+
* @param buffer Uint8Array to convert
|
|
2661
|
+
*/
|
|
2662
|
+
function buf2hex(bytes) {
|
|
2663
|
+
return Array.from(bytes)
|
|
2664
|
+
.map((x) => ((x >> 4) & 0xf).toString(16) + (x & 0xf).toString(16))
|
|
2665
|
+
.join('');
|
|
2666
|
+
}
|
|
2667
|
+
/**
|
|
2668
|
+
* @description Convert a string to a byte string representation
|
|
2669
|
+
* @param str String to convert
|
|
2670
|
+
*/
|
|
2671
|
+
function stringToBytes(str) {
|
|
2672
|
+
return Buffer.from(str, 'utf8').toString('hex');
|
|
2673
|
+
}
|
|
2674
|
+
/**
|
|
2675
|
+
* @description Convert byte string representation to string
|
|
2676
|
+
* @param hex byte string to convert
|
|
2677
|
+
*/
|
|
2678
|
+
function bytesToString(hex) {
|
|
2679
|
+
return Buffer.from(hex2buf(hex)).toString('utf8');
|
|
2680
|
+
}
|
|
2681
|
+
/**
|
|
2682
|
+
* @description Convert hex string/UintArray/Buffer to bytes
|
|
2683
|
+
* @param hex String value to convert to bytes
|
|
2684
|
+
*/
|
|
2685
|
+
function hex2Bytes(hex) {
|
|
2686
|
+
const hexDigits = stripHexPrefix(hex);
|
|
2687
|
+
if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
|
|
2688
|
+
throw new InvalidHexStringError(hex, `Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
|
|
2689
|
+
}
|
|
2690
|
+
return Buffer.from(hexDigits, 'hex');
|
|
2691
|
+
}
|
|
2692
|
+
/**
|
|
2693
|
+
* @description Converts a number or Bignumber to hexadecimal string
|
|
2694
|
+
* @param val The value that will be converted to a hexadecimal string value
|
|
2695
|
+
*/
|
|
2696
|
+
function toHexBuf(val, bitLength = 8) {
|
|
2697
|
+
return Buffer.from(num2PaddedHex(val, bitLength), 'hex');
|
|
2698
|
+
}
|
|
2699
|
+
function numToHexBuffer(val, bitLength = 8) {
|
|
2700
|
+
return Buffer.from(num2PaddedHex(val, bitLength), 'hex');
|
|
2701
|
+
}
|
|
2702
|
+
/**
|
|
2703
|
+
* @description Converts a number or BigNumber to a padded hexadecimal string
|
|
2704
|
+
* @param val The value that will be converted into a padded hexadecimal string value
|
|
2705
|
+
* @param bitLength The length of bits
|
|
2706
|
+
*
|
|
2707
|
+
*/
|
|
2708
|
+
function num2PaddedHex(val, bitLength = 8) {
|
|
2709
|
+
if (new BigNumber(val).isPositive()) {
|
|
2710
|
+
const nibbleLength = Math.ceil(bitLength / 4);
|
|
2711
|
+
const hex = val.toString(16);
|
|
2712
|
+
// check whether nibble (4 bits) length is higher or lower than the current hex string length
|
|
2713
|
+
let targetLength = hex.length >= nibbleLength ? hex.length : nibbleLength;
|
|
2714
|
+
// make sure the hex string target length is even
|
|
2715
|
+
targetLength = targetLength % 2 == 0 ? targetLength : targetLength + 1;
|
|
2716
|
+
return padHexWithZero(hex, targetLength);
|
|
2717
|
+
}
|
|
2718
|
+
else {
|
|
2719
|
+
const twosCompliment = new BigNumber(2)
|
|
2720
|
+
.pow(bitLength)
|
|
2721
|
+
.minus(new BigNumber(val).abs());
|
|
2722
|
+
return twosCompliment.toString(16);
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
function padHexWithZero(hex, targetLength) {
|
|
2726
|
+
const padString = '0';
|
|
2727
|
+
if (hex.length >= targetLength) {
|
|
2728
|
+
return hex;
|
|
2729
|
+
}
|
|
2730
|
+
else {
|
|
2731
|
+
const padLength = targetLength - hex.length;
|
|
2732
|
+
return padString.repeat(padLength) + hex;
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
2735
|
+
/**
|
|
2736
|
+
*
|
|
2737
|
+
* @description Strips the first 2 characters of a hex string (0x)
|
|
2738
|
+
*
|
|
2739
|
+
* @param hex string to strip prefix from
|
|
2740
|
+
*/
|
|
2741
|
+
function stripHexPrefix(hex) {
|
|
2742
|
+
return hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
2743
|
+
}
|
|
2744
|
+
function splitAddress(addr) {
|
|
2745
|
+
const i = addr.indexOf('%');
|
|
2746
|
+
if (i >= 0) {
|
|
2747
|
+
return [addr.slice(0, i), addr.slice(i)];
|
|
2748
|
+
}
|
|
2749
|
+
else {
|
|
2750
|
+
return [addr, null];
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
function compareArrays(a, b) {
|
|
2754
|
+
let i = 0;
|
|
2755
|
+
while (i < a.length && i < b.length && a[i] === b[i])
|
|
2756
|
+
i++;
|
|
2757
|
+
const aa = i < a.length ? a[i] : 0;
|
|
2758
|
+
const bb = i < b.length ? b[i] : 0;
|
|
2759
|
+
return aa < bb ? -1 : aa > bb ? 1 : 0;
|
|
2760
|
+
}
|
|
2761
|
+
|
|
2762
|
+
/**
|
|
2763
|
+
* @description Used to check if a value has one of the allowed prefixes.
|
|
2764
|
+
* @returns true if the value has one of the allowed prefixes, false otherwise
|
|
2765
|
+
* @example
|
|
2766
|
+
* ```
|
|
2767
|
+
* import { isValidPrefixedValue } from '@taquito/utils';
|
|
2768
|
+
* const value = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx'
|
|
2769
|
+
* const isValid = isValidPrefixedValue(value, [PrefixV2.Ed25519PublicKeyHash])
|
|
2770
|
+
* console.log(isValid) // true
|
|
2771
|
+
* ```
|
|
2772
|
+
*/
|
|
2773
|
+
function isValidPrefixedValue(value, prefixes) {
|
|
2774
|
+
return validatePrefixedValue(value, prefixes) === ValidationResult.VALID;
|
|
2775
|
+
}
|
|
2776
|
+
/**
|
|
2777
|
+
* @description This function is called by the validation functions ([[validateAddress]], [[validateChain]], [[validateContractAddress]], [[validateKeyHash]], [[validateSignature]], [[validatePublicKey]]).
|
|
2778
|
+
* Verify if the value can be decoded or return `1` (INVALID_CHECKSUM) / `5` (INVALID_ENCODING), then check if the prefix is valid or allowed or return `0` (NO_PREFIX_MATCHED) / `4` (PREFIX_NOT_ALLOWED)
|
|
2779
|
+
* If all checks pass, return `3` (VALID).
|
|
2780
|
+
* @param value Value to validate
|
|
2781
|
+
* @param prefixes prefix the value should have
|
|
2782
|
+
* @returns 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2783
|
+
* @example
|
|
2784
|
+
* ```
|
|
2785
|
+
* import { validatePrefixedValue } from '@taquito/utils';
|
|
2786
|
+
* const value = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx'
|
|
2787
|
+
* const validation = validatePrefixedValue(value, [PrefixV2.Ed25519PublicKeyHash])
|
|
2788
|
+
* console.log(validation) // 3
|
|
2789
|
+
* ```
|
|
1118
2790
|
*/
|
|
1119
|
-
function
|
|
1120
|
-
|
|
2791
|
+
function validatePrefixedValue(value, prefixes) {
|
|
2792
|
+
try {
|
|
2793
|
+
b58DecodeAndCheckPrefix(value, prefixes);
|
|
2794
|
+
}
|
|
2795
|
+
catch (err) {
|
|
2796
|
+
if (err instanceof ParameterValidationError && err.result !== undefined) {
|
|
2797
|
+
return err.result;
|
|
2798
|
+
}
|
|
2799
|
+
return ValidationResult.OTHER;
|
|
2800
|
+
}
|
|
2801
|
+
return ValidationResult.VALID;
|
|
1121
2802
|
}
|
|
1122
2803
|
/**
|
|
1123
|
-
* @description
|
|
1124
|
-
*
|
|
1125
|
-
* @returns
|
|
1126
|
-
*
|
|
2804
|
+
* @description Used to check if an address or a contract address is valid.
|
|
2805
|
+
*
|
|
2806
|
+
* @returns
|
|
2807
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2808
|
+
*
|
|
2809
|
+
* @example
|
|
2810
|
+
* ```
|
|
2811
|
+
* import { validateAddress } from '@taquito/utils';
|
|
2812
|
+
* const pkh = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx'
|
|
2813
|
+
* const validation = validateAddress(pkh)
|
|
2814
|
+
* console.log(validation)
|
|
2815
|
+
* // This example return 3 which correspond to VALID
|
|
2816
|
+
* ```
|
|
1127
2817
|
*/
|
|
1128
|
-
function
|
|
1129
|
-
const
|
|
1130
|
-
return
|
|
2818
|
+
function validateAddress(value) {
|
|
2819
|
+
const [addr] = splitAddress(value !== null && value !== void 0 ? value : '');
|
|
2820
|
+
return validatePrefixedValue(addr, addressPrefixes);
|
|
1131
2821
|
}
|
|
1132
2822
|
/**
|
|
1133
|
-
* @description
|
|
1134
|
-
*
|
|
1135
|
-
* @returns
|
|
1136
|
-
*
|
|
2823
|
+
* @description Used to check if a chain id is valid.
|
|
2824
|
+
*
|
|
2825
|
+
* @returns
|
|
2826
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2827
|
+
*
|
|
2828
|
+
* @example
|
|
2829
|
+
* ```
|
|
2830
|
+
* import { validateChain } from '@taquito/utils';
|
|
2831
|
+
* const chainId = 'NetXdQprcVkpaWU'
|
|
2832
|
+
* const validation = validateChain(chainId)
|
|
2833
|
+
* console.log(validation)
|
|
2834
|
+
* // This example return 3 which correspond to VALID
|
|
2835
|
+
* ```
|
|
1137
2836
|
*/
|
|
1138
|
-
function
|
|
1139
|
-
|
|
1140
|
-
return b58Encode(blakeHash, PrefixV2.OperationHash);
|
|
2837
|
+
function validateChain(value) {
|
|
2838
|
+
return validatePrefixedValue(value, [PrefixV2.ChainID]);
|
|
1141
2839
|
}
|
|
1142
2840
|
/**
|
|
1143
|
-
* @description
|
|
1144
|
-
*
|
|
1145
|
-
* @
|
|
2841
|
+
* @description Used to check if a contract address is valid.
|
|
2842
|
+
*
|
|
2843
|
+
* @returns
|
|
2844
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2845
|
+
*
|
|
2846
|
+
* @example
|
|
2847
|
+
* ```
|
|
2848
|
+
* import { validateContractAddress } from '@taquito/utils';
|
|
2849
|
+
* const contractAddress = 'KT1JVErLYTgtY8uGGZ4mso2npTSxqVLDRVbC'
|
|
2850
|
+
* const validation = validateContractAddress(contractAddress)
|
|
2851
|
+
* console.log(validation)
|
|
2852
|
+
* // This example return 3 which correspond to VALID
|
|
2853
|
+
* ```
|
|
1146
2854
|
*/
|
|
1147
|
-
function
|
|
1148
|
-
|
|
1149
|
-
if (hex.length % 2 !== 0) {
|
|
1150
|
-
throw new InvalidHexStringError(hex, `Expecting even number of characters`);
|
|
1151
|
-
}
|
|
1152
|
-
if (!hex.match(/^([\da-f]{2})*$/gi)) {
|
|
1153
|
-
throw new InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
|
|
1154
|
-
}
|
|
1155
|
-
const res = new Uint8Array(hex.length / 2);
|
|
1156
|
-
let j = 0;
|
|
1157
|
-
for (let i = 0; i < hex.length; i += 2) {
|
|
1158
|
-
const ss = hex.slice(i, i + 2);
|
|
1159
|
-
const x = parseInt(ss, 16);
|
|
1160
|
-
if (Number.isNaN(x)) {
|
|
1161
|
-
throw new InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
|
|
1162
|
-
}
|
|
1163
|
-
res[j++] = x;
|
|
1164
|
-
}
|
|
1165
|
-
return res;
|
|
2855
|
+
function validateContractAddress(value) {
|
|
2856
|
+
return validatePrefixedValue(value, [PrefixV2.ContractHash]);
|
|
1166
2857
|
}
|
|
1167
2858
|
/**
|
|
1168
|
-
* @description
|
|
1169
|
-
*
|
|
1170
|
-
* @
|
|
2859
|
+
* @description Used to check if a key hash is valid.
|
|
2860
|
+
*
|
|
2861
|
+
* @returns
|
|
2862
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2863
|
+
*
|
|
2864
|
+
* @example
|
|
2865
|
+
* ```
|
|
2866
|
+
* import { validateKeyHash } from '@taquito/utils';
|
|
2867
|
+
* const keyHashWithoutPrefix = '1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx'
|
|
2868
|
+
* const validation = validateKeyHash(keyHashWithoutPrefix)
|
|
2869
|
+
* console.log(validation)
|
|
2870
|
+
* // This example return 0 which correspond to NO_PREFIX_MATCHED
|
|
2871
|
+
* ```
|
|
1171
2872
|
*/
|
|
1172
|
-
function
|
|
1173
|
-
|
|
1174
|
-
r.set(b1);
|
|
1175
|
-
r.set(b2, b1.length);
|
|
1176
|
-
return r;
|
|
2873
|
+
function validateKeyHash(value) {
|
|
2874
|
+
return validatePrefixedValue(value, publicKeyHashPrefixes);
|
|
1177
2875
|
}
|
|
1178
2876
|
/**
|
|
1179
|
-
* @description
|
|
1180
|
-
*
|
|
2877
|
+
* @description Used to check if a signature is valid.
|
|
2878
|
+
*
|
|
2879
|
+
* @returns
|
|
2880
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2881
|
+
*
|
|
2882
|
+
* @example
|
|
2883
|
+
* ```
|
|
2884
|
+
* import { validateSignature } from '@taquito/utils';
|
|
2885
|
+
* const signature = 'edsigtkpiSSschcaCt9pUVrpNPf7TTcgvgDEDD6NCEHMy8NNQJCGnMfLZzYoQj74yLjo9wx6MPVV29CvVzgi7qEcEUok3k7AuMg'
|
|
2886
|
+
* const validation = validateSignature(signature)
|
|
2887
|
+
* console.log(validation)
|
|
2888
|
+
* // This example return 3 which correspond to VALID
|
|
2889
|
+
* ```
|
|
1181
2890
|
*/
|
|
1182
|
-
function
|
|
1183
|
-
|
|
1184
|
-
if (Object.prototype.hasOwnProperty.call(s, 'prim')) {
|
|
1185
|
-
if (s.prim === 'Pair') {
|
|
1186
|
-
ret.push(mic2arr(s.args[0]));
|
|
1187
|
-
ret = ret.concat(mic2arr(s.args[1]));
|
|
1188
|
-
}
|
|
1189
|
-
else if (s.prim === 'Elt') {
|
|
1190
|
-
ret = {
|
|
1191
|
-
key: mic2arr(s.args[0]),
|
|
1192
|
-
val: mic2arr(s.args[1]),
|
|
1193
|
-
};
|
|
1194
|
-
}
|
|
1195
|
-
else if (s.prim === 'True') {
|
|
1196
|
-
ret = true;
|
|
1197
|
-
}
|
|
1198
|
-
else if (s.prim === 'False') {
|
|
1199
|
-
ret = false;
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
else if (Array.isArray(s)) {
|
|
1203
|
-
const sc = s.length;
|
|
1204
|
-
for (let i = 0; i < sc; i++) {
|
|
1205
|
-
const n = mic2arr(s[i]);
|
|
1206
|
-
if (typeof n.key !== 'undefined') {
|
|
1207
|
-
if (Array.isArray(ret)) {
|
|
1208
|
-
ret = {
|
|
1209
|
-
keys: [],
|
|
1210
|
-
vals: [],
|
|
1211
|
-
};
|
|
1212
|
-
}
|
|
1213
|
-
ret.keys.push(n.key);
|
|
1214
|
-
ret.vals.push(n.val);
|
|
1215
|
-
}
|
|
1216
|
-
else {
|
|
1217
|
-
ret.push(n);
|
|
1218
|
-
}
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
else if (Object.prototype.hasOwnProperty.call(s, 'string')) {
|
|
1222
|
-
ret = s.string;
|
|
1223
|
-
}
|
|
1224
|
-
else if (Object.prototype.hasOwnProperty.call(s, 'int')) {
|
|
1225
|
-
ret = parseInt(s.int, 10);
|
|
1226
|
-
}
|
|
1227
|
-
else {
|
|
1228
|
-
ret = s;
|
|
1229
|
-
}
|
|
1230
|
-
return ret;
|
|
2891
|
+
function validateSignature(value) {
|
|
2892
|
+
return validatePrefixedValue(value, signaturePrefixes);
|
|
1231
2893
|
}
|
|
1232
2894
|
/**
|
|
1233
|
-
* @description
|
|
1234
|
-
*
|
|
2895
|
+
* @description Used to check if a public key is valid.
|
|
2896
|
+
*
|
|
2897
|
+
* @returns
|
|
2898
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2899
|
+
*
|
|
2900
|
+
* @example
|
|
2901
|
+
* ```
|
|
2902
|
+
* import { validatePublicKey } from '@taquito/utils';
|
|
2903
|
+
* const publicKey = 'edpkvS5QFv7KRGfa3b87gg9DBpxSm3NpSwnjhUjNBQrRUUR66F7C9g'
|
|
2904
|
+
* const validation = validatePublicKey(publicKey)
|
|
2905
|
+
* console.log(validation)
|
|
2906
|
+
* // This example return 3 which correspond to VALID
|
|
2907
|
+
* ```
|
|
1235
2908
|
*/
|
|
1236
|
-
function
|
|
1237
|
-
return
|
|
1238
|
-
.map((x) => ((x >> 4) & 0xf).toString(16) + (x & 0xf).toString(16))
|
|
1239
|
-
.join('');
|
|
2909
|
+
function validatePublicKey(value) {
|
|
2910
|
+
return validatePrefixedValue(value, publicKeyPrefixes);
|
|
1240
2911
|
}
|
|
1241
2912
|
/**
|
|
1242
|
-
* @description
|
|
1243
|
-
*
|
|
2913
|
+
* @description Used to check if an operation hash is valid.
|
|
2914
|
+
*
|
|
2915
|
+
* @returns
|
|
2916
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2917
|
+
*
|
|
2918
|
+
* @example
|
|
2919
|
+
* ```
|
|
2920
|
+
* import { validateOperation } from '@taquito/utils';
|
|
2921
|
+
* const operationHash = 'oo6JPEAy8VuMRGaFuMmLNFFGdJgiaKfnmT1CpHJfKP3Ye5ZahiP'
|
|
2922
|
+
* const validation = validateOperation(operationHash)
|
|
2923
|
+
* console.log(validation)
|
|
2924
|
+
* // This example return 3 which correspond to VALID
|
|
2925
|
+
* ```
|
|
1244
2926
|
*/
|
|
1245
|
-
function
|
|
1246
|
-
return
|
|
2927
|
+
function validateOperation(value) {
|
|
2928
|
+
return validatePrefixedValue(value, [PrefixV2.OperationHash]);
|
|
1247
2929
|
}
|
|
1248
2930
|
/**
|
|
1249
|
-
* @description
|
|
1250
|
-
*
|
|
2931
|
+
* @description Used to check if a protocol hash is valid.
|
|
2932
|
+
*
|
|
2933
|
+
* @returns
|
|
2934
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2935
|
+
*
|
|
2936
|
+
* @example
|
|
2937
|
+
* ```
|
|
2938
|
+
* import { validateProtocol } from '@taquito/utils';
|
|
2939
|
+
* const protocolHash = 'PtHangz2aRngywmSRGGvrcTyMbbdpWdpFKuS4uMWxg2RaH9i1qx'
|
|
2940
|
+
* const validation = validateProtocol(protocolHash)
|
|
2941
|
+
* console.log(validation)
|
|
2942
|
+
* // This example return 3 which correspond to VALID
|
|
2943
|
+
* ```
|
|
1251
2944
|
*/
|
|
1252
|
-
function
|
|
1253
|
-
return
|
|
2945
|
+
function validateProtocol(value) {
|
|
2946
|
+
return validatePrefixedValue(value, [PrefixV2.ProtocolHash]);
|
|
1254
2947
|
}
|
|
1255
2948
|
/**
|
|
1256
|
-
* @description
|
|
1257
|
-
*
|
|
2949
|
+
* @description Used to check if a block hash is valid.
|
|
2950
|
+
*
|
|
2951
|
+
* @returns
|
|
2952
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2953
|
+
*
|
|
2954
|
+
* @example
|
|
2955
|
+
* ```
|
|
2956
|
+
* import { validateBlock } from '@taquito/utils';
|
|
2957
|
+
* const blockHash = 'PtHangz2aRngywmSRGGvrcTyMbbdpWdpFKuS4uMWxg2RaH9i1qx'
|
|
2958
|
+
* const validation = validateBlock(blockHash)
|
|
2959
|
+
* console.log(validation)
|
|
2960
|
+
* // This example return 3 which correspond to VALID
|
|
2961
|
+
* ```
|
|
1258
2962
|
*/
|
|
1259
|
-
function
|
|
1260
|
-
|
|
1261
|
-
if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
|
|
1262
|
-
throw new InvalidHexStringError(hex, `Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
|
|
1263
|
-
}
|
|
1264
|
-
return Buffer.from(hexDigits, 'hex');
|
|
2963
|
+
function validateBlock(value) {
|
|
2964
|
+
return validatePrefixedValue(value, [PrefixV2.BlockHash]);
|
|
1265
2965
|
}
|
|
1266
2966
|
/**
|
|
1267
|
-
* @description
|
|
1268
|
-
* @
|
|
2967
|
+
* @description Used to check if a spending key is valid.
|
|
2968
|
+
* @returns
|
|
2969
|
+
* 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
|
|
2970
|
+
*
|
|
1269
2971
|
*/
|
|
1270
|
-
function
|
|
1271
|
-
return
|
|
2972
|
+
function validateSpendingKey(value) {
|
|
2973
|
+
return validatePrefixedValue(value, [PrefixV2.SaplingSpendingKey]);
|
|
1272
2974
|
}
|
|
1273
|
-
function
|
|
1274
|
-
return
|
|
2975
|
+
function validateSmartRollupAddress(value) {
|
|
2976
|
+
return validatePrefixedValue(value, [PrefixV2.SmartRollupHash]);
|
|
1275
2977
|
}
|
|
2978
|
+
|
|
2979
|
+
// IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
|
|
2980
|
+
const VERSION = {
|
|
2981
|
+
"commitHash": "7912b77f57f943dff619383900bd46a7a593a244",
|
|
2982
|
+
"version": "24.0.0-beta.0"
|
|
2983
|
+
};
|
|
2984
|
+
|
|
2985
|
+
const BLS12_381_DST = 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_';
|
|
2986
|
+
const POP_DST = 'BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_';
|
|
1276
2987
|
/**
|
|
1277
|
-
* @description
|
|
1278
|
-
* @param
|
|
1279
|
-
* @param
|
|
2988
|
+
* @description Verify signature of a payload
|
|
2989
|
+
* @param message The forged message including the magic byte (11 for block, 12 for preattestation, 13 for attestation, 3 for generic, 5 for the PACK format of michelson) in string or Uint8Array
|
|
2990
|
+
* @param publicKey The public key to verify the signature against
|
|
2991
|
+
* @param signature The signature to verify
|
|
2992
|
+
* @param watermark Optional if not included in the message
|
|
2993
|
+
* @param pop Optional if verifying proof of possession signature
|
|
2994
|
+
* @returns A boolean indicating if the signature matches
|
|
2995
|
+
* @throws {@link InvalidPublicKeyError} | {@link InvalidSignatureError} | {@link InvalidMessageError}
|
|
2996
|
+
* @example
|
|
2997
|
+
* ```
|
|
2998
|
+
* const message = '03d0c10e3ed11d7c6e3357f6ef335bab9e8f2bd54d0ce20c482e241191a6e4b8ce6c01be917311d9ac46959750e405d57e268e2ed9e174a80794fbd504e12a4a000141eb3781afed2f69679ff2bbe1c5375950b0e40d00ff000000005e05050505050507070100000024747a32526773486e74516b72794670707352466261313652546656503539684b72654a4d07070100000024747a315a6672455263414c42776d4171776f6e525859565142445439426a4e6a42484a750001';
|
|
2999
|
+
* const pk = 'sppk7c7hkPj47yjYFEHX85q46sFJGw6RBrqoVSHwAJAT4e14KJwzoey';
|
|
3000
|
+
* const sig = 'spsig1cdLkp1RLgUHAp13aRFkZ6MQDPp7xCnjAExGL3MBSdMDmT6JgQSX8cufyDgJRM3sinFtiCzLbsyP6d365EHoNevxhT47nx'
|
|
3001
|
+
*
|
|
3002
|
+
* const response = verifySignature(message, pk, sig);
|
|
3003
|
+
* ```
|
|
1280
3004
|
*
|
|
1281
3005
|
*/
|
|
1282
|
-
function
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
3006
|
+
function verifySignature(message, publicKey, signature, watermark, pop) {
|
|
3007
|
+
const [pk, pre] = (() => {
|
|
3008
|
+
try {
|
|
3009
|
+
return b58DecodeAndCheckPrefix(publicKey, publicKeyPrefixes);
|
|
3010
|
+
}
|
|
3011
|
+
catch (err) {
|
|
3012
|
+
if (err instanceof ParameterValidationError) {
|
|
3013
|
+
throw new InvalidPublicKeyError(publicKey, err.result);
|
|
3014
|
+
}
|
|
3015
|
+
else {
|
|
3016
|
+
throw err;
|
|
3017
|
+
}
|
|
3018
|
+
}
|
|
3019
|
+
})();
|
|
3020
|
+
const sig = (() => {
|
|
3021
|
+
try {
|
|
3022
|
+
const [sig] = b58DecodeAndCheckPrefix(signature, signaturePrefixes);
|
|
3023
|
+
return sig;
|
|
3024
|
+
}
|
|
3025
|
+
catch (err) {
|
|
3026
|
+
if (err instanceof ParameterValidationError) {
|
|
3027
|
+
throw new InvalidSignatureError(signature, err.result);
|
|
3028
|
+
}
|
|
3029
|
+
else {
|
|
3030
|
+
throw err;
|
|
3031
|
+
}
|
|
3032
|
+
}
|
|
3033
|
+
})();
|
|
3034
|
+
let msg;
|
|
3035
|
+
if (typeof message === 'string') {
|
|
3036
|
+
msg = hex2buf(message);
|
|
1291
3037
|
}
|
|
1292
3038
|
else {
|
|
1293
|
-
|
|
1294
|
-
.pow(bitLength)
|
|
1295
|
-
.minus(new BigNumber(val).abs());
|
|
1296
|
-
return twosCompliment.toString(16);
|
|
3039
|
+
msg = message;
|
|
1297
3040
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
const padString = '0';
|
|
1301
|
-
if (hex.length >= targetLength) {
|
|
1302
|
-
return hex;
|
|
3041
|
+
if (msg.length === 0) {
|
|
3042
|
+
throw new InvalidMessageError(buf2hex(msg), `can't be empty`);
|
|
1303
3043
|
}
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
return padString.repeat(padLength) + hex;
|
|
3044
|
+
if (typeof watermark !== 'undefined') {
|
|
3045
|
+
msg = mergebuf(watermark, msg);
|
|
1307
3046
|
}
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
*
|
|
1311
|
-
* @description Strips the first 2 characters of a hex string (0x)
|
|
1312
|
-
*
|
|
1313
|
-
* @param hex string to strip prefix from
|
|
1314
|
-
*/
|
|
1315
|
-
function stripHexPrefix(hex) {
|
|
1316
|
-
return hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
1317
|
-
}
|
|
1318
|
-
function splitAddress(addr) {
|
|
1319
|
-
const i = addr.indexOf('%');
|
|
1320
|
-
if (i >= 0) {
|
|
1321
|
-
return [addr.slice(0, i), addr.slice(i)];
|
|
3047
|
+
if (pop) {
|
|
3048
|
+
return verifyBLSPopSignature(sig, msg, pk);
|
|
1322
3049
|
}
|
|
1323
3050
|
else {
|
|
1324
|
-
|
|
3051
|
+
switch (pre) {
|
|
3052
|
+
case PrefixV2.P256PublicKey:
|
|
3053
|
+
return verifyP2Signature(sig, msg, pk);
|
|
3054
|
+
case PrefixV2.Secp256k1PublicKey:
|
|
3055
|
+
return verifySpSignature(sig, msg, pk);
|
|
3056
|
+
case PrefixV2.Ed25519PublicKey:
|
|
3057
|
+
return verifyEdSignature(sig, msg, pk);
|
|
3058
|
+
default:
|
|
3059
|
+
return verifyBLSSignature(sig, msg, pk);
|
|
3060
|
+
}
|
|
1325
3061
|
}
|
|
1326
3062
|
}
|
|
1327
|
-
function
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
3063
|
+
function verifyEdSignature(sig, msg, publicKey) {
|
|
3064
|
+
const hash$1 = hash(msg, 32);
|
|
3065
|
+
try {
|
|
3066
|
+
return verify(publicKey, hash$1, sig);
|
|
3067
|
+
}
|
|
3068
|
+
catch (_a) {
|
|
3069
|
+
return false;
|
|
3070
|
+
}
|
|
1334
3071
|
}
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
3072
|
+
function verifySpSignature(sig, msg, publicKey) {
|
|
3073
|
+
const hash$1 = hash(msg, 32);
|
|
3074
|
+
try {
|
|
3075
|
+
return secp256k1.verify(sig, hash$1, publicKey);
|
|
3076
|
+
}
|
|
3077
|
+
catch (_a) {
|
|
3078
|
+
return false;
|
|
3079
|
+
}
|
|
1343
3080
|
}
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
const n = new Uint8Array(prefix.length + payloadAr.length);
|
|
1353
|
-
n.set(prefix);
|
|
1354
|
-
n.set(payloadAr, prefix.length);
|
|
1355
|
-
return bs58check.encode(toBuffer(n));
|
|
3081
|
+
function verifyP2Signature(sig, msg, publicKey) {
|
|
3082
|
+
const hash$1 = hash(msg, 32);
|
|
3083
|
+
try {
|
|
3084
|
+
return p256.verify(sig, hash$1, publicKey);
|
|
3085
|
+
}
|
|
3086
|
+
catch (_a) {
|
|
3087
|
+
return false;
|
|
3088
|
+
}
|
|
1356
3089
|
}
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
function b58decode(payload) {
|
|
1363
|
-
const buf = bs58check.decode(payload);
|
|
1364
|
-
const prefixMap = {
|
|
1365
|
-
[prefix.tz1.toString()]: '0000',
|
|
1366
|
-
[prefix.tz2.toString()]: '0001',
|
|
1367
|
-
[prefix.tz3.toString()]: '0002',
|
|
1368
|
-
[prefix.tz4.toString()]: '0003',
|
|
1369
|
-
};
|
|
1370
|
-
const pref = prefixMap[new Uint8Array(buf.slice(0, 3)).toString()];
|
|
1371
|
-
if (pref) {
|
|
1372
|
-
// tz addresses
|
|
1373
|
-
const hex = buf2hex(buf.slice(3));
|
|
1374
|
-
return pref + hex;
|
|
3090
|
+
const bls = bls12_381.longSignatures; // AKA MinPK
|
|
3091
|
+
function verifyBLSSignature(sig, msg, publicKey) {
|
|
3092
|
+
try {
|
|
3093
|
+
const point = bls.hash(msg, BLS12_381_DST);
|
|
3094
|
+
return bls.verify(sig, point, publicKey);
|
|
1375
3095
|
}
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
return '01' + buf2hex(buf.slice(3, 42)) + '00';
|
|
3096
|
+
catch (_a) {
|
|
3097
|
+
return false;
|
|
1379
3098
|
}
|
|
1380
3099
|
}
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
// tz4 address currently
|
|
1390
|
-
return buf2hex(buf.slice(3, 42));
|
|
3100
|
+
function verifyBLSPopSignature(sig, msg, publicKey) {
|
|
3101
|
+
try {
|
|
3102
|
+
const point = bls.hash(msg, POP_DST);
|
|
3103
|
+
return bls.verify(sig, point, publicKey);
|
|
3104
|
+
}
|
|
3105
|
+
catch (_a) {
|
|
3106
|
+
return false;
|
|
3107
|
+
}
|
|
1391
3108
|
}
|
|
3109
|
+
|
|
1392
3110
|
/**
|
|
1393
|
-
*
|
|
1394
|
-
*
|
|
1395
|
-
* @param value Address to base58 encode (tz1, tz2, tz3 or KT1)
|
|
3111
|
+
* @category Error
|
|
3112
|
+
* @description Error that indicates invalid protocol hash being passed or used
|
|
1396
3113
|
*/
|
|
1397
|
-
|
|
1398
|
-
|
|
3114
|
+
class InvalidProtocolHashError extends ParameterValidationError {
|
|
3115
|
+
constructor(protocolHash, errorDetails) {
|
|
3116
|
+
super(`The protocol hash '${protocolHash}' is invalid`, errorDetails);
|
|
3117
|
+
this.protocolHash = protocolHash;
|
|
3118
|
+
this.name = 'InvalidProtocolHashError';
|
|
3119
|
+
this.name = this.constructor.name;
|
|
3120
|
+
}
|
|
1399
3121
|
}
|
|
1400
3122
|
/**
|
|
1401
|
-
*
|
|
1402
|
-
*
|
|
1403
|
-
* @param value Address to base58 encode (tz4) hex dec
|
|
1404
|
-
* @returns return address
|
|
3123
|
+
* @category Error
|
|
3124
|
+
* @description Error that indicates unable to convert data type from one to another
|
|
1405
3125
|
*/
|
|
1406
|
-
|
|
1407
|
-
|
|
3126
|
+
class ValueConversionError extends UnsupportedActionError {
|
|
3127
|
+
constructor(value, desiredType) {
|
|
3128
|
+
super(`Unable to convert ${value} to a ${desiredType}`);
|
|
3129
|
+
this.value = value;
|
|
3130
|
+
this.desiredType = desiredType;
|
|
3131
|
+
this.name = this.constructor.name;
|
|
3132
|
+
}
|
|
1408
3133
|
}
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
3134
|
+
|
|
3135
|
+
const TZ_DECIMALS = 6;
|
|
3136
|
+
const MTZ_DECIMALS = 3;
|
|
3137
|
+
function getDecimal(format) {
|
|
3138
|
+
switch (format) {
|
|
3139
|
+
case 'tz':
|
|
3140
|
+
return TZ_DECIMALS;
|
|
3141
|
+
case 'mtz':
|
|
3142
|
+
return MTZ_DECIMALS;
|
|
3143
|
+
case 'mutez':
|
|
3144
|
+
default:
|
|
3145
|
+
return 0;
|
|
3146
|
+
}
|
|
1416
3147
|
}
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
3148
|
+
function format(from = 'mutez', to = 'mutez', amount) {
|
|
3149
|
+
const bigNum = new BigNumber(amount);
|
|
3150
|
+
if (bigNum.isNaN()) {
|
|
3151
|
+
return amount;
|
|
3152
|
+
}
|
|
3153
|
+
return bigNum
|
|
3154
|
+
.multipliedBy(Math.pow(10, getDecimal(from)))
|
|
3155
|
+
.dividedBy(Math.pow(10, getDecimal(to)));
|
|
1424
3156
|
}
|
|
1425
3157
|
|
|
1426
|
-
export { BLS12_381_DST, InvalidProtocolHashError, POP_DST,
|
|
3158
|
+
export { BLS12_381_DST, InvalidProtocolHashError, POP_DST, PrefixV2, VERSION, ValueConversionError, addressPrefixes, b58DecodeAddress, b58DecodeAndCheckPrefix, b58DecodeBlsAddress, b58DecodePublicKey, b58DecodePublicKeyHash, b58Encode, buf2hex, bytesToString, compareArrays, encodeAddress, encodeBlsAddress, encodeExpr, encodeKey, encodeKeyHash, encodeOpHash, format, getPkhfromPk, hex2Bytes, hex2buf, isValidPrefixedValue, mergebuf, mic2arr, num2PaddedHex, numToHexBuffer, payloadLength, publicKeyHashPrefixes, publicKeyPrefixes, signaturePrefixes, splitAddress, stringToBytes, stripHexPrefix, toHexBuf, validateAddress, validateBlock, validateChain, validateContractAddress, validateKeyHash, validateOperation, validateProtocol, validatePublicKey, validateSignature, validateSmartRollupAddress, validateSpendingKey, verifySignature };
|
|
1427
3159
|
//# sourceMappingURL=taquito-utils.es6.js.map
|