@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.
@@ -18,719 +18,25 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
18
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.signaturePrefixes = exports.publicKeyHashPrefixes = exports.publicKeyPrefixes = exports.addressPrefixes = exports.format = exports.POP_DST = exports.BLS12_381_DST = exports.verifySignature = exports.validatePkAndExtractPrefix = exports.payloadLength = exports.PrefixV2 = exports.prefixLength = exports.Prefix = exports.prefix = exports.VERSION = void 0;
22
- exports.b58DecodeAndCheckPrefix = b58DecodeAndCheckPrefix;
23
- exports.b58DecodePublicKey = b58DecodePublicKey;
24
- exports.b58DecodePublicKeyHash = b58DecodePublicKeyHash;
25
- exports.b58DecodeBlsAddress = b58DecodeBlsAddress;
26
- exports.b58DecodeAddress = b58DecodeAddress;
27
- exports.getPkhfromPk = getPkhfromPk;
28
- exports.b58Encode = b58Encode;
29
- exports.encodeKey = encodeKey;
30
- exports.encodeKeyHash = encodeKeyHash;
31
- exports.encodeAddress = encodeAddress;
32
- exports.encodeBlsAddress = encodeBlsAddress;
33
- exports.encodeExpr = encodeExpr;
34
- exports.encodeOpHash = encodeOpHash;
35
- exports.hex2buf = hex2buf;
36
- exports.mergebuf = mergebuf;
37
- exports.mic2arr = mic2arr;
38
- exports.buf2hex = buf2hex;
39
- exports.stringToBytes = stringToBytes;
40
- exports.bytesToString = bytesToString;
41
- exports.hex2Bytes = hex2Bytes;
42
- exports.toHexBuf = toHexBuf;
43
- exports.numToHexBuffer = numToHexBuffer;
44
- exports.num2PaddedHex = num2PaddedHex;
45
- exports.stripHexPrefix = stripHexPrefix;
46
- exports.splitAddress = splitAddress;
47
- exports.compareArrays = compareArrays;
48
- exports.b58cdecode = b58cdecode;
49
- exports.b58cencode = b58cencode;
50
- exports.b58decode = b58decode;
51
- exports.b58decodeL2Address = b58decodeL2Address;
52
- exports.encodePubKey = encodePubKey;
53
- exports.encodeL2Address = encodeL2Address;
54
- exports.char2Bytes = char2Bytes;
55
- exports.bytes2Char = bytes2Char;
56
- /*
57
- * Some code in this file is originally from sotez and eztz
58
- * Copyright (c) 2018 Andrew Kishino
59
- * Copyright (c) 2017 Stephen Andrews
60
- */
61
- const buffer_1 = require("buffer");
62
- const constants_1 = require("./constants");
63
- const constants_2 = require("./constants"); // (deprecated will be removed in the next minor release)
64
- const blake2b_1 = require("@stablelib/blake2b");
65
- const bs58check_1 = require("bs58check");
66
- const bignumber_js_1 = require("bignumber.js");
67
- const typedarray_to_buffer_1 = require("typedarray-to-buffer");
68
- const core_1 = require("@taquito/core");
69
- const validators_1 = require("./validators");
21
+ exports.format = exports.POP_DST = exports.BLS12_381_DST = exports.verifySignature = exports.payloadLength = exports.PrefixV2 = exports.VERSION = void 0;
22
+ // Re-export everything from encoding
23
+ __exportStar(require("./encoding"), exports);
24
+ // Re-export everything from validators
70
25
  __exportStar(require("./validators"), exports);
26
+ // Re-export version
71
27
  var version_1 = require("./version");
72
28
  Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return version_1.VERSION; } });
