@taquito/utils 22.0.0-beta.0 → 23.0.0-RC.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,26 +18,41 @@ 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.getPkhfromPk = exports.buf2hex = exports.mic2arr = exports.mergebuf = exports.hex2buf = exports.b58cdecode = exports.format = exports.validatePkAndExtractPrefix = exports.verifySignature = exports.prefixLength = exports.Prefix = exports.prefix = exports.VERSION = void 0;
22
- exports.encodeExpr = encodeExpr;
23
- exports.encodeOpHash = encodeOpHash;
24
- exports.b58cencode = b58cencode;
25
- exports.b58decode = b58decode;
26
- exports.b58decodeL2Address = b58decodeL2Address;
27
- exports.encodePubKey = encodePubKey;
28
- exports.encodeAddress = encodeAddress;
29
- exports.encodeL2Address = encodeL2Address;
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;
30
29
  exports.encodeKey = encodeKey;
31
30
  exports.encodeKeyHash = encodeKeyHash;
32
- exports.char2Bytes = char2Bytes;
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;
33
39
  exports.stringToBytes = stringToBytes;
34
- exports.bytes2Char = bytes2Char;
35
40
  exports.bytesToString = bytesToString;
36
41
  exports.hex2Bytes = hex2Bytes;
37
42
  exports.toHexBuf = toHexBuf;
38
43
  exports.numToHexBuffer = numToHexBuffer;
39
44
  exports.num2PaddedHex = num2PaddedHex;
40
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;
41
56
  /*
42
57
  * Some code in this file is originally from sotez and eztz
43
58
  * Copyright (c) 2018 Andrew Kishino
@@ -45,246 +60,448 @@ exports.stripHexPrefix = stripHexPrefix;
45
60
  */
46
61
  const buffer_1 = require("buffer");
47
62
  const constants_1 = require("./constants");
48
- const verify_signature_1 = require("./verify-signature");
63
+ const constants_2 = require("./constants"); // (deprecated will be removed in the next minor release)
49
64
  const blake2b_1 = require("@stablelib/blake2b");
50
- const blakejs_1 = require("blakejs");
51
65
  const bs58check_1 = require("bs58check");
52
- const errors_1 = require("./errors");
53
66
  const bignumber_js_1 = require("bignumber.js");
67
+ const typedarray_to_buffer_1 = require("typedarray-to-buffer");
54
68
  const core_1 = require("@taquito/core");
69
+ const validators_1 = require("./validators");
55
70
  __exportStar(require("./validators"), exports);
56
71
  var version_1 = require("./version");
57
72
  Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return version_1.VERSION; } });
58
- var constants_2 = require("./constants");
59
- Object.defineProperty(exports, "prefix", { enumerable: true, get: function () { return constants_2.prefix; } });
60
- Object.defineProperty(exports, "Prefix", { enumerable: true, get: function () { return constants_2.Prefix; } });
61
- Object.defineProperty(exports, "prefixLength", { enumerable: true, get: function () { return constants_2.prefixLength; } });
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; } });
62
82
  var verify_signature_2 = require("./verify-signature");
63
83
  Object.defineProperty(exports, "verifySignature", { enumerable: true, get: function () { return verify_signature_2.verifySignature; } });
64
- Object.defineProperty(exports, "validatePkAndExtractPrefix", { enumerable: true, get: function () { return verify_signature_2.validatePkAndExtractPrefix; } });
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; } });
65
86
  __exportStar(require("./errors"), exports);
66
87
  var format_1 = require("./format");
67
88
  Object.defineProperty(exports, "format", { enumerable: true, get: function () { return format_1.format; } });
68
89
  /**
69
- *
70
- * @description Hash a string using the BLAKE2b algorithm, base58 encode the hash obtained and appends the prefix 'expr' to it
71
- *
72
- * @param value Value in hex
90
+ * @description list of prefixes that can be used to decode an address
73
91
  */
74
- function encodeExpr(value) {
75
- const blakeHash = blakejs_1.default.blake2b((0, exports.hex2buf)(value), undefined, 32);
76
- return b58cencode(blakeHash, constants_1.prefix['expr']);
77
- }
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
+ ];
78
101
  /**
79
- *
80
- * @description Return the operation hash of a signed operation
81
- * @param value Value in hex of a signed operation
102
+ * @description list of prefixes that can be used to decode a public key
82
103
  */
