@tezos-x/octez.js-michel-codec 0.9.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/README.md +174 -0
- package/dist/lib/base58.js +255 -0
- package/dist/lib/binary.js +1272 -0
- package/dist/lib/errors.js +90 -0
- package/dist/lib/formatters.js +60 -0
- package/dist/lib/global-constants.js +12 -0
- package/dist/lib/macros.js +548 -0
- package/dist/lib/micheline-emitter.js +132 -0
- package/dist/lib/micheline-parser.js +403 -0
- package/dist/lib/micheline.js +5 -0
- package/dist/lib/michelson-contract.js +93 -0
- package/dist/lib/michelson-typecheck.js +1752 -0
- package/dist/lib/michelson-types.js +83 -0
- package/dist/lib/michelson-validator.js +835 -0
- package/dist/lib/octez.js-michel-codec.js +38 -0
- package/dist/lib/scan.js +128 -0
- package/dist/lib/utils.js +321 -0
- package/dist/lib/version.js +8 -0
- package/dist/octez.js-michel-codec.es6.js +5790 -0
- package/dist/octez.js-michel-codec.es6.js.map +1 -0
- package/dist/octez.js-michel-codec.umd.js +5854 -0
- package/dist/octez.js-michel-codec.umd.js.map +1 -0
- package/dist/types/base58.d.ts +13 -0
- package/dist/types/binary.d.ts +127 -0
- package/dist/types/errors.d.ts +57 -0
- package/dist/types/formatters.d.ts +6 -0
- package/dist/types/global-constants.d.ts +3 -0
- package/dist/types/macros.d.ts +13 -0
- package/dist/types/micheline-emitter.d.ts +17 -0
- package/dist/types/micheline-parser.d.ts +130 -0
- package/dist/types/micheline.d.ts +44 -0
- package/dist/types/michelson-contract.d.ts +24 -0
- package/dist/types/michelson-typecheck.d.ts +38 -0
- package/dist/types/michelson-types.d.ts +147 -0
- package/dist/types/michelson-validator.d.ts +66 -0
- package/dist/types/octez.js-michel-codec.d.ts +16 -0
- package/dist/types/scan.d.ts +26 -0
- package/dist/types/utils.d.ts +77 -0
- package/dist/types/version.d.ts +4 -0
- package/package.json +94 -0
- package/signature.json +315 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Parser = exports.JSONParseError = exports.MichelineParseError = void 0;
|
|
4
|
+
const scan_1 = require("./scan");
|
|
5
|
+
const micheline_1 = require("./micheline");
|
|
6
|
+
const macros_1 = require("./macros");
|
|
7
|
+
const global_constants_1 = require("./global-constants");
|
|
8
|
+
const octez_js_core_1 = require("@tezos-x/octez.js-core");
|
|
9
|
+
/**
|
|
10
|
+
* @category Error
|
|
11
|
+
* @description Error that indicates a failure when parsing Micheline expressions
|
|
12
|
+
*/
|
|
13
|
+
class MichelineParseError extends octez_js_core_1.TaquitoError {
|
|
14
|
+
/**
|
|
15
|
+
* @param token A token caused the error
|
|
16
|
+
* @param message An error message
|
|
17
|
+
*/
|
|
18
|
+
constructor(token, message) {
|
|
19
|
+
super();
|
|
20
|
+
this.token = token;
|
|
21
|
+
this.message = message;
|
|
22
|
+
this.name = 'MichelineParseError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.MichelineParseError = MichelineParseError;
|
|
26
|
+
/**
|
|
27
|
+
* @category Error
|
|
28
|
+
* @description Error indicates a failure when parsing Micheline JSON
|
|
29
|
+
*/
|
|
30
|
+
class JSONParseError extends octez_js_core_1.TaquitoError {
|
|
31
|
+
/**
|
|
32
|
+
* @param node A node caused the error
|
|
33
|
+
* @param message An error message
|
|
34
|
+
*/
|
|
35
|
+
constructor(node, message) {
|
|
36
|
+
super();
|
|
37
|
+
this.node = node;
|
|
38
|
+
this.message = message;
|
|
39
|
+
this.name = 'JSONParseError';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.JSONParseError = JSONParseError;
|
|
43
|
+
const errEOF = new MichelineParseError(null, 'Unexpected EOF');
|
|
44
|
+
function isAnnotation(tok) {
|
|
45
|
+
return tok.t === scan_1.Literal.Ident && (tok.v[0] === '@' || tok.v[0] === '%' || tok.v[0] === ':');
|
|
46
|
+
}
|
|
47
|
+
const intRe = new RegExp('^-?[0-9]+$');
|
|
48
|
+
const bytesRe = new RegExp('^([0-9a-fA-F]{2})*$');
|
|
49
|
+
/**
|
|
50
|
+
* Converts and validates Michelson expressions between JSON-based Michelson and Micheline
|
|
51
|
+
*
|
|
52
|
+
* Pretty Print a Michelson Smart Contract:
|
|
53
|
+
* ```
|
|
54
|
+
* const contract = await Tezos.contract.at("KT1Vsw3kh9638gqWoHTjvHCoHLPKvCbMVbCg");
|
|
55
|
+
* const p = new Parser();
|
|
56
|
+
*
|
|
57
|
+
* const michelsonCode = p.parseJSON(contract.script.code);
|
|
58
|
+
* const storage = p.parseJSON(contract.script.storage);
|
|
59
|
+
*
|
|
60
|
+
* console.log("Pretty print Michelson smart contract:");
|
|
61
|
+
* console.log(emitMicheline(michelsonCode, {indent:" ", newline: "\n",}));
|
|
62
|
+
*
|
|
63
|
+
* console.log("Pretty print Storage:");
|
|
64
|
+
* console.log(emitMicheline(storage, {indent:" ", newline: "\n",}));
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* Encode a Michelson expression for initial storage of a smart contract
|
|
68
|
+
* ```
|
|
69
|
+
* const src = `(Pair (Pair { Elt 1
|
|
70
|
+
* (Pair (Pair "tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx")
|
|
71
|
+
* 0x0501000000026869) }
|
|
72
|
+
* 10000000)
|
|
73
|
+
* (Pair 2 333))`;
|
|
74
|
+
*
|
|
75
|
+
* const p = new Parser();
|
|
76
|
+
*
|
|
77
|
+
* const exp = p.parseMichelineExpression(src);
|
|
78
|
+
* console.log(JSON.stringify(exp));
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
class Parser {
|
|
82
|
+
constructor(opt) {
|
|
83
|
+
this.opt = opt;
|
|
84
|
+
}
|
|
85
|
+
expand(ex) {
|
|
86
|
+
var _a, _b, _c;
|
|
87
|
+
if (((_a = this.opt) === null || _a === void 0 ? void 0 : _a.expandGlobalConstant) !== undefined && ex.prim === 'constant') {
|
|
88
|
+
const ret = (0, global_constants_1.expandGlobalConstants)(ex, this.opt.expandGlobalConstant);
|
|
89
|
+
if (ret !== ex) {
|
|
90
|
+
ret[micheline_1.sourceReference] = Object.assign(Object.assign({}, (ex[micheline_1.sourceReference] || { first: 0, last: 0 })), { globalConstant: ex });
|
|
91
|
+
}
|
|
92
|
+
return ret;
|
|
93
|
+
}
|
|
94
|
+
if (((_b = this.opt) === null || _b === void 0 ? void 0 : _b.expandMacros) !== undefined ? (_c = this.opt) === null || _c === void 0 ? void 0 : _c.expandMacros : true) {
|
|
95
|
+
const ret = (0, macros_1.expandMacros)(ex, this.opt);
|
|
96
|
+
if (ret !== ex) {
|
|
97
|
+
ret[micheline_1.sourceReference] = Object.assign(Object.assign({}, (ex[micheline_1.sourceReference] || { first: 0, last: 0 })), { macro: ex });
|
|
98
|
+
}
|
|
99
|
+
return ret;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
return ex;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
parseListExpr(scanner, start) {
|
|
106
|
+
var _a;
|
|
107
|
+
const ref = {
|
|
108
|
+
first: start.first,
|
|
109
|
+
last: start.last,
|
|
110
|
+
};
|
|
111
|
+
const expectBracket = start.t === '(';
|
|
112
|
+
let tok;
|
|
113
|
+
if (expectBracket) {
|
|
114
|
+
tok = scanner.next();
|
|
115
|
+
if (tok.done) {
|
|
116
|
+
throw errEOF;
|
|
117
|
+
}
|
|
118
|
+
ref.last = tok.value.last;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
tok = { value: start };
|
|
122
|
+
}
|
|
123
|
+
if (tok.value.t !== scan_1.Literal.Ident) {
|
|
124
|
+
throw new MichelineParseError(tok.value, `not an identifier: ${tok.value.v}`);
|
|
125
|
+
}
|
|
126
|
+
const ret = {
|
|
127
|
+
prim: tok.value.v,
|
|
128
|
+
[micheline_1.sourceReference]: ref,
|
|
129
|
+
};
|
|
130
|
+
for (;;) {
|
|
131
|
+
const tok = scanner.next();
|
|
132
|
+
if (tok.done) {
|
|
133
|
+
if (expectBracket) {
|
|
134
|
+
throw errEOF;
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
else if (tok.value.t === ')') {
|
|
139
|
+
if (!expectBracket) {
|
|
140
|
+
throw new MichelineParseError(tok.value, 'unexpected closing bracket');
|
|
141
|
+
}
|
|
142
|
+
ref.last = tok.value.last;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
else if (isAnnotation(tok.value)) {
|
|
146
|
+
ret.annots = ret.annots || [];
|
|
147
|
+
ret.annots.push(tok.value.v);
|
|
148
|
+
ref.last = tok.value.last;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
ret.args = ret.args || [];
|
|
152
|
+
const arg = this.parseExpr(scanner, tok.value);
|
|
153
|
+
ref.last = ((_a = arg[micheline_1.sourceReference]) === null || _a === void 0 ? void 0 : _a.last) || ref.last;
|
|
154
|
+
ret.args.push(arg);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return this.expand(ret);
|
|
158
|
+
}
|
|
159
|
+
parseArgs(scanner, start) {
|
|
160
|
+
var _a;
|
|
161
|
+
// Identifier with arguments
|
|
162
|
+
const ref = {
|
|
163
|
+
first: start.first,
|
|
164
|
+
last: start.last,
|
|
165
|
+
};
|
|
166
|
+
const p = {
|
|
167
|
+
prim: start.v,
|
|
168
|
+
[micheline_1.sourceReference]: ref,
|
|
169
|
+
};
|
|
170
|
+
for (;;) {
|
|
171
|
+
const t = scanner.next();
|
|
172
|
+
if (t.done || t.value.t === '}' || t.value.t === ';') {
|
|
173
|
+
return [p, t];
|
|
174
|
+
}
|
|
175
|
+
if (isAnnotation(t.value)) {
|
|
176
|
+
ref.last = t.value.last;
|
|
177
|
+
p.annots = p.annots || [];
|
|
178
|
+
p.annots.push(t.value.v);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
const arg = this.parseExpr(scanner, t.value);
|
|
182
|
+
ref.last = ((_a = arg[micheline_1.sourceReference]) === null || _a === void 0 ? void 0 : _a.last) || ref.last;
|
|
183
|
+
p.args = p.args || [];
|
|
184
|
+
p.args.push(arg);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
parseSequenceExpr(scanner, start) {
|
|
189
|
+
var _a, _b;
|
|
190
|
+
const ref = {
|
|
191
|
+
first: start.first,
|
|
192
|
+
last: start.last,
|
|
193
|
+
};
|
|
194
|
+
const seq = [];
|
|
195
|
+
seq[micheline_1.sourceReference] = ref;
|
|
196
|
+
const expectBracket = start.t === '{';
|
|
197
|
+
let tok = start.t === '{' ? null : { value: start };
|
|
198
|
+
for (;;) {
|
|
199
|
+
if (tok === null) {
|
|
200
|
+
tok = scanner.next();
|
|
201
|
+
if (!tok.done) {
|
|
202
|
+
ref.last = tok.value.last;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (tok.done) {
|
|
206
|
+
if (expectBracket) {
|
|
207
|
+
throw errEOF;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
return seq;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (tok.value.t === '}') {
|
|
214
|
+
if (!expectBracket) {
|
|
215
|
+
throw new MichelineParseError(tok.value, 'unexpected closing bracket');
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
return seq;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
else if (tok.value.t === scan_1.Literal.Ident) {
|
|
222
|
+
// Identifier with arguments
|
|
223
|
+
const [itm, n] = this.parseArgs(scanner, tok.value);
|
|
224
|
+
ref.last = ((_a = itm[micheline_1.sourceReference]) === null || _a === void 0 ? void 0 : _a.last) || ref.last;
|
|
225
|
+
seq.push(this.expand(itm));
|
|
226
|
+
tok = n;
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
// Other
|
|
230
|
+
const ex = this.parseExpr(scanner, tok.value);
|
|
231
|
+
ref.last = ((_b = ex[micheline_1.sourceReference]) === null || _b === void 0 ? void 0 : _b.last) || ref.last;
|
|
232
|
+
seq.push(ex);
|
|
233
|
+
tok = null;
|
|
234
|
+
}
|
|
235
|
+
if (tok === null) {
|
|
236
|
+
tok = scanner.next();
|
|
237
|
+
if (!tok.done) {
|
|
238
|
+
ref.last = tok.value.last;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (!tok.done && tok.value.t === ';') {
|
|
242
|
+
tok = null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
parseExpr(scanner, tok) {
|
|
247
|
+
switch (tok.t) {
|
|
248
|
+
case scan_1.Literal.Ident:
|
|
249
|
+
return this.expand({
|
|
250
|
+
prim: tok.v,
|
|
251
|
+
[micheline_1.sourceReference]: { first: tok.first, last: tok.last },
|
|
252
|
+
});
|
|
253
|
+
case scan_1.Literal.Number:
|
|
254
|
+
return { int: tok.v, [micheline_1.sourceReference]: { first: tok.first, last: tok.last } };
|
|
255
|
+
case scan_1.Literal.String:
|
|
256
|
+
return {
|
|
257
|
+
string: JSON.parse(tok.v),
|
|
258
|
+
[micheline_1.sourceReference]: { first: tok.first, last: tok.last },
|
|
259
|
+
};
|
|
260
|
+
case scan_1.Literal.Bytes:
|
|
261
|
+
return { bytes: tok.v.slice(2), [micheline_1.sourceReference]: { first: tok.first, last: tok.last } };
|
|
262
|
+
case '{':
|
|
263
|
+
return this.parseSequenceExpr(scanner, tok);
|
|
264
|
+
default:
|
|
265
|
+
return this.parseListExpr(scanner, tok);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Parses a Micheline sequence expression, such as smart contract source. Enclosing curly brackets may be omitted.
|
|
270
|
+
* @param src A Micheline sequence `{parameter ...; storage int; code { DUP ; ...};}` or `parameter ...; storage int; code { DUP ; ...};`
|
|
271
|
+
*/
|
|
272
|
+
parseSequence(src) {
|
|
273
|
+
if (typeof src !== 'string') {
|
|
274
|
+
throw new TypeError(`string type was expected, got ${typeof src} instead`);
|
|
275
|
+
}
|
|
276
|
+
const scanner = (0, scan_1.scan)(src);
|
|
277
|
+
const tok = scanner.next();
|
|
278
|
+
if (tok.done) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
return this.parseSequenceExpr(scanner, tok.value);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Parse a Micheline sequence expression. Enclosing curly brackets may be omitted.
|
|
285
|
+
* @param src A Michelson list expression such as `(Pair {Elt "0" 0} 0)` or `Pair {Elt "0" 0} 0`
|
|
286
|
+
* @returns An AST node or null for empty document.
|
|
287
|
+
*/
|
|
288
|
+
parseList(src) {
|
|
289
|
+
if (typeof src !== 'string') {
|
|
290
|
+
throw new TypeError(`string type was expected, got ${typeof src} instead`);
|
|
291
|
+
}
|
|
292
|
+
const scanner = (0, scan_1.scan)(src);
|
|
293
|
+
const tok = scanner.next();
|
|
294
|
+
if (tok.done) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
return this.parseListExpr(scanner, tok.value);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Parse any Michelson expression
|
|
301
|
+
* @param src A Michelson expression such as `(Pair {Elt "0" 0} 0)` or `{parameter ...; storage int; code { DUP ; ...};}`
|
|
302
|
+
* @returns An AST node or null for empty document.
|
|
303
|
+
*/
|
|
304
|
+
parseMichelineExpression(src) {
|
|
305
|
+
if (typeof src !== 'string') {
|
|
306
|
+
throw new TypeError(`string type was expected, got ${typeof src} instead`);
|
|
307
|
+
}
|
|
308
|
+
const scanner = (0, scan_1.scan)(src);
|
|
309
|
+
const tok = scanner.next();
|
|
310
|
+
if (tok.done) {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
return this.parseExpr(scanner, tok.value);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Parse a Micheline sequence expression, such as smart contract source. Enclosing curly brackets may be omitted.
|
|
317
|
+
* An alias for `parseSequence`
|
|
318
|
+
* @param src A Micheline sequence `{parameter ...; storage int; code { DUP ; ...};}` or `parameter ...; storage int; code { DUP ; ...};`
|
|
319
|
+
*/
|
|
320
|
+
parseScript(src) {
|
|
321
|
+
return this.parseSequence(src);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Parse a Micheline sequence expression. Enclosing curly brackets may be omitted.
|
|
325
|
+
* An alias for `parseList`
|
|
326
|
+
* @param src A Michelson list expression such as `(Pair {Elt "0" 0} 0)` or `Pair {Elt "0" 0} 0`
|
|
327
|
+
* @returns An AST node or null for empty document.
|
|
328
|
+
*/
|
|
329
|
+
parseData(src) {
|
|
330
|
+
return this.parseList(src);
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Takes a JSON-encoded Michelson, validates it, strips away unneeded properties and optionally expands macros (See {@link ParserOptions}).
|
|
334
|
+
* @param src An object containing JSON-encoded Michelson, usually returned by `JSON.parse()`
|
|
335
|
+
*/
|
|
336
|
+
parseJSON(src) {
|
|
337
|
+
if (typeof src !== 'object') {
|
|
338
|
+
throw new TypeError(`object type was expected, got ${typeof src} instead`);
|
|
339
|
+
}
|
|
340
|
+
if (Array.isArray(src)) {
|
|
341
|
+
const ret = [];
|
|
342
|
+
for (const n of src) {
|
|
343
|
+
if (n === null || typeof n !== 'object') {
|
|
344
|
+
throw new JSONParseError(n, `unexpected sequence element: ${n}`);
|
|
345
|
+
}
|
|
346
|
+
ret.push(this.parseJSON(n));
|
|
347
|
+
}
|
|
348
|
+
return ret;
|
|
349
|
+
}
|
|
350
|
+
else if ('prim' in src) {
|
|
351
|
+
const p = src;
|
|
352
|
+
if (typeof p.prim === 'string' &&
|
|
353
|
+
(p.annots === undefined || Array.isArray(p.annots)) &&
|
|
354
|
+
(p.args === undefined || Array.isArray(p.args))) {
|
|
355
|
+
const ret = {
|
|
356
|
+
prim: p.prim,
|
|
357
|
+
};
|
|
358
|
+
if (p.annots !== undefined) {
|
|
359
|
+
for (const a of p.annots) {
|
|
360
|
+
if (typeof a !== 'string') {
|
|
361
|
+
throw new JSONParseError(a, `string expected: ${a}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
ret.annots = p.annots;
|
|
365
|
+
}
|
|
366
|
+
if (p.args !== undefined) {
|
|
367
|
+
ret.args = [];
|
|
368
|
+
for (const a of p.args) {
|
|
369
|
+
if (a === null || typeof a !== 'object') {
|
|
370
|
+
throw new JSONParseError(a, `unexpected argument: ${a}`);
|
|
371
|
+
}
|
|
372
|
+
ret.args.push(this.parseJSON(a));
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return this.expand(ret);
|
|
376
|
+
}
|
|
377
|
+
throw new JSONParseError(src, `malformed prim expression: ${src}`);
|
|
378
|
+
}
|
|
379
|
+
else if ('string' in src) {
|
|
380
|
+
if (typeof src.string === 'string') {
|
|
381
|
+
return { string: src.string };
|
|
382
|
+
}
|
|
383
|
+
throw new JSONParseError(src, `malformed string literal: ${src}`);
|
|
384
|
+
}
|
|
385
|
+
else if ('int' in src) {
|
|
386
|
+
if (typeof src.int === 'string' && intRe.test(src.int)) {
|
|
387
|
+
return { int: src.int };
|
|
388
|
+
}
|
|
389
|
+
throw new JSONParseError(src, `malformed int literal: ${src}`);
|
|
390
|
+
}
|
|
391
|
+
else if ('bytes' in src) {
|
|
392
|
+
if (typeof src.bytes === 'string' &&
|
|
393
|
+
bytesRe.test(src.bytes)) {
|
|
394
|
+
return { bytes: src.bytes };
|
|
395
|
+
}
|
|
396
|
+
throw new JSONParseError(src, `malformed bytes literal: ${src}`);
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
throw new JSONParseError(src, `unexpected object: ${src}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
exports.Parser = Parser;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Michelson abstract syntax tree types https://tezos.gitlab.io/whitedoc/michelson.html#concrete-syntax
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.sourceReference = void 0;
|
|
5
|
+
exports.sourceReference = Symbol('source_reference');
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dummyContract = exports.Contract = void 0;
|
|
4
|
+
const michelson_typecheck_1 = require("./michelson-typecheck");
|
|
5
|
+
const micheline_parser_1 = require("./micheline-parser");
|
|
6
|
+
const michelson_validator_1 = require("./michelson-validator");
|
|
7
|
+
const errors_1 = require("./errors");
|
|
8
|
+
class Contract {
|
|
9
|
+
constructor(contract, opt) {
|
|
10
|
+
this.contract = contract;
|
|
11
|
+
this.ctx = Object.assign({ contract }, opt);
|
|
12
|
+
this.output = (0, michelson_typecheck_1.assertContractValid)(contract, this.ctx);
|
|
13
|
+
}
|
|
14
|
+
static parse(src, opt) {
|
|
15
|
+
const p = new micheline_parser_1.Parser(opt);
|
|
16
|
+
const expr = typeof src === 'string' ? p.parseScript(src) : p.parseJSON(src);
|
|
17
|
+
if (expr === null) {
|
|
18
|
+
throw new errors_1.InvalidMichelsonError('empty Michelson');
|
|
19
|
+
}
|
|
20
|
+
if ((0, michelson_validator_1.assertMichelsonContract)(expr)) {
|
|
21
|
+
return new Contract(expr, opt);
|
|
22
|
+
}
|
|
23
|
+
throw undefined;
|
|
24
|
+
}
|
|
25
|
+
static parseTypeExpression(src, opt) {
|
|
26
|
+
const p = new micheline_parser_1.Parser(opt);
|
|
27
|
+
const expr = typeof src === 'string' ? p.parseScript(src) : p.parseJSON(src);
|
|
28
|
+
if (expr === null) {
|
|
29
|
+
throw new errors_1.InvalidTypeExpressionError('empty type expression');
|
|
30
|
+
}
|
|
31
|
+
// remove assertTypeAnnotationsValid from if block because: () => void || throw error
|
|
32
|
+
if ((0, michelson_validator_1.assertMichelsonType)(expr)) {
|
|
33
|
+
(0, michelson_typecheck_1.assertTypeAnnotationsValid)(expr);
|
|
34
|
+
return expr;
|
|
35
|
+
}
|
|
36
|
+
throw undefined;
|
|
37
|
+
}
|
|
38
|
+
static parseDataExpression(src, opt) {
|
|
39
|
+
const p = new micheline_parser_1.Parser(opt);
|
|
40
|
+
const expr = typeof src === 'string' ? p.parseScript(src) : p.parseJSON(src);
|
|
41
|
+
if (expr === null) {
|
|
42
|
+
throw new errors_1.InvalidDataExpressionError('empty data expression');
|
|
43
|
+
}
|
|
44
|
+
if ((0, michelson_validator_1.assertMichelsonData)(expr)) {
|
|
45
|
+
return expr;
|
|
46
|
+
}
|
|
47
|
+
throw undefined;
|
|
48
|
+
}
|
|
49
|
+
section(section) {
|
|
50
|
+
return (0, michelson_typecheck_1.contractSection)(this.contract, section);
|
|
51
|
+
}
|
|
52
|
+
entryPoints() {
|
|
53
|
+
return (0, michelson_typecheck_1.contractEntryPoints)(this.contract);
|
|
54
|
+
}
|
|
55
|
+
entryPoint(ep) {
|
|
56
|
+
return (0, michelson_typecheck_1.contractEntryPoint)(this.contract, ep);
|
|
57
|
+
}
|
|
58
|
+
assertDataValid(d, t) {
|
|
59
|
+
(0, michelson_typecheck_1.assertDataValid)(d, t, this.ctx);
|
|
60
|
+
}
|
|
61
|
+
isDataValid(d, t) {
|
|
62
|
+
return (0, michelson_typecheck_1.isDataValid)(d, t, this.ctx);
|
|
63
|
+
}
|
|
64
|
+
assertParameterValid(ep, d) {
|
|
65
|
+
const t = this.entryPoint(ep || undefined);
|
|
66
|
+
if (t === null) {
|
|
67
|
+
throw new errors_1.InvalidEntrypointError(ep === null || ep === void 0 ? void 0 : ep.toString());
|
|
68
|
+
}
|
|
69
|
+
this.assertDataValid(d, t);
|
|
70
|
+
}
|
|
71
|
+
isParameterValid(ep, d) {
|
|
72
|
+
try {
|
|
73
|
+
this.assertParameterValid(ep, d);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
catch (_a) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
functionType(inst, stack) {
|
|
81
|
+
return (0, michelson_typecheck_1.functionType)(inst, stack, this.ctx);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.Contract = Contract;
|
|
85
|
+
// TODO: dummyContract not used anywhere in the codebase can be deleted?
|
|
86
|
+
exports.dummyContract = new Contract([
|
|
87
|
+
{ prim: 'parameter', args: [{ prim: 'unit' }] },
|
|
88
|
+
{ prim: 'storage', args: [{ prim: 'unit' }] },
|
|
89
|
+
{
|
|
90
|
+
prim: 'code',
|
|
91
|
+
args: [[{ prim: 'CAR' }, { prim: 'NIL', args: [{ prim: 'operation' }] }, { prim: 'PAIR' }]],
|
|
92
|
+
},
|
|
93
|
+
]);
|