73
- var constants_3 = require("./constants"); // (deprecated will be removed in the next minor release)
74
- Object.defineProperty(exports, "prefix", { enumerable: true, get: function () { return constants_3.prefix; } });
75
- Object.defineProperty(exports, "Prefix", { enumerable: true, get: function () { return constants_3.Prefix; } });
76
- Object.defineProperty(exports, "prefixLength", { enumerable: true, get: function () { return constants_3.prefixLength; } });
77
- var constants_4 = require("./constants");
78
- Object.defineProperty(exports, "PrefixV2", { enumerable: true, get: function () { return constants_4.PrefixV2; } });
79
- Object.defineProperty(exports, "payloadLength", { enumerable: true, get: function () { return constants_4.payloadLength; } });
80
- var verify_signature_1 = require("./verify-signature"); // (deprecated will be removed in the next minor release)
81
- Object.defineProperty(exports, "validatePkAndExtractPrefix", { enumerable: true, get: function () { return verify_signature_1.validatePkAndExtractPrefix; } });
82
- var verify_signature_2 = require("./verify-signature");
83
- Object.defineProperty(exports, "verifySignature", { enumerable: true, get: function () { return verify_signature_2.verifySignature; } });
84
- Object.defineProperty(exports, "BLS12_381_DST", { enumerable: true, get: function () { return verify_signature_2.BLS12_381_DST; } });
85
- Object.defineProperty(exports, "POP_DST", { enumerable: true, get: function () { return verify_signature_2.POP_DST; } });
29
+ // Re-export constants
30
+ var constants_1 = require("./constants");
31
+ Object.defineProperty(exports, "PrefixV2", { enumerable: true, get: function () { return constants_1.PrefixV2; } });
32
+ Object.defineProperty(exports, "payloadLength", { enumerable: true, get: function () { return constants_1.payloadLength; } });
33
+ // Re-export from verify-signature
34
+ var verify_signature_1 = require("./verify-signature");
35
+ Object.defineProperty(exports, "verifySignature", { enumerable: true, get: function () { return verify_signature_1.verifySignature; } });
36
+ Object.defineProperty(exports, "BLS12_381_DST", { enumerable: true, get: function () { return verify_signature_1.BLS12_381_DST; } });
37
+ Object.defineProperty(exports, "POP_DST", { enumerable: true, get: function () { return verify_signature_1.POP_DST; } });
38
+ // Re-export errors
86
39
  __exportStar(require("./errors"), exports);
40
+ // Re-export format
87
41
  var format_1 = require("./format");
88
42
  Object.defineProperty(exports, "format", { enumerable: true, get: function () { return format_1.format; } });