83
- function encodeOpHash(value) {
84
- const blakeHash = blakejs_1.default.blake2b((0, exports.hex2buf)(value), undefined, 32);
85
- return b58cencode(blakeHash, constants_1.prefix.o);
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
+ }
86
290
  }
87
291
  /**
88
- *
89
- * @description Base58 encode a string or a Uint8Array and append a prefix to it
90
- *
91
- * @param value Value to base58 encode
92
- * @param prefix prefix to append to the encoded string
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'
93
296
  */
94
- function b58cencode(value, prefix) {
95
- const payloadAr = typeof value === 'string' ? Uint8Array.from(buffer_1.Buffer.from(value, 'hex')) : value;
96
- const n = new Uint8Array(prefix.length + payloadAr.length);
97
- n.set(prefix);
98
- n.set(payloadAr, prefix.length);
99
- return bs58check_1.default.encode(buffer_1.Buffer.from(n.buffer));
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);
100
318
  }
101
319
  /**
102
- *
103
- * @description Base58 decode a string and remove the prefix from it
104
- *
105
- * @param value Value to base58 decode
106
- * @param prefix prefix to remove from the decoded string
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'
107
324
  */
108
- const b58cdecode = (enc, prefixArg) => bs58check_1.default.decode(enc).slice(prefixArg.length);
109
- exports.b58cdecode = b58cdecode;
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
+ }
110
333
  /**
111
- *
112
- * @description Base58 decode a string with predefined prefix
113
- *
114
- * @param value Value to base58 decode
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'
115
338
  */
