@taquito/utils 16.1.2 → 16.2.0-beta-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.
@@ -3,6 +3,8 @@ import { verify } from '@stablelib/ed25519';
3
3
  import { hash } from '@stablelib/blake2b';
4
4
  import blake from 'blakejs';
5
5
  import bs58check from 'bs58check';
6
+ import { InvalidMessageError, InvalidPublicKeyError, InvalidSignatureError, ParameterValidationError, UnsupportedActionError, InvalidHexStringError } from '@taquito/core';
7
+ export { DeprecationError, InvalidAddressError, InvalidBlockHashError, InvalidChainIdError, InvalidContractAddressError, InvalidHexStringError, InvalidKeyError, InvalidKeyHashError, InvalidMessageError, InvalidOperationHashError, InvalidOperationKindError, InvalidPublicKeyError, InvalidSignatureError, ProhibitedActionError } from '@taquito/core';
6
8
  import BigNumber from 'bignumber.js';
7
9
  import elliptic from 'elliptic';
8
10
  import toBuffer from 'typedarray-to-buffer';
@@ -136,194 +138,6 @@ const prefixLength = {
136
138
  [Prefix.SRC1]: 32,
137
139
  };
138
140
 
139
- /**
140
- * @category Error
141
- * @description Error that indicates an invalid key being passed or used
142
- */
143
- class InvalidKeyError extends Error {
144
- constructor(key, errorDetail) {
145
- super();
146
- this.key = key;
147
- this.errorDetail = errorDetail;
148
- this.name = 'InvalidKeyError';
149
- const baseMessage = `The key ${key} is invalid.`;
150
- this.message = errorDetail ? `${baseMessage} ${errorDetail}` : baseMessage;
151
- }
152
- }
153
- /**
154
- * @category Error
155
- * @description Error that indicates an Invalid Public Key being passed or used
156
- */
157
- class InvalidPublicKeyError extends Error {
158
- constructor(publicKey, errorDetail) {
159
- super();
160
- this.publicKey = publicKey;
161
- this.name = 'InvalidPublicKeyError';
162
- const baseMessage = `The public key '${publicKey}' is invalid.`;
163
- this.message = errorDetail ? `${baseMessage} ${errorDetail}` : baseMessage;
164
- }
165
- }
166
- /**
167
- * @category Error
168
- * @description Error that indicates an invalid signature being passed or used
169
- */
170
- class InvalidSignatureError extends Error {
171
- constructor(signature, errorDetail) {
172
- super();
173
- this.signature = signature;
174
- this.name = 'InvalidSignatureError';
175
- const baseMessage = `The signature '${signature}' is invalid.`;
176
- this.message = errorDetail ? `${baseMessage} ${errorDetail}` : baseMessage;
177
- }
178
- }
179
- /**
180
- * @category Error
181
- * @description Error that indicates an invalid message being passed or used
182
- */
183
- class InvalidMessageError extends Error {
184
- constructor(msg, errorDetail) {
185
- super();
186
- this.msg = msg;
187
- this.errorDetail = errorDetail;
188
- this.name = 'InvalidMessageError';
189
- const baseMessage = `The message '${msg}' is invalid.`;
190
- this.message = errorDetail ? `${baseMessage} ${errorDetail}` : baseMessage;
191
- }
192
- }
193
- /**
194
- * @category Error
195
- * @description Error that indicates an invalid contract address being passed or used
196
- */
197
- class InvalidContractAddressError extends Error {
198
- constructor(contractAddress) {
199
- super(`The contract address '${contractAddress}' is invalid`);
200
- this.contractAddress = contractAddress;
201
- this.name = 'InvalidContractAddressError';
202
- }
203
- }
204
- /**
205
- * @category Error
206
- * @description Error that indicates an invalid address being passed or used (both contract and implicit)
207
- */
208
- class InvalidAddressError extends Error {
209
- constructor(address, errorDetail) {
210
- super();
211
- this.address = address;
212
- this.name = 'InvalidAddressError';
213
- const baseMessage = `The address '${address}' is invalid.`;
214
- this.message = errorDetail ? `${baseMessage} ${errorDetail}` : baseMessage;
215
- }
216
- }
217
- /**
218
- * @category Error
219
- * @description Error that indicates an invalid chain id being passed or used
220
- */
221
- class InvalidChainIdError extends Error {
222
- constructor(chainId) {
223
- super(`The chain id '${chainId}' is invalid`);
224
- this.chainId = chainId;
225
- this.name = 'InvalidChainIdError';
226
- }
227
- }
228
- /**
229
- * @category Error
230
- * @description Error that indicates an invalid key hash being passed or used
231
- */
232
- class InvalidKeyHashError extends Error {
233
- constructor(keyHash) {
234
- super(`The public key hash '${keyHash}' is invalid`);
235
- this.keyHash = keyHash;
236
- this.name = 'InvalidKeyHashError';
237
- }
238
- }
239
- /**
240
- * @category Error
241
- * @description Error that indicates an invalid block hash being passed or used
242
- */ class InvalidBlockHashError extends Error {
243
- constructor(blockHash) {
244
- super(`The block hash '${blockHash}' is invalid`);
245
- this.blockHash = blockHash;
246
- this.name = 'InvalidBlockHashError';
247
- }
248
- }
249
- /**
250
- * @category Error
251
- * @description Error that indicates invalid protocol hash being passed or used
252
- */
253
- class InvalidProtocolHashError extends Error {
254
- constructor(protocolHash) {
255
- super(`The protocol hash '${protocolHash}' is invalid`);
256
- this.protocolHash = protocolHash;
257
- this.name = 'InvalidProtocolHashError';
258
- }
259
- }
260
- /**
261
- * @category Error
262
- * @description Error that indicates an invalid operation hash being passed or used
263
- */ class InvalidOperationHashError extends Error {
264
- constructor(operationHash) {
265
- super(`The operation hash '${operationHash}' is invalid`);
266
- this.operationHash = operationHash;
267
- this.name = 'InvalidOperationHashError';
268
- }
269
- }
270
- /**
271
- * @category Error
272
- * @description Error that indicates an invalid operation kind being passed or used
273
- */
274
- class InvalidOperationKindError extends Error {
275
- constructor(operationKind) {
276
- super(`The operation kind '${operationKind}' is unsupported`);
277
- this.operationKind = operationKind;
278
- this.name = 'InvalidOperationKindError';
279
- }
280
- }
281
- /**
282
- * @category Error
283
- * @description General error that indicates something is no longer supported and/or deprecated
284
- */
285
- class DeprecationError extends Error {
286
- constructor(message) {
287
- super(message);
288
- this.message = message;
289
- this.name = 'DeprecationError';
290
- }
291
- }
292
- /**
293
- * @category Error
294
- * @description General error that indicates an action is prohibited or not allowed
295
- */
296
- class ProhibitedActionError extends Error {
297
- constructor(message) {
298
- super(message);
299
- this.message = message;
300
- this.name = 'ProhibitedActionError';
301
- }
302
- }
303
- /**
304
- * @category Error
305
- * @description General error that indicates a failure when trying to convert data from one type to another
306
- */
307
- class ValueConversionError extends Error {
308
- constructor(value, desiredType) {
309
- super(`Unable to convert ${value} to a ${desiredType}`);
310
- this.value = value;
311
- this.desiredType = desiredType;
312
- this.name = 'ValueConversionError';
313
- }
314
- }
315
- /**
316
- * @category Error
317
- * @description Error that indicates an invalid hex string being passed or used
318
- */
319
- class InvalidHexStringError extends Error {
320
- constructor(message) {
321
- super(message);
322
- this.message = message;
323
- this.name = 'InvalidHexStringError';
324
- }
325
- }
326
-
327
141
  /**
328
142
  * @description Verify signature of a payload
329
143
  *
@@ -332,7 +146,7 @@ class InvalidHexStringError extends Error {
332
146
  * @param publicKey The public key to verify the signature against
333
147
  * @param signature The signature to verify
334
148
  * @returns A boolean indicating if the signature matches
335
- *
149
+ * @throws {@link InvalidPublicKeyError} | {@link InvalidSignatureError} | {@link InvalidMessageError}
336
150
  * @example
337
151
  * ```
338
152
  * const message = '03d0c10e3ed11d7c6e3357f6ef335bab9e8f2bd54d0ce20c482e241191a6e4b8ce6c01be917311d9ac46959750e405d57e268e2ed9e174a80794fbd504e12a4a000141eb3781afed2f69679ff2bbe1c5375950b0e40d00ff000000005e05050505050507070100000024747a32526773486e74516b72794670707352466261313652546656503539684b72654a4d07070100000024747a315a6672455263414c42776d4171776f6e525859565142445439426a4e6a42484a750001';
@@ -364,44 +178,28 @@ function verifySignature(messageBytes, publicKey, signature) {
364
178
  }
365
179
  function validateMessageNotEmpty(message) {
366
180
  if (message === '') {
367
- throw new InvalidMessageError(message, 'The message provided for verifying signature cannot be empty.');
181
+ throw new InvalidMessageError(message, ': Cannot be empty.');
368
182
  }
369
183
  return message;
370
184
  }
371
185
  function validatePkAndExtractPrefix(publicKey) {
372
186
  if (publicKey === '') {
373
- throw new InvalidPublicKeyError(publicKey, 'Public key cannot be empty');
187
+ throw new InvalidPublicKeyError(publicKey, ': Can not be empty');
374
188
  }
375
189
  const pkPrefix = publicKey.substring(0, 4);
376
- const validation = validatePublicKey(publicKey);
377
- if (validation !== ValidationResult.VALID) {
378
- if (validation === ValidationResult.INVALID_CHECKSUM) {
379
- throw new InvalidPublicKeyError(publicKey, 'The public key provided has an invalid checksum');
380
- }
381
- else if (validation === ValidationResult.INVALID_LENGTH) {
382
- throw new InvalidPublicKeyError(publicKey, 'The public key provided has an invalid length');
383
- }
384
- else if (validation === ValidationResult.NO_PREFIX_MATCHED) {
385
- throw new InvalidPublicKeyError(publicKey, `The public key provided has an unsupported prefix: ${pkPrefix}`);
386
- }
190
+ const publicKeyValidation = validatePublicKey(publicKey);
191
+ if (publicKeyValidation !== ValidationResult.VALID) {
192
+ throw new InvalidPublicKeyError(publicKey, invalidErrorDetail(publicKeyValidation));
387
193
  }
388
194
  return pkPrefix;
389
195
  }
390
196
  function validateSigAndExtractPrefix(signature) {
391
197
  const signaturePrefix = signature.startsWith('sig')
392
- ? signature.substr(0, 3)
393
- : signature.substr(0, 5);
198
+ ? signature.substring(0, 3)
199
+ : signature.substring(0, 5);
394
200
  const validation = validateSignature(signature);
395
201
  if (validation !== ValidationResult.VALID) {
396
- if (validation === ValidationResult.INVALID_CHECKSUM) {
397
- throw new InvalidSignatureError(signature, `invalid checksum`);
398
- }
399
- else if (validation === ValidationResult.INVALID_LENGTH) {
400
- throw new InvalidSignatureError(signature, 'invalid length');
401
- }
402
- else if (validation === ValidationResult.NO_PREFIX_MATCHED) {
403
- throw new InvalidSignatureError(signaturePrefix, 'unsupported prefix');
404
- }
202
+ throw new InvalidSignatureError(signature, invalidErrorDetail(validation));
405
203
  }
406
204
  return signaturePrefix;
407
205
  }
@@ -436,6 +234,34 @@ function verifySpOrP2Sig(decodedSig, bytesHash, key) {
436
234
  return false;
437
235
  }
438
236
 
237
+ /**
238
+ * @category Error
239
+ * @description Error indicates invalid protocol hash being passed or used
240
+ */
241
+ class InvalidProtocolHashError extends ParameterValidationError {
242
+ constructor(protocolHash, errorDetails) {
243
+ super();
244
+ this.protocolHash = protocolHash;
245
+ this.name = 'InvalidProtocolHashError';
246
+ this.name = 'InvalidProtocolHashError';
247
+ this.message = `The protocol hash '${protocolHash}' is invalid`;
248
+ errorDetails ? (this.message += `: ${errorDetails}`) : null;
249
+ }
250
+ }
251
+ /**
252
+ * @category Error
253
+ * @description Error indicates unable to convert data type from one to another
254
+ */
255
+ class ValueConversionError extends UnsupportedActionError {
256
+ constructor(value, desiredType) {
257
+ super();
258
+ this.value = value;
259
+ this.desiredType = desiredType;
260
+ this.name = 'ValueConversionError';
261
+ this.message = `Unable to convert ${value} to a ${desiredType}`;
262
+ }
263
+ }
264
+
439
265
  var ValidationResult;
440
266
  (function (ValidationResult) {
441
267
  ValidationResult[ValidationResult["NO_PREFIX_MATCHED"] = 0] = "NO_PREFIX_MATCHED";
@@ -494,6 +320,7 @@ const pkPrefix = [Prefix.EDPK, Prefix.SPPK, Prefix.P2PK, Prefix.BLPK];
494
320
  const operationPrefix = [Prefix.O];
495
321
  const protocolPrefix = [Prefix.P];
496
322
  const blockPrefix = [Prefix.B];
323
+ const smartRollupPrefix = [Prefix.SR1];
497
324
  /**
498
325
  * @description Used to check if an address or a contract address is valid.
499
326
  *
@@ -510,7 +337,11 @@ const blockPrefix = [Prefix.B];
510
337
  * ```
511
338
  */
512
339
  function validateAddress(value) {
513
- return validatePrefixedValue(value, [...implicitPrefix, ...contractPrefix]);
340
+ return validatePrefixedValue(value, [
341
+ ...implicitPrefix,
342
+ ...contractPrefix,
343
+ ...smartRollupPrefix,
344
+ ]);
514
345
  }
515
346
  /**
516
347
  * @description Used to check if a chain id is valid.
@@ -663,12 +494,27 @@ function validateBlock(value) {
663
494
  */
664
495
  function validateSpendingKey(value) {
665
496
  return validatePrefixedValue(value, [Prefix.SASK]);
497
+ }
498
+ function invalidErrorDetail(validation) {
499
+ switch (validation) {
500
+ case ValidationResult.NO_PREFIX_MATCHED:
501
+ return ': Invalid prefix';
502
+ case ValidationResult.INVALID_CHECKSUM:
503
+ return ': Checksum failed';
504
+ case ValidationResult.INVALID_LENGTH:
505
+ return ': Invalid length';
506
+ default:
507
+ return '';
508
+ }
509
+ }
510
+ function validateSmartRollupAddress(value) {
511
+ return validatePrefixedValue(value, [...smartRollupPrefix]);
666
512
  }
667
513
 
668
514
  // IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
669
515
  const VERSION = {
670
- "commitHash": "bcc2118ddfafc1995fd125cd74d198fda042bf48",
671
- "version": "16.1.2"
516
+ "commitHash": "babcbaf464fd3571a3b88cf7023fefe87809d86d",
517
+ "version": "16.2.0-beta-RC.0"
672
518
  };
673
519
 
674
520
  const TZ_DECIMALS = 6;
@@ -849,6 +695,7 @@ function encodeKeyHash(value) {
849
695
  * @description Convert an hex string to a Uint8Array
850
696
  *
851
697
  * @param hex Hex string to convert
698
+ * @throws {@link ValueConversionError}
852
699
  */
853
700
  const hex2buf = (hex) => {
854
701
  const match = hex.match(/[\da-f]{2}/gi);
@@ -1003,7 +850,7 @@ function bytes2Char(hex) {
1003
850
  */
1004
851
  function hex2Bytes(hex) {
1005
852
  if (!hex.match(/[\da-f]{2}/gi)) {
1006
- throw new InvalidHexStringError(`The hex string ${hex} does not have an even number of characters`);
853
+ throw new InvalidHexStringError(hex, `: Expecting even number of characters`);
1007
854
  }
1008
855
  return Buffer.from(hex, 'hex');
1009
856
  }
@@ -1060,5 +907,5 @@ function stripHexPrefix(hex) {
1060
907
  return hex.startsWith('0x') ? hex.slice(2) : hex;
1061
908
  }
1062
909
 
1063
- export { DeprecationError, InvalidAddressError, InvalidBlockHashError, InvalidChainIdError, InvalidContractAddressError, InvalidHexStringError, InvalidKeyError, InvalidKeyHashError, InvalidMessageError, InvalidOperationHashError, InvalidOperationKindError, InvalidProtocolHashError, InvalidPublicKeyError, InvalidSignatureError, Prefix, ProhibitedActionError, VERSION, ValidationResult, ValueConversionError, b58cdecode, b58cencode, b58decode, b58decodeL2Address, buf2hex, bytes2Char, char2Bytes, encodeExpr, encodeKey, encodeKeyHash, encodeL2Address, encodeOpHash, encodePubKey, format, getPkhfromPk, hex2Bytes, hex2buf, isValidPrefix, mergebuf, mic2arr, num2PaddedHex, prefix, prefixLength, stripHexPrefix, toHexBuf, validateAddress, validateBlock, validateChain, validateContractAddress, validateKeyHash, validateOperation, validatePkAndExtractPrefix, validateProtocol, validatePublicKey, validateSignature, validateSpendingKey, verifySignature };
910
+ export { InvalidProtocolHashError, Prefix, VERSION, ValidationResult, ValueConversionError, b58cdecode, b58cencode, b58decode, b58decodeL2Address, buf2hex, bytes2Char, char2Bytes, encodeExpr, encodeKey, encodeKeyHash, encodeL2Address, encodeOpHash, encodePubKey, format, getPkhfromPk, hex2Bytes, hex2buf, invalidErrorDetail, isValidPrefix, mergebuf, mic2arr, num2PaddedHex, prefix, prefixLength, stripHexPrefix, toHexBuf, validateAddress, validateBlock, validateChain, validateContractAddress, validateKeyHash, validateOperation, validatePkAndExtractPrefix, validateProtocol, validatePublicKey, validateSignature, validateSmartRollupAddress, validateSpendingKey, verifySignature };
1064
911
  //# sourceMappingURL=taquito-utils.es6.js.map