89
- /**
90
- * @description list of prefixes that can be used to decode an address
91
- */
92
- exports.addressPrefixes = [
93
- constants_1.PrefixV2.P256PublicKeyHash,
94
- constants_1.PrefixV2.Secp256k1PublicKeyHash,
95
- constants_1.PrefixV2.Ed25519PublicKeyHash,
96
- constants_1.PrefixV2.BLS12_381PublicKeyHash,
97
- constants_1.PrefixV2.ContractHash,
98
- constants_1.PrefixV2.SmartRollupHash,
99
- // PrefixV2.ZkRollupHash,
100
- ];
101
- /**
102
- * @description list of prefixes that can be used to decode a public key
103
- */
104
- exports.publicKeyPrefixes = [
105
- constants_1.PrefixV2.P256PublicKey,
106
- constants_1.PrefixV2.Secp256k1PublicKey,
107
- constants_1.PrefixV2.Ed25519PublicKey,
108
- constants_1.PrefixV2.BLS12_381PublicKey,
109
- ];
110
- /**
111
- * @description list of prefixes that can be used to decode a public key hash
112
- */
113
- exports.publicKeyHashPrefixes = [
114
- constants_1.PrefixV2.P256PublicKeyHash,
115
- constants_1.PrefixV2.Secp256k1PublicKeyHash,
116
- constants_1.PrefixV2.Ed25519PublicKeyHash,
117
- constants_1.PrefixV2.BLS12_381PublicKeyHash,
118
- ];
119
- /**
120
- * @description list of prefixes that can be used to decode a signature
121
- */
122
- exports.signaturePrefixes = [
123
- constants_1.PrefixV2.P256Signature,
124
- constants_1.PrefixV2.Secp256k1Signature,
125
- constants_1.PrefixV2.Ed25519Signature,
126
- constants_1.PrefixV2.BLS12_381Signature,
127
- constants_1.PrefixV2.GenericSignature,
128
- ];
129
- function b58DecodeAndCheckPrefix(src, allowed, payloadOnly) {
130
- const buf = (() => {
131
- try {
132
- return bs58check_1.default.decode(src);
133
- }
134
- catch (err) {
135
- if (err instanceof Error) {
136
- if (err.message.includes('checksum')) {
137
- throw new core_1.ParameterValidationError(validators_1.ValidationResult.INVALID_CHECKSUM);
138
- }
139
- else {
140
- throw new core_1.ParameterValidationError(validators_1.ValidationResult.INVALID_ENCODING);
141
- }
142
- }
143
- else {
144
- throw err;
145
- }
146
- }
147
- })();
148
- let key;
149
- for (key in constants_1.PrefixV2) {
150
- const p = constants_1.PrefixV2[key];
151
- const pre = constants_1.prefixV2[p];
152
- if (buf.length === pre.length + constants_1.payloadLength[p] &&
153
- buf.slice(0, pre.length).every((v, i) => v == pre[i])) {
154
- if (allowed !== undefined && allowed.indexOf(p) < 0) {
155
- throw new core_1.ParameterValidationError(validators_1.ValidationResult.PREFIX_NOT_ALLOWED);
156
- }
157
- if (payloadOnly) {
158
- return buf.slice(pre.length);
159
- }
160
- else {
161
- return [buf.slice(pre.length), p];
162
- }
163
- }
164
- }
165
- throw new core_1.ParameterValidationError(validators_1.ValidationResult.NO_PREFIX_MATCHED);
166
- }
167
- function b58DecodePublicKey(value, fmt) {
168
- const [data, pre] = b58DecodeAndCheckPrefix(value, exports.publicKeyPrefixes);
169
- let tag;
170
- switch (pre) {
171
- case constants_1.PrefixV2.Ed25519PublicKey:
172
- tag = 0;
173
- break;
174
- case constants_1.PrefixV2.Secp256k1PublicKey:
175
- tag = 1;
176
- break;
177
- case constants_1.PrefixV2.P256PublicKey:
178
- tag = 2;
179
- break;
180
- case constants_1.PrefixV2.BLS12_381PublicKey:
181
- tag = 3;
182
- break;
183
- default:
184
- throw new core_1.InvalidKeyError(validators_1.ValidationResult.NO_PREFIX_MATCHED);
185
- }
186
- const buf = new Uint8Array(data.length + 1);
187
- buf[0] = tag;
188
- buf.set(data, 1);
189
- if (fmt !== undefined && fmt === 'array') {
190
- return buf;
191
- }
192
- else {
193
- return buf2hex(buf);
194
- }
195
- }
196
- function b58DecodePublicKeyHash(value, fmt) {
197
- const [data, pre] = b58DecodeAndCheckPrefix(value, exports.publicKeyHashPrefixes);
198
- const buf = new Uint8Array(21);
199
- let tag;
200
- switch (pre) {
201
- case constants_1.PrefixV2.Ed25519PublicKeyHash:
202
- tag = 0;
203
- break;
204
- case constants_1.PrefixV2.Secp256k1PublicKeyHash:
205
- tag = 1;
206
- break;
207
- case constants_1.PrefixV2.P256PublicKeyHash:
208
- tag = 2;
209
- break;
210
- case constants_1.PrefixV2.BLS12_381PublicKeyHash:
211
- tag = 3;
212
- break;
213
- default:
214
- throw new core_1.InvalidAddressError(value, validators_1.ValidationResult.NO_PREFIX_MATCHED);
215
- }
216
- buf[0] = tag;
217
- buf.set(data, 1);
218
- if (fmt !== undefined && fmt === 'array') {
219
- return buf;
220
- }
221
- else {
222
- return buf2hex(buf);
223
- }
224
- }
225
- function b58DecodeBlsAddress(value, fmt) {
226
- const [buf, pre] = b58DecodeAndCheckPrefix(value);
227
- if (pre !== constants_1.PrefixV2.BLS12_381PublicKeyHash) {
228
- throw new core_1.InvalidKeyError(validators_1.ValidationResult.NO_PREFIX_MATCHED);
229
- }
230
- if (fmt !== undefined && fmt === 'array') {
231
- return buf;
232
- }
233
- else {
234
- return buf2hex(buf);
235
- }
236
- }
237
- function b58DecodeAddress(value, fmt) {
238
- const i = value.indexOf('%');
239
- if (i >= 0) {
240
- value = value.slice(0, i);
241
- }
242
- const [data, pre] = b58DecodeAndCheckPrefix(value, exports.addressPrefixes);
243
- const buf = new Uint8Array(22);
244
- if (pre === constants_1.PrefixV2.ContractHash ||
245
- pre === constants_1.PrefixV2.SmartRollupHash ||
246
- pre === constants_1.PrefixV2.ZkRollupHash) {
247
- let tag;
248
- switch (pre) {
249
- case constants_1.PrefixV2.ContractHash:
250
- tag = 1;
251
- break;
252
- case constants_1.PrefixV2.SmartRollupHash:
253
- tag = 3;
254
- break;
255
- case constants_1.PrefixV2.ZkRollupHash:
256
- tag = 4;
257
- break;
258
- }
259
- buf[0] = tag;
260
- buf.set(data, 1);
261
- }
262
- else {
263
- let tag;
264
- switch (pre) {
265
- case constants_1.PrefixV2.Ed25519PublicKeyHash:
266
- tag = 0;
267
- break;
268
- case constants_1.PrefixV2.Secp256k1PublicKeyHash:
269
- tag = 1;
270
- break;
271
- case constants_1.PrefixV2.P256PublicKeyHash:
272
- tag = 2;
273
- break;
274
- case constants_1.PrefixV2.BLS12_381PublicKeyHash:
275
- tag = 3;
276
- break;
277
- default:
278
- throw new core_1.InvalidAddressError(value, validators_1.ValidationResult.NO_PREFIX_MATCHED);
279
- }
280
- buf[0] = 0;
281
- buf[1] = tag;
282
- buf.set(data, 2);
283
- }
284
- if (fmt !== undefined && fmt === 'array') {
285
- return buf;
286
- }
287
- else {
288
- return buf2hex(buf);
289
- }
290
- }
291
- /**
292
- * @description Gets Tezos address (PKH) from Public Key
293
- * @param publicKey Base58 Public Key
294
- * @returns A string of the Tezos address (PKH) that was derived from the given Public Key
295
- * @example getPkhfromPk('edpkuNjKKT48xBoT5asPrWdmuM1Yw8D93MwgFgVvtca8jb5pstzaCh') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe'
296
- */
297
- function getPkhfromPk(publicKey) {
298
- const [key, pre] = b58DecodeAndCheckPrefix(publicKey);
299
- let pkhPre;
300
- switch (pre) {
301
- case constants_1.PrefixV2.P256PublicKey:
302
- pkhPre = constants_1.PrefixV2.P256PublicKeyHash;
303
- break;
304
- case constants_1.PrefixV2.Secp256k1PublicKey:
305
- pkhPre = constants_1.PrefixV2.Secp256k1PublicKeyHash;
306
- break;
307
- case constants_1.PrefixV2.Ed25519PublicKey:
308
- pkhPre = constants_1.PrefixV2.Ed25519PublicKeyHash;
309
- break;
310
- case constants_1.PrefixV2.BLS12_381PublicKey:
311
- pkhPre = constants_1.PrefixV2.BLS12_381PublicKeyHash;
312
- break;
313
- default:
314
- throw new core_1.InvalidPublicKeyError(publicKey, validators_1.ValidationResult.NO_PREFIX_MATCHED);
315
- }
316
- const hashed = (0, blake2b_1.hash)(key, 20);
317
- return b58Encode(hashed, pkhPre);
318
- }
319
- /**
320
- * @description Add the prefix to a hex string or Uint8Array and Base58 encode it
321
- * @param value Value to Base58 encode
322
- * @param pre prefix ID to append to the encoded string
323
- * @example b58Encode('e96b9f8b19af9c7ffa0c0480e1977b295850961f', PrefixV2.Ed25519PublicKeyHash) // returns 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM'
324
- */
325
- function b58Encode(value, pre) {
326
- const data = typeof value === 'string' ? hex2buf(value) : value;
327
- const p = constants_1.prefixV2[pre];
328
- const n = new Uint8Array(p.length + data.length);
329
- n.set(p);
330
- n.set(data, p.length);
331
- return bs58check_1.default.encode((0, typedarray_to_buffer_1.default)(n));
332
- }
333
- /**
334
- * @description Parse binary public key and return Base58 representation
335
- * @param value Binary key data
336
- * @returns return prefixed public key
337
- * @example encodeKey('02033aba7da4a2e7b5dd9f074555c118829aff16213ea1b65859686bd5fcfeaf3616') // return 'p2pk66xmhjiN7LpfrDGFwpxPtJxkLtPjQ6HUxJbKmRbxSR7RMpamDwi'
338
- */
339
- function encodeKey(value) {
340
- let buf;
341
- if (typeof value === 'string') {
342
- buf = hex2buf(value);
343
- }
344
- else {
345
- buf = value;
346
- }
347
- let pre;
348
- switch (buf[0]) {
349
- case 0:
350
- pre = constants_1.PrefixV2.Ed25519PublicKey;
351
- break;
352
- case 1:
353
- pre = constants_1.PrefixV2.Secp256k1PublicKey;
354
- break;
355
- case 2:
356
- pre = constants_1.PrefixV2.P256PublicKey;
357
- break;
358
- case 3:
359
- pre = constants_1.PrefixV2.BLS12_381PublicKey;
360
- break;
361
- default:
362
- throw new Error('invalid address format');
363
- }
364
- return b58Encode(buf.slice(1), pre);
365
- }
366
- /**
367
- * @description Parse binary public key hash and return Base58 representation
368
- * @param value Key hash to parse
369
- * @returns return prefixed public key hash
370
- * @example encodeKeyHash('0001907d6a7e9f084df840d6e67ffa8db5464f87d4d1') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe'
371
- */
372
- function encodeKeyHash(value) {
373
- let buf;
374
- if (typeof value === 'string') {
375
- buf = hex2buf(value);
376
- }
377
- else {
378
- buf = value;
379
- }
380
- let pre;
381
- switch (buf[0]) {
382
- case 0:
383
- pre = constants_1.PrefixV2.Ed25519PublicKeyHash;
384
- break;
385
- case 1:
386
- pre = constants_1.PrefixV2.Secp256k1PublicKeyHash;
387
- break;
388
- case 2:
389
- pre = constants_1.PrefixV2.P256PublicKeyHash;
390
- break;
391
- case 3:
392
- pre = constants_1.PrefixV2.BLS12_381PublicKeyHash;
393
- break;
394
- default:
395
- throw new Error('invalid address format');
396
- }
397
- return b58Encode(buf.slice(1, 21), pre);
398
- }
399
- /**
400
- * @description Parse binary Contract ID and return Base58 representation
401
- * @param value Address to parse (tz1, tz2, tz3, KT1, or sr1).
402
- * @example encodeAddress('0000e96b9f8b19af9c7ffa0c0480e1977b295850961f') // return 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM'
403
- */
404
- function encodeAddress(value) {
405
- let buf;
406
- if (typeof value === 'string') {
407
- buf = hex2buf(value);
408
- }
409
- else {
410
- buf = value;
411
- }
412
- switch (buf[0]) {
413
- case 0: // implicit
414
- return encodeKeyHash(buf.slice(1));
415
- case 1: // contract hash
416
- return b58Encode(buf.slice(1, 21), constants_1.PrefixV2.ContractHash);
417
- case 3: // smart rollup hash
418
- return b58Encode(buf.slice(1, 21), constants_1.PrefixV2.SmartRollupHash);
419
- case 4: // zk rollup hash
420
- return b58Encode(buf.slice(1, 21), constants_1.PrefixV2.ZkRollupHash);
421
- default:
422
- throw new Error('invalid address format');
423
- }
424
- }
425
- /**
426
- * @description Base58 encode an address without predefined prefix
427
- * @param value Address to base58 encode (tz4) hex dec
428
- * @returns return address
429
- * @example encodeBlsAddress('af2dc3c40667abc0e89c0ef40171d22aed08d5eb') // return 'tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8'
430
- */
431
- function encodeBlsAddress(value) {
432
- return b58Encode(value, constants_1.PrefixV2.BLS12_381PublicKeyHash);
433
- }
434
- /**
435
- * @description convert a fragment of Michelson code in hex string to an 'expr' prefix + base58 encoded BLAKE2b hash string
436
- * @param value a fragment of Michelson code in hex string
437
- * @returns return 'expr' prefix + base58 encoded BLAKE2b hash
438
- * @example encodeExpr('050a000000160000b2e19a9e74440d86c59f13dab8a18ff873e889ea') // return 'exprv6UsC1sN3Fk2XfgcJCL8NCerP5rCGy1PRESZAqr7L2JdzX55EN'
439
- */
440
- function encodeExpr(value) {
441
- const blakeHash = (0, blake2b_1.hash)(hex2buf(value), 32);
442
- return b58Encode(blakeHash, constants_1.PrefixV2.ScriptExpr);
443
- }
444
- /**
445
- * @description convert a signed operation in hex string to an 'op' prefix + base58 encoded BLAKE2b hash string
446
- * @param value signed operation in hex string
447
- * @returns return 'op' prefix + base58 encoded BLAKE2b hash
448
- * @example encodeOpHash('0f185d8a30061e8134c162dbb7a6c3ab8f5fdb153363ccd6149b49a33481156a6c00b2e19a9e74440d86c59f13dab8a18ff873e889eaa304ab05da13000001f1585a7384f36e45fb43dc37e8ce172bced3e05700ff0000000002002110c033f3a990c2e46a3d6054ecc2f74072aae7a34b5ac4d9ce9edc11c2410a97695682108951786f05b361da03b97245dc9897e1955e08b5b8d9e153b0bdeb0d') // return 'opapqvVXmebRTCFd2GQFydr4tJj3V5QocQuTmuhbatcHm4Seo2t'
449
- */
450
- function encodeOpHash(value) {
451
- const blakeHash = (0, blake2b_1.hash)(hex2buf(value), 32);
452
- return b58Encode(blakeHash, constants_1.PrefixV2.OperationHash);
453
- }
454
- /**
455
- * @description Convert an hex string to a Uint8Array
456
- * @param hex Hex string to convert
457
- * @throws {@link ValueConversionError}
458
- */
459
- function hex2buf(hex) {
460
- hex = hex.startsWith('0x') ? hex.slice(2) : hex;
461
- if (hex.length % 2 !== 0) {
462
- throw new core_1.InvalidHexStringError(hex, `Expecting even number of characters`);
463
- }
464
- if (!hex.match(/^([\da-f]{2})*$/gi)) {
465
- throw new core_1.InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
466
- }
467
- const res = new Uint8Array(hex.length / 2);
468
- let j = 0;
469
- for (let i = 0; i < hex.length; i += 2) {
470
- const ss = hex.slice(i, i + 2);
471
- const x = parseInt(ss, 16);
472
- if (Number.isNaN(x)) {
473
- throw new core_1.InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
474
- }
475
- res[j++] = x;
476
- }
477
- return res;
478
- }
479
- /**
480
- * @description Merge 2 buffers together
481
- * @param b1 First buffer
482
- * @param b2 Second buffer
483
- */
484
- function mergebuf(b1, b2) {
485
- const r = new Uint8Array(b1.length + b2.length);
486
- r.set(b1);
487
- r.set(b2, b1.length);
488
- return r;
489
- }
490
- /**
491
- * @description Flatten a michelson json representation to an array
492
- * @param s michelson json
493
- */
494
- function mic2arr(s) {
495
- let ret = [];
496
- if (Object.prototype.hasOwnProperty.call(s, 'prim')) {
497
- if (s.prim === 'Pair') {
498
- ret.push(mic2arr(s.args[0]));
499
- ret = ret.concat(mic2arr(s.args[1]));
500
- }
501
- else if (s.prim === 'Elt') {
502
- ret = {
503
- key: mic2arr(s.args[0]),
504
- val: mic2arr(s.args[1]),
505
- };
506
- }
507
- else if (s.prim === 'True') {
508
- ret = true;
509
- }
510
- else if (s.prim === 'False') {
511
- ret = false;
512
- }
513
- }
514
- else if (Array.isArray(s)) {
515
- const sc = s.length;
516
- for (let i = 0; i < sc; i++) {
517
- const n = mic2arr(s[i]);
518
- if (typeof n.key !== 'undefined') {
519
- if (Array.isArray(ret)) {
520
- ret = {
521
- keys: [],
522
- vals: [],
523
- };
524
- }
525
- ret.keys.push(n.key);
526
- ret.vals.push(n.val);
527
- }
528
- else {
529
- ret.push(n);
530
- }
531
- }
532
- }
533
- else if (Object.prototype.hasOwnProperty.call(s, 'string')) {
534
- ret = s.string;
535
- }
536
- else if (Object.prototype.hasOwnProperty.call(s, 'int')) {
537
- ret = parseInt(s.int, 10);
538
- }
539
- else {
540
- ret = s;
541
- }
542
- return ret;
543
- }
544
- /**
545
- * @description Convert a Uint8Array to an hex string
546
- * @param buffer Uint8Array to convert
547
- */
548
- function buf2hex(bytes) {
549
- return Array.from(bytes)
550
- .map((x) => ((x >> 4) & 0xf).toString(16) + (x & 0xf).toString(16))
551
- .join('');
552
- }
553
- /**
554
- * @description Convert a string to a byte string representation
555
- * @param str String to convert
556
- */
557
- function stringToBytes(str) {
558
- return buffer_1.Buffer.from(str, 'utf8').toString('hex');
559
- }
560
- /**
561
- * @description Convert byte string representation to string
562
- * @param hex byte string to convert
563
- */
564
- function bytesToString(hex) {
565
- return buffer_1.Buffer.from(hex2buf(hex)).toString('utf8');
566
- }
567
- /**
568
- * @description Convert hex string/UintArray/Buffer to bytes
569
- * @param hex String value to convert to bytes
570
- */
571
- function hex2Bytes(hex) {
572
- const hexDigits = stripHexPrefix(hex);
573
- if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
574
- throw new core_1.InvalidHexStringError(hex, `Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
575
- }
576
- return buffer_1.Buffer.from(hexDigits, 'hex');
577
- }
578
- /**
579
- * @description Converts a number or Bignumber to hexadecimal string
580
- * @param val The value that will be converted to a hexadecimal string value
581
- */
582
- function toHexBuf(val, bitLength = 8) {
583
- return buffer_1.Buffer.from(num2PaddedHex(val, bitLength), 'hex');
584
- }
585
- function numToHexBuffer(val, bitLength = 8) {
586
- return buffer_1.Buffer.from(num2PaddedHex(val, bitLength), 'hex');
587
- }
588
- /**
589
- * @description Converts a number or BigNumber to a padded hexadecimal string
590
- * @param val The value that will be converted into a padded hexadecimal string value
591
- * @param bitLength The length of bits
592
- *
593
- */
594
- function num2PaddedHex(val, bitLength = 8) {
595
- if (new bignumber_js_1.default(val).isPositive()) {
596
- const nibbleLength = Math.ceil(bitLength / 4);
597
- const hex = val.toString(16);
598
- // check whether nibble (4 bits) length is higher or lower than the current hex string length
599
- let targetLength = hex.length >= nibbleLength ? hex.length : nibbleLength;
600
- // make sure the hex string target length is even
601
- targetLength = targetLength % 2 == 0 ? targetLength : targetLength + 1;
602
- return padHexWithZero(hex, targetLength);
603
- }
604
- else {
605
- const twosCompliment = new bignumber_js_1.default(2)
606
- .pow(bitLength)
607
- .minus(new bignumber_js_1.default(val).abs());
608
- return twosCompliment.toString(16);
609
- }
610
- }
611
- function padHexWithZero(hex, targetLength) {
612
- const padString = '0';
613
- if (hex.length >= targetLength) {
614
- return hex;
615
- }
616
- else {
617
- const padLength = targetLength - hex.length;
618
- return padString.repeat(padLength) + hex;
619
- }
620
- }
621
- /**
622
- *
623
- * @description Strips the first 2 characters of a hex string (0x)
624
- *
625
- * @param hex string to strip prefix from
626
- */
627
- function stripHexPrefix(hex) {
628
- return hex.startsWith('0x') ? hex.slice(2) : hex;
629
- }
630
- function splitAddress(addr) {
631
- const i = addr.indexOf('%');
632
- if (i >= 0) {
633
- return [addr.slice(0, i), addr.slice(i)];
634
- }
635
- else {
636
- return [addr, null];
637
- }
638
- }
639
- function compareArrays(a, b) {
640
- let i = 0;
641
- while (i < a.length && i < b.length && a[i] === b[i])
642
- i++;
643
- const aa = i < a.length ? a[i] : 0;
644
- const bb = i < b.length ? b[i] : 0;
645
- return aa < bb ? -1 : aa > bb ? 1 : 0;
646
- }
647
- /**
648
- * @deprecated use b58DecodeAndCheckPrefix instead, this function will be removed in the next minor release
649
- * @description Base58 decode a string and remove the prefix from it
650
- * @param enc Value to base58 decode
651
- * @param prefixArg prefix to remove from the decoded string
652
- */
653
- function b58cdecode(enc, prefixArg) {
654
- return bs58check_1.default.decode(enc).slice(prefixArg.length);
655
- }
656
- /**
657
- * @deprecated use b58Encode instead, this function will be removed in the next minor release
658
- * @description Base58 encode a string or a Uint8Array and append a prefix to it
659
- * @param value Value to base58 encode
660
- * @param prefix prefix to append to the encoded string
661
- */
662
- function b58cencode(value, prefix) {
663
- const payloadAr = typeof value === 'string' ? hex2buf(value) : value;
664
- const n = new Uint8Array(prefix.length + payloadAr.length);
665
- n.set(prefix);
666
- n.set(payloadAr, prefix.length);
667
- return bs58check_1.default.encode((0, typedarray_to_buffer_1.default)(n));
668
- }
669
- /**
670
- * @deprecated use b58DecodePublicKey, b58DecodePublicKeyHash, b58DecodeBlsAddress, b58DecodeAddress instead, this function will be removed in the next minor release
671
- * @description Base58 decode a string with predefined prefix
672
- * @param payload Value to base58 decode
673
- */
674
- function b58decode(payload) {
675
- const buf = bs58check_1.default.decode(payload);
676
- const prefixMap = {
677
- [constants_2.prefix.tz1.toString()]: '0000',
678
- [constants_2.prefix.tz2.toString()]: '0001',
679
- [constants_2.prefix.tz3.toString()]: '0002',
680
- [constants_2.prefix.tz4.toString()]: '0003',
681
- };
682
- const pref = prefixMap[new Uint8Array(buf.slice(0, 3)).toString()];
683
- if (pref) {
684
- // tz addresses
685
- const hex = buf2hex(buf.slice(3));
686
- return pref + hex;
687
- }
688
- else {
689
- // other (kt addresses)
690
- return '01' + buf2hex(buf.slice(3, 42)) + '00';
691
- }
692
- }
693
- /**
694
- * @deprecated use b58DecodeBlsAddress instead, this function will be removed in the next minor release
695
- * @description b58 decode a string without predefined prefix
696
- * @param payload Value to base58 decode
697
- * @returns string of bytes
698
- */
699
- function b58decodeL2Address(payload) {
700
- const buf = bs58check_1.default.decode(payload);
701
- // tz4 address currently
702
- return buf2hex(buf.slice(3, 42));
703
- }
704
- /**
705
- * @deprecated use encodeAddress instead, this function will be removed in the next minor release
706
- * @description Base58 encode an address using predefined prefix
707
- * @param value Address to base58 encode (tz1, tz2, tz3 or KT1)
708
- */
709
- function encodePubKey(value) {
710
- return encodeAddress(value);
711
- }
712
- /**
713
- * @deprecated use encodeBlsAddress instead, this function will be removed in the next minor release
714
- * @description Base58 encode an address without predefined prefix
715
- * @param value Address to base58 encode (tz4) hex dec
716
- * @returns return address
717
- */
718
- function encodeL2Address(value) {
719
- return b58cencode(value, constants_2.prefix.tz4);
720
- }
721
- /**
722
- * @deprecated use stringToBytes instead, this function will be removed in the next minor release
723
- * @description Convert a string to bytes
724
- * @param str String to convert
725
- */
726
- function char2Bytes(str) {
727
- return buffer_1.Buffer.from(str, 'utf8').toString('hex');
728
- }
729
- /**
730
- * @deprecated use bytesToString instead, this function will be removed in the next minor release
731
- * @description Convert bytes to a string
732
- * @param hex Bytes to convert
733
- */
734
- function bytes2Char(hex) {
735
- return buffer_1.Buffer.from(hex2buf(hex)).toString('utf8');
736
- }