116
- function b58decode(payload) {
117
- const buf = bs58check_1.default.decode(payload);
118
- const prefixMap = {
119
- [constants_1.prefix.tz1.toString()]: '0000',
120
- [constants_1.prefix.tz2.toString()]: '0001',
121
- [constants_1.prefix.tz3.toString()]: '0002',
122
- [constants_1.prefix.tz4.toString()]: '0003',
123
- };
124
- const pref = prefixMap[new Uint8Array(buf.slice(0, 3)).toString()];
125
- if (pref) {
126
- // tz addresses
127
- const hex = (0, exports.buf2hex)(buf.slice(3));
128
- return pref + hex;
339
+ function encodeKey(value) {
340
+ let buf;
341
+ if (typeof value === 'string') {
342
+ buf = hex2buf(value);
129
343
  }
130
344
  else {
131
- // other (kt addresses)
132
- return '01' + (0, exports.buf2hex)(buf.slice(3, 42)) + '00';
345
+ buf = value;
133
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);
134
365
  }
135
366
  /**
136
- *
137
- * @description b58 decode a string without predefined prefix
138
- * @param value
139
- * @returns string of bytes
140
- * @deprecated use b58decode instead
141
- */
142
- function b58decodeL2Address(payload) {
143
- const buf = bs58check_1.default.decode(payload);
144
- // tz4 address currently
145
- return (0, exports.buf2hex)(buf.slice(3, 42));
146
- }
147
- /**
148
- *
149
- * @description Base58 encode an address using predefined prefix
150
- *
151
- * @param value Address to base58 encode (tz1, tz2, tz3 or KT1)
152
- * @deprecated use encodeAddress instead, same functionality with a more descriptive name
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'
153
371
  */
154
- function encodePubKey(value) {
155
- if (value.substring(0, 2) === '00') {
156
- const pref = {
157
- '0000': constants_1.prefix.tz1,
158
- '0001': constants_1.prefix.tz2,
159
- '0002': constants_1.prefix.tz3,
160
- '0003': constants_1.prefix.tz4,
161
- };
162
- return b58cencode(value.substring(4), pref[value.substring(0, 4)]);
372
+ function encodeKeyHash(value) {
373
+ let buf;
374
+ if (typeof value === 'string') {
375
+ buf = hex2buf(value);
376
+ }
377
+ else {
378
+ buf = value;
163
379
  }
164
- return b58cencode(value.substring(2, 42), constants_1.prefix.KT);
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);
165
398
  }
166
399
  /**
167
- *
168
- * @description Base58 encode an address using predefined prefix (tz1, tz2, tz3, or KT1 without annotation)
169
- *
170
- * @param value Address to base58 encode (tz1, tz2, tz3 or KT1). Supports value with or without '0x' prefix
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'
171
403
  */
172
404
  function encodeAddress(value) {
173
- if (value.substring(0, 2) === '0x') {
174
- value = value.slice(2);
405
+ let buf;
406
+ if (typeof value === 'string') {
407
+ buf = hex2buf(value);
408
+ }
409
+ else {
410
+ buf = value;
175
411
  }
176
- if (value.substring(0, 2) === '00') {
177
- const pref = {
178
- '0000': constants_1.prefix.tz1,
179
- '0001': constants_1.prefix.tz2,
180
- '0002': constants_1.prefix.tz3,
181
- '0003': constants_1.prefix.tz4,
182
- };
183
- return b58cencode(value.substring(4), pref[value.substring(0, 4)]);
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');
184
423
  }
185
- return b58cencode(value.substring(2, 42), constants_1.prefix.KT);
186
424
  }
187
425
  /**
188
- *
189
426
  * @description Base58 encode an address without predefined prefix
190
427
  * @param value Address to base58 encode (tz4) hex dec
191
428
  * @returns return address
192
- * @deprecated use encodeAddress instead
429
+ * @example encodeBlsAddress('af2dc3c40667abc0e89c0ef40171d22aed08d5eb') // return 'tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8'
193
430
  */
194
- function encodeL2Address(value) {
195
- return b58cencode(value, constants_1.prefix.tz4);
431
+ function encodeBlsAddress(value) {
432
+ return b58Encode(value, constants_1.PrefixV2.BLS12_381PublicKeyHash);
196
433
  }
197
434
  /**
198
- *
199
- * @description Base58 encode a key according to its prefix
200
- *
201
- * @param value Key to base58 encode
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'
202
439
  */
203
- function encodeKey(value) {
204
- if (value[0] === '0') {
205
- const pref = {
206
- '00': new Uint8Array([13, 15, 37, 217]),
207
- '01': new Uint8Array([3, 254, 226, 86]),
208
- '02': new Uint8Array([3, 178, 139, 127]),
209
- };
210
- return b58cencode(value.substring(2), pref[value.substring(0, 2)]);
211
- }
440
+ function encodeExpr(value) {
441
+ const blakeHash = (0, blake2b_1.hash)(hex2buf(value), 32);
442
+ return b58Encode(blakeHash, constants_1.PrefixV2.ScriptExpr);
212
443
  }
213
444
  /**
214
- *
215
- * @description Base58 encode a key hash according to its prefix
216
- *
217
- * @param value Key hash to base58 encode
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'
218
449
  */
219
- function encodeKeyHash(value) {
220
- if (value[0] === '0') {
221
- const pref = {
222
- '00': new Uint8Array([6, 161, 159]),
223
- '01': new Uint8Array([6, 161, 161]),
224
- '02': new Uint8Array([6, 161, 164]),
225
- '03': new Uint8Array([6, 161, 166]),
226
- };
227
- return b58cencode(value.substring(2), pref[value.substring(0, 2)]);
228
- }
450
+ function encodeOpHash(value) {
451
+ const blakeHash = (0, blake2b_1.hash)(hex2buf(value), 32);
452
+ return b58Encode(blakeHash, constants_1.PrefixV2.OperationHash);
229
453
  }
230
454
  /**
231
- *
232
455
  * @description Convert an hex string to a Uint8Array
233
- *
234
456
  * @param hex Hex string to convert
235
457
  * @throws {@link ValueConversionError}
236
458
  */
237
- const hex2buf = (hex) => {
459
+ function hex2buf(hex) {
460
+ hex = hex.startsWith('0x') ? hex.slice(2) : hex;
238
461
  if (hex.length % 2 !== 0) {
239
- throw new core_1.InvalidHexStringError(hex, `: Expecting even number of characters`);
462
+ throw new core_1.InvalidHexStringError(hex, `Expecting even number of characters`);
240
463
  }
241
- const hexDigits = stripHexPrefix(hex);
242
- if (!hexDigits.match(/^([\da-f]{2})*$/gi)) {
243
- throw new core_1.InvalidHexStringError(hex, `: Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
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'`);
244
466
  }
245
- const out = new Uint8Array(hexDigits.length / 2);
467
+ const res = new Uint8Array(hex.length / 2);
246
468
  let j = 0;
247
- for (let i = 0; i < hexDigits.length; i += 2) {
248
- const v = parseInt(hexDigits.slice(i, i + 2), 16);
249
- if (Number.isNaN(v)) {
250
- throw new errors_1.ValueConversionError(hex, 'Uint8Array');
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'`);
251
474
  }
252
- out[j++] = v;
475
+ res[j++] = x;
253
476
  }
254
- return out;
255
- };
256
- exports.hex2buf = hex2buf;
477
+ return res;
478
+ }
257
479
  /**
258
- *
259
480
  * @description Merge 2 buffers together
260
- *
261
481
  * @param b1 First buffer
262
482
  * @param b2 Second buffer
263
483
  */
264
- const mergebuf = (b1, b2) => {
484
+ function mergebuf(b1, b2) {
265
485
  const r = new Uint8Array(b1.length + b2.length);
266
486
  r.set(b1);
267
487
  r.set(b2, b1.length);
268
488
  return r;
269
- };
270
- exports.mergebuf = mergebuf;
489
+ }
271
490
  /**
272
- *
273
491
  * @description Flatten a michelson json representation to an array
274
- *
275
492
  * @param s michelson json
276
493
  */
277
- const mic2arr = function me2(s) {
494
+ function mic2arr(s) {
278
495
  let ret = [];
279
496
  if (Object.prototype.hasOwnProperty.call(s, 'prim')) {
280
497
  if (s.prim === 'Pair') {
281
- ret.push(me2(s.args[0]));
282
- ret = ret.concat(me2(s.args[1]));
498
+ ret.push(mic2arr(s.args[0]));
499
+ ret = ret.concat(mic2arr(s.args[1]));
283
500
  }
284
501
  else if (s.prim === 'Elt') {
285
502
  ret = {
286
- key: me2(s.args[0]),
287
- val: me2(s.args[1]),
503
+ key: mic2arr(s.args[0]),
504
+ val: mic2arr(s.args[1]),
288
505
  };
289
506
  }
290
507
  else if (s.prim === 'True') {
@@ -297,7 +514,7 @@ const mic2arr = function me2(s) {
297
514
  else if (Array.isArray(s)) {
298
515
  const sc = s.length;
299
516
  for (let i = 0; i < sc; i++) {
300
- const n = me2(s[i]);
517
+ const n = mic2arr(s[i]);
301
518
  if (typeof n.key !== 'undefined') {
302
519
  if (Array.isArray(ret)) {
303
520
  ret = {
@@ -323,113 +540,43 @@ const mic2arr = function me2(s) {
323
540
  ret = s;
324
541
  }
325
542
  return ret;
326
- };
327
- exports.mic2arr = mic2arr;
543
+ }
328
544
  /**
329
- *
330
545
  * @description Convert a Uint8Array to an hex string
331
- *
332
546
  * @param buffer Uint8Array to convert
333
547
  */
334
- const buf2hex = (buffer) => {
335
- const hexParts = [];
336
- buffer.forEach((byte) => {
337
- const hex = byte.toString(16);
338
- const paddedHex = `00${hex}`.slice(-2);
339
- hexParts.push(paddedHex);
340
- });
341
- return hexParts.join('');
342
- };
343
- exports.buf2hex = buf2hex;
344
- /**
345
- *
346
- * @description Gets Tezos address (PKH) from Public Key
347
- *
348
- * @param publicKey Public Key
349
- * @returns A string of the Tezos address (PKH) that was derived from the given Public Key
350
- */
351
- const getPkhfromPk = (publicKey) => {
352
- let encodingPrefix;
353
- let prefixLen;
354
- const keyPrefix = (0, verify_signature_1.validatePkAndExtractPrefix)(publicKey);
355
- const decoded = (0, exports.b58cdecode)(publicKey, constants_1.prefix[keyPrefix]);
356
- switch (keyPrefix) {
357
- case constants_1.Prefix.EDPK:
358
- encodingPrefix = constants_1.prefix[constants_1.Prefix.TZ1];
359
- prefixLen = constants_1.prefixLength[constants_1.Prefix.TZ1];
360
- break;
361
- case constants_1.Prefix.SPPK:
362
- encodingPrefix = constants_1.prefix[constants_1.Prefix.TZ2];
363
- prefixLen = constants_1.prefixLength[constants_1.Prefix.TZ2];
364
- break;
365
- case constants_1.Prefix.P2PK:
366
- encodingPrefix = constants_1.prefix[constants_1.Prefix.TZ3];
367
- prefixLen = constants_1.prefixLength[constants_1.Prefix.TZ3];
368
- break;
369
- case constants_1.Prefix.BLPK:
370
- encodingPrefix = constants_1.prefix[constants_1.Prefix.TZ4];
371
- prefixLen = constants_1.prefixLength[constants_1.Prefix.TZ4];
372
- }
373
- const hashed = (0, blake2b_1.hash)(decoded, prefixLen);
374
- const result = b58cencode(hashed, encodingPrefix);
375
- return result;
376
- };
377
- exports.getPkhfromPk = getPkhfromPk;
378
- /**
379
- *
380
- * @description Convert a string to bytes
381
- *
382
- * @param str String to convert
383
- * @deprecated use stringToBytes instead, same functionality with a more descriptive name
384
- */
385
- function char2Bytes(str) {
386
- return buffer_1.Buffer.from(str, 'utf8').toString('hex');
548
+ function buf2hex(bytes) {
549
+ return Array.from(bytes)
550
+ .map((x) => ((x >> 4) & 0xf).toString(16) + (x & 0xf).toString(16))
551
+ .join('');
387
552
  }
388
553
  /**
389
- *
390
554
  * @description Convert a string to a byte string representation
391
- *
392
555
  * @param str String to convert
393
556
  */
394
557
  function stringToBytes(str) {
395
558
  return buffer_1.Buffer.from(str, 'utf8').toString('hex');
396
559
  }
397
560
  /**
398
- *
399
- * @description Convert bytes to a string
400
- *
401
- * @param str Bytes to convert
402
- * @deprecated use hexStringToBytes instead, same functionality with a more descriptive name
403
- */
404
- function bytes2Char(hex) {
405
- return buffer_1.Buffer.from((0, exports.hex2buf)(hex)).toString('utf8');
406
- }
407
- /**
408
- *
409
561
  * @description Convert byte string representation to string
410
- *
411
- * @param str byte string to convert
562
+ * @param hex byte string to convert
412
563
  */
413
564
  function bytesToString(hex) {
414
- return buffer_1.Buffer.from((0, exports.hex2buf)(hex)).toString('utf8');
565
+ return buffer_1.Buffer.from(hex2buf(hex)).toString('utf8');
415
566
  }
416
567
  /**
417
- *
418
568
  * @description Convert hex string/UintArray/Buffer to bytes
419
- *
420
569
  * @param hex String value to convert to bytes
421
570
  */
422
571
  function hex2Bytes(hex) {
423
572
  const hexDigits = stripHexPrefix(hex);
424
573
  if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
425
- throw new core_1.InvalidHexStringError(hex, `: Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
574
+ throw new core_1.InvalidHexStringError(hex, `Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
426
575
  }
427
576
  return buffer_1.Buffer.from(hexDigits, 'hex');
428
577
  }
429
578
  /**
430
- *
431
579
  * @description Converts a number or Bignumber to hexadecimal string
432
- *
433
580
  * @param val The value that will be converted to a hexadecimal string value
434
581
  */
435
582
  function toHexBuf(val, bitLength = 8) {
@@ -439,7 +586,6 @@ function numToHexBuffer(val, bitLength = 8) {
439
586
  return buffer_1.Buffer.from(num2PaddedHex(val, bitLength), 'hex');
440
587
  }
441
588
  /**
442
- *
443
589
  * @description Converts a number or BigNumber to a padded hexadecimal string
444
590
  * @param val The value that will be converted into a padded hexadecimal string value
445
591
  * @param bitLength The length of bits
@@ -481,3 +627,110 @@ function padHexWithZero(hex, targetLength) {
481
627
  function stripHexPrefix(hex) {
482
628
  return hex.startsWith('0x') ? hex.slice(2) : hex;
483
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
+ }