@guardian-network/policy-compiler 0.3.2

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.
Files changed (57) hide show
  1. package/README.md +34 -0
  2. package/dist/LacLangCompiler.d.ts +15 -0
  3. package/dist/LacLangCompiler.d.ts.map +1 -0
  4. package/dist/LacLangCompiler.js +87 -0
  5. package/dist/LacLangCompiler.js.map +1 -0
  6. package/dist/decorators/constants/error-annotations.constant.d.ts +4 -0
  7. package/dist/decorators/constants/error-annotations.constant.d.ts.map +1 -0
  8. package/dist/decorators/constants/error-annotations.constant.js +7 -0
  9. package/dist/decorators/constants/error-annotations.constant.js.map +1 -0
  10. package/dist/decorators/decorators-factory.d.ts +2 -0
  11. package/dist/decorators/decorators-factory.d.ts.map +1 -0
  12. package/dist/decorators/decorators-factory.js +16 -0
  13. package/dist/decorators/decorators-factory.js.map +1 -0
  14. package/dist/decorators/index.d.ts +3 -0
  15. package/dist/decorators/index.d.ts.map +1 -0
  16. package/dist/decorators/index.js +21 -0
  17. package/dist/decorators/index.js.map +1 -0
  18. package/dist/decorators/utils.helper.d.ts +2 -0
  19. package/dist/decorators/utils.helper.d.ts.map +1 -0
  20. package/dist/decorators/utils.helper.js +15 -0
  21. package/dist/decorators/utils.helper.js.map +1 -0
  22. package/dist/errors/ErrorFactory.d.ts +7 -0
  23. package/dist/errors/ErrorFactory.d.ts.map +1 -0
  24. package/dist/errors/ErrorFactory.js +17 -0
  25. package/dist/errors/ErrorFactory.js.map +1 -0
  26. package/dist/errors/index.d.ts +2 -0
  27. package/dist/errors/index.d.ts.map +1 -0
  28. package/dist/errors/index.js +6 -0
  29. package/dist/errors/index.js.map +1 -0
  30. package/dist/errors/validation-errors.d.ts +11 -0
  31. package/dist/errors/validation-errors.d.ts.map +1 -0
  32. package/dist/errors/validation-errors.js +26 -0
  33. package/dist/errors/validation-errors.js.map +1 -0
  34. package/dist/index.d.ts +2 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +6 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/utils.helper.d.ts +2 -0
  39. package/dist/utils.helper.d.ts.map +1 -0
  40. package/dist/utils.helper.js +16 -0
  41. package/dist/utils.helper.js.map +1 -0
  42. package/dist/validations.helper.d.ts +4 -0
  43. package/dist/validations.helper.d.ts.map +1 -0
  44. package/dist/validations.helper.js +38 -0
  45. package/dist/validations.helper.js.map +1 -0
  46. package/package.json +78 -0
  47. package/src/LacLangCompiler.ts +114 -0
  48. package/src/decorators/constants/error-annotations.constant.ts +5 -0
  49. package/src/decorators/decorators-factory.ts +22 -0
  50. package/src/decorators/index.ts +2 -0
  51. package/src/decorators/utils.helper.ts +12 -0
  52. package/src/errors/ErrorFactory.ts +25 -0
  53. package/src/errors/index.ts +1 -0
  54. package/src/errors/validation-errors.ts +22 -0
  55. package/src/index.ts +1 -0
  56. package/src/utils.helper.ts +14 -0
  57. package/src/validations.helper.ts +52 -0
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Policy definition DSL (LacLang) compiler
2
+ The package exposes programmatical compiler of LacLang sources.
3
+ The resulting object can be used directely to initialize policy onchain.
4
+
5
+ `LacLangCompiler` class, exported from the `src` directory, represents the LacLang compiler.
6
+ The instance of the compiler can be constructed both from file (path provided) or from sources (string provided).
7
+ ```javascript
8
+ const compiler = await LacLangCompiler.fromFile(lacSourcePath);
9
+ ```
10
+ ```javascript
11
+ const compiler = await LacLangCompiler.fromSources(lacSources);
12
+ ```
13
+
14
+ Also, compiler options can be provided.
15
+ Compiler options are setting the type checking mode - whether it will be onchain or DSL declarations.
16
+ If any of the type checking modes are selected, the JSON-RPC provider also must be defined. If neither were selected, types are not checked and the code is immediately translated.
17
+ ```javascript
18
+ interface LacLangCompilerOptions {
19
+ checkTypesAgainstOnchainDescriptors?: boolean;
20
+ checkTypesAgainstDslDeclarations?: boolean;
21
+ provider?: ContractRunner;
22
+ }
23
+ ```
24
+
25
+ The compilation results are obtained after invoking `compile` method on the preconfigured compiler:
26
+ ```javascript
27
+ const compilationResult = await compiler.compile();
28
+ ```
29
+ The resulting object can be immediately passed directely to the onchain policy handler.
30
+ ```javascript
31
+ await policyHandler.set(compilationResult);
32
+ ```
33
+
34
+ The compiler sequentially translates the DSL into IR, and then IR into final representation. FR is natively compatible with onchain policy handler.
@@ -0,0 +1,15 @@
1
+ import { ICompiler, LacLangCompilerOptions, OnchainPresentation, NodeTreeInitData as ParsingResult, TranspilerOutput } from '@guardian-network/shared';
2
+ export declare class LacLangCompiler implements ICompiler {
3
+ protected sources: string;
4
+ protected readonly options: LacLangCompilerOptions;
5
+ protected readonly _cwd: string;
6
+ protected static build<R>(this: new (sources: string, options: LacLangCompilerOptions, _cwd: string) => R, sources: string, options?: LacLangCompilerOptions): R;
7
+ static fromFile<R>(this: new (sources: string, options: LacLangCompilerOptions, _cwd: string) => R, sourcesPath: string, options?: LacLangCompilerOptions): Promise<R>;
8
+ static fromSources<R>(this: new (sources: string, options: LacLangCompilerOptions, _cwd: string) => R, sources: string, options?: LacLangCompilerOptions): R;
9
+ constructor(sources: string, options: LacLangCompilerOptions, _cwd: string);
10
+ compile(): Promise<OnchainPresentation>;
11
+ protected compileSources: () => Promise<OnchainPresentation>;
12
+ protected transpileDSL(cwd: string): Promise<TranspilerOutput>;
13
+ protected parseIntermediateRepresentation(transpilerOutput: TranspilerOutput): Promise<Array<ParsingResult>>;
14
+ }
15
+ //# sourceMappingURL=LacLangCompiler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LacLangCompiler.d.ts","sourceRoot":"","sources":["../src/LacLangCompiler.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,SAAS,EACT,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,IAAI,aAAa,EACjC,gBAAgB,EACjB,MAAM,0BAA0B,CAAC;AAelC,qBAAa,eAAgB,YAAW,SAAS;IAuC7C,SAAS,CAAC,OAAO,EAAE,MAAM;IACzB,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,sBAAsB;IAClD,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM;IAxCjC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EACtB,IAAI,EAAE,KACJ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,sBAAsB,EAC/B,IAAI,EAAE,MAAM,KACT,CAAC,EACN,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,sBAA2B,GACnC,CAAC;WAIS,QAAQ,CAAC,CAAC,EACrB,IAAI,EAAE,KACJ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,sBAAsB,EAC/B,IAAI,EAAE,MAAM,KACT,CAAC,EACN,WAAW,EAAE,MAAM,EACnB,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,CAAC,CAAC;IAKb,MAAM,CAAC,WAAW,CAAC,CAAC,EAClB,IAAI,EAAE,KACJ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,sBAAsB,EAC/B,IAAI,EAAE,MAAM,KACT,CAAC,EACN,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,sBAA2B,GACnC,CAAC;gBAKQ,OAAO,EAAE,MAAM,EACN,OAAO,EAAE,sBAAsB,EAC/B,IAAI,EAAE,MAAM;IAM3B,OAAO,IAAI,OAAO,CAAC,mBAAmB,CAAC;IAI7C,SAAS,CAAC,cAAc,QAAa,OAAO,CAAC,mBAAmB,CAAC,CAa/D;cAGc,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;cAYpD,+BAA+B,CAC7C,gBAAgB,EAAE,gBAAgB,GACjC,OAAO,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;CASjC"}
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.LacLangCompiler = void 0;
13
+ const policy_dsl_1 = require("@guardian-network/policy-dsl");
14
+ const policy_intermediate_representation_1 = require("@guardian-network/policy-intermediate-representation");
15
+ const path_1 = require("path");
16
+ const process_1 = require("process");
17
+ const decorators_1 = require("./decorators");
18
+ const utils_helper_1 = require("./utils.helper");
19
+ const validations_helper_1 = require("./validations.helper");
20
+ class LacLangCompiler {
21
+ sources;
22
+ options;
23
+ _cwd;
24
+ static build(sources, options = {}) {
25
+ return new this(sources, options, (0, process_1.cwd)());
26
+ }
27
+ static async fromFile(sourcesPath, options = {}) {
28
+ const sources = await (0, utils_helper_1.readFromFile)(sourcesPath);
29
+ return new this(sources, options, (0, path_1.dirname)(sourcesPath));
30
+ }
31
+ static fromSources(sources, options = {}) {
32
+ return new this(sources, options, (0, process_1.cwd)());
33
+ }
34
+ constructor(sources, options, _cwd) {
35
+ this.sources = sources;
36
+ this.options = options;
37
+ this._cwd = _cwd;
38
+ (0, validations_helper_1.validateProviderIsSupplied)(this.options);
39
+ }
40
+ async compile() {
41
+ return this.compileSources();
42
+ }
43
+ compileSources = async () => {
44
+ const transpilerOutput = await this.transpileDSL(this._cwd);
45
+ const parserOutput = await this.parseIntermediateRepresentation(transpilerOutput);
46
+ const finalRepresentation = {
47
+ rootNode: transpilerOutput.rootNode,
48
+ nodes: parserOutput,
49
+ };
50
+ (0, validations_helper_1.validateFinalRepresentation)(finalRepresentation);
51
+ return finalRepresentation;
52
+ };
53
+ async transpileDSL(cwd) {
54
+ const transpilerOutput = policy_dsl_1.Transpiler.create(this.sources, {
55
+ partialSources: false,
56
+ sourcesDir: cwd,
57
+ })
58
+ .transpile()
59
+ .toIntermediateRepresentation();
60
+ return Promise.resolve(transpilerOutput);
61
+ }
62
+ async parseIntermediateRepresentation(transpilerOutput) {
63
+ const parser = policy_intermediate_representation_1.ParserWithValidation.fromCompilerConfiguration(this.options, transpilerOutput);
64
+ const parserOutput = await parser.process();
65
+ return parserOutput;
66
+ }
67
+ }
68
+ exports.LacLangCompiler = LacLangCompiler;
69
+ __decorate([
70
+ (0, decorators_1.propagateWithAnnotation)(decorators_1.COMPILE_ANNOTATION),
71
+ __metadata("design:type", Function),
72
+ __metadata("design:paramtypes", []),
73
+ __metadata("design:returntype", Promise)
74
+ ], LacLangCompiler.prototype, "compile", null);
75
+ __decorate([
76
+ (0, decorators_1.propagateWithAnnotation)(decorators_1.TRANSPILE_ANNOTATION),
77
+ __metadata("design:type", Function),
78
+ __metadata("design:paramtypes", [String]),
79
+ __metadata("design:returntype", Promise)
80
+ ], LacLangCompiler.prototype, "transpileDSL", null);
81
+ __decorate([
82
+ (0, decorators_1.propagateWithAnnotation)(decorators_1.PARSING_ANNOTATION),
83
+ __metadata("design:type", Function),
84
+ __metadata("design:paramtypes", [Object]),
85
+ __metadata("design:returntype", Promise)
86
+ ], LacLangCompiler.prototype, "parseIntermediateRepresentation", null);
87
+ //# sourceMappingURL=LacLangCompiler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LacLangCompiler.js","sourceRoot":"","sources":["../src/LacLangCompiler.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6DAA0D;AAC1D,6GAA4F;AAQ5F,+BAA+B;AAC/B,qCAA8B;AAC9B,6CAKsB;AACtB,iDAA8C;AAC9C,6DAG8B;AAE9B,MAAa,eAAe;IAuCd;IACS;IACA;IAxCX,MAAM,CAAC,KAAK,CAMpB,OAAe,EACf,UAAkC,EAAE;QAEpC,OAAO,IAAI,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,IAAA,aAAG,GAAE,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,QAAQ,CAMnB,WAAmB,EACnB,UAAkC,EAAE;QAEpC,MAAM,OAAO,GAAG,MAAM,IAAA,2BAAY,EAAC,WAAW,CAAC,CAAC;QAChD,OAAO,IAAI,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,IAAA,cAAO,EAAC,WAAW,CAAC,CAAC,CAAC;IAC1D,CAAC;IAED,MAAM,CAAC,WAAW,CAMhB,OAAe,EACf,UAAkC,EAAE;QAEpC,OAAO,IAAI,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,IAAA,aAAG,GAAE,CAAC,CAAC;IAC3C,CAAC;IAED,YACY,OAAe,EACN,OAA+B,EAC/B,IAAY;QAFrB,YAAO,GAAP,OAAO,CAAQ;QACN,YAAO,GAAP,OAAO,CAAwB;QAC/B,SAAI,GAAJ,IAAI,CAAQ;QAE/B,IAAA,+CAA0B,EAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3C,CAAC;IAGK,AAAN,KAAK,CAAC,OAAO;QACX,OAAO,IAAI,CAAC,cAAc,EAAE,CAAC;IAC/B,CAAC;IAES,cAAc,GAAG,KAAK,IAAkC,EAAE;QAClE,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5D,MAAM,YAAY,GAChB,MAAM,IAAI,CAAC,+BAA+B,CAAC,gBAAgB,CAAC,CAAC;QAG/D,MAAM,mBAAmB,GAAwB;YAC/C,QAAQ,EAAE,gBAAgB,CAAC,QAAQ;YACnC,KAAK,EAAE,YAAY;SACpB,CAAC;QAEF,IAAA,gDAA2B,EAAC,mBAAmB,CAAC,CAAC;QACjD,OAAO,mBAAmB,CAAC;IAC7B,CAAC,CAAC;IAGc,AAAN,KAAK,CAAC,YAAY,CAAC,GAAW;QACtC,MAAM,gBAAgB,GAAG,uBAAU,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE;YACvD,cAAc,EAAE,KAAK;YACrB,UAAU,EAAE,GAAG;SAChB,CAAC;aACC,SAAS,EAAE;aACX,4BAA4B,EAAE,CAAC;QAElC,OAAO,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC3C,CAAC;IAGe,AAAN,KAAK,CAAC,+BAA+B,CAC7C,gBAAkC;QAElC,MAAM,MAAM,GAAG,yDAAoB,CAAC,yBAAyB,CAC3D,IAAI,CAAC,OAAO,EACZ,gBAAgB,CACjB,CAAC;QAEF,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;QAC5C,OAAO,YAAY,CAAC;IACtB,CAAC;CACF;AA1FD,0CA0FC;AA3CO;IADL,IAAA,oCAAuB,EAAC,+BAAkB,CAAC;;;;8CAG3C;AAkBe;IADf,IAAA,oCAAuB,EAAC,iCAAoB,CAAC;;;;mDAU7C;AAGe;IADf,IAAA,oCAAuB,EAAC,+BAAkB,CAAC;;;;sEAW3C"}
@@ -0,0 +1,4 @@
1
+ export declare const COMPILE_ANNOTATION = "Compile result (final-representation) validation error";
2
+ export declare const TRANSPILE_ANNOTATION = "DSL transpiler error during compilation";
3
+ export declare const PARSING_ANNOTATION = "Inermedtiate-representation parser error during compilation";
4
+ //# sourceMappingURL=error-annotations.constant.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-annotations.constant.d.ts","sourceRoot":"","sources":["../../../src/decorators/constants/error-annotations.constant.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,2DAC2B,CAAC;AAC3D,eAAO,MAAM,oBAAoB,4CAA4C,CAAC;AAC9E,eAAO,MAAM,kBAAkB,gEACgC,CAAC"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PARSING_ANNOTATION = exports.TRANSPILE_ANNOTATION = exports.COMPILE_ANNOTATION = void 0;
4
+ exports.COMPILE_ANNOTATION = 'Compile result (final-representation) validation error';
5
+ exports.TRANSPILE_ANNOTATION = 'DSL transpiler error during compilation';
6
+ exports.PARSING_ANNOTATION = 'Inermedtiate-representation parser error during compilation';
7
+ //# sourceMappingURL=error-annotations.constant.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-annotations.constant.js","sourceRoot":"","sources":["../../../src/decorators/constants/error-annotations.constant.ts"],"names":[],"mappings":";;;AAAa,QAAA,kBAAkB,GAC7B,wDAAwD,CAAC;AAC9C,QAAA,oBAAoB,GAAG,yCAAyC,CAAC;AACjE,QAAA,kBAAkB,GAC7B,6DAA6D,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const propagateWithAnnotation: (annotation: string) => (target: Object, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
2
+ //# sourceMappingURL=decorators-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators-factory.d.ts","sourceRoot":"","sources":["../../src/decorators/decorators-factory.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,uBAAuB,eAAgB,MAAM,cAE9C,MAAM,cACF,MAAM,cACN,kBAAkB,KAC7B,kBAcJ,CAAC"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.propagateWithAnnotation = void 0;
4
+ const utils_helper_1 = require("./utils.helper");
5
+ const propagateWithAnnotation = (annotation) => {
6
+ const decorator = (target, methodName, descriptor) => {
7
+ const originalMethod = descriptor.value;
8
+ descriptor.value = function (...args) {
9
+ return (0, utils_helper_1.execOrPropagate)(() => originalMethod.apply(this, args), annotation);
10
+ };
11
+ return descriptor;
12
+ };
13
+ return decorator;
14
+ };
15
+ exports.propagateWithAnnotation = propagateWithAnnotation;
16
+ //# sourceMappingURL=decorators-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators-factory.js","sourceRoot":"","sources":["../../src/decorators/decorators-factory.ts"],"names":[],"mappings":";;;AAAA,iDAAiD;AAE1C,MAAM,uBAAuB,GAAG,CAAC,UAAkB,EAAE,EAAE;IAC5D,MAAM,SAAS,GAAG,CAChB,MAAc,EACd,UAAkB,EAClB,UAA8B,EACV,EAAE;QACtB,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAExC,UAAU,CAAC,KAAK,GAAG,UAAU,GAAG,IAAW;YACzC,OAAO,IAAA,8BAAe,EACpB,GAAG,EAAE,CAAC,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,EACtC,UAAU,CACX,CAAC;QACJ,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;IAEF,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAnBW,QAAA,uBAAuB,2BAmBlC"}
@@ -0,0 +1,3 @@
1
+ export * from './constants/error-annotations.constant';
2
+ export { propagateWithAnnotation } from './decorators-factory';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC;AACvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.propagateWithAnnotation = void 0;
18
+ __exportStar(require("./constants/error-annotations.constant"), exports);
19
+ var decorators_factory_1 = require("./decorators-factory");
20
+ Object.defineProperty(exports, "propagateWithAnnotation", { enumerable: true, get: function () { return decorators_factory_1.propagateWithAnnotation; } });
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,yEAAuD;AACvD,2DAA+D;AAAtD,6HAAA,uBAAuB,OAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const execOrPropagate: <K>(method: () => K | Promise<K>, annotation: string) => Promise<Awaited<K>>;
2
+ //# sourceMappingURL=utils.helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.helper.d.ts","sourceRoot":"","sources":["../../src/decorators/utils.helper.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,GAAU,CAAC,UAC7B,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,cAChB,MAAM,KACjB,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAQpB,CAAC"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.execOrPropagate = void 0;
4
+ const execOrPropagate = async (method, annotation) => {
5
+ try {
6
+ const result = await method();
7
+ return result;
8
+ }
9
+ catch (e) {
10
+ console.error(annotation);
11
+ throw e;
12
+ }
13
+ };
14
+ exports.execOrPropagate = execOrPropagate;
15
+ //# sourceMappingURL=utils.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.helper.js","sourceRoot":"","sources":["../../src/decorators/utils.helper.ts"],"names":[],"mappings":";;;AAAO,MAAM,eAAe,GAAG,KAAK,EAClC,MAA4B,EAC5B,UAAkB,EACG,EAAE;IACvB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,MAAM,EAAE,CAAC;QAC9B,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QAC1B,MAAM,CAAC,CAAC;IACV,CAAC;AACH,CAAC,CAAC;AAXW,QAAA,eAAe,mBAW1B"}
@@ -0,0 +1,7 @@
1
+ import { CyclicReferenceError, NoProviderError, SelfReferenceError } from './validation-errors';
2
+ export declare class ErrorFactory {
3
+ static selfReference: (selfNode: string) => SelfReferenceError;
4
+ static cyclicfReference: (referencedNode: string, selfNode: string) => CyclicReferenceError;
5
+ static noProvider: () => NoProviderError;
6
+ }
7
+ //# sourceMappingURL=ErrorFactory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorFactory.d.ts","sourceRoot":"","sources":["../../src/errors/ErrorFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,kBAAkB,EACnB,MAAM,qBAAqB,CAAC;AAE7B,qBAAa,YAAY;IACvB,MAAM,CAAC,aAAa,2CAIlB;IAEF,MAAM,CAAC,gBAAgB,qEAIrB;IAEF,MAAM,CAAC,UAAU,wBAIf;CACH"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorFactory = void 0;
4
+ const validation_errors_1 = require("./validation-errors");
5
+ class ErrorFactory {
6
+ static selfReference = (...params) => {
7
+ return validation_errors_1.SelfReferenceError.create(...params);
8
+ };
9
+ static cyclicfReference = (...params) => {
10
+ return validation_errors_1.CyclicReferenceError.create(...params);
11
+ };
12
+ static noProvider = (...params) => {
13
+ return validation_errors_1.NoProviderError.create(...params);
14
+ };
15
+ }
16
+ exports.ErrorFactory = ErrorFactory;
17
+ //# sourceMappingURL=ErrorFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorFactory.js","sourceRoot":"","sources":["../../src/errors/ErrorFactory.ts"],"names":[],"mappings":";;;AAAA,2DAI6B;AAE7B,MAAa,YAAY;IACvB,MAAM,CAAC,aAAa,GAAG,CACrB,GAAG,MAAoD,EACvD,EAAE;QACF,OAAO,sCAAkB,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,MAAM,CAAC,gBAAgB,GAAG,CACxB,GAAG,MAAsD,EACzD,EAAE;QACF,OAAO,wCAAoB,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC;IAChD,CAAC,CAAC;IAEF,MAAM,CAAC,UAAU,GAAG,CAClB,GAAG,MAAiD,EACpD,EAAE;QACF,OAAO,mCAAe,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC;IAC3C,CAAC,CAAC;;AAjBJ,oCAkBC"}
@@ -0,0 +1,2 @@
1
+ export { ErrorFactory } from './ErrorFactory';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorFactory = void 0;
4
+ var ErrorFactory_1 = require("./ErrorFactory");
5
+ Object.defineProperty(exports, "ErrorFactory", { enumerable: true, get: function () { return ErrorFactory_1.ErrorFactory; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":";;;AAAA,+CAA8C;AAArC,4GAAA,YAAY,OAAA"}
@@ -0,0 +1,11 @@
1
+ import { BaseError } from '@guardian-network/shared';
2
+ export declare class CyclicReferenceError extends BaseError {
3
+ static create: (referencedNode: string, selfNode: string) => CyclicReferenceError;
4
+ }
5
+ export declare class SelfReferenceError extends BaseError {
6
+ static create: (selfNode: string) => SelfReferenceError;
7
+ }
8
+ export declare class NoProviderError extends BaseError {
9
+ static create: () => NoProviderError;
10
+ }
11
+ //# sourceMappingURL=validation-errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-errors.d.ts","sourceRoot":"","sources":["../../src/errors/validation-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAErD,qBAAa,oBAAqB,SAAQ,SAAS;IACjD,MAAM,CAAC,MAAM,mBAAoB,MAAM,YAAY,MAAM,0BAGvD;CACH;AAED,qBAAa,kBAAmB,SAAQ,SAAS;IAC/C,MAAM,CAAC,MAAM,aAAc,MAAM,wBAG/B;CACH;AAED,qBAAa,eAAgB,SAAQ,SAAS;IAC5C,MAAM,CAAC,MAAM,wBAGX;CACH"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NoProviderError = exports.SelfReferenceError = exports.CyclicReferenceError = void 0;
4
+ const shared_1 = require("@guardian-network/shared");
5
+ class CyclicReferenceError extends shared_1.BaseError {
6
+ static create = (referencedNode, selfNode) => {
7
+ const errorMessage = `Node ${selfNode} cyclically references ${referencedNode}`;
8
+ return this.build(errorMessage);
9
+ };
10
+ }
11
+ exports.CyclicReferenceError = CyclicReferenceError;
12
+ class SelfReferenceError extends shared_1.BaseError {
13
+ static create = (selfNode) => {
14
+ const errorMessage = `Node ${selfNode} cyclically references itself`;
15
+ return this.build(errorMessage);
16
+ };
17
+ }
18
+ exports.SelfReferenceError = SelfReferenceError;
19
+ class NoProviderError extends shared_1.BaseError {
20
+ static create = () => {
21
+ const errorMessage = 'Provider needed if typing checks enabled';
22
+ return this.build(errorMessage);
23
+ };
24
+ }
25
+ exports.NoProviderError = NoProviderError;
26
+ //# sourceMappingURL=validation-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-errors.js","sourceRoot":"","sources":["../../src/errors/validation-errors.ts"],"names":[],"mappings":";;;AAAA,qDAAqD;AAErD,MAAa,oBAAqB,SAAQ,kBAAS;IACjD,MAAM,CAAC,MAAM,GAAG,CAAC,cAAsB,EAAE,QAAgB,EAAE,EAAE;QAC3D,MAAM,YAAY,GAAG,QAAQ,QAAQ,0BAA0B,cAAc,EAAE,CAAC;QAChF,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAClC,CAAC,CAAC;;AAJJ,oDAKC;AAED,MAAa,kBAAmB,SAAQ,kBAAS;IAC/C,MAAM,CAAC,MAAM,GAAG,CAAC,QAAgB,EAAE,EAAE;QACnC,MAAM,YAAY,GAAG,QAAQ,QAAQ,+BAA+B,CAAC;QACrE,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAClC,CAAC,CAAC;;AAJJ,gDAKC;AAED,MAAa,eAAgB,SAAQ,kBAAS;IAC5C,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE;QACnB,MAAM,YAAY,GAAG,0CAA0C,CAAC;QAChE,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAClC,CAAC,CAAC;;AAJJ,0CAKC"}
@@ -0,0 +1,2 @@
1
+ export { LacLangCompiler } from './LacLangCompiler';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LacLangCompiler = void 0;
4
+ var LacLangCompiler_1 = require("./LacLangCompiler");
5
+ Object.defineProperty(exports, "LacLangCompiler", { enumerable: true, get: function () { return LacLangCompiler_1.LacLangCompiler; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qDAAoD;AAA3C,kHAAA,eAAe,OAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const readFromFile: (filePath: string) => Promise<string>;
2
+ //# sourceMappingURL=utils.helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.helper.d.ts","sourceRoot":"","sources":["../src/utils.helper.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,YAAY,aAAoB,MAAM,KAAG,OAAO,CAAC,MAAM,CASnE,CAAC"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readFromFile = void 0;
4
+ const promises_1 = require("node:fs/promises");
5
+ const DEFAULT_ENCODING = 'utf-8';
6
+ const readFromFile = async (filePath) => {
7
+ try {
8
+ const conent = await (0, promises_1.readFile)(filePath, DEFAULT_ENCODING);
9
+ return conent;
10
+ }
11
+ catch (error) {
12
+ throw new Error(`Error reading file at ${filePath} with description ${error.message}`);
13
+ }
14
+ };
15
+ exports.readFromFile = readFromFile;
16
+ //# sourceMappingURL=utils.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.helper.js","sourceRoot":"","sources":["../src/utils.helper.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAE5C,MAAM,gBAAgB,GAAG,OAAO,CAAC;AAE1B,MAAM,YAAY,GAAG,KAAK,EAAE,QAAgB,EAAmB,EAAE;IACtE,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,IAAA,mBAAQ,EAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;QAC1D,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,KAAU,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CACb,yBAAyB,QAAQ,qBAAqB,KAAK,CAAC,OAAO,EAAE,CACtE,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AATW,QAAA,YAAY,gBASvB"}
@@ -0,0 +1,4 @@
1
+ import { LacLangCompilerOptions, OnchainPresentation } from '@guardian-network/shared';
2
+ export declare const validateProviderIsSupplied: (options: LacLangCompilerOptions) => void;
3
+ export declare const validateFinalRepresentation: (representation: OnchainPresentation) => void;
4
+ //# sourceMappingURL=validations.helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validations.helper.d.ts","sourceRoot":"","sources":["../src/validations.helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,sBAAsB,EACtB,mBAAmB,EACpB,MAAM,0BAA0B,CAAC;AAGlC,eAAO,MAAM,0BAA0B,YAAa,sBAAsB,SAQzE,CAAC;AAEF,eAAO,MAAM,2BAA2B,mBACtB,mBAAmB,SAWpC,CAAC"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateFinalRepresentation = exports.validateProviderIsSupplied = void 0;
4
+ const shared_1 = require("@guardian-network/shared");
5
+ const errors_1 = require("./errors");
6
+ const validateProviderIsSupplied = (options) => {
7
+ const isProviderSupplied = !!options.provider;
8
+ const needsDslTypesCheck = !!options.checkTypesAgainstDslDeclarations;
9
+ const needsOnchainTypesCheck = !!options.checkTypesAgainstOnchainDescriptors;
10
+ if ((needsDslTypesCheck || needsOnchainTypesCheck) && !isProviderSupplied) {
11
+ throw errors_1.ErrorFactory.noProvider();
12
+ }
13
+ };
14
+ exports.validateProviderIsSupplied = validateProviderIsSupplied;
15
+ const validateFinalRepresentation = (representation) => {
16
+ const nodes = representation.nodes.map(({ id, substitutedExecArgs: substitutions }) => ({
17
+ id: id.toString(),
18
+ references: substitutions.map((subst) => subst.supplierNodeId.toString()),
19
+ }));
20
+ validateCyclicity(nodes);
21
+ validateSelfReference(nodes);
22
+ };
23
+ exports.validateFinalRepresentation = validateFinalRepresentation;
24
+ const validateCyclicity = (nodes) => {
25
+ const cycleFound = (0, shared_1.findCycle)(nodes);
26
+ if (!!cycleFound) {
27
+ throw errors_1.ErrorFactory.cyclicfReference(cycleFound.nodeId, cycleFound.parentNodeId);
28
+ }
29
+ };
30
+ const validateSelfReference = (nodes) => {
31
+ for (let [, node] of nodes.entries()) {
32
+ const isSelfReference = node.references.includes(node.id);
33
+ if (isSelfReference) {
34
+ throw errors_1.ErrorFactory.selfReference(node.id);
35
+ }
36
+ }
37
+ };
38
+ //# sourceMappingURL=validations.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validations.helper.js","sourceRoot":"","sources":["../src/validations.helper.ts"],"names":[],"mappings":";;;AAAA,qDAKkC;AAClC,qCAAwC;AAEjC,MAAM,0BAA0B,GAAG,CAAC,OAA+B,EAAE,EAAE;IAC5E,MAAM,kBAAkB,GAAG,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC;IAC9C,MAAM,kBAAkB,GAAG,CAAC,CAAC,OAAO,CAAC,gCAAgC,CAAC;IACtE,MAAM,sBAAsB,GAAG,CAAC,CAAC,OAAO,CAAC,mCAAmC,CAAC;IAE7E,IAAI,CAAC,kBAAkB,IAAI,sBAAsB,CAAC,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1E,MAAM,qBAAY,CAAC,UAAU,EAAE,CAAC;IAClC,CAAC;AACH,CAAC,CAAC;AARW,QAAA,0BAA0B,8BAQrC;AAEK,MAAM,2BAA2B,GAAG,CACzC,cAAmC,EACnC,EAAE;IACF,MAAM,KAAK,GAAc,cAAc,CAAC,KAAK,CAAC,GAAG,CAC/C,CAAC,EAAE,EAAE,EAAE,mBAAmB,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC,CAAC;QAC/C,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE;QACjB,UAAU,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,QAAQ,EAAE,CAAC;KAC1E,CAAC,CACH,CAAC;IAEF,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACzB,qBAAqB,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC,CAAC;AAZW,QAAA,2BAA2B,+BAYtC;AAEF,MAAM,iBAAiB,GAAG,CAAC,KAAgB,EAAE,EAAE;IAC7C,MAAM,UAAU,GAAG,IAAA,kBAAS,EAAC,KAAK,CAAC,CAAC;IAEpC,IAAI,CAAC,CAAC,UAAU,EAAE,CAAC;QACjB,MAAM,qBAAY,CAAC,gBAAgB,CACjC,UAAU,CAAC,MAAM,EACjB,UAAU,CAAC,YAAY,CACxB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,CAAC,KAAgB,EAAE,EAAE;IACjD,KAAK,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QACrC,MAAM,eAAe,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAE1D,IAAI,eAAe,EAAE,CAAC;YACpB,MAAM,qBAAY,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;AACH,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@guardian-network/policy-compiler",
3
+ "author": "vpriadko@lacero.io, v.grabovski@lacero.io",
4
+ "description": "Artifcts high level declaration language",
5
+ "version": "0.3.2",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "engines": {
13
+ "node": ">=18.20.4"
14
+ },
15
+ "publishConfig": {
16
+ "registry": "https://registry.npmjs.org",
17
+ "access": "public"
18
+ },
19
+ "keywords": [],
20
+ "devDependencies": {
21
+ "@eslint/eslintrc": "^3.1.0",
22
+ "@eslint/js": "^9.13.0",
23
+ "@istanbuljs/nyc-config-typescript": "^1.0.2",
24
+ "@nomicfoundation/hardhat-ethers": "^3.1.3",
25
+ "@nomicfoundation/hardhat-toolbox": "6.1.2",
26
+ "@types/chai": "4.3.3",
27
+ "@types/mocha": "^10.0.9",
28
+ "@types/node": "^22.8.0",
29
+ "@typescript-eslint/eslint-plugin": "^8.61.1",
30
+ "@typescript-eslint/parser": "^8.8.0",
31
+ "chai": "^4.5.0",
32
+ "copyfiles": "^2.4.1",
33
+ "eslint": "^9.11.1",
34
+ "eslint-config-prettier": "^9.1.0",
35
+ "eslint-import-resolver-typescript": "^3.6.3",
36
+ "eslint-plugin-import": "^2.31.0",
37
+ "eslint-plugin-mocha": "^10.5.0",
38
+ "eslint-plugin-n": "^17.10.3",
39
+ "eslint-plugin-prettier": "^5.2.1",
40
+ "eslint-plugin-promise": "^7.1.0",
41
+ "eslint-plugin-unused-imports": "^4.1.4",
42
+ "globals": "^15.11.0",
43
+ "hardhat": "2.27.2",
44
+ "hardhat-deploy": "^0.12.4",
45
+ "mocha": "^10.8.2",
46
+ "nyc": "^18.0.0",
47
+ "prettier": "^3.3.3",
48
+ "prettier-plugin-organize-imports": "^4.1.0",
49
+ "rimraf": "^6.0.1"
50
+ },
51
+ "dependencies": {
52
+ "dotenv": "^16.4.5",
53
+ "ethers": "^6.16.0",
54
+ "ts-node": "^10.9.2",
55
+ "typescript": "^5.6.2",
56
+ "@guardian-network/shared": "0.3.2",
57
+ "@guardian-network/policy-intermediate-representation": "0.3.2",
58
+ "@guardian-network/policy-dsl": "0.3.2"
59
+ },
60
+ "scripts": {
61
+ "build": "pnpm format:all:fix && tsc -p tsconfig-prod.json && pnpm copy-dts",
62
+ "check": "pnpm lint:ts && pnpm check:ts",
63
+ "check:ts": "tsc -p tsconfig-prod.json --noEmit",
64
+ "check:ts:dev": "tsc -p tsconfig.json --noEmit",
65
+ "clean": "pnpm remove:dist",
66
+ "copy-dts": "copyfiles -u 1 \"src/**/*.d.ts\" dist",
67
+ "format:all:fix": "pnpm format:ts:fix",
68
+ "format:ts:fix": "prettier -w -c \"src/**/*.ts\" \"test/**/*.ts\"",
69
+ "lint:ts": "eslint",
70
+ "lint:ts:fix": "eslint --fix",
71
+ "publish-package": "pnpm publish --no-git-checks",
72
+ "remove:dist": "npx rimraf dist",
73
+ "test": "mocha \"test/*.test.ts\" ",
74
+ "tests:coverage:ts": "nyc pnpm test",
75
+ "test:onchain": "pnpm exec hardhat test --bail test/compiler-onchain-types.test.ts",
76
+ "version": "pnpm version"
77
+ }
78
+ }
@@ -0,0 +1,114 @@
1
+ import { Transpiler } from '@guardian-network/policy-dsl';
2
+ import { ParserWithValidation } from '@guardian-network/policy-intermediate-representation';
3
+ import {
4
+ ICompiler,
5
+ LacLangCompilerOptions,
6
+ OnchainPresentation,
7
+ NodeTreeInitData as ParsingResult,
8
+ TranspilerOutput,
9
+ } from '@guardian-network/shared';
10
+ import { dirname } from 'path';
11
+ import { cwd } from 'process';
12
+ import {
13
+ COMPILE_ANNOTATION,
14
+ PARSING_ANNOTATION,
15
+ propagateWithAnnotation,
16
+ TRANSPILE_ANNOTATION,
17
+ } from './decorators';
18
+ import { readFromFile } from './utils.helper';
19
+ import {
20
+ validateFinalRepresentation,
21
+ validateProviderIsSupplied,
22
+ } from './validations.helper';
23
+
24
+ export class LacLangCompiler implements ICompiler {
25
+ protected static build<R>(
26
+ this: new (
27
+ sources: string,
28
+ options: LacLangCompilerOptions,
29
+ _cwd: string,
30
+ ) => R,
31
+ sources: string,
32
+ options: LacLangCompilerOptions = {},
33
+ ): R {
34
+ return new this(sources, options, cwd());
35
+ }
36
+
37
+ static async fromFile<R>(
38
+ this: new (
39
+ sources: string,
40
+ options: LacLangCompilerOptions,
41
+ _cwd: string,
42
+ ) => R,
43
+ sourcesPath: string,
44
+ options: LacLangCompilerOptions = {},
45
+ ): Promise<R> {
46
+ const sources = await readFromFile(sourcesPath);
47
+ return new this(sources, options, dirname(sourcesPath));
48
+ }
49
+
50
+ static fromSources<R>(
51
+ this: new (
52
+ sources: string,
53
+ options: LacLangCompilerOptions,
54
+ _cwd: string,
55
+ ) => R,
56
+ sources: string,
57
+ options: LacLangCompilerOptions = {},
58
+ ): R {
59
+ return new this(sources, options, cwd());
60
+ }
61
+
62
+ constructor(
63
+ protected sources: string,
64
+ protected readonly options: LacLangCompilerOptions,
65
+ protected readonly _cwd: string,
66
+ ) {
67
+ validateProviderIsSupplied(this.options);
68
+ }
69
+
70
+ @propagateWithAnnotation(COMPILE_ANNOTATION)
71
+ async compile(): Promise<OnchainPresentation> {
72
+ return this.compileSources();
73
+ }
74
+
75
+ protected compileSources = async (): Promise<OnchainPresentation> => {
76
+ const transpilerOutput = await this.transpileDSL(this._cwd);
77
+ const parserOutput =
78
+ await this.parseIntermediateRepresentation(transpilerOutput);
79
+
80
+ // note: actually is onchain representation
81
+ const finalRepresentation: OnchainPresentation = {
82
+ rootNode: transpilerOutput.rootNode,
83
+ nodes: parserOutput,
84
+ };
85
+
86
+ validateFinalRepresentation(finalRepresentation);
87
+ return finalRepresentation;
88
+ };
89
+
90
+ @propagateWithAnnotation(TRANSPILE_ANNOTATION)
91
+ protected async transpileDSL(cwd: string): Promise<TranspilerOutput> {
92
+ const transpilerOutput = Transpiler.create(this.sources, {
93
+ partialSources: false,
94
+ sourcesDir: cwd,
95
+ })
96
+ .transpile()
97
+ .toIntermediateRepresentation();
98
+
99
+ return Promise.resolve(transpilerOutput);
100
+ }
101
+
102
+ @propagateWithAnnotation(PARSING_ANNOTATION)
103
+ protected async parseIntermediateRepresentation(
104
+ transpilerOutput: TranspilerOutput,
105
+ ): Promise<Array<ParsingResult>> {
106
+ const parser = ParserWithValidation.fromCompilerConfiguration(
107
+ this.options,
108
+ transpilerOutput,
109
+ );
110
+
111
+ const parserOutput = await parser.process();
112
+ return parserOutput;
113
+ }
114
+ }
@@ -0,0 +1,5 @@
1
+ export const COMPILE_ANNOTATION =
2
+ 'Compile result (final-representation) validation error';
3
+ export const TRANSPILE_ANNOTATION = 'DSL transpiler error during compilation';
4
+ export const PARSING_ANNOTATION =
5
+ 'Inermedtiate-representation parser error during compilation';
@@ -0,0 +1,22 @@
1
+ import { execOrPropagate } from './utils.helper';
2
+
3
+ export const propagateWithAnnotation = (annotation: string) => {
4
+ const decorator = (
5
+ target: Object,
6
+ methodName: string,
7
+ descriptor: PropertyDescriptor,
8
+ ): PropertyDescriptor => {
9
+ const originalMethod = descriptor.value;
10
+
11
+ descriptor.value = function (...args: any[]) {
12
+ return execOrPropagate(
13
+ () => originalMethod.apply(this, args),
14
+ annotation,
15
+ );
16
+ };
17
+
18
+ return descriptor;
19
+ };
20
+
21
+ return decorator;
22
+ };
@@ -0,0 +1,2 @@
1
+ export * from './constants/error-annotations.constant';
2
+ export { propagateWithAnnotation } from './decorators-factory';
@@ -0,0 +1,12 @@
1
+ export const execOrPropagate = async <K>(
2
+ method: () => K | Promise<K>,
3
+ annotation: string,
4
+ ): Promise<Awaited<K>> => {
5
+ try {
6
+ const result = await method();
7
+ return result;
8
+ } catch (e) {
9
+ console.error(annotation);
10
+ throw e;
11
+ }
12
+ };
@@ -0,0 +1,25 @@
1
+ import {
2
+ CyclicReferenceError,
3
+ NoProviderError,
4
+ SelfReferenceError,
5
+ } from './validation-errors';
6
+
7
+ export class ErrorFactory {
8
+ static selfReference = (
9
+ ...params: Parameters<typeof SelfReferenceError.create>
10
+ ) => {
11
+ return SelfReferenceError.create(...params);
12
+ };
13
+
14
+ static cyclicfReference = (
15
+ ...params: Parameters<typeof CyclicReferenceError.create>
16
+ ) => {
17
+ return CyclicReferenceError.create(...params);
18
+ };
19
+
20
+ static noProvider = (
21
+ ...params: Parameters<typeof NoProviderError.create>
22
+ ) => {
23
+ return NoProviderError.create(...params);
24
+ };
25
+ }
@@ -0,0 +1 @@
1
+ export { ErrorFactory } from './ErrorFactory';
@@ -0,0 +1,22 @@
1
+ import { BaseError } from '@guardian-network/shared';
2
+
3
+ export class CyclicReferenceError extends BaseError {
4
+ static create = (referencedNode: string, selfNode: string) => {
5
+ const errorMessage = `Node ${selfNode} cyclically references ${referencedNode}`;
6
+ return this.build(errorMessage);
7
+ };
8
+ }
9
+
10
+ export class SelfReferenceError extends BaseError {
11
+ static create = (selfNode: string) => {
12
+ const errorMessage = `Node ${selfNode} cyclically references itself`;
13
+ return this.build(errorMessage);
14
+ };
15
+ }
16
+
17
+ export class NoProviderError extends BaseError {
18
+ static create = () => {
19
+ const errorMessage = 'Provider needed if typing checks enabled';
20
+ return this.build(errorMessage);
21
+ };
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { LacLangCompiler } from './LacLangCompiler';
@@ -0,0 +1,14 @@
1
+ import { readFile } from 'node:fs/promises';
2
+
3
+ const DEFAULT_ENCODING = 'utf-8';
4
+
5
+ export const readFromFile = async (filePath: string): Promise<string> => {
6
+ try {
7
+ const conent = await readFile(filePath, DEFAULT_ENCODING);
8
+ return conent;
9
+ } catch (error: any) {
10
+ throw new Error(
11
+ `Error reading file at ${filePath} with description ${error.message}`,
12
+ );
13
+ }
14
+ };
@@ -0,0 +1,52 @@
1
+ import {
2
+ DslNode,
3
+ findCycle,
4
+ LacLangCompilerOptions,
5
+ OnchainPresentation,
6
+ } from '@guardian-network/shared';
7
+ import { ErrorFactory } from './errors';
8
+
9
+ export const validateProviderIsSupplied = (options: LacLangCompilerOptions) => {
10
+ const isProviderSupplied = !!options.provider;
11
+ const needsDslTypesCheck = !!options.checkTypesAgainstDslDeclarations;
12
+ const needsOnchainTypesCheck = !!options.checkTypesAgainstOnchainDescriptors;
13
+
14
+ if ((needsDslTypesCheck || needsOnchainTypesCheck) && !isProviderSupplied) {
15
+ throw ErrorFactory.noProvider();
16
+ }
17
+ };
18
+
19
+ export const validateFinalRepresentation = (
20
+ representation: OnchainPresentation,
21
+ ) => {
22
+ const nodes: DslNode[] = representation.nodes.map(
23
+ ({ id, substitutedExecArgs: substitutions }) => ({
24
+ id: id.toString(),
25
+ references: substitutions.map((subst) => subst.supplierNodeId.toString()),
26
+ }),
27
+ );
28
+
29
+ validateCyclicity(nodes);
30
+ validateSelfReference(nodes);
31
+ };
32
+
33
+ const validateCyclicity = (nodes: DslNode[]) => {
34
+ const cycleFound = findCycle(nodes);
35
+
36
+ if (!!cycleFound) {
37
+ throw ErrorFactory.cyclicfReference(
38
+ cycleFound.nodeId,
39
+ cycleFound.parentNodeId,
40
+ );
41
+ }
42
+ };
43
+
44
+ const validateSelfReference = (nodes: DslNode[]) => {
45
+ for (let [, node] of nodes.entries()) {
46
+ const isSelfReference = node.references.includes(node.id);
47
+
48
+ if (isSelfReference) {
49
+ throw ErrorFactory.selfReference(node.id);
50
+ }
51
+ }
52
+ };