@taquito/utils 23.0.3 → 24.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/constants.js +1 -128
- package/dist/lib/encoding.js +603 -0
- package/dist/lib/taquito-utils.js +16 -710
- package/dist/lib/validators.js +10 -27
- package/dist/lib/verify-signature.js +20 -35
- package/dist/lib/version.js +2 -2
- package/dist/taquito-utils.es6.js +2690 -958
- package/dist/taquito-utils.es6.js.map +1 -1
- package/dist/taquito-utils.umd.js +2578 -858
- package/dist/taquito-utils.umd.js.map +1 -1
- package/dist/types/constants.d.ts +0 -101
- package/dist/types/encoding.d.ts +184 -0
- package/dist/types/taquito-utils.d.ts +1 -234
- package/dist/types/validators.d.ts +1 -6
- package/dist/types/verify-signature.d.ts +0 -8
- package/package.json +8 -8
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
* @module @taquito/utils
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.signaturePrefixes = exports.publicKeyHashPrefixes = exports.publicKeyPrefixes = exports.addressPrefixes = void 0;
|
|
8
|
+
exports.b58DecodeAndCheckPrefix = b58DecodeAndCheckPrefix;
|
|
9
|
+
exports.b58DecodePublicKey = b58DecodePublicKey;
|
|
10
|
+
exports.b58DecodePublicKeyHash = b58DecodePublicKeyHash;
|
|
11
|
+
exports.b58DecodeBlsAddress = b58DecodeBlsAddress;
|
|
12
|
+
exports.b58DecodeAddress = b58DecodeAddress;
|
|
13
|
+
exports.getPkhfromPk = getPkhfromPk;
|
|
14
|
+
exports.b58Encode = b58Encode;
|
|
15
|
+
exports.encodeKey = encodeKey;
|
|
16
|
+
exports.encodeKeyHash = encodeKeyHash;
|
|
17
|
+
exports.encodeAddress = encodeAddress;
|
|
18
|
+
exports.encodeBlsAddress = encodeBlsAddress;
|
|
19
|
+
exports.encodeExpr = encodeExpr;
|
|
20
|
+
exports.encodeOpHash = encodeOpHash;
|
|
21
|
+
exports.hex2buf = hex2buf;
|
|
22
|
+
exports.mergebuf = mergebuf;
|
|
23
|
+
exports.mic2arr = mic2arr;
|
|
24
|
+
exports.buf2hex = buf2hex;
|
|
25
|
+
exports.stringToBytes = stringToBytes;
|
|
26
|
+
exports.bytesToString = bytesToString;
|
|
27
|
+
exports.hex2Bytes = hex2Bytes;
|
|
28
|
+
exports.toHexBuf = toHexBuf;
|
|
29
|
+
exports.numToHexBuffer = numToHexBuffer;
|
|
30
|
+
exports.num2PaddedHex = num2PaddedHex;
|
|
31
|
+
exports.stripHexPrefix = stripHexPrefix;
|
|
32
|
+
exports.splitAddress = splitAddress;
|
|
33
|
+
exports.compareArrays = compareArrays;
|
|
34
|
+
/*
|
|
35
|
+
* Some code in this file is originally from sotez and eztz
|
|
36
|
+
* Copyright (c) 2018 Andrew Kishino
|
|
37
|
+
* Copyright (c) 2017 Stephen Andrews
|
|
38
|
+
*/
|
|
39
|
+
const buffer_1 = require("buffer");
|
|
40
|
+
const constants_1 = require("./constants");
|
|
41
|
+
const blake2b_1 = require("@stablelib/blake2b");
|
|
42
|
+
const bs58check_1 = require("bs58check");
|
|
43
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
44
|
+
const typedarray_to_buffer_1 = require("typedarray-to-buffer");
|
|
45
|
+
const core_1 = require("@taquito/core");
|
|
46
|
+
/**
|
|
47
|
+
* @description list of prefixes that can be used to decode an address
|
|
48
|
+
*/
|
|
49
|
+
exports.addressPrefixes = [
|
|
50
|
+
constants_1.PrefixV2.P256PublicKeyHash,
|
|
51
|
+
constants_1.PrefixV2.Secp256k1PublicKeyHash,
|
|
52
|
+
constants_1.PrefixV2.Ed25519PublicKeyHash,
|
|
53
|
+
constants_1.PrefixV2.BLS12_381PublicKeyHash,
|
|
54
|
+
constants_1.PrefixV2.ContractHash,
|
|
55
|
+
constants_1.PrefixV2.SmartRollupHash,
|
|
56
|
+
// PrefixV2.ZkRollupHash,
|
|
57
|
+
];
|
|
58
|
+
/**
|
|
59
|
+
* @description list of prefixes that can be used to decode a public key
|
|
60
|
+
*/
|
|
61
|
+
exports.publicKeyPrefixes = [
|
|
62
|
+
constants_1.PrefixV2.P256PublicKey,
|
|
63
|
+
constants_1.PrefixV2.Secp256k1PublicKey,
|
|
64
|
+
constants_1.PrefixV2.Ed25519PublicKey,
|
|
65
|
+
constants_1.PrefixV2.BLS12_381PublicKey,
|
|
66
|
+
];
|
|
67
|
+
/**
|
|
68
|
+
* @description list of prefixes that can be used to decode a public key hash
|
|
69
|
+
*/
|
|
70
|
+
exports.publicKeyHashPrefixes = [
|
|
71
|
+
constants_1.PrefixV2.P256PublicKeyHash,
|
|
72
|
+
constants_1.PrefixV2.Secp256k1PublicKeyHash,
|
|
73
|
+
constants_1.PrefixV2.Ed25519PublicKeyHash,
|
|
74
|
+
constants_1.PrefixV2.BLS12_381PublicKeyHash,
|
|
75
|
+
];
|
|
76
|
+
/**
|
|
77
|
+
* @description list of prefixes that can be used to decode a signature
|
|
78
|
+
*/
|
|
79
|
+
exports.signaturePrefixes = [
|
|
80
|
+
constants_1.PrefixV2.P256Signature,
|
|
81
|
+
constants_1.PrefixV2.Secp256k1Signature,
|
|
82
|
+
constants_1.PrefixV2.Ed25519Signature,
|
|
83
|
+
constants_1.PrefixV2.BLS12_381Signature,
|
|
84
|
+
constants_1.PrefixV2.GenericSignature,
|
|
85
|
+
];
|
|
86
|
+
function b58DecodeAndCheckPrefix(src, allowed, payloadOnly) {
|
|
87
|
+
const buf = (() => {
|
|
88
|
+
try {
|
|
89
|
+
return bs58check_1.default.decode(src);
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
if (err instanceof Error) {
|
|
93
|
+
if (err.message.includes('checksum')) {
|
|
94
|
+
throw new core_1.ParameterValidationError(core_1.ValidationResult.INVALID_CHECKSUM);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new core_1.ParameterValidationError(core_1.ValidationResult.INVALID_ENCODING);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
throw err;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
})();
|
|
105
|
+
let key;
|
|
106
|
+
for (key in constants_1.PrefixV2) {
|
|
107
|
+
const p = constants_1.PrefixV2[key];
|
|
108
|
+
const pre = constants_1.prefixV2[p];
|
|
109
|
+
if (buf.length === pre.length + constants_1.payloadLength[p] &&
|
|
110
|
+
buf.slice(0, pre.length).every((v, i) => v == pre[i])) {
|
|
111
|
+
if (allowed !== undefined && allowed.indexOf(p) < 0) {
|
|
112
|
+
throw new core_1.ParameterValidationError(core_1.ValidationResult.PREFIX_NOT_ALLOWED);
|
|
113
|
+
}
|
|
114
|
+
if (payloadOnly) {
|
|
115
|
+
return buf.slice(pre.length);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
return [buf.slice(pre.length), p];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
throw new core_1.ParameterValidationError(core_1.ValidationResult.NO_PREFIX_MATCHED);
|
|
123
|
+
}
|
|
124
|
+
function b58DecodePublicKey(value, fmt) {
|
|
125
|
+
const [data, pre] = b58DecodeAndCheckPrefix(value, exports.publicKeyPrefixes);
|
|
126
|
+
let tag;
|
|
127
|
+
switch (pre) {
|
|
128
|
+
case constants_1.PrefixV2.Ed25519PublicKey:
|
|
129
|
+
tag = 0;
|
|
130
|
+
break;
|
|
131
|
+
case constants_1.PrefixV2.Secp256k1PublicKey:
|
|
132
|
+
tag = 1;
|
|
133
|
+
break;
|
|
134
|
+
case constants_1.PrefixV2.P256PublicKey:
|
|
135
|
+
tag = 2;
|
|
136
|
+
break;
|
|
137
|
+
case constants_1.PrefixV2.BLS12_381PublicKey:
|
|
138
|
+
tag = 3;
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
throw new core_1.InvalidKeyError(core_1.ValidationResult.NO_PREFIX_MATCHED);
|
|
142
|
+
}
|
|
143
|
+
const buf = new Uint8Array(data.length + 1);
|
|
144
|
+
buf[0] = tag;
|
|
145
|
+
buf.set(data, 1);
|
|
146
|
+
if (fmt !== undefined && fmt === 'array') {
|
|
147
|
+
return buf;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
return buf2hex(buf);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function b58DecodePublicKeyHash(value, fmt) {
|
|
154
|
+
const [data, pre] = b58DecodeAndCheckPrefix(value, exports.publicKeyHashPrefixes);
|
|
155
|
+
const buf = new Uint8Array(21);
|
|
156
|
+
let tag;
|
|
157
|
+
switch (pre) {
|
|
158
|
+
case constants_1.PrefixV2.Ed25519PublicKeyHash:
|
|
159
|
+
tag = 0;
|
|
160
|
+
break;
|
|
161
|
+
case constants_1.PrefixV2.Secp256k1PublicKeyHash:
|
|
162
|
+
tag = 1;
|
|
163
|
+
break;
|
|
164
|
+
case constants_1.PrefixV2.P256PublicKeyHash:
|
|
165
|
+
tag = 2;
|
|
166
|
+
break;
|
|
167
|
+
case constants_1.PrefixV2.BLS12_381PublicKeyHash:
|
|
168
|
+
tag = 3;
|
|
169
|
+
break;
|
|
170
|
+
default:
|
|
171
|
+
throw new core_1.InvalidAddressError(value, core_1.ValidationResult.NO_PREFIX_MATCHED);
|
|
172
|
+
}
|
|
173
|
+
buf[0] = tag;
|
|
174
|
+
buf.set(data, 1);
|
|
175
|
+
if (fmt !== undefined && fmt === 'array') {
|
|
176
|
+
return buf;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
return buf2hex(buf);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function b58DecodeBlsAddress(value, fmt) {
|
|
183
|
+
const [buf, pre] = b58DecodeAndCheckPrefix(value);
|
|
184
|
+
if (pre !== constants_1.PrefixV2.BLS12_381PublicKeyHash) {
|
|
185
|
+
throw new core_1.InvalidKeyError(core_1.ValidationResult.NO_PREFIX_MATCHED);
|
|
186
|
+
}
|
|
187
|
+
if (fmt !== undefined && fmt === 'array') {
|
|
188
|
+
return buf;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
return buf2hex(buf);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function b58DecodeAddress(value, fmt) {
|
|
195
|
+
const i = value.indexOf('%');
|
|
196
|
+
if (i >= 0) {
|
|
197
|
+
value = value.slice(0, i);
|
|
198
|
+
}
|
|
199
|
+
const [data, pre] = b58DecodeAndCheckPrefix(value, exports.addressPrefixes);
|
|
200
|
+
const buf = new Uint8Array(22);
|
|
201
|
+
if (pre === constants_1.PrefixV2.ContractHash ||
|
|
202
|
+
pre === constants_1.PrefixV2.SmartRollupHash ||
|
|
203
|
+
pre === constants_1.PrefixV2.ZkRollupHash) {
|
|
204
|
+
let tag;
|
|
205
|
+
switch (pre) {
|
|
206
|
+
case constants_1.PrefixV2.ContractHash:
|
|
207
|
+
tag = 1;
|
|
208
|
+
break;
|
|
209
|
+
case constants_1.PrefixV2.SmartRollupHash:
|
|
210
|
+
tag = 3;
|
|
211
|
+
break;
|
|
212
|
+
case constants_1.PrefixV2.ZkRollupHash:
|
|
213
|
+
tag = 4;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
buf[0] = tag;
|
|
217
|
+
buf.set(data, 1);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
let tag;
|
|
221
|
+
switch (pre) {
|
|
222
|
+
case constants_1.PrefixV2.Ed25519PublicKeyHash:
|
|
223
|
+
tag = 0;
|
|
224
|
+
break;
|
|
225
|
+
case constants_1.PrefixV2.Secp256k1PublicKeyHash:
|
|
226
|
+
tag = 1;
|
|
227
|
+
break;
|
|
228
|
+
case constants_1.PrefixV2.P256PublicKeyHash:
|
|
229
|
+
tag = 2;
|
|
230
|
+
break;
|
|
231
|
+
case constants_1.PrefixV2.BLS12_381PublicKeyHash:
|
|
232
|
+
tag = 3;
|
|
233
|
+
break;
|
|
234
|
+
default:
|
|
235
|
+
throw new core_1.InvalidAddressError(value, core_1.ValidationResult.NO_PREFIX_MATCHED);
|
|
236
|
+
}
|
|
237
|
+
buf[0] = 0;
|
|
238
|
+
buf[1] = tag;
|
|
239
|
+
buf.set(data, 2);
|
|
240
|
+
}
|
|
241
|
+
if (fmt !== undefined && fmt === 'array') {
|
|
242
|
+
return buf;
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
return buf2hex(buf);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @description Gets Tezos address (PKH) from Public Key
|
|
250
|
+
* @param publicKey Base58 Public Key
|
|
251
|
+
* @returns A string of the Tezos address (PKH) that was derived from the given Public Key
|
|
252
|
+
* @example getPkhfromPk('edpkuNjKKT48xBoT5asPrWdmuM1Yw8D93MwgFgVvtca8jb5pstzaCh') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe'
|
|
253
|
+
*/
|
|
254
|
+
function getPkhfromPk(publicKey) {
|
|
255
|
+
const [key, pre] = b58DecodeAndCheckPrefix(publicKey);
|
|
256
|
+
let pkhPre;
|
|
257
|
+
switch (pre) {
|
|
258
|
+
case constants_1.PrefixV2.P256PublicKey:
|
|
259
|
+
pkhPre = constants_1.PrefixV2.P256PublicKeyHash;
|
|
260
|
+
break;
|
|
261
|
+
case constants_1.PrefixV2.Secp256k1PublicKey:
|
|
262
|
+
pkhPre = constants_1.PrefixV2.Secp256k1PublicKeyHash;
|
|
263
|
+
break;
|
|
264
|
+
case constants_1.PrefixV2.Ed25519PublicKey:
|
|
265
|
+
pkhPre = constants_1.PrefixV2.Ed25519PublicKeyHash;
|
|
266
|
+
break;
|
|
267
|
+
case constants_1.PrefixV2.BLS12_381PublicKey:
|
|
268
|
+
pkhPre = constants_1.PrefixV2.BLS12_381PublicKeyHash;
|
|
269
|
+
break;
|
|
270
|
+
default:
|
|
271
|
+
throw new core_1.InvalidPublicKeyError(publicKey, core_1.ValidationResult.NO_PREFIX_MATCHED);
|
|
272
|
+
}
|
|
273
|
+
const hashed = (0, blake2b_1.hash)(key, 20);
|
|
274
|
+
return b58Encode(hashed, pkhPre);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* @description Add the prefix to a hex string or Uint8Array and Base58 encode it
|
|
278
|
+
* @param value Value to Base58 encode
|
|
279
|
+
* @param pre prefix ID to append to the encoded string
|
|
280
|
+
* @example b58Encode('e96b9f8b19af9c7ffa0c0480e1977b295850961f', PrefixV2.Ed25519PublicKeyHash) // returns 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM'
|
|
281
|
+
*/
|
|
282
|
+
function b58Encode(value, pre) {
|
|
283
|
+
const data = typeof value === 'string' ? hex2buf(value) : value;
|
|
284
|
+
const p = constants_1.prefixV2[pre];
|
|
285
|
+
const n = new Uint8Array(p.length + data.length);
|
|
286
|
+
n.set(p);
|
|
287
|
+
n.set(data, p.length);
|
|
288
|
+
return bs58check_1.default.encode((0, typedarray_to_buffer_1.default)(n));
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* @description Parse binary public key and return Base58 representation
|
|
292
|
+
* @param value Binary key data
|
|
293
|
+
* @returns return prefixed public key
|
|
294
|
+
* @example encodeKey('02033aba7da4a2e7b5dd9f074555c118829aff16213ea1b65859686bd5fcfeaf3616') // return 'p2pk66xmhjiN7LpfrDGFwpxPtJxkLtPjQ6HUxJbKmRbxSR7RMpamDwi'
|
|
295
|
+
*/
|
|
296
|
+
function encodeKey(value) {
|
|
297
|
+
let buf;
|
|
298
|
+
if (typeof value === 'string') {
|
|
299
|
+
buf = hex2buf(value);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
buf = value;
|
|
303
|
+
}
|
|
304
|
+
let pre;
|
|
305
|
+
switch (buf[0]) {
|
|
306
|
+
case 0:
|
|
307
|
+
pre = constants_1.PrefixV2.Ed25519PublicKey;
|
|
308
|
+
break;
|
|
309
|
+
case 1:
|
|
310
|
+
pre = constants_1.PrefixV2.Secp256k1PublicKey;
|
|
311
|
+
break;
|
|
312
|
+
case 2:
|
|
313
|
+
pre = constants_1.PrefixV2.P256PublicKey;
|
|
314
|
+
break;
|
|
315
|
+
case 3:
|
|
316
|
+
pre = constants_1.PrefixV2.BLS12_381PublicKey;
|
|
317
|
+
break;
|
|
318
|
+
default:
|
|
319
|
+
throw new Error('invalid address format');
|
|
320
|
+
}
|
|
321
|
+
return b58Encode(buf.slice(1), pre);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* @description Parse binary public key hash and return Base58 representation
|
|
325
|
+
* @param value Key hash to parse
|
|
326
|
+
* @returns return prefixed public key hash
|
|
327
|
+
* @example encodeKeyHash('0001907d6a7e9f084df840d6e67ffa8db5464f87d4d1') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe'
|
|
328
|
+
*/
|
|
329
|
+
function encodeKeyHash(value) {
|
|
330
|
+
let buf;
|
|
331
|
+
if (typeof value === 'string') {
|
|
332
|
+
buf = hex2buf(value);
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
buf = value;
|
|
336
|
+
}
|
|
337
|
+
let pre;
|
|
338
|
+
switch (buf[0]) {
|
|
339
|
+
case 0:
|
|
340
|
+
pre = constants_1.PrefixV2.Ed25519PublicKeyHash;
|
|
341
|
+
break;
|
|
342
|
+
case 1:
|
|
343
|
+
pre = constants_1.PrefixV2.Secp256k1PublicKeyHash;
|
|
344
|
+
break;
|
|
345
|
+
case 2:
|
|
346
|
+
pre = constants_1.PrefixV2.P256PublicKeyHash;
|
|
347
|
+
break;
|
|
348
|
+
case 3:
|
|
349
|
+
pre = constants_1.PrefixV2.BLS12_381PublicKeyHash;
|
|
350
|
+
break;
|
|
351
|
+
default:
|
|
352
|
+
throw new Error('invalid address format');
|
|
353
|
+
}
|
|
354
|
+
return b58Encode(buf.slice(1, 21), pre);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* @description Parse binary Contract ID and return Base58 representation
|
|
358
|
+
* @param value Address to parse (tz1, tz2, tz3, KT1, or sr1).
|
|
359
|
+
* @example encodeAddress('0000e96b9f8b19af9c7ffa0c0480e1977b295850961f') // return 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM'
|
|
360
|
+
*/
|
|
361
|
+
function encodeAddress(value) {
|
|
362
|
+
let buf;
|
|
363
|
+
if (typeof value === 'string') {
|
|
364
|
+
buf = hex2buf(value);
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
buf = value;
|
|
368
|
+
}
|
|
369
|
+
switch (buf[0]) {
|
|
370
|
+
case 0: // implicit
|
|
371
|
+
return encodeKeyHash(buf.slice(1));
|
|
372
|
+
case 1: // contract hash
|
|
373
|
+
return b58Encode(buf.slice(1, 21), constants_1.PrefixV2.ContractHash);
|
|
374
|
+
case 3: // smart rollup hash
|
|
375
|
+
return b58Encode(buf.slice(1, 21), constants_1.PrefixV2.SmartRollupHash);
|
|
376
|
+
case 4: // zk rollup hash
|
|
377
|
+
return b58Encode(buf.slice(1, 21), constants_1.PrefixV2.ZkRollupHash);
|
|
378
|
+
default:
|
|
379
|
+
throw new Error('invalid address format');
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* @description Base58 encode an address without predefined prefix
|
|
384
|
+
* @param value Address to base58 encode (tz4) hex dec
|
|
385
|
+
* @returns return address
|
|
386
|
+
* @example encodeBlsAddress('af2dc3c40667abc0e89c0ef40171d22aed08d5eb') // return 'tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8'
|
|
387
|
+
*/
|
|
388
|
+
function encodeBlsAddress(value) {
|
|
389
|
+
return b58Encode(value, constants_1.PrefixV2.BLS12_381PublicKeyHash);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* @description convert a fragment of Michelson code in hex string to an 'expr' prefix + base58 encoded BLAKE2b hash string
|
|
393
|
+
* @param value a fragment of Michelson code in hex string
|
|
394
|
+
* @returns return 'expr' prefix + base58 encoded BLAKE2b hash
|
|
395
|
+
* @example encodeExpr('050a000000160000b2e19a9e74440d86c59f13dab8a18ff873e889ea') // return 'exprv6UsC1sN3Fk2XfgcJCL8NCerP5rCGy1PRESZAqr7L2JdzX55EN'
|
|
396
|
+
*/
|
|
397
|
+
function encodeExpr(value) {
|
|
398
|
+
const blakeHash = (0, blake2b_1.hash)(hex2buf(value), 32);
|
|
399
|
+
return b58Encode(blakeHash, constants_1.PrefixV2.ScriptExpr);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* @description convert a signed operation in hex string to an 'op' prefix + base58 encoded BLAKE2b hash string
|
|
403
|
+
* @param value signed operation in hex string
|
|
404
|
+
* @returns return 'op' prefix + base58 encoded BLAKE2b hash
|
|
405
|
+
* @example encodeOpHash('0f185d8a30061e8134c162dbb7a6c3ab8f5fdb153363ccd6149b49a33481156a6c00b2e19a9e74440d86c59f13dab8a18ff873e889eaa304ab05da13000001f1585a7384f36e45fb43dc37e8ce172bced3e05700ff0000000002002110c033f3a990c2e46a3d6054ecc2f74072aae7a34b5ac4d9ce9edc11c2410a97695682108951786f05b361da03b97245dc9897e1955e08b5b8d9e153b0bdeb0d') // return 'opapqvVXmebRTCFd2GQFydr4tJj3V5QocQuTmuhbatcHm4Seo2t'
|
|
406
|
+
*/
|
|
407
|
+
function encodeOpHash(value) {
|
|
408
|
+
const blakeHash = (0, blake2b_1.hash)(hex2buf(value), 32);
|
|
409
|
+
return b58Encode(blakeHash, constants_1.PrefixV2.OperationHash);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* @description Convert an hex string to a Uint8Array
|
|
413
|
+
* @param hex Hex string to convert
|
|
414
|
+
* @throws {@link ValueConversionError}
|
|
415
|
+
*/
|
|
416
|
+
function hex2buf(hex) {
|
|
417
|
+
hex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
418
|
+
if (hex.length % 2 !== 0) {
|
|
419
|
+
throw new core_1.InvalidHexStringError(hex, `Expecting even number of characters`);
|
|
420
|
+
}
|
|
421
|
+
if (!hex.match(/^([\da-f]{2})*$/gi)) {
|
|
422
|
+
throw new core_1.InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
|
|
423
|
+
}
|
|
424
|
+
const res = new Uint8Array(hex.length / 2);
|
|
425
|
+
let j = 0;
|
|
426
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
427
|
+
const ss = hex.slice(i, i + 2);
|
|
428
|
+
const x = parseInt(ss, 16);
|
|
429
|
+
if (Number.isNaN(x)) {
|
|
430
|
+
throw new core_1.InvalidHexStringError(hex, `Only characters 0-9, a-f and A-F are expected. Optionally, it can be prefixed with '0x'`);
|
|
431
|
+
}
|
|
432
|
+
res[j++] = x;
|
|
433
|
+
}
|
|
434
|
+
return res;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* @description Merge 2 buffers together
|
|
438
|
+
* @param b1 First buffer
|
|
439
|
+
* @param b2 Second buffer
|
|
440
|
+
*/
|
|
441
|
+
function mergebuf(b1, b2) {
|
|
442
|
+
const r = new Uint8Array(b1.length + b2.length);
|
|
443
|
+
r.set(b1);
|
|
444
|
+
r.set(b2, b1.length);
|
|
445
|
+
return r;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* @description Flatten a michelson json representation to an array
|
|
449
|
+
* @param s michelson json
|
|
450
|
+
*/
|
|
451
|
+
function mic2arr(s) {
|
|
452
|
+
let ret = [];
|
|
453
|
+
if (Object.prototype.hasOwnProperty.call(s, 'prim')) {
|
|
454
|
+
if (s.prim === 'Pair') {
|
|
455
|
+
ret.push(mic2arr(s.args[0]));
|
|
456
|
+
ret = ret.concat(mic2arr(s.args[1]));
|
|
457
|
+
}
|
|
458
|
+
else if (s.prim === 'Elt') {
|
|
459
|
+
ret = {
|
|
460
|
+
key: mic2arr(s.args[0]),
|
|
461
|
+
val: mic2arr(s.args[1]),
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
else if (s.prim === 'True') {
|
|
465
|
+
ret = true;
|
|
466
|
+
}
|
|
467
|
+
else if (s.prim === 'False') {
|
|
468
|
+
ret = false;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
else if (Array.isArray(s)) {
|
|
472
|
+
const sc = s.length;
|
|
473
|
+
for (let i = 0; i < sc; i++) {
|
|
474
|
+
const n = mic2arr(s[i]);
|
|
475
|
+
if (typeof n.key !== 'undefined') {
|
|
476
|
+
if (Array.isArray(ret)) {
|
|
477
|
+
ret = {
|
|
478
|
+
keys: [],
|
|
479
|
+
vals: [],
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
ret.keys.push(n.key);
|
|
483
|
+
ret.vals.push(n.val);
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
ret.push(n);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
else if (Object.prototype.hasOwnProperty.call(s, 'string')) {
|
|
491
|
+
ret = s.string;
|
|
492
|
+
}
|
|
493
|
+
else if (Object.prototype.hasOwnProperty.call(s, 'int')) {
|
|
494
|
+
ret = parseInt(s.int, 10);
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
ret = s;
|
|
498
|
+
}
|
|
499
|
+
return ret;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* @description Convert a Uint8Array to an hex string
|
|
503
|
+
* @param buffer Uint8Array to convert
|
|
504
|
+
*/
|
|
505
|
+
function buf2hex(bytes) {
|
|
506
|
+
return Array.from(bytes)
|
|
507
|
+
.map((x) => ((x >> 4) & 0xf).toString(16) + (x & 0xf).toString(16))
|
|
508
|
+
.join('');
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* @description Convert a string to a byte string representation
|
|
512
|
+
* @param str String to convert
|
|
513
|
+
*/
|
|
514
|
+
function stringToBytes(str) {
|
|
515
|
+
return buffer_1.Buffer.from(str, 'utf8').toString('hex');
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* @description Convert byte string representation to string
|
|
519
|
+
* @param hex byte string to convert
|
|
520
|
+
*/
|
|
521
|
+
function bytesToString(hex) {
|
|
522
|
+
return buffer_1.Buffer.from(hex2buf(hex)).toString('utf8');
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* @description Convert hex string/UintArray/Buffer to bytes
|
|
526
|
+
* @param hex String value to convert to bytes
|
|
527
|
+
*/
|
|
528
|
+
function hex2Bytes(hex) {
|
|
529
|
+
const hexDigits = stripHexPrefix(hex);
|
|
530
|
+
if (!hexDigits.match(/^(0x)?([\da-f]{2})*$/gi)) {
|
|
531
|
+
throw new core_1.InvalidHexStringError(hex, `Expecting even number of characters: 0-9, a-z, A-Z, optionally prefixed with 0x`);
|
|
532
|
+
}
|
|
533
|
+
return buffer_1.Buffer.from(hexDigits, 'hex');
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* @description Converts a number or Bignumber to hexadecimal string
|
|
537
|
+
* @param val The value that will be converted to a hexadecimal string value
|
|
538
|
+
*/
|
|
539
|
+
function toHexBuf(val, bitLength = 8) {
|
|
540
|
+
return buffer_1.Buffer.from(num2PaddedHex(val, bitLength), 'hex');
|
|
541
|
+
}
|
|
542
|
+
function numToHexBuffer(val, bitLength = 8) {
|
|
543
|
+
return buffer_1.Buffer.from(num2PaddedHex(val, bitLength), 'hex');
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* @description Converts a number or BigNumber to a padded hexadecimal string
|
|
547
|
+
* @param val The value that will be converted into a padded hexadecimal string value
|
|
548
|
+
* @param bitLength The length of bits
|
|
549
|
+
*
|
|
550
|
+
*/
|
|
551
|
+
function num2PaddedHex(val, bitLength = 8) {
|
|
552
|
+
if (new bignumber_js_1.default(val).isPositive()) {
|
|
553
|
+
const nibbleLength = Math.ceil(bitLength / 4);
|
|
554
|
+
const hex = val.toString(16);
|
|
555
|
+
// check whether nibble (4 bits) length is higher or lower than the current hex string length
|
|
556
|
+
let targetLength = hex.length >= nibbleLength ? hex.length : nibbleLength;
|
|
557
|
+
// make sure the hex string target length is even
|
|
558
|
+
targetLength = targetLength % 2 == 0 ? targetLength : targetLength + 1;
|
|
559
|
+
return padHexWithZero(hex, targetLength);
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
const twosCompliment = new bignumber_js_1.default(2)
|
|
563
|
+
.pow(bitLength)
|
|
564
|
+
.minus(new bignumber_js_1.default(val).abs());
|
|
565
|
+
return twosCompliment.toString(16);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
function padHexWithZero(hex, targetLength) {
|
|
569
|
+
const padString = '0';
|
|
570
|
+
if (hex.length >= targetLength) {
|
|
571
|
+
return hex;
|
|
572
|
+
}
|
|
573
|
+
else {
|
|
574
|
+
const padLength = targetLength - hex.length;
|
|
575
|
+
return padString.repeat(padLength) + hex;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
*
|
|
580
|
+
* @description Strips the first 2 characters of a hex string (0x)
|
|
581
|
+
*
|
|
582
|
+
* @param hex string to strip prefix from
|
|
583
|
+
*/
|
|
584
|
+
function stripHexPrefix(hex) {
|
|
585
|
+
return hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
586
|
+
}
|
|
587
|
+
function splitAddress(addr) {
|
|
588
|
+
const i = addr.indexOf('%');
|
|
589
|
+
if (i >= 0) {
|
|
590
|
+
return [addr.slice(0, i), addr.slice(i)];
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
return [addr, null];
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function compareArrays(a, b) {
|
|
597
|
+
let i = 0;
|
|
598
|
+
while (i < a.length && i < b.length && a[i] === b[i])
|
|
599
|
+
i++;
|
|
600
|
+
const aa = i < a.length ? a[i] : 0;
|
|
601
|
+
const bb = i < b.length ? b[i] : 0;
|
|
602
|
+
return aa < bb ? -1 : aa > bb ? 1 : 0;
|
|
603
|
+
}
|