@taquito/utils 23.0.0-beta.0 → 23.0.0-beta.1

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.
@@ -1,10 +1,14 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('buffer'), require('@stablelib/ed25519'), require('@stablelib/blake2b'), require('blakejs'), require('bs58check'), require('@taquito/core'), require('bignumber.js'), require('elliptic'), require('typedarray-to-buffer')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'buffer', '@stablelib/ed25519', '@stablelib/blake2b', 'blakejs', 'bs58check', '@taquito/core', 'bignumber.js', 'elliptic', 'typedarray-to-buffer'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoUtils = {}, global.buffer, global.ed25519, global.blake2b, global.blake, global.bs58check, global.core, global.BigNumber, global.elliptic, global.toBuffer));
5
- })(this, (function (exports, buffer, ed25519, blake2b, blake, bs58check, core, BigNumber, elliptic, toBuffer) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('buffer'), require('@stablelib/blake2b'), require('bs58check'), require('bignumber.js'), require('typedarray-to-buffer'), require('@taquito/core'), require('@stablelib/ed25519'), require('elliptic'), require('@noble/curves/bls12-381')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'buffer', '@stablelib/blake2b', 'bs58check', 'bignumber.js', 'typedarray-to-buffer', '@taquito/core', '@stablelib/ed25519', 'elliptic', '@noble/curves/bls12-381'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoUtils = {}, global.buffer, global.blake2b, global.bs58check, global.BigNumber, global.toBuffer, global.core, global.ed25519, global.elliptic, global.bls12381));
5
+ })(this, (function (exports, buffer, blake2b, bs58check, BigNumber, toBuffer, core, ed25519, elliptic, bls12381) { 'use strict';
6
6
 
7
7
  // ref https://gitlab.com/tezos/tezos/-/blob/master/src/lib_crypto/base58.ml
8
+ /**
9
+ * @deprecated use PrefixV2 instead, this enum will be removed in the next minor release
10
+ * @description base58 prefix definition enum
11
+ */
8
12
  exports.Prefix = void 0;
9
13
  (function (Prefix) {
10
14
  Prefix["TZ1"] = "tz1";
@@ -49,6 +53,10 @@
49
53
  Prefix["SRC1"] = "src1";
50
54
  Prefix["SH"] = "sh";
51
55
  })(exports.Prefix || (exports.Prefix = {}));
56
+ /**
57
+ * @deprecated use prefixV2 instead this constant will be removed in the next minor release
58
+ * @description base58 prefix to bytes mapping
59
+ */
52
60
  const prefix = {
53
61
  [exports.Prefix.TZ1]: new Uint8Array([6, 161, 159]),
54
62
  [exports.Prefix.TZ2]: new Uint8Array([6, 161, 161]),
@@ -93,6 +101,10 @@
93
101
  [exports.Prefix.SRC1]: new Uint8Array([17, 165, 134, 138]),
94
102
  [exports.Prefix.SH]: new Uint8Array([2, 116, 180]),
95
103
  };
104
+ /**
105
+ * @deprecated use payloadLength instead this constant will be removed in the next minor release
106
+ * @description base58 prefix to payload length mapping
107
+ */
96
108
  const prefixLength = {
97
109
  [exports.Prefix.TZ1]: 20,
98
110
  [exports.Prefix.TZ2]: 20,
@@ -120,200 +132,252 @@
120
132
  [exports.Prefix.SRC1]: 32,
121
133
  [exports.Prefix.SH]: 48,
122
134
  };
123
-
124
135
  /**
125
- * @description Verify signature of a payload
126
- *
127
- * @param messageBytes The forged message including the magic byte (11 for block,
128
- * 12 for preattestation, 13 for attestation, 3 for generic, 5 for the PACK format of michelson)
129
- * @param publicKey The public key to verify the signature against
130
- * @param signature The signature to verify
131
- * @returns A boolean indicating if the signature matches
132
- * @throws {@link InvalidPublicKeyError} | {@link InvalidSignatureError} | {@link InvalidMessageError}
133
- * @example
134
- * ```
135
- * const message = '03d0c10e3ed11d7c6e3357f6ef335bab9e8f2bd54d0ce20c482e241191a6e4b8ce6c01be917311d9ac46959750e405d57e268e2ed9e174a80794fbd504e12a4a000141eb3781afed2f69679ff2bbe1c5375950b0e40d00ff000000005e05050505050507070100000024747a32526773486e74516b72794670707352466261313652546656503539684b72654a4d07070100000024747a315a6672455263414c42776d4171776f6e525859565142445439426a4e6a42484a750001';
136
- * const pk = 'sppk7c7hkPj47yjYFEHX85q46sFJGw6RBrqoVSHwAJAT4e14KJwzoey';
137
- * const sig = 'spsig1cdLkp1RLgUHAp13aRFkZ6MQDPp7xCnjAExGL3MBSdMDmT6JgQSX8cufyDgJRM3sinFtiCzLbsyP6d365EHoNevxhT47nx'
138
- *
139
- * const response = verifySignature(message, pk, sig);
140
- * ```
141
- *
136
+ * @description base58 name to prefix mapping
142
137
  */
143
- function verifySignature(messageBytes, publicKey, signature, watermark) {
144
- const pkPrefix = validatePkAndExtractPrefix(publicKey);
145
- const sigPrefix = validateSigAndExtractPrefix(signature);
146
- const decodedPublicKey = b58cdecode(publicKey, prefix[pkPrefix]);
147
- const decodedSig = b58cdecode(signature, prefix[sigPrefix]);
148
- let messageBuf = hex2buf(validateMessageNotEmpty(messageBytes));
149
- if (typeof watermark !== 'undefined') {
150
- messageBuf = mergebuf(watermark, messageBuf);
151
- }
152
- const bytesHash = blake2b.hash(messageBuf, 32);
153
- if (pkPrefix === exports.Prefix.EDPK) {
154
- return verifyEdSignature(decodedSig, bytesHash, decodedPublicKey);
155
- }
156
- else if (pkPrefix === exports.Prefix.SPPK) {
157
- return verifySpSignature(decodedSig, bytesHash, decodedPublicKey);
158
- }
159
- else if (pkPrefix === exports.Prefix.P2PK) {
160
- return verifyP2Signature(decodedSig, bytesHash, decodedPublicKey);
161
- }
162
- else {
163
- return false;
164
- }
165
- }
166
- function validateMessageNotEmpty(message) {
167
- if (message === '') {
168
- throw new core.InvalidMessageError(message, `can't be empty`);
169
- }
170
- return message;
171
- }
172
- function validatePkAndExtractPrefix(publicKey) {
173
- if (publicKey === '') {
174
- throw new core.InvalidPublicKeyError(publicKey, `can't be empty`);
175
- }
176
- const pkPrefix = publicKey.substring(0, 4);
177
- const publicKeyValidation = validatePublicKey(publicKey);
178
- if (publicKeyValidation !== exports.ValidationResult.VALID) {
179
- throw new core.InvalidPublicKeyError(publicKey, invalidDetail(publicKeyValidation));
180
- }
181
- return pkPrefix;
182
- }
183
- function validateSigAndExtractPrefix(signature) {
184
- const signaturePrefix = signature.startsWith('sig')
185
- ? signature.substring(0, 3)
186
- : signature.substring(0, 5);
187
- const validation = validateSignature(signature);
188
- if (validation !== exports.ValidationResult.VALID) {
189
- throw new core.InvalidSignatureError(signature, invalidDetail(validation));
190
- }
191
- return signaturePrefix;
192
- }
193
- function verifyEdSignature(decodedSig, bytesHash, decodedPublicKey) {
194
- try {
195
- return ed25519.verify(decodedPublicKey, bytesHash, decodedSig);
196
- }
197
- catch (e) {
198
- return false;
199
- }
200
- }
201
- function verifySpSignature(decodedSig, bytesHash, decodedPublicKey) {
202
- const key = new elliptic.ec('secp256k1').keyFromPublic(decodedPublicKey);
203
- return verifySpOrP2Sig(decodedSig, bytesHash, key);
204
- }
205
- function verifyP2Signature(decodedSig, bytesHash, decodedPublicKey) {
206
- const key = new elliptic.ec('p256').keyFromPublic(decodedPublicKey);
207
- return verifySpOrP2Sig(decodedSig, bytesHash, key);
208
- }
209
- function verifySpOrP2Sig(decodedSig, bytesHash, key) {
210
- const hexSig = buf2hex(toBuffer(decodedSig));
211
- const match = hexSig.match(/([a-f\d]{64})/gi);
212
- if (match) {
213
- try {
214
- const [r, s] = match;
215
- return key.verify(bytesHash, { r, s });
216
- }
217
- catch (e) {
218
- return false;
219
- }
220
- }
221
- return false;
222
- }
223
-
138
+ exports.PrefixV2 = void 0;
139
+ (function (PrefixV2) {
140
+ PrefixV2["BlockHash"] = "B";
141
+ PrefixV2["OperationHash"] = "o";
142
+ PrefixV2["OperationListHash"] = "Lo";
143
+ PrefixV2["OperationListListHash"] = "LLo";
144
+ PrefixV2["ProtocolHash"] = "P";
145
+ PrefixV2["ContextHash"] = "Co";
146
+ PrefixV2["BlockMetadataHash"] = "bm";
147
+ PrefixV2["OperationMetadataHash"] = "r";
148
+ PrefixV2["OperationMetadataListHash"] = "Lr";
149
+ PrefixV2["OperationMetadataListListHash"] = "LLr";
150
+ PrefixV2["Ed25519PublicKeyHash"] = "tz1";
151
+ PrefixV2["Secp256k1PublicKeyHash"] = "tz2";
152
+ PrefixV2["P256PublicKeyHash"] = "tz3";
153
+ PrefixV2["ContractHash"] = "KT1";
154
+ PrefixV2["BlindedPublicKeyHash"] = "btz1";
155
+ PrefixV2["BLS12_381PublicKeyHash"] = "tz4";
156
+ PrefixV2["TXRollupAddress"] = "txr1";
157
+ PrefixV2["ZkRollupHash"] = "epx1";
158
+ PrefixV2["ScRollupHash"] = "scr1";
159
+ PrefixV2["SmartRollupHash"] = "sr1";
160
+ PrefixV2["CryptoboxPublicKeyHash"] = "id";
161
+ PrefixV2["Ed25519Seed"] = "edsk";
162
+ PrefixV2["Ed25519PublicKey"] = "edpk";
163
+ PrefixV2["Secp256k1SecretKey"] = "spsk";
164
+ PrefixV2["P256SecretKey"] = "p2sk";
165
+ PrefixV2["BLS12_381SecretKey"] = "BLsk";
166
+ PrefixV2["ValueHash"] = "vh";
167
+ PrefixV2["CycleNonce"] = "nce";
168
+ PrefixV2["ScriptExpr"] = "expr";
169
+ PrefixV2["InboxHash"] = "txi";
170
+ PrefixV2["MessageHash"] = "txm";
171
+ PrefixV2["CommitmentHash"] = "txc";
172
+ PrefixV2["MessageResultHash"] = "txmr";
173
+ PrefixV2["MessageResultListHash"] = "txM";
174
+ PrefixV2["WithdrawListHash"] = "txw";
175
+ PrefixV2["ScRollupStateHash"] = "scs1";
176
+ PrefixV2["ScRollupCommitmentHash"] = "scc1";
177
+ PrefixV2["SmartRollupStateHash"] = "srs1";
178
+ PrefixV2["SmartRollupCommitmentHash"] = "src1";
179
+ PrefixV2["Ed25519EncryptedSeed"] = "edesk";
180
+ PrefixV2["Secp256k1EncryptedSecretKey"] = "spesk";
181
+ PrefixV2["P256EncryptedSecretKey"] = "p2esk";
182
+ PrefixV2["BLS12_381EncryptedSecretKey"] = "BLesk";
183
+ PrefixV2["Secp256k1EncryptedScalar"] = "seesk";
184
+ PrefixV2["Secp256k1PublicKey"] = "sppk";
185
+ PrefixV2["P256PublicKey"] = "p2pk";
186
+ PrefixV2["Secp256k1Scalar"] = "SSp";
187
+ PrefixV2["Secp256k1Element"] = "GSp";
188
+ PrefixV2["Ed25519SecretKey"] = "_edsk";
189
+ PrefixV2["Ed25519Signature"] = "edsig";
190
+ PrefixV2["Secp256k1Signature"] = "spsig1";
191
+ PrefixV2["P256Signature"] = "p2sig";
192
+ PrefixV2["GenericSignature"] = "sig";
193
+ PrefixV2["ChainID"] = "Net";
194
+ PrefixV2["SaplingSpendingKey"] = "sask";
195
+ PrefixV2["EncryptedSaplingSpendingKey"] = "_sask";
196
+ PrefixV2["SaplingAddress"] = "zet1";
197
+ PrefixV2["GenericAggregateSignature"] = "asig";
198
+ PrefixV2["BLS12_381Signature"] = "BLsig";
199
+ PrefixV2["BLS12_381PublicKey"] = "BLpk";
200
+ PrefixV2["SlotHeader"] = "sh";
201
+ })(exports.PrefixV2 || (exports.PrefixV2 = {}));
224
202
  /**
225
- * @category Error
226
- * @description Error that indicates invalid protocol hash being passed or used
203
+ * @description base58 prefix to bytes mapping
227
204
  */
228
- class InvalidProtocolHashError extends core.ParameterValidationError {
229
- constructor(protocolHash, errorDetails) {
230
- super();
231
- this.protocolHash = protocolHash;
232
- this.errorDetails = errorDetails;
233
- this.name = 'InvalidProtocolHashError';
234
- this.name = 'InvalidProtocolHashError';
235
- this.message = `The protocol hash '${protocolHash}' is invalid`;
236
- errorDetails ? (this.message += `: ${errorDetails}`) : null;
237
- }
238
- }
205
+ const prefixV2 = {
206
+ [exports.PrefixV2.BlockHash]: new Uint8Array([1, 52]),
207
+ [exports.PrefixV2.OperationHash]: new Uint8Array([5, 116]),
208
+ [exports.PrefixV2.OperationListHash]: new Uint8Array([133, 233]),
209
+ [exports.PrefixV2.OperationListListHash]: new Uint8Array([29, 159, 109]),
210
+ [exports.PrefixV2.ProtocolHash]: new Uint8Array([2, 170]),
211
+ [exports.PrefixV2.ContextHash]: new Uint8Array([79, 199]),
212
+ [exports.PrefixV2.BlockMetadataHash]: new Uint8Array([234, 249]),
213
+ [exports.PrefixV2.OperationMetadataHash]: new Uint8Array([5, 183]),
214
+ [exports.PrefixV2.OperationMetadataListHash]: new Uint8Array([134, 39]),
215
+ [exports.PrefixV2.OperationMetadataListListHash]: new Uint8Array([29, 159, 182]),
216
+ [exports.PrefixV2.Ed25519PublicKeyHash]: new Uint8Array([6, 161, 159]),
217
+ [exports.PrefixV2.Secp256k1PublicKeyHash]: new Uint8Array([6, 161, 161]),
218
+ [exports.PrefixV2.P256PublicKeyHash]: new Uint8Array([6, 161, 164]),
219
+ [exports.PrefixV2.ContractHash]: new Uint8Array([2, 90, 121]),
220
+ [exports.PrefixV2.BlindedPublicKeyHash]: new Uint8Array([1, 2, 49, 223]),
221
+ [exports.PrefixV2.BLS12_381PublicKeyHash]: new Uint8Array([6, 161, 166]),
222
+ [exports.PrefixV2.TXRollupAddress]: new Uint8Array([1, 128, 120, 31]),
223
+ [exports.PrefixV2.ZkRollupHash]: new Uint8Array([1, 23, 224, 125]),
224
+ [exports.PrefixV2.ScRollupHash]: new Uint8Array([1, 118, 132, 217]),
225
+ [exports.PrefixV2.SmartRollupHash]: new Uint8Array([6, 124, 117]),
226
+ [exports.PrefixV2.CryptoboxPublicKeyHash]: new Uint8Array([153, 103]),
227
+ [exports.PrefixV2.Ed25519Seed]: new Uint8Array([13, 15, 58, 7]),
228
+ [exports.PrefixV2.Ed25519PublicKey]: new Uint8Array([13, 15, 37, 217]),
229
+ [exports.PrefixV2.Secp256k1SecretKey]: new Uint8Array([17, 162, 224, 201]),
230
+ [exports.PrefixV2.P256SecretKey]: new Uint8Array([16, 81, 238, 189]),
231
+ [exports.PrefixV2.BLS12_381SecretKey]: new Uint8Array([3, 150, 192, 40]),
232
+ [exports.PrefixV2.ValueHash]: new Uint8Array([1, 106, 242]),
233
+ [exports.PrefixV2.CycleNonce]: new Uint8Array([69, 220, 169]),
234
+ [exports.PrefixV2.ScriptExpr]: new Uint8Array([13, 44, 64, 27]),
235
+ [exports.PrefixV2.InboxHash]: new Uint8Array([79, 148, 196]),
236
+ [exports.PrefixV2.MessageHash]: new Uint8Array([79, 149, 30]),
237
+ [exports.PrefixV2.CommitmentHash]: new Uint8Array([79, 148, 17]),
238
+ [exports.PrefixV2.MessageResultHash]: new Uint8Array([18, 7, 206, 87]),
239
+ [exports.PrefixV2.MessageResultListHash]: new Uint8Array([79, 146, 82]),
240
+ [exports.PrefixV2.WithdrawListHash]: new Uint8Array([79, 150, 72]),
241
+ [exports.PrefixV2.ScRollupStateHash]: new Uint8Array([17, 144, 122, 202]),
242
+ [exports.PrefixV2.ScRollupCommitmentHash]: new Uint8Array([17, 144, 21, 100]),
243
+ [exports.PrefixV2.SmartRollupStateHash]: new Uint8Array([17, 165, 235, 240]),
244
+ [exports.PrefixV2.SmartRollupCommitmentHash]: new Uint8Array([17, 165, 134, 138]),
245
+ [exports.PrefixV2.Ed25519EncryptedSeed]: new Uint8Array([7, 90, 60, 179, 41]),
246
+ [exports.PrefixV2.Secp256k1EncryptedSecretKey]: new Uint8Array([9, 237, 241, 174, 150]),
247
+ [exports.PrefixV2.P256EncryptedSecretKey]: new Uint8Array([9, 48, 57, 115, 171]),
248
+ [exports.PrefixV2.BLS12_381EncryptedSecretKey]: new Uint8Array([2, 5, 30, 53, 25]),
249
+ [exports.PrefixV2.Secp256k1EncryptedScalar]: new Uint8Array([1, 131, 36, 86, 248]),
250
+ [exports.PrefixV2.Secp256k1PublicKey]: new Uint8Array([3, 254, 226, 86]),
251
+ [exports.PrefixV2.P256PublicKey]: new Uint8Array([3, 178, 139, 127]),
252
+ [exports.PrefixV2.Secp256k1Scalar]: new Uint8Array([38, 248, 136]),
253
+ [exports.PrefixV2.Secp256k1Element]: new Uint8Array([5, 92, 0]),
254
+ [exports.PrefixV2.Ed25519SecretKey]: new Uint8Array([43, 246, 78, 7]),
255
+ [exports.PrefixV2.Ed25519Signature]: new Uint8Array([9, 245, 205, 134, 18]),
256
+ [exports.PrefixV2.Secp256k1Signature]: new Uint8Array([13, 115, 101, 19, 63]),
257
+ [exports.PrefixV2.P256Signature]: new Uint8Array([54, 240, 44, 52]),
258
+ [exports.PrefixV2.GenericSignature]: new Uint8Array([4, 130, 43]),
259
+ [exports.PrefixV2.ChainID]: new Uint8Array([87, 82, 0]),
260
+ [exports.PrefixV2.SaplingSpendingKey]: new Uint8Array([11, 237, 20, 92]),
261
+ [exports.PrefixV2.EncryptedSaplingSpendingKey]: new Uint8Array([11, 237, 20, 92]),
262
+ [exports.PrefixV2.SaplingAddress]: new Uint8Array([18, 71, 40, 223]),
263
+ [exports.PrefixV2.GenericAggregateSignature]: new Uint8Array([2, 75, 234, 101]),
264
+ [exports.PrefixV2.BLS12_381Signature]: new Uint8Array([40, 171, 64, 207]),
265
+ [exports.PrefixV2.BLS12_381PublicKey]: new Uint8Array([6, 149, 135, 204]),
266
+ [exports.PrefixV2.SlotHeader]: new Uint8Array([2, 116, 180]),
267
+ };
239
268
  /**
240
- * @category Error
241
- * @description Error that indicates unable to convert data type from one to another
269
+ * @description base58 prefix to payload length mapping
242
270
  */
243
- class ValueConversionError extends core.UnsupportedActionError {
244
- constructor(value, desiredType) {
245
- super();
246
- this.value = value;
247
- this.desiredType = desiredType;
248
- this.name = 'ValueConversionError';
249
- this.message = `Unable to convert ${value} to a ${desiredType}`;
250
- }
251
- }
271
+ const payloadLength = {
272
+ [exports.PrefixV2.BlockHash]: 32,
273
+ [exports.PrefixV2.OperationHash]: 32,
274
+ [exports.PrefixV2.OperationListHash]: 32,
275
+ [exports.PrefixV2.OperationListListHash]: 32,
276
+ [exports.PrefixV2.ProtocolHash]: 32,
277
+ [exports.PrefixV2.ContextHash]: 32,
278
+ [exports.PrefixV2.BlockMetadataHash]: 32,
279
+ [exports.PrefixV2.OperationMetadataHash]: 32,
280
+ [exports.PrefixV2.OperationMetadataListHash]: 32,
281
+ [exports.PrefixV2.OperationMetadataListListHash]: 32,
282
+ [exports.PrefixV2.Ed25519PublicKeyHash]: 20,
283
+ [exports.PrefixV2.Secp256k1PublicKeyHash]: 20,
284
+ [exports.PrefixV2.P256PublicKeyHash]: 20,
285
+ [exports.PrefixV2.ContractHash]: 20,
286
+ [exports.PrefixV2.BlindedPublicKeyHash]: 20,
287
+ [exports.PrefixV2.BLS12_381PublicKeyHash]: 20,
288
+ [exports.PrefixV2.TXRollupAddress]: 20,
289
+ [exports.PrefixV2.ZkRollupHash]: 20,
290
+ [exports.PrefixV2.ScRollupHash]: 20,
291
+ [exports.PrefixV2.SmartRollupHash]: 20,
292
+ [exports.PrefixV2.CryptoboxPublicKeyHash]: 16,
293
+ [exports.PrefixV2.Ed25519Seed]: 32,
294
+ [exports.PrefixV2.Ed25519PublicKey]: 32,
295
+ [exports.PrefixV2.Secp256k1SecretKey]: 32,
296
+ [exports.PrefixV2.P256SecretKey]: 32,
297
+ [exports.PrefixV2.BLS12_381SecretKey]: 32,
298
+ [exports.PrefixV2.ValueHash]: 32,
299
+ [exports.PrefixV2.CycleNonce]: 32,
300
+ [exports.PrefixV2.ScriptExpr]: 32,
301
+ [exports.PrefixV2.InboxHash]: 32,
302
+ [exports.PrefixV2.MessageHash]: 32,
303
+ [exports.PrefixV2.CommitmentHash]: 32,
304
+ [exports.PrefixV2.MessageResultHash]: 32,
305
+ [exports.PrefixV2.MessageResultListHash]: 32,
306
+ [exports.PrefixV2.WithdrawListHash]: 32,
307
+ [exports.PrefixV2.ScRollupStateHash]: 32,
308
+ [exports.PrefixV2.ScRollupCommitmentHash]: 32,
309
+ [exports.PrefixV2.SmartRollupStateHash]: 32,
310
+ [exports.PrefixV2.SmartRollupCommitmentHash]: 32,
311
+ [exports.PrefixV2.Ed25519EncryptedSeed]: 56,
312
+ [exports.PrefixV2.Secp256k1EncryptedSecretKey]: 56,
313
+ [exports.PrefixV2.P256EncryptedSecretKey]: 56,
314
+ [exports.PrefixV2.BLS12_381EncryptedSecretKey]: 56,
315
+ [exports.PrefixV2.Secp256k1EncryptedScalar]: 60,
316
+ [exports.PrefixV2.Secp256k1PublicKey]: 33,
317
+ [exports.PrefixV2.P256PublicKey]: 33,
318
+ [exports.PrefixV2.Secp256k1Scalar]: 33,
319
+ [exports.PrefixV2.Secp256k1Element]: 33,
320
+ [exports.PrefixV2.Ed25519SecretKey]: 64,
321
+ [exports.PrefixV2.Ed25519Signature]: 64,
322
+ [exports.PrefixV2.Secp256k1Signature]: 64,
323
+ [exports.PrefixV2.P256Signature]: 64,
324
+ [exports.PrefixV2.GenericSignature]: 64,
325
+ [exports.PrefixV2.ChainID]: 4,
326
+ [exports.PrefixV2.SaplingSpendingKey]: 169,
327
+ [exports.PrefixV2.EncryptedSaplingSpendingKey]: 193,
328
+ [exports.PrefixV2.SaplingAddress]: 43,
329
+ [exports.PrefixV2.GenericAggregateSignature]: 96,
330
+ [exports.PrefixV2.BLS12_381Signature]: 96,
331
+ [exports.PrefixV2.BLS12_381PublicKey]: 48,
332
+ [exports.PrefixV2.SlotHeader]: 48,
333
+ };
252
334
 
253
- exports.ValidationResult = void 0;
254
- (function (ValidationResult) {
255
- ValidationResult[ValidationResult["NO_PREFIX_MATCHED"] = 0] = "NO_PREFIX_MATCHED";
256
- ValidationResult[ValidationResult["INVALID_CHECKSUM"] = 1] = "INVALID_CHECKSUM";
257
- ValidationResult[ValidationResult["INVALID_LENGTH"] = 2] = "INVALID_LENGTH";
258
- ValidationResult[ValidationResult["VALID"] = 3] = "VALID";
259
- })(exports.ValidationResult || (exports.ValidationResult = {}));
260
- function isValidPrefix(value) {
261
- if (typeof value !== 'string') {
262
- return false;
263
- }
264
- return value in prefix;
335
+ /**
336
+ * @description Used to check if a value has one of the allowed prefixes.
337
+ * @returns true if the value has one of the allowed prefixes, false otherwise
338
+ * @example
339
+ * ```
340
+ * import { isValidPrefixedValue } from '@taquito/utils';
341
+ * const value = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx'
342
+ * const isValid = isValidPrefixedValue(value, [PrefixV2.Ed25519PublicKeyHash])
343
+ * console.log(isValid) // true
344
+ * ```
345
+ */
346
+ function isValidPrefixedValue(value, prefixes) {
347
+ return validatePrefixedValue(value, prefixes) === core.ValidationResult.VALID;
265
348
  }
266
349
  /**
267
350
  * @description This function is called by the validation functions ([[validateAddress]], [[validateChain]], [[validateContractAddress]], [[validateKeyHash]], [[validateSignature]], [[validatePublicKey]]).
268
- * Verify if the value has the right prefix or return `NO_PREFIX_MATCHED`,
269
- * decode the value using base58 and return `INVALID_CHECKSUM` if it fails,
270
- * check if the length of the value matches the prefix type or return `INVALID_LENGTH`.
271
- * If all checks pass, return `VALID`.
272
- *
351
+ * 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)
352
+ * If all checks pass, return `3` (VALID).
273
353
  * @param value Value to validate
274
354
  * @param prefixes prefix the value should have
355
+ * @returns 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
356
+ * @example
357
+ * ```
358
+ * import { validatePrefixedValue } from '@taquito/utils';
359
+ * const value = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx'
360
+ * const validation = validatePrefixedValue(value, [PrefixV2.Ed25519PublicKeyHash])
361
+ * console.log(validation) // 3
362
+ * ```
275
363
  */
276
364
  function validatePrefixedValue(value, prefixes) {
277
- const match = new RegExp(`^(${prefixes.join('|')})`).exec(value);
278
- if (!match || match.length === 0) {
279
- return exports.ValidationResult.NO_PREFIX_MATCHED;
280
- }
281
- const prefixKey = match[0];
282
- if (!isValidPrefix(prefixKey)) {
283
- return exports.ValidationResult.NO_PREFIX_MATCHED;
284
- }
285
- // Check whether annotation exist before starting validation
286
- if (value.includes('%')) {
287
- value = value.split('%')[0];
288
- }
289
- const kt1Regex = /^(KT1\w{33})$/;
290
- if (!kt1Regex.test(value) && prefixKey === 'KT1') {
291
- return exports.ValidationResult.INVALID_CHECKSUM;
292
- }
293
- // decodeUnsafe return undefined if decoding fail
294
- let decoded = bs58check.decodeUnsafe(value);
295
- if (!decoded) {
296
- return exports.ValidationResult.INVALID_CHECKSUM;
365
+ try {
366
+ b58DecodeAndCheckPrefix(value, prefixes);
297
367
  }
298
- decoded = decoded.slice(prefix[prefixKey].length);
299
- if (decoded.length !== prefixLength[prefixKey]) {
300
- return exports.ValidationResult.INVALID_LENGTH;
368
+ catch (err) {
369
+ if (err instanceof core.ParameterValidationError && err.result !== undefined) {
370
+ return err.result;
371
+ }
372
+ return core.ValidationResult.OTHER;
301
373
  }
302
- return exports.ValidationResult.VALID;
374
+ return core.ValidationResult.VALID;
303
375
  }
304
- const implicitPrefix = [exports.Prefix.TZ1, exports.Prefix.TZ2, exports.Prefix.TZ3, exports.Prefix.TZ4];
305
- const contractPrefix = [exports.Prefix.KT1];
306
- const signaturePrefix = [exports.Prefix.EDSIG, exports.Prefix.P2SIG, exports.Prefix.SPSIG, exports.Prefix.SIG];
307
- const pkPrefix = [exports.Prefix.EDPK, exports.Prefix.SPPK, exports.Prefix.P2PK, exports.Prefix.BLPK];
308
- const operationPrefix = [exports.Prefix.O];
309
- const protocolPrefix = [exports.Prefix.P];
310
- const blockPrefix = [exports.Prefix.B];
311
- const smartRollupPrefix = [exports.Prefix.SR1];
312
376
  /**
313
377
  * @description Used to check if an address or a contract address is valid.
314
378
  *
315
379
  * @returns
316
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
380
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
317
381
  *
318
382
  * @example
319
383
  * ```
@@ -325,13 +389,14 @@
325
389
  * ```
326
390
  */
327
391
  function validateAddress(value) {
328
- return validatePrefixedValue(value, [...implicitPrefix, ...contractPrefix, ...smartRollupPrefix]);
392
+ const [addr] = splitAddress(value !== null && value !== void 0 ? value : '');
393
+ return validatePrefixedValue(addr, addressPrefixes);
329
394
  }
330
395
  /**
331
396
  * @description Used to check if a chain id is valid.
332
397
  *
333
398
  * @returns
334
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
399
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
335
400
  *
336
401
  * @example
337
402
  * ```
@@ -343,13 +408,13 @@
343
408
  * ```
344
409
  */
345
410
  function validateChain(value) {
346
- return validatePrefixedValue(value, [exports.Prefix.NET]);
411
+ return validatePrefixedValue(value, [exports.PrefixV2.ChainID]);
347
412
  }
348
413
  /**
349
414
  * @description Used to check if a contract address is valid.
350
415
  *
351
416
  * @returns
352
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
417
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
353
418
  *
354
419
  * @example
355
420
  * ```
@@ -361,13 +426,13 @@
361
426
  * ```
362
427
  */
363
428
  function validateContractAddress(value) {
364
- return validatePrefixedValue(value, contractPrefix);
429
+ return validatePrefixedValue(value, [exports.PrefixV2.ContractHash]);
365
430
  }
366
431
  /**
367
432
  * @description Used to check if a key hash is valid.
368
433
  *
369
434
  * @returns
370
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
435
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
371
436
  *
372
437
  * @example
373
438
  * ```
@@ -379,13 +444,13 @@
379
444
  * ```
380
445
  */
381
446
  function validateKeyHash(value) {
382
- return validatePrefixedValue(value, implicitPrefix);
447
+ return validatePrefixedValue(value, publicKeyHashPrefixes);
383
448
  }
384
449
  /**
385
450
  * @description Used to check if a signature is valid.
386
451
  *
387
452
  * @returns
388
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
453
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
389
454
  *
390
455
  * @example
391
456
  * ```
@@ -397,13 +462,13 @@
397
462
  * ```
398
463
  */
399
464
  function validateSignature(value) {
400
- return validatePrefixedValue(value, signaturePrefix);
465
+ return validatePrefixedValue(value, signaturePrefixes);
401
466
  }
402
467
  /**
403
468
  * @description Used to check if a public key is valid.
404
469
  *
405
470
  * @returns
406
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
471
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
407
472
  *
408
473
  * @example
409
474
  * ```
@@ -415,13 +480,13 @@
415
480
  * ```
416
481
  */
417
482
  function validatePublicKey(value) {
418
- return validatePrefixedValue(value, pkPrefix);
483
+ return validatePrefixedValue(value, publicKeyPrefixes);
419
484
  }
420
485
  /**
421
486
  * @description Used to check if an operation hash is valid.
422
487
  *
423
488
  * @returns
424
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
489
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
425
490
  *
426
491
  * @example
427
492
  * ```
@@ -433,13 +498,13 @@
433
498
  * ```
434
499
  */
435
500
  function validateOperation(value) {
436
- return validatePrefixedValue(value, operationPrefix);
501
+ return validatePrefixedValue(value, [exports.PrefixV2.OperationHash]);
437
502
  }
438
503
  /**
439
504
  * @description Used to check if a protocol hash is valid.
440
505
  *
441
506
  * @returns
442
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
507
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
443
508
  *
444
509
  * @example
445
510
  * ```
@@ -451,13 +516,13 @@
451
516
  * ```
452
517
  */
453
518
  function validateProtocol(value) {
454
- return validatePrefixedValue(value, protocolPrefix);
519
+ return validatePrefixedValue(value, [exports.PrefixV2.ProtocolHash]);
455
520
  }
456
521
  /**
457
522
  * @description Used to check if a block hash is valid.
458
523
  *
459
524
  * @returns
460
- * 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
525
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
461
526
  *
462
527
  * @example
463
528
  * ```
@@ -469,38 +534,209 @@
469
534
  * ```
470
535
  */
471
536
  function validateBlock(value) {
472
- return validatePrefixedValue(value, blockPrefix);
537
+ return validatePrefixedValue(value, [exports.PrefixV2.BlockHash]);
473
538
  }
474
539
  /**
475
- * @description Used to check if a spending key is valid.
476
- * @returns 0 (NO_PREFIX_MATCHED), 1 (INVALID_CHECKSUM), 2 (INVALID_LENGTH) or 3 (VALID).
477
- *
540
+ * @deprecated this function will be removed in the next minor release
541
+ * @description generates a readable error string from a validation result
478
542
  */
479
- function validateSpendingKey(value) {
480
- return validatePrefixedValue(value, [exports.Prefix.SASK]);
481
- }
482
543
  function invalidDetail(validation) {
483
544
  switch (validation) {
484
- case exports.ValidationResult.NO_PREFIX_MATCHED:
545
+ case core.ValidationResult.NO_PREFIX_MATCHED:
485
546
  return 'with unsupported prefix';
486
- case exports.ValidationResult.INVALID_CHECKSUM:
547
+ case core.ValidationResult.INVALID_CHECKSUM:
487
548
  return 'failed checksum';
488
- case exports.ValidationResult.INVALID_LENGTH:
549
+ case core.ValidationResult.INVALID_LENGTH:
489
550
  return 'with incorrect length';
490
551
  default:
491
552
  return '';
492
553
  }
493
554
  }
555
+ /**
556
+ * @description Used to check if a spending key is valid.
557
+ * @returns
558
+ * 0 = NO_PREFIX_MATCHED, 1 = INVALID_CHECKSUM, 2 = INVALID_LENGTH, 3 = VALID, 4 = PREFIX_NOT_ALLOWED, 5 = INVALID_ENCODING, 6 = OTHER,
559
+ *
560
+ */
561
+ function validateSpendingKey(value) {
562
+ return validatePrefixedValue(value, [exports.PrefixV2.SaplingSpendingKey]);
563
+ }
494
564
  function validateSmartRollupAddress(value) {
495
- return validatePrefixedValue(value, [...smartRollupPrefix]);
565
+ return validatePrefixedValue(value, [exports.PrefixV2.SmartRollupHash]);
496
566
  }
497
567
 
498
568
  // IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
499
569
  const VERSION = {
500
- "commitHash": "7af2138a9e5c5b230c4b4c726f35c2f2e67b721c",
501
- "version": "23.0.0-beta.0"
570
+ "commitHash": "10b3de10de15ae68d47b1fca922d3129d2f79641",
571
+ "version": "23.0.0-beta.1"
502
572
  };
503
573
 
574
+ const BLS12_381_DST = 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_';
575
+ const POP_DST = 'BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_';
576
+ /**
577
+ * @description Verify signature of a payload
578
+ * @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
579
+ * @param publicKey The public key to verify the signature against
580
+ * @param signature The signature to verify
581
+ * @param watermark Optional if not included in the message
582
+ * @param pop Optional if verifying proof of possession signature
583
+ * @returns A boolean indicating if the signature matches
584
+ * @throws {@link InvalidPublicKeyError} | {@link InvalidSignatureError} | {@link InvalidMessageError}
585
+ * @example
586
+ * ```
587
+ * const message = '03d0c10e3ed11d7c6e3357f6ef335bab9e8f2bd54d0ce20c482e241191a6e4b8ce6c01be917311d9ac46959750e405d57e268e2ed9e174a80794fbd504e12a4a000141eb3781afed2f69679ff2bbe1c5375950b0e40d00ff000000005e05050505050507070100000024747a32526773486e74516b72794670707352466261313652546656503539684b72654a4d07070100000024747a315a6672455263414c42776d4171776f6e525859565142445439426a4e6a42484a750001';
588
+ * const pk = 'sppk7c7hkPj47yjYFEHX85q46sFJGw6RBrqoVSHwAJAT4e14KJwzoey';
589
+ * const sig = 'spsig1cdLkp1RLgUHAp13aRFkZ6MQDPp7xCnjAExGL3MBSdMDmT6JgQSX8cufyDgJRM3sinFtiCzLbsyP6d365EHoNevxhT47nx'
590
+ *
591
+ * const response = verifySignature(message, pk, sig);
592
+ * ```
593
+ *
594
+ */
595
+ function verifySignature(message, publicKey, signature, watermark, pop) {
596
+ const [pk, pre] = (() => {
597
+ try {
598
+ return b58DecodeAndCheckPrefix(publicKey, publicKeyPrefixes);
599
+ }
600
+ catch (err) {
601
+ if (err instanceof core.ParameterValidationError) {
602
+ throw new core.InvalidPublicKeyError(publicKey, err.result);
603
+ }
604
+ else {
605
+ throw err;
606
+ }
607
+ }
608
+ })();
609
+ const sig = (() => {
610
+ try {
611
+ const [sig] = b58DecodeAndCheckPrefix(signature, signaturePrefixes);
612
+ return sig;
613
+ }
614
+ catch (err) {
615
+ if (err instanceof core.ParameterValidationError) {
616
+ throw new core.InvalidSignatureError(signature, err.result);
617
+ }
618
+ else {
619
+ throw err;
620
+ }
621
+ }
622
+ })();
623
+ let msg;
624
+ if (typeof message === 'string') {
625
+ msg = hex2buf(message);
626
+ }
627
+ else {
628
+ msg = message;
629
+ }
630
+ if (msg.length === 0) {
631
+ throw new core.InvalidMessageError(buf2hex(msg), `can't be empty`);
632
+ }
633
+ if (typeof watermark !== 'undefined') {
634
+ msg = mergebuf(watermark, msg);
635
+ }
636
+ if (pop) {
637
+ return verifyBLSPopSignature(sig, msg, pk);
638
+ }
639
+ else {
640
+ switch (pre) {
641
+ case exports.PrefixV2.P256PublicKey:
642
+ return verifyP2Signature(sig, msg, pk);
643
+ case exports.PrefixV2.Secp256k1PublicKey:
644
+ return verifySpSignature(sig, msg, pk);
645
+ case exports.PrefixV2.Ed25519PublicKey:
646
+ return verifyEdSignature(sig, msg, pk);
647
+ default:
648
+ return verifyBLSSignature(sig, msg, pk);
649
+ }
650
+ }
651
+ }
652
+ /**
653
+ * @deprecated use b58DecodeAndCheckPrefix instead, this function will be removed in the next minor release
654
+ * @description validates a public key and extracts the prefix
655
+ */
656
+ function validatePkAndExtractPrefix(publicKey) {
657
+ if (publicKey === '') {
658
+ throw new core.InvalidPublicKeyError(publicKey, `can't be empty`);
659
+ }
660
+ const pkPrefix = publicKey.substring(0, 4);
661
+ const publicKeyValidation = validatePublicKey(publicKey);
662
+ if (publicKeyValidation !== core.ValidationResult.VALID) {
663
+ throw new core.InvalidPublicKeyError(publicKey, invalidDetail(publicKeyValidation));
664
+ }
665
+ return pkPrefix;
666
+ }
667
+ function verifyEdSignature(sig, msg, publicKey) {
668
+ const hash = blake2b.hash(msg, 32);
669
+ try {
670
+ return ed25519.verify(publicKey, hash, sig);
671
+ }
672
+ catch (_a) {
673
+ return false;
674
+ }
675
+ }
676
+ function verifySpSignature(sig, msg, publicKey) {
677
+ const key = new elliptic.ec('secp256k1').keyFromPublic(publicKey);
678
+ return verifySpOrP2Sig(sig, msg, key);
679
+ }
680
+ function verifyP2Signature(sig, msg, publicKey) {
681
+ const key = new elliptic.ec('p256').keyFromPublic(publicKey);
682
+ return verifySpOrP2Sig(sig, msg, key);
683
+ }
684
+ function verifySpOrP2Sig(sig, msg, key) {
685
+ const r = sig.slice(0, 32);
686
+ const s = sig.slice(32);
687
+ const hash = blake2b.hash(msg, 32);
688
+ try {
689
+ return key.verify(hash, { r, s });
690
+ }
691
+ catch (_a) {
692
+ return false;
693
+ }
694
+ }
695
+ const bls = bls12381.bls12_381.longSignatures; // AKA MinPK
696
+ function verifyBLSSignature(sig, msg, publicKey) {
697
+ try {
698
+ const point = bls.hash(msg, BLS12_381_DST);
699
+ return bls.verify(sig, point, publicKey);
700
+ }
701
+ catch (_a) {
702
+ return false;
703
+ }
704
+ }
705
+ function verifyBLSPopSignature(sig, msg, publicKey) {
706
+ try {
707
+ const point = bls.hash(msg, POP_DST);
708
+ return bls.verify(sig, point, publicKey);
709
+ }
710
+ catch (_a) {
711
+ return false;
712
+ }
713
+ }
714
+
715
+ /**
716
+ * @category Error
717
+ * @description Error that indicates invalid protocol hash being passed or used
718
+ */
719
+ class InvalidProtocolHashError extends core.ParameterValidationError {
720
+ constructor(protocolHash, errorDetails) {
721
+ super(`The protocol hash '${protocolHash}' is invalid`, errorDetails);
722
+ this.protocolHash = protocolHash;
723
+ this.name = 'InvalidProtocolHashError';
724
+ this.name = this.constructor.name;
725
+ }
726
+ }
727
+ /**
728
+ * @category Error
729
+ * @description Error that indicates unable to convert data type from one to another
730
+ */
731
+ class ValueConversionError extends core.UnsupportedActionError {
732
+ constructor(value, desiredType) {
733
+ super(`Unable to convert ${value} to a ${desiredType}`);
734
+ this.value = value;
735
+ this.desiredType = desiredType;
736
+ this.name = this.constructor.name;
737
+ }
738
+ }
739
+
504
740
  const TZ_DECIMALS = 6;
505
741
  const MTZ_DECIMALS = 3;
506
742
  function getDecimal(format) {
@@ -534,222 +770,421 @@
534
770
  * Copyright (c) 2017 Stephen Andrews
535
771
  */
536
772
  /**
537
- *
538
- * @description Hash a string using the BLAKE2b algorithm, base58 encode the hash obtained and appends the prefix 'expr' to it
539
- *
540
- * @param value Value in hex
773
+ * @description list of prefixes that can be used to decode an address
541
774
  */
542
- function encodeExpr(value) {
543
- const blakeHash = blake.blake2b(hex2buf(value), undefined, 32);
544
- return b58cencode(blakeHash, prefix['expr']);
545
- }
775
+ const addressPrefixes = [
776
+ exports.PrefixV2.P256PublicKeyHash,
777
+ exports.PrefixV2.Secp256k1PublicKeyHash,
778
+ exports.PrefixV2.Ed25519PublicKeyHash,
779
+ exports.PrefixV2.BLS12_381PublicKeyHash,
780
+ exports.PrefixV2.ContractHash,
781
+ exports.PrefixV2.SmartRollupHash,
782
+ // PrefixV2.ZkRollupHash,
783
+ ];
546
784
  /**
547
- *
548
- * @description Return the operation hash of a signed operation
549
- * @param value Value in hex of a signed operation
785
+ * @description list of prefixes that can be used to decode a public key
550
786
  */
551
- function encodeOpHash(value) {
552
- const blakeHash = blake.blake2b(hex2buf(value), undefined, 32);
553
- return b58cencode(blakeHash, prefix.o);
787
+ const publicKeyPrefixes = [
788
+ exports.PrefixV2.P256PublicKey,
789
+ exports.PrefixV2.Secp256k1PublicKey,
790
+ exports.PrefixV2.Ed25519PublicKey,
791
+ exports.PrefixV2.BLS12_381PublicKey,
792
+ ];
793
+ /**
794
+ * @description list of prefixes that can be used to decode a public key hash
795
+ */
796
+ const publicKeyHashPrefixes = [
797
+ exports.PrefixV2.P256PublicKeyHash,
798
+ exports.PrefixV2.Secp256k1PublicKeyHash,
799
+ exports.PrefixV2.Ed25519PublicKeyHash,
800
+ exports.PrefixV2.BLS12_381PublicKeyHash,
801
+ ];
802
+ /**
803
+ * @description list of prefixes that can be used to decode a signature
804
+ */
805
+ const signaturePrefixes = [
806
+ exports.PrefixV2.P256Signature,
807
+ exports.PrefixV2.Secp256k1Signature,
808
+ exports.PrefixV2.Ed25519Signature,
809
+ exports.PrefixV2.BLS12_381Signature,
810
+ exports.PrefixV2.GenericSignature,
811
+ ];
812
+ function b58DecodeAndCheckPrefix(src, allowed, payloadOnly) {
813
+ const buf = (() => {
814
+ try {
815
+ return bs58check.decode(src);
816
+ }
817
+ catch (err) {
818
+ if (err instanceof Error) {
819
+ if (err.message.includes('checksum')) {
820
+ throw new core.ParameterValidationError(core.ValidationResult.INVALID_CHECKSUM);
821
+ }
822
+ else {
823
+ throw new core.ParameterValidationError(core.ValidationResult.INVALID_ENCODING);
824
+ }
825
+ }
826
+ else {
827
+ throw err;
828
+ }
829
+ }
830
+ })();
831
+ let key;
832
+ for (key in exports.PrefixV2) {
833
+ const p = exports.PrefixV2[key];
834
+ const pre = prefixV2[p];
835
+ if (buf.length === pre.length + payloadLength[p] &&
836
+ buf.slice(0, pre.length).every((v, i) => v == pre[i])) {
837
+ if (allowed !== undefined && allowed.indexOf(p) < 0) {
838
+ throw new core.ParameterValidationError(core.ValidationResult.PREFIX_NOT_ALLOWED);
839
+ }
840
+ if (payloadOnly) {
841
+ return buf.slice(pre.length);
842
+ }
843
+ else {
844
+ return [buf.slice(pre.length), p];
845
+ }
846
+ }
847
+ }
848
+ throw new core.ParameterValidationError(core.ValidationResult.NO_PREFIX_MATCHED);
849
+ }
850
+ function b58DecodePublicKey(value, fmt) {
851
+ const [data, pre] = b58DecodeAndCheckPrefix(value, publicKeyPrefixes);
852
+ let tag;
853
+ switch (pre) {
854
+ case exports.PrefixV2.Ed25519PublicKey:
855
+ tag = 0;
856
+ break;
857
+ case exports.PrefixV2.Secp256k1PublicKey:
858
+ tag = 1;
859
+ break;
860
+ case exports.PrefixV2.P256PublicKey:
861
+ tag = 2;
862
+ break;
863
+ case exports.PrefixV2.BLS12_381PublicKey:
864
+ tag = 3;
865
+ break;
866
+ default:
867
+ throw new core.InvalidKeyError(core.ValidationResult.NO_PREFIX_MATCHED);
868
+ }
869
+ const buf = new Uint8Array(data.length + 1);
870
+ buf[0] = tag;
871
+ buf.set(data, 1);
872
+ if (fmt !== undefined && fmt === 'array') {
873
+ return buf;
874
+ }
875
+ else {
876
+ return buf2hex(buf);
877
+ }
878
+ }
879
+ function b58DecodePublicKeyHash(value, fmt) {
880
+ const [data, pre] = b58DecodeAndCheckPrefix(value, publicKeyHashPrefixes);
881
+ const buf = new Uint8Array(21);
882
+ let tag;
883
+ switch (pre) {
884
+ case exports.PrefixV2.Ed25519PublicKeyHash:
885
+ tag = 0;
886
+ break;
887
+ case exports.PrefixV2.Secp256k1PublicKeyHash:
888
+ tag = 1;
889
+ break;
890
+ case exports.PrefixV2.P256PublicKeyHash:
891
+ tag = 2;
892
+ break;
893
+ case exports.PrefixV2.BLS12_381PublicKeyHash:
894
+ tag = 3;
895
+ break;
896
+ default:
897
+ throw new core.InvalidAddressError(value, core.ValidationResult.NO_PREFIX_MATCHED);
898
+ }
899
+ buf[0] = tag;
900
+ buf.set(data, 1);
901
+ if (fmt !== undefined && fmt === 'array') {
902
+ return buf;
903
+ }
904
+ else {
905
+ return buf2hex(buf);
906
+ }
907
+ }
908
+ function b58DecodeBlsAddress(value, fmt) {
909
+ const [buf, pre] = b58DecodeAndCheckPrefix(value);
910
+ if (pre !== exports.PrefixV2.BLS12_381PublicKeyHash) {
911
+ throw new core.InvalidKeyError(core.ValidationResult.NO_PREFIX_MATCHED);
912
+ }
913
+ if (fmt !== undefined && fmt === 'array') {
914
+ return buf;
915
+ }
916
+ else {
917
+ return buf2hex(buf);
918
+ }
919
+ }
920
+ function b58DecodeAddress(value, fmt) {
921
+ const i = value.indexOf('%');
922
+ if (i >= 0) {
923
+ value = value.slice(0, i);
924
+ }
925
+ const [data, pre] = b58DecodeAndCheckPrefix(value, addressPrefixes);
926
+ const buf = new Uint8Array(22);
927
+ if (pre === exports.PrefixV2.ContractHash ||
928
+ pre === exports.PrefixV2.SmartRollupHash ||
929
+ pre === exports.PrefixV2.ZkRollupHash) {
930
+ let tag;
931
+ switch (pre) {
932
+ case exports.PrefixV2.ContractHash:
933
+ tag = 1;
934
+ break;
935
+ case exports.PrefixV2.SmartRollupHash:
936
+ tag = 3;
937
+ break;
938
+ case exports.PrefixV2.ZkRollupHash:
939
+ tag = 4;
940
+ break;
941
+ }
942
+ buf[0] = tag;
943
+ buf.set(data, 1);
944
+ }
945
+ else {
946
+ let tag;
947
+ switch (pre) {
948
+ case exports.PrefixV2.Ed25519PublicKeyHash:
949
+ tag = 0;
950
+ break;
951
+ case exports.PrefixV2.Secp256k1PublicKeyHash:
952
+ tag = 1;
953
+ break;
954
+ case exports.PrefixV2.P256PublicKeyHash:
955
+ tag = 2;
956
+ break;
957
+ case exports.PrefixV2.BLS12_381PublicKeyHash:
958
+ tag = 3;
959
+ break;
960
+ default:
961
+ throw new core.InvalidAddressError(value, core.ValidationResult.NO_PREFIX_MATCHED);
962
+ }
963
+ buf[0] = 0;
964
+ buf[1] = tag;
965
+ buf.set(data, 2);
966
+ }
967
+ if (fmt !== undefined && fmt === 'array') {
968
+ return buf;
969
+ }
970
+ else {
971
+ return buf2hex(buf);
972
+ }
554
973
  }
555
974
  /**
556
- *
557
- * @description Base58 encode a string or a Uint8Array and append a prefix to it
558
- *
559
- * @param value Value to base58 encode
560
- * @param prefix prefix to append to the encoded string
975
+ * @description Gets Tezos address (PKH) from Public Key
976
+ * @param publicKey Base58 Public Key
977
+ * @returns A string of the Tezos address (PKH) that was derived from the given Public Key
978
+ * @example getPkhfromPk('edpkuNjKKT48xBoT5asPrWdmuM1Yw8D93MwgFgVvtca8jb5pstzaCh') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe'
561
979
  */
562
- function b58cencode(value, prefix) {
563
- const payloadAr = typeof value === 'string' ? Uint8Array.from(buffer.Buffer.from(value, 'hex')) : value;
564
- const n = new Uint8Array(prefix.length + payloadAr.length);
565
- n.set(prefix);
566
- n.set(payloadAr, prefix.length);
567
- return bs58check.encode(buffer.Buffer.from(n.buffer));
980
+ function getPkhfromPk(publicKey) {
981
+ const [key, pre] = b58DecodeAndCheckPrefix(publicKey);
982
+ let pkhPre;
983
+ switch (pre) {
984
+ case exports.PrefixV2.P256PublicKey:
985
+ pkhPre = exports.PrefixV2.P256PublicKeyHash;
986
+ break;
987
+ case exports.PrefixV2.Secp256k1PublicKey:
988
+ pkhPre = exports.PrefixV2.Secp256k1PublicKeyHash;
989
+ break;
990
+ case exports.PrefixV2.Ed25519PublicKey:
991
+ pkhPre = exports.PrefixV2.Ed25519PublicKeyHash;
992
+ break;
993
+ case exports.PrefixV2.BLS12_381PublicKey:
994
+ pkhPre = exports.PrefixV2.BLS12_381PublicKeyHash;
995
+ break;
996
+ default:
997
+ throw new core.InvalidPublicKeyError(publicKey, core.ValidationResult.NO_PREFIX_MATCHED);
998
+ }
999
+ const hashed = blake2b.hash(key, 20);
1000
+ return b58Encode(hashed, pkhPre);
568
1001
  }
569
1002
  /**
570
- *
571
- * @description Base58 decode a string and remove the prefix from it
572
- *
573
- * @param value Value to base58 decode
574
- * @param prefix prefix to remove from the decoded string
1003
+ * @description Add the prefix to a hex string or Uint8Array and Base58 encode it
1004
+ * @param value Value to Base58 encode
1005
+ * @param pre prefix ID to append to the encoded string
1006
+ * @example b58Encode('e96b9f8b19af9c7ffa0c0480e1977b295850961f', PrefixV2.Ed25519PublicKeyHash) // returns 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM'
575
1007
  */
576
- const b58cdecode = (enc, prefixArg) => bs58check.decode(enc).slice(prefixArg.length);
1008
+ function b58Encode(value, pre) {
1009
+ const data = typeof value === 'string' ? hex2buf(value) : value;
1010
+ const p = prefixV2[pre];
1011
+ const n = new Uint8Array(p.length + data.length);
1012
+ n.set(p);
1013
+ n.set(data, p.length);
1014
+ return bs58check.encode(toBuffer(n));
1015
+ }
577
1016
  /**
578
- *
579
- * @description Base58 decode a string with predefined prefix
580
- *
581
- * @param value Value to base58 decode
1017
+ * @description Parse binary public key and return Base58 representation
1018
+ * @param value Binary key data
1019
+ * @returns return prefixed public key
1020
+ * @example encodeKey('02033aba7da4a2e7b5dd9f074555c118829aff16213ea1b65859686bd5fcfeaf3616') // return 'p2pk66xmhjiN7LpfrDGFwpxPtJxkLtPjQ6HUxJbKmRbxSR7RMpamDwi'
582
1021
  */
583
- function b58decode(payload) {
584
- const buf = bs58check.decode(payload);
585
- const prefixMap = {
586
- [prefix.tz1.toString()]: '0000',
587
- [prefix.tz2.toString()]: '0001',
588
- [prefix.tz3.toString()]: '0002',
589
- [prefix.tz4.toString()]: '0003',
590
- };
591
- const pref = prefixMap[new Uint8Array(buf.slice(0, 3)).toString()];
592
- if (pref) {
593
- // tz addresses
594
- const hex = buf2hex(buf.slice(3));
595
- return pref + hex;
1022
+ function encodeKey(value) {
1023
+ let buf;
1024
+ if (typeof value === 'string') {
1025
+ buf = hex2buf(value);
596
1026
  }
597
1027
  else {
598
- // other (kt addresses)
599
- return '01' + buf2hex(buf.slice(3, 42)) + '00';
1028
+ buf = value;
600
1029
  }
1030
+ let pre;
1031
+ switch (buf[0]) {
1032
+ case 0:
1033
+ pre = exports.PrefixV2.Ed25519PublicKey;
1034
+ break;
1035
+ case 1:
1036
+ pre = exports.PrefixV2.Secp256k1PublicKey;
1037
+ break;
1038
+ case 2:
1039
+ pre = exports.PrefixV2.P256PublicKey;
1040
+ break;
1041
+ case 3:
1042
+ pre = exports.PrefixV2.BLS12_381PublicKey;
1043
+ break;
1044
+ default:
1045
+ throw new Error('invalid address format');
1046
+ }
1047
+ return b58Encode(buf.slice(1), pre);
601
1048
  }
602
1049
  /**
603
- *
604
- * @description b58 decode a string without predefined prefix
605
- * @param value
606
- * @returns string of bytes
607
- * @deprecated use b58decode instead
608
- */
609
- function b58decodeL2Address(payload) {
610
- const buf = bs58check.decode(payload);
611
- // tz4 address currently
612
- return buf2hex(buf.slice(3, 42));
613
- }
614
- /**
615
- *
616
- * @description Base58 encode an address using predefined prefix
617
- *
618
- * @param value Address to base58 encode (tz1, tz2, tz3 or KT1)
619
- * @deprecated use encodeAddress instead, same functionality with a more descriptive name
1050
+ * @description Parse binary public key hash and return Base58 representation
1051
+ * @param value Key hash to parse
1052
+ * @returns return prefixed public key hash
1053
+ * @example encodeKeyHash('0001907d6a7e9f084df840d6e67ffa8db5464f87d4d1') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe'
620
1054
  */
621
- function encodePubKey(value) {
622
- if (value.substring(0, 2) === '00') {
623
- const pref = {
624
- '0000': prefix.tz1,
625
- '0001': prefix.tz2,
626
- '0002': prefix.tz3,
627
- '0003': prefix.tz4,
628
- };
629
- return b58cencode(value.substring(4), pref[value.substring(0, 4)]);
630
- }
631
- return b58cencode(value.substring(2, 42), prefix.KT);
1055
+ function encodeKeyHash(value) {
1056
+ let buf;
1057
+ if (typeof value === 'string') {
1058
+ buf = hex2buf(value);
1059
+ }
1060
+ else {
1061
+ buf = value;
1062
+ }
1063
+ let pre;
1064
+ switch (buf[0]) {
1065
+ case 0:
1066
+ pre = exports.PrefixV2.Ed25519PublicKeyHash;
1067
+ break;
1068
+ case 1:
1069
+ pre = exports.PrefixV2.Secp256k1PublicKeyHash;
1070
+ break;
1071
+ case 2:
1072
+ pre = exports.PrefixV2.P256PublicKeyHash;
1073
+ break;
1074
+ case 3:
1075
+ pre = exports.PrefixV2.BLS12_381PublicKeyHash;
1076
+ break;
1077
+ default:
1078
+ throw new Error('invalid address format');
1079
+ }
1080
+ return b58Encode(buf.slice(1, 21), pre);
632
1081
  }
633
1082
  /**
634
- *
635
- * @description Base58 encode an address using predefined prefix (tz1, tz2, tz3, or KT1 without annotation)
636
- *
637
- * @param value Address to base58 encode (tz1, tz2, tz3 or KT1). Supports value with or without '0x' prefix
1083
+ * @description Parse binary Contract ID and return Base58 representation
1084
+ * @param value Address to parse (tz1, tz2, tz3, KT1, or sr1).
1085
+ * @example encodeAddress('0000e96b9f8b19af9c7ffa0c0480e1977b295850961f') // return 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM'
638
1086
  */
639
1087
  function encodeAddress(value) {
640
- if (value.substring(0, 2) === '0x') {
641
- value = value.slice(2);
642
- }
643
- if (value.substring(0, 2) === '00') {
644
- const pref = {
645
- '0000': prefix.tz1,
646
- '0001': prefix.tz2,
647
- '0002': prefix.tz3,
648
- '0003': prefix.tz4,
649
- };
650
- return b58cencode(value.substring(4), pref[value.substring(0, 4)]);
651
- }
652
- return b58cencode(value.substring(2, 42), prefix.KT);
1088
+ let buf;
1089
+ if (typeof value === 'string') {
1090
+ buf = hex2buf(value);
1091
+ }
1092
+ else {
1093
+ buf = value;
1094
+ }
1095
+ switch (buf[0]) {
1096
+ case 0: // implicit
1097
+ return encodeKeyHash(buf.slice(1));
1098
+ case 1: // contract hash
1099
+ return b58Encode(buf.slice(1, 21), exports.PrefixV2.ContractHash);
1100
+ case 3: // smart rollup hash
1101
+ return b58Encode(buf.slice(1, 21), exports.PrefixV2.SmartRollupHash);
1102
+ case 4: // zk rollup hash
1103
+ return b58Encode(buf.slice(1, 21), exports.PrefixV2.ZkRollupHash);
1104
+ default:
1105
+ throw new Error('invalid address format');
1106
+ }
653
1107
  }
654
1108
  /**
655
- *
656
1109
  * @description Base58 encode an address without predefined prefix
657
1110
  * @param value Address to base58 encode (tz4) hex dec
658
1111
  * @returns return address
659
- * @deprecated use encodeAddress instead
1112
+ * @example encodeBlsAddress('af2dc3c40667abc0e89c0ef40171d22aed08d5eb') // return 'tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8'
660
1113
  */
661
- function encodeL2Address(value) {
662
- return b58cencode(value, prefix.tz4);
1114
+ function encodeBlsAddress(value) {
1115
+ return b58Encode(value, exports.PrefixV2.BLS12_381PublicKeyHash);
663
1116
  }
664
1117
  /**
665
- *
666
- * @description Base58 encode a key according to its prefix
667
- *
668
- * @param value Key to base58 encode
1118
+ * @description convert a fragment of Michelson code in hex string to an 'expr' prefix + base58 encoded BLAKE2b hash string
1119
+ * @param value a fragment of Michelson code in hex string
1120
+ * @returns return 'expr' prefix + base58 encoded BLAKE2b hash
1121
+ * @example encodeExpr('050a000000160000b2e19a9e74440d86c59f13dab8a18ff873e889ea') // return 'exprv6UsC1sN3Fk2XfgcJCL8NCerP5rCGy1PRESZAqr7L2JdzX55EN'
669
1122
  */
670
- function encodeKey(value) {
671
- if (value[0] === '0') {
672
- const pref = {
673
- '00': new Uint8Array([13, 15, 37, 217]),
674
- '01': new Uint8Array([3, 254, 226, 86]),
675
- '02': new Uint8Array([3, 178, 139, 127]),
676
- };
677
- return b58cencode(value.substring(2), pref[value.substring(0, 2)]);
678
- }
1123
+ function encodeExpr(value) {
1124
+ const blakeHash = blake2b.hash(hex2buf(value), 32);
1125
+ return b58Encode(blakeHash, exports.PrefixV2.ScriptExpr);
679
1126
  }
680
1127
  /**
681
- *
682
- * @description Base58 encode a key hash according to its prefix
683
- *
684
- * @param value Key hash to base58 encode
1128
+ * @description convert a signed operation in hex string to an 'op' prefix + base58 encoded BLAKE2b hash string
1129
+ * @param value signed operation in hex string
1130
+ * @returns return 'op' prefix + base58 encoded BLAKE2b hash
1131
+ * @example encodeOpHash('0f185d8a30061e8134c162dbb7a6c3ab8f5fdb153363ccd6149b49a33481156a6c00b2e19a9e74440d86c59f13dab8a18ff873e889eaa304ab05da13000001f1585a7384f36e45fb43dc37e8ce172bced3e05700ff0000000002002110c033f3a990c2e46a3d6054ecc2f74072aae7a34b5ac4d9ce9edc11c2410a97695682108951786f05b361da03b97245dc9897e1955e08b5b8d9e153b0bdeb0d') // return 'opapqvVXmebRTCFd2GQFydr4tJj3V5QocQuTmuhbatcHm4Seo2t'
685
1132
  */
686
- function encodeKeyHash(value) {
687
- if (value[0] === '0') {
688
- const pref = {
689
- '00': new Uint8Array([6, 161, 159]),
690
- '01': new Uint8Array([6, 161, 161]),
691
- '02': new Uint8Array([6, 161, 164]),
692
- '03': new Uint8Array([6, 161, 167]),
693
- };
694
- return b58cencode(value.substring(2), pref[value.substring(0, 2)]);
695
- }
1133
+ function encodeOpHash(value) {
1134
+ const blakeHash = blake2b.hash(hex2buf(value), 32);
1135
+ return b58Encode(blakeHash, exports.PrefixV2.OperationHash);
696
1136
  }
697
1137
  /**
698
- *
699
1138
  * @description Convert an hex string to a Uint8Array
700
- *
701
1139
  * @param hex Hex string to convert
702
1140
  * @throws {@link ValueConversionError}
703
1141
  */
704
- const hex2buf = (hex) => {
1142
+ function hex2buf(hex) {
1143
+ hex = hex.startsWith('0x') ? hex.slice(2) : hex;
705
1144
  if (hex.length % 2 !== 0) {
706
- throw new core.InvalidHexStringError(hex, `: Expecting even number of characters`);
1145
+ throw new core.InvalidHexStringError(hex, `Expecting even number of characters`);
707
1146
  }
708
- const hexDigits = stripHexPrefix(hex);
709
- if (!hexDigits.match(/^([\da-f]{2})*$/gi)) {
710
- throw new core.InvalidHexStringError(hex, `: Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
1147
+ if (!hex.match(/^([\da-f]{2})*$/gi)) {
1148
+ throw new core.InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
711
1149
  }
712
- const out = new Uint8Array(hexDigits.length / 2);
1150
+ const res = new Uint8Array(hex.length / 2);
713
1151
  let j = 0;
714
- for (let i = 0; i < hexDigits.length; i += 2) {
715
- const v = parseInt(hexDigits.slice(i, i + 2), 16);
716
- if (Number.isNaN(v)) {
717
- throw new ValueConversionError(hex, 'Uint8Array');
1152
+ for (let i = 0; i < hex.length; i += 2) {
1153
+ const ss = hex.slice(i, i + 2);
1154
+ const x = parseInt(ss, 16);
1155
+ if (Number.isNaN(x)) {
1156
+ throw new core.InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
718
1157
  }
719
- out[j++] = v;
1158
+ res[j++] = x;
720
1159
  }
721
- return out;
722
- };
1160
+ return res;
1161
+ }
723
1162
  /**
724
- *
725
1163
  * @description Merge 2 buffers together
726
- *
727
1164
  * @param b1 First buffer
728
1165
  * @param b2 Second buffer
729
1166
  */
730
- const mergebuf = (b1, b2) => {
1167
+ function mergebuf(b1, b2) {
731
1168
  const r = new Uint8Array(b1.length + b2.length);
732
1169
  r.set(b1);
733
1170
  r.set(b2, b1.length);
734
1171
  return r;
735
- };
1172
+ }
736
1173
  /**
737
- *
738
1174
  * @description Flatten a michelson json representation to an array
739
- *
740
1175
  * @param s michelson json
741
1176
  */
742
- const mic2arr = function me2(s) {
1177
+ function mic2arr(s) {
743
1178
  let ret = [];
744
1179
  if (Object.prototype.hasOwnProperty.call(s, 'prim')) {
745
1180
  if (s.prim === 'Pair') {
746
- ret.push(me2(s.args[0]));
747
- ret = ret.concat(me2(s.args[1]));
1181
+ ret.push(mic2arr(s.args[0]));
1182
+ ret = ret.concat(mic2arr(s.args[1]));
748
1183
  }
749
1184
  else if (s.prim === 'Elt') {
750
1185
  ret = {
751
- key: me2(s.args[0]),
752
- val: me2(s.args[1]),
1186
+ key: mic2arr(s.args[0]),
1187
+ val: mic2arr(s.args[1]),
753
1188
  };
754
1189
  }
755
1190
  else if (s.prim === 'True') {
@@ -762,7 +1197,7 @@
762
1197
  else if (Array.isArray(s)) {
763
1198
  const sc = s.length;
764
1199
  for (let i = 0; i < sc; i++) {
765
- const n = me2(s[i]);
1200
+ const n = mic2arr(s[i]);
766
1201
  if (typeof n.key !== 'undefined') {
767
1202
  if (Array.isArray(ret)) {
768
1203
  ret = {
@@ -788,110 +1223,43 @@
788
1223
  ret = s;
789
1224
  }
790
1225
  return ret;
791
- };
1226
+ }
792
1227
  /**
793
- *
794
1228
  * @description Convert a Uint8Array to an hex string
795
- *
796
1229
  * @param buffer Uint8Array to convert
797
1230
  */
798
- const buf2hex = (buffer) => {
799
- const hexParts = [];
800
- buffer.forEach((byte) => {
801
- const hex = byte.toString(16);
802
- const paddedHex = `00${hex}`.slice(-2);
803
- hexParts.push(paddedHex);
804
- });
805
- return hexParts.join('');
806
- };
807
- /**
808
- *
809
- * @description Gets Tezos address (PKH) from Public Key
810
- *
811
- * @param publicKey Public Key
812
- * @returns A string of the Tezos address (PKH) that was derived from the given Public Key
813
- */
814
- const getPkhfromPk = (publicKey) => {
815
- let encodingPrefix;
816
- let prefixLen;
817
- const keyPrefix = validatePkAndExtractPrefix(publicKey);
818
- const decoded = b58cdecode(publicKey, prefix[keyPrefix]);
819
- switch (keyPrefix) {
820
- case exports.Prefix.EDPK:
821
- encodingPrefix = prefix[exports.Prefix.TZ1];
822
- prefixLen = prefixLength[exports.Prefix.TZ1];
823
- break;
824
- case exports.Prefix.SPPK:
825
- encodingPrefix = prefix[exports.Prefix.TZ2];
826
- prefixLen = prefixLength[exports.Prefix.TZ2];
827
- break;
828
- case exports.Prefix.P2PK:
829
- encodingPrefix = prefix[exports.Prefix.TZ3];
830
- prefixLen = prefixLength[exports.Prefix.TZ3];
831
- break;
832
- case exports.Prefix.BLPK:
833
- encodingPrefix = prefix[exports.Prefix.TZ4];
834
- prefixLen = prefixLength[exports.Prefix.TZ4];
835
- }
836
- const hashed = blake2b.hash(decoded, prefixLen);
837
- const result = b58cencode(hashed, encodingPrefix);
838
- return result;
839
- };
840
- /**
841
- *
842
- * @description Convert a string to bytes
843
- *
844
- * @param str String to convert
845
- * @deprecated use stringToBytes instead, same functionality with a more descriptive name
846
- */
847
- function char2Bytes(str) {
848
- return buffer.Buffer.from(str, 'utf8').toString('hex');
1231
+ function buf2hex(bytes) {
1232
+ return Array.from(bytes)
1233
+ .map((x) => ((x >> 4) & 0xf).toString(16) + (x & 0xf).toString(16))
1234
+ .join('');
849
1235
  }
850
1236
  /**
851
- *
852
1237
  * @description Convert a string to a byte string representation
853
- *
854
1238
  * @param str String to convert
855
1239
  */
856
1240
  function stringToBytes(str) {
857
1241
  return buffer.Buffer.from(str, 'utf8').toString('hex');
858
1242
  }
859
1243
  /**
860
- *
861
- * @description Convert bytes to a string
862
- *
863
- * @param str Bytes to convert
864
- * @deprecated use hexStringToBytes instead, same functionality with a more descriptive name
865
- */
866
- function bytes2Char(hex) {
867
- return buffer.Buffer.from(hex2buf(hex)).toString('utf8');
868
- }
869
- /**
870
- *
871
1244
  * @description Convert byte string representation to string
872
- *
873
- * @param str byte string to convert
1245
+ * @param hex byte string to convert
874
1246
  */
875
1247
  function bytesToString(hex) {
876
1248
  return buffer.Buffer.from(hex2buf(hex)).toString('utf8');
877
1249
  }
878
1250
  /**
879
- *
880
1251
  * @description Convert hex string/UintArray/Buffer to bytes
881
- *
882
1252
  * @param hex String value to convert to bytes
883
1253
  */
884
1254
  function hex2Bytes(hex) {
885
1255
  const hexDigits = stripHexPrefix(hex);
886
1256
  if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
887
- throw new core.InvalidHexStringError(hex, `: Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
1257
+ throw new core.InvalidHexStringError(hex, `Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
888
1258
  }
889
1259
  return buffer.Buffer.from(hexDigits, 'hex');
890
1260
  }
891
1261
  /**
892
- *
893
1262
  * @description Converts a number or Bignumber to hexadecimal string
894
- *
895
1263
  * @param val The value that will be converted to a hexadecimal string value
896
1264
  */
897
1265
  function toHexBuf(val, bitLength = 8) {
@@ -901,7 +1269,6 @@
901
1269
  return buffer.Buffer.from(num2PaddedHex(val, bitLength), 'hex');
902
1270
  }
903
1271
  /**
904
- *
905
1272
  * @description Converts a number or BigNumber to a padded hexadecimal string
906
1273
  * @param val The value that will be converted into a padded hexadecimal string value
907
1274
  * @param bitLength The length of bits
@@ -943,6 +1310,113 @@
943
1310
  function stripHexPrefix(hex) {
944
1311
  return hex.startsWith('0x') ? hex.slice(2) : hex;
945
1312
  }
1313
+ function splitAddress(addr) {
1314
+ const i = addr.indexOf('%');
1315
+ if (i >= 0) {
1316
+ return [addr.slice(0, i), addr.slice(i)];
1317
+ }
1318
+ else {
1319
+ return [addr, null];
1320
+ }
1321
+ }
1322
+ function compareArrays(a, b) {
1323
+ let i = 0;
1324
+ while (i < a.length && i < b.length && a[i] === b[i])
1325
+ i++;
1326
+ const aa = i < a.length ? a[i] : 0;
1327
+ const bb = i < b.length ? b[i] : 0;
1328
+ return aa < bb ? -1 : aa > bb ? 1 : 0;
1329
+ }
1330
+ /**
1331
+ * @deprecated use b58DecodeAndCheckPrefix instead, this function will be removed in the next minor release
1332
+ * @description Base58 decode a string and remove the prefix from it
1333
+ * @param enc Value to base58 decode
1334
+ * @param prefixArg prefix to remove from the decoded string
1335
+ */
1336
+ function b58cdecode(enc, prefixArg) {
1337
+ return bs58check.decode(enc).slice(prefixArg.length);
1338
+ }
1339
+ /**
1340
+ * @deprecated use b58Encode instead, this function will be removed in the next minor release
1341
+ * @description Base58 encode a string or a Uint8Array and append a prefix to it
1342
+ * @param value Value to base58 encode
1343
+ * @param prefix prefix to append to the encoded string
1344
+ */
1345
+ function b58cencode(value, prefix) {
1346
+ const payloadAr = typeof value === 'string' ? hex2buf(value) : value;
1347
+ const n = new Uint8Array(prefix.length + payloadAr.length);
1348
+ n.set(prefix);
1349
+ n.set(payloadAr, prefix.length);
1350
+ return bs58check.encode(toBuffer(n));
1351
+ }
1352
+ /**
1353
+ * @deprecated use b58DecodeAndCheckPrefix instead, this function will be removed in the next minor release
1354
+ * @description Base58 decode a string with predefined prefix
1355
+ * @param payload Value to base58 decode
1356
+ */
1357
+ function b58decode(payload) {
1358
+ const buf = bs58check.decode(payload);
1359
+ const prefixMap = {
1360
+ [prefix.tz1.toString()]: '0000',
1361
+ [prefix.tz2.toString()]: '0001',
1362
+ [prefix.tz3.toString()]: '0002',
1363
+ [prefix.tz4.toString()]: '0003',
1364
+ };
1365
+ const pref = prefixMap[new Uint8Array(buf.slice(0, 3)).toString()];
1366
+ if (pref) {
1367
+ // tz addresses
1368
+ const hex = buf2hex(buf.slice(3));
1369
+ return pref + hex;
1370
+ }
1371
+ else {
1372
+ // other (kt addresses)
1373
+ return '01' + buf2hex(buf.slice(3, 42)) + '00';
1374
+ }
1375
+ }
1376
+ /**
1377
+ * @deprecated use b58DecodeBlsAddress instead, this function will be removed in the next minor release
1378
+ * @description b58 decode a string without predefined prefix
1379
+ * @param payload Value to base58 decode
1380
+ * @returns string of bytes
1381
+ */
1382
+ function b58decodeL2Address(payload) {
1383
+ const buf = bs58check.decode(payload);
1384
+ // tz4 address currently
1385
+ return buf2hex(buf.slice(3, 42));
1386
+ }
1387
+ /**
1388
+ * @deprecated use encodeAddress instead, this function will be removed in the next minor release
1389
+ * @description Base58 encode an address using predefined prefix
1390
+ * @param value Address to base58 encode (tz1, tz2, tz3 or KT1)
1391
+ */
1392
+ function encodePubKey(value) {
1393
+ return encodeAddress(value);
1394
+ }
1395
+ /**
1396
+ * @deprecated use encodeBlsAddress instead, this function will be removed in the next minor release
1397
+ * @description Base58 encode an address without predefined prefix
1398
+ * @param value Address to base58 encode (tz4) hex dec
1399
+ * @returns return address
1400
+ */
1401
+ function encodeL2Address(value) {
1402
+ return b58cencode(value, prefix.tz4);
1403
+ }
1404
+ /**
1405
+ * @deprecated use stringToBytes instead, this function will be removed in the next minor release
1406
+ * @description Convert a string to bytes
1407
+ * @param str String to convert
1408
+ */
1409
+ function char2Bytes(str) {
1410
+ return buffer.Buffer.from(str, 'utf8').toString('hex');
1411
+ }
1412
+ /**
1413
+ * @deprecated use bytesToString instead, this function will be removed in the next minor release
1414
+ * @description Convert bytes to a string
1415
+ * @param hex Bytes to convert
1416
+ */
1417
+ function bytes2Char(hex) {
1418
+ return buffer.Buffer.from(hex2buf(hex)).toString('utf8');
1419
+ }
946
1420
 
947
1421
  Object.defineProperty(exports, "DeprecationError", {
948
1422
  enumerable: true,
@@ -1000,9 +1474,22 @@
1000
1474
  enumerable: true,
1001
1475
  get: function () { return core.ProhibitedActionError; }
1002
1476
  });
1477
+ Object.defineProperty(exports, "ValidationResult", {
1478
+ enumerable: true,
1479
+ get: function () { return core.ValidationResult; }
1480
+ });
1481
+ exports.BLS12_381_DST = BLS12_381_DST;
1003
1482
  exports.InvalidProtocolHashError = InvalidProtocolHashError;
1483
+ exports.POP_DST = POP_DST;
1004
1484
  exports.VERSION = VERSION;
1005
1485
  exports.ValueConversionError = ValueConversionError;
1486
+ exports.addressPrefixes = addressPrefixes;
1487
+ exports.b58DecodeAddress = b58DecodeAddress;
1488
+ exports.b58DecodeAndCheckPrefix = b58DecodeAndCheckPrefix;
1489
+ exports.b58DecodeBlsAddress = b58DecodeBlsAddress;
1490
+ exports.b58DecodePublicKey = b58DecodePublicKey;
1491
+ exports.b58DecodePublicKeyHash = b58DecodePublicKeyHash;
1492
+ exports.b58Encode = b58Encode;
1006
1493
  exports.b58cdecode = b58cdecode;
1007
1494
  exports.b58cencode = b58cencode;
1008
1495
  exports.b58decode = b58decode;
@@ -1011,7 +1498,9 @@
1011
1498
  exports.bytes2Char = bytes2Char;
1012
1499
  exports.bytesToString = bytesToString;
1013
1500
  exports.char2Bytes = char2Bytes;
1501
+ exports.compareArrays = compareArrays;
1014
1502
  exports.encodeAddress = encodeAddress;
1503
+ exports.encodeBlsAddress = encodeBlsAddress;
1015
1504
  exports.encodeExpr = encodeExpr;
1016
1505
  exports.encodeKey = encodeKey;
1017
1506
  exports.encodeKeyHash = encodeKeyHash;
@@ -1023,13 +1512,18 @@
1023
1512
  exports.hex2Bytes = hex2Bytes;
1024
1513
  exports.hex2buf = hex2buf;
1025
1514
  exports.invalidDetail = invalidDetail;
1026
- exports.isValidPrefix = isValidPrefix;
1515
+ exports.isValidPrefixedValue = isValidPrefixedValue;
1027
1516
  exports.mergebuf = mergebuf;
1028
1517
  exports.mic2arr = mic2arr;
1029
1518
  exports.num2PaddedHex = num2PaddedHex;
1030
1519
  exports.numToHexBuffer = numToHexBuffer;
1520
+ exports.payloadLength = payloadLength;
1031
1521
  exports.prefix = prefix;
1032
1522
  exports.prefixLength = prefixLength;
1523
+ exports.publicKeyHashPrefixes = publicKeyHashPrefixes;
1524
+ exports.publicKeyPrefixes = publicKeyPrefixes;
1525
+ exports.signaturePrefixes = signaturePrefixes;
1526
+ exports.splitAddress = splitAddress;
1033
1527
  exports.stringToBytes = stringToBytes;
1034
1528
  exports.stripHexPrefix = stripHexPrefix;
1035
1529
  exports.toHexBuf = toHexBuf;