@guardian-network/policy-variables 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 (77) hide show
  1. package/README.md +73 -0
  2. package/dist/VariablesPopulator.d.ts +18 -0
  3. package/dist/VariablesPopulator.d.ts.map +1 -0
  4. package/dist/VariablesPopulator.js +64 -0
  5. package/dist/VariablesPopulator.js.map +1 -0
  6. package/dist/errors/ErrorFactory.d.ts +11 -0
  7. package/dist/errors/ErrorFactory.d.ts.map +1 -0
  8. package/dist/errors/ErrorFactory.js +29 -0
  9. package/dist/errors/ErrorFactory.js.map +1 -0
  10. package/dist/errors/index.d.ts +2 -0
  11. package/dist/errors/index.d.ts.map +1 -0
  12. package/dist/errors/index.js +18 -0
  13. package/dist/errors/index.js.map +1 -0
  14. package/dist/errors/validation-errors.d.ts +26 -0
  15. package/dist/errors/validation-errors.d.ts.map +1 -0
  16. package/dist/errors/validation-errors.js +61 -0
  17. package/dist/errors/validation-errors.js.map +1 -0
  18. package/dist/index.d.ts +4 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +8 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/injection/StaticInjector.d.ts +5 -0
  23. package/dist/injection/StaticInjector.d.ts.map +1 -0
  24. package/dist/injection/StaticInjector.js +29 -0
  25. package/dist/injection/StaticInjector.js.map +1 -0
  26. package/dist/injection/index.d.ts +2 -0
  27. package/dist/injection/index.d.ts.map +1 -0
  28. package/dist/injection/index.js +6 -0
  29. package/dist/injection/index.js.map +1 -0
  30. package/dist/insertion/Inserter.d.ts +14 -0
  31. package/dist/insertion/Inserter.d.ts.map +1 -0
  32. package/dist/insertion/Inserter.js +72 -0
  33. package/dist/insertion/Inserter.js.map +1 -0
  34. package/dist/insertion/index.d.ts +2 -0
  35. package/dist/insertion/index.d.ts.map +1 -0
  36. package/dist/insertion/index.js +6 -0
  37. package/dist/insertion/index.js.map +1 -0
  38. package/dist/types/index.d.ts +3 -0
  39. package/dist/types/index.d.ts.map +1 -0
  40. package/dist/types/index.js +19 -0
  41. package/dist/types/index.js.map +1 -0
  42. package/dist/types/interfaces.d.ts +5 -0
  43. package/dist/types/interfaces.d.ts.map +1 -0
  44. package/dist/types/interfaces.js +3 -0
  45. package/dist/types/interfaces.js.map +1 -0
  46. package/dist/types/types.d.ts +42 -0
  47. package/dist/types/types.d.ts.map +1 -0
  48. package/dist/types/types.js +3 -0
  49. package/dist/types/types.js.map +1 -0
  50. package/dist/utils/format-variables.helper.d.ts +4 -0
  51. package/dist/utils/format-variables.helper.d.ts.map +1 -0
  52. package/dist/utils/format-variables.helper.js +54 -0
  53. package/dist/utils/format-variables.helper.js.map +1 -0
  54. package/dist/utils/index.d.ts +3 -0
  55. package/dist/utils/index.d.ts.map +1 -0
  56. package/dist/utils/index.js +19 -0
  57. package/dist/utils/index.js.map +1 -0
  58. package/dist/utils/validations.helper.d.ts +4 -0
  59. package/dist/utils/validations.helper.d.ts.map +1 -0
  60. package/dist/utils/validations.helper.js +70 -0
  61. package/dist/utils/validations.helper.js.map +1 -0
  62. package/package.json +70 -0
  63. package/src/VariablesPopulator.ts +120 -0
  64. package/src/errors/ErrorFactory.ts +53 -0
  65. package/src/errors/index.ts +1 -0
  66. package/src/errors/validation-errors.ts +57 -0
  67. package/src/index.ts +7 -0
  68. package/src/injection/StaticInjector.ts +57 -0
  69. package/src/injection/index.ts +1 -0
  70. package/src/insertion/Inserter.ts +127 -0
  71. package/src/insertion/index.ts +1 -0
  72. package/src/types/index.ts +2 -0
  73. package/src/types/interfaces.ts +6 -0
  74. package/src/types/types.ts +66 -0
  75. package/src/utils/format-variables.helper.ts +83 -0
  76. package/src/utils/index.ts +2 -0
  77. package/src/utils/validations.helper.ts +116 -0
@@ -0,0 +1,42 @@
1
+ import { OnchainVariablesDescription, PrimitiveEncodeParamTypes } from '@guardian-network/shared';
2
+ export type AllowedVariablesType = PrimitiveEncodeParamTypes;
3
+ export type VarDescription = {
4
+ name: string;
5
+ type: string;
6
+ uniqueName: string;
7
+ index: number;
8
+ injection?: string;
9
+ };
10
+ export type VarValue = {
11
+ index: number;
12
+ value: AllowedVariablesType;
13
+ };
14
+ export type SuppliedVariables = {
15
+ nodeId: NodeId;
16
+ values: AllowedVariablesType[];
17
+ };
18
+ export type Variable = {
19
+ typename: string;
20
+ name: string;
21
+ };
22
+ type Injection = {
23
+ value: string;
24
+ index: number;
25
+ };
26
+ export type TypedRawOnchainVariablesDescription = {
27
+ nodeId: NodeId;
28
+ nodeIndex: number;
29
+ artifactAddress: string;
30
+ descriptions: Array<Variable>;
31
+ injections: Array<Injection>;
32
+ };
33
+ export type NodeVariablesDescription = OnchainVariablesDescription | TypedRawOnchainVariablesDescription;
34
+ export type NodeVariablesConfig = {
35
+ nodeId: NodeId;
36
+ variables: VarDescription[];
37
+ };
38
+ export type VarName = string;
39
+ export type VarIndex = number;
40
+ export type NodeId = string;
41
+ export {};
42
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types/types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,yBAAyB,EAC1B,MAAM,0BAA0B,CAAC;AAUlC,MAAM,MAAM,oBAAoB,GAAG,yBAAyB,CAAC;AAG7D,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,oBAAoB,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,oBAAoB,EAAE,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,SAAS,GAAG;IACf,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,mCAAmC,GAAG;IAChD,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC9B,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAChC,2BAA2B,GAC3B,mCAAmC,CAAC;AAIxC,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,cAAc,EAAE,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAC7B,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAC9B,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import { NodeVariablesConfig, NodeVariablesDescription, TypedRawOnchainVariablesDescription } from '../types';
2
+ export declare const translateVarsDescriptionToConfig: (nodesVarsList: NodeVariablesDescription[]) => NodeVariablesConfig[];
3
+ export declare const rawOnchainVariablesDescriptionToOffchainView: (nodeVars: NodeVariablesDescription) => TypedRawOnchainVariablesDescription;
4
+ //# sourceMappingURL=format-variables.helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format-variables.helper.d.ts","sourceRoot":"","sources":["../../src/utils/format-variables.helper.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,mCAAmC,EAEpC,MAAM,UAAU,CAAC;AAElB,eAAO,MAAM,gCAAgC,kBAC5B,wBAAwB,EAAE,KACxC,mBAAmB,EA0CrB,CAAC;AAEF,eAAO,MAAM,4CAA4C,aAC7C,wBAAwB,KACjC,mCAkBF,CAAC"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rawOnchainVariablesDescriptionToOffchainView = exports.translateVarsDescriptionToConfig = void 0;
4
+ const errors_1 = require("../errors");
5
+ const translateVarsDescriptionToConfig = (nodesVarsList) => {
6
+ const nodesVarsConfig = [];
7
+ for (let nodeVars of nodesVarsList.map(exports.rawOnchainVariablesDescriptionToOffchainView)) {
8
+ nodesVarsConfig.push({
9
+ nodeId: nodeVars.nodeId,
10
+ variables: [],
11
+ });
12
+ for (const [index, variable] of nodeVars.descriptions.entries()) {
13
+ const variableConfig = {
14
+ name: variable.name,
15
+ type: variable.typename,
16
+ uniqueName: buildUniqueVariablesName(variable, nodeVars),
17
+ index,
18
+ };
19
+ nodesVarsConfig[nodesVarsConfig.length - 1].variables.push(variableConfig);
20
+ }
21
+ for (const injection of nodeVars.injections) {
22
+ const injectableVar = nodesVarsConfig[nodesVarsConfig.length - 1].variables.find((el) => el.index == Number(injection.index));
23
+ if (!!injectableVar) {
24
+ injectableVar.injection = injection.value;
25
+ }
26
+ else
27
+ throw errors_1.ErrorFactory.injectionFormatting(injection.value, injection.index);
28
+ }
29
+ }
30
+ return [...nodesVarsConfig];
31
+ };
32
+ exports.translateVarsDescriptionToConfig = translateVarsDescriptionToConfig;
33
+ const rawOnchainVariablesDescriptionToOffchainView = (nodeVars) => {
34
+ const { nodeId, descriptions, injections, artifactAddress, nodeIndex } = nodeVars;
35
+ const result = {
36
+ nodeId,
37
+ nodeIndex: Number(nodeIndex),
38
+ artifactAddress,
39
+ descriptions: descriptions.map(({ typename, name }) => ({
40
+ typename,
41
+ name,
42
+ })),
43
+ injections: injections.map(({ value, index }) => ({
44
+ value,
45
+ index: Number(index),
46
+ })),
47
+ };
48
+ return result;
49
+ };
50
+ exports.rawOnchainVariablesDescriptionToOffchainView = rawOnchainVariablesDescriptionToOffchainView;
51
+ const buildUniqueVariablesName = (variable, varDescription) => {
52
+ return `${variable.name}_${variable.typename}_${varDescription.artifactAddress}_${varDescription.nodeIndex}`;
53
+ };
54
+ //# sourceMappingURL=format-variables.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format-variables.helper.js","sourceRoot":"","sources":["../../src/utils/format-variables.helper.ts"],"names":[],"mappings":";;;AAAA,sCAAyC;AAQlC,MAAM,gCAAgC,GAAG,CAC9C,aAAyC,EAClB,EAAE;IACzB,MAAM,eAAe,GAA0B,EAAE,CAAC;IAElD,KAAK,IAAI,QAAQ,IAAI,aAAa,CAAC,GAAG,CACpC,oDAA4C,CAC7C,EAAE,CAAC;QACF,eAAe,CAAC,IAAI,CAAC;YACnB,MAAM,EAAE,QAAQ,CAAC,MAAM;YACvB,SAAS,EAAE,EAAE;SACd,CAAC,CAAC;QAGH,KAAK,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,IAAI,QAAQ,CAAC,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC;YAChE,MAAM,cAAc,GAAG;gBACrB,IAAI,EAAE,QAAQ,CAAC,IAAI;gBACnB,IAAI,EAAE,QAAQ,CAAC,QAAQ;gBACvB,UAAU,EAAE,wBAAwB,CAAC,QAAQ,EAAE,QAAQ,CAAC;gBACxD,KAAK;aACN,CAAC;YAEF,eAAe,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CACxD,cAAc,CACf,CAAC;QACJ,CAAC;QAGD,KAAK,MAAM,SAAS,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;YAC5C,MAAM,aAAa,GA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AAC,IAAI,IAAI,QAAQ,CAAC,QAAQ,IAAI,cAAc,CAAC,eAAe,IAAI,cAAc,CAAC,SAAS,EAAE,CAAC;AAC/G,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './format-variables.helper';
2
+ export * from './validations.helper';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,19 @@
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
+ __exportStar(require("./format-variables.helper"), exports);
18
+ __exportStar(require("./validations.helper"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C;AAC1C,uDAAqC"}
@@ -0,0 +1,4 @@
1
+ import { AllowedVariablesType, NodeVariablesConfig, SuppliedVariables, VarDescription } from '../types';
2
+ export declare const validateVarValueTypeWithErr: (varValue: AllowedVariablesType, expectedTypeAsString: string) => void;
3
+ export declare const validateAllVariablesSupplied: (varsConfig: NodeVariablesConfig[], knownVariables: SuppliedVariables[], isVariableInjectable?: (varDescription: VarDescription) => boolean) => void;
4
+ //# sourceMappingURL=validations.helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validations.helper.d.ts","sourceRoot":"","sources":["../../src/utils/validations.helper.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,cAAc,EACf,MAAM,UAAU,CAAC;AAElB,eAAO,MAAM,2BAA2B,aAC5B,oBAAoB,wBACR,MAAM,SAU7B,CAAC;AAqDF,eAAO,MAAM,4BAA4B,eAC3B,mBAAmB,EAAE,kBACjB,iBAAiB,EAAE,yBAEb,CACpB,cAAc,EAAE,cAAc,KAC3B,OAAO,SA8Bb,CAAC"}
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateAllVariablesSupplied = exports.validateVarValueTypeWithErr = void 0;
4
+ const shared_1 = require("@guardian-network/shared");
5
+ const errors_1 = require("../errors");
6
+ const validateVarValueTypeWithErr = (varValue, expectedTypeAsString) => {
7
+ const isValid = validateVarValueType(varValue, expectedTypeAsString);
8
+ if (!isValid) {
9
+ throw errors_1.ErrorFactory.variableTypeNotMet(varValue.toString(), expectedTypeAsString);
10
+ }
11
+ };
12
+ exports.validateVarValueTypeWithErr = validateVarValueTypeWithErr;
13
+ const validateVarValueType = (varValue, expectedTypeString) => {
14
+ let verified = false;
15
+ const vatiableSolidityType = expectedTypeString;
16
+ switch (vatiableSolidityType) {
17
+ case 'string':
18
+ verified ||= typeof varValue == 'string';
19
+ break;
20
+ case 'uint256':
21
+ verified ||= typeof varValue == 'number';
22
+ break;
23
+ case 'bool':
24
+ verified ||= typeof varValue == 'boolean';
25
+ break;
26
+ case 'address':
27
+ try {
28
+ (0, shared_1.verifyAddress)(varValue);
29
+ verified = true;
30
+ }
31
+ catch (_) {
32
+ verified = false;
33
+ }
34
+ break;
35
+ case 'bytes':
36
+ try {
37
+ (0, shared_1.verifyBytes)(varValue);
38
+ verified = true;
39
+ }
40
+ catch (_) {
41
+ verified = false;
42
+ }
43
+ break;
44
+ default:
45
+ throw errors_1.ErrorFactory.providedVariableWithNotKnownType(varValue.toString(), vatiableSolidityType);
46
+ }
47
+ return verified;
48
+ };
49
+ const defaultExtraCondition = (_) => false;
50
+ const validateAllVariablesSupplied = (varsConfig, knownVariables, isVariableInjectable = defaultExtraCondition) => {
51
+ for (let i = 0; i < varsConfig.length; i++) {
52
+ const suppliedVars = knownVariables[i];
53
+ const expectedVarsConfig = varsConfig[i];
54
+ for (let j = 0; j < expectedVarsConfig.variables.length; j++) {
55
+ const variable = expectedVarsConfig.variables[j];
56
+ const isInjection = isVariableInjectable(variable);
57
+ const isVariableSupplied = suppliedVars.values[j] !== undefined;
58
+ if (!isVariableSupplied && !isInjection) {
59
+ throw errors_1.ErrorFactory.variableNotFilled(variable.uniqueName, variable.injection);
60
+ }
61
+ if (isVariableSupplied) {
62
+ const value = suppliedVars.values[j];
63
+ const expectedType = variable.type;
64
+ (0, exports.validateVarValueTypeWithErr)(value, expectedType);
65
+ }
66
+ }
67
+ }
68
+ };
69
+ exports.validateAllVariablesSupplied = validateAllVariablesSupplied;
70
+ //# sourceMappingURL=validations.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validations.helper.js","sourceRoot":"","sources":["../../src/utils/validations.helper.ts"],"names":[],"mappings":";;;AACA,qDAIkC;AAClC,sCAAyC;AAQlC,MAAM,2BAA2B,GAAG,CACzC,QAA8B,EAC9B,oBAA4B,EAC5B,EAAE;IACF,MAAM,OAAO,GAAG,oBAAoB,CAAC,QAAQ,EAAE,oBAAoB,CAAC,CAAC;IAErE,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,qBAAY,CAAC,kBAAkB,CACnC,QAAQ,CAAC,QAAQ,EAAE,EACnB,oBAAoB,CACrB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAZW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AU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3C,MAAM,YAAY,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;QACvC,MAAM,kBAAkB,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;QAEzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,kBAAkB,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7D,MAAM,QAAQ,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;YAGjD,MAAM,WAAW,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAC;YACnD,MAAM,kBAAkB,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC;YAGhE,IAAI,CAAC,kBAAkB,IAAI,CAAC,WAAW,EAAE,CAAC;gBACxC,MAAM,qBAAY,CAAC,iBAAiB,CAClC,QAAQ,CAAC,UAAU,EACnB,QAAQ,CAAC,SAAS,CACnB,CAAC;YACJ,CAAC;YAID,IAAI,kBAAkB,EAAE,CAAC;gBACvB,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;gBACrC,MAAM,YAAY,GAAG,QAAQ,CAAC,IAAI,CAAC;gBACnC,IAAA,mCAA2B,EAAC,KAAK,EAAE,YAAY,CAAC,CAAC;YACnD,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AApCW,QAAA,4BAA4B,gCAoCvC"}
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@guardian-network/policy-variables",
3
+ "author": "vpriadko@lacero.io",
4
+ "description": "Variables scrapping and encoding for policies infrastructure",
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
+ "@types/chai": "4.3.3",
25
+ "@types/chai-as-promised": "^8.0.1",
26
+ "@types/mocha": "^10.0.9",
27
+ "@types/node": "^22.8.0",
28
+ "@typescript-eslint/eslint-plugin": "^8.61.1",
29
+ "chai": "^4.5.0",
30
+ "chai-as-promised": "^8.0.1",
31
+ "copyfiles": "^2.4.1",
32
+ "eslint": "^9.11.1",
33
+ "eslint-config-prettier": "^9.1.0",
34
+ "eslint-import-resolver-typescript": "^3.6.3",
35
+ "eslint-plugin-import": "^2.31.0",
36
+ "eslint-plugin-mocha": "^10.5.0",
37
+ "eslint-plugin-n": "^17.10.3",
38
+ "eslint-plugin-prettier": "^5.2.1",
39
+ "eslint-plugin-promise": "^7.1.0",
40
+ "eslint-plugin-unused-imports": "^4.1.4",
41
+ "globals": "^15.11.0",
42
+ "mocha": "^10.8.2",
43
+ "nyc": "^18.0.0",
44
+ "prettier": "^3.3.3",
45
+ "prettier-plugin-organize-imports": "^4.1.0",
46
+ "rimraf": "^6.0.1"
47
+ },
48
+ "dependencies": {
49
+ "ts-node": "^10.9.2",
50
+ "typescript": "^5.6.2",
51
+ "@guardian-network/shared": "0.3.2"
52
+ },
53
+ "scripts": {
54
+ "build": "pnpm format:all:fix && tsc -p tsconfig-prod.json && pnpm copy-dts",
55
+ "check": "pnpm lint:ts && pnpm check:ts",
56
+ "check:ts": "tsc -p tsconfig-prod.json --noEmit",
57
+ "check:ts:dev": "tsc -p tsconfig.json --noEmit",
58
+ "clean": "pnpm remove:dist",
59
+ "copy-dts": "copyfiles -u 1 \"src/**/*.d.ts\" dist",
60
+ "format:all:fix": "pnpm format:ts:fix",
61
+ "format:ts:fix": "prettier -w -c \"src/**/*.ts\" \"test/**/*.ts\"",
62
+ "lint:ts": "eslint",
63
+ "lint:ts:fix": "eslint --fix",
64
+ "publish-package": "pnpm publish --no-git-checks",
65
+ "remove:dist": "npx rimraf dist",
66
+ "test": "mocha",
67
+ "tests:coverage:ts": "nyc pnpm test",
68
+ "version": "pnpm version"
69
+ }
70
+ }
@@ -0,0 +1,120 @@
1
+ import {
2
+ VariablesStruct,
3
+ solidityEncodeSingleParam,
4
+ } from '@guardian-network/shared';
5
+ import { ErrorFactory } from './errors';
6
+ import { StaticInjector } from './injection';
7
+ import { Inserter } from './insertion';
8
+ import {
9
+ AllowedVariablesType,
10
+ IAsyncMapGetter,
11
+ NodeVariablesConfig,
12
+ NodeVariablesDescription,
13
+ SuppliedVariables,
14
+ VarDescription,
15
+ } from './types';
16
+ import {
17
+ translateVarsDescriptionToConfig,
18
+ validateAllVariablesSupplied,
19
+ validateVarValueTypeWithErr,
20
+ } from './utils';
21
+
22
+ // note: fill each variable with respective data
23
+ export class VariablesPopulator {
24
+ protected varsConfig: NodeVariablesConfig[];
25
+ private inserter: Inserter;
26
+ private suppliedVars: SuppliedVariables[];
27
+
28
+ constructor(varsDescriptions: Array<NodeVariablesDescription>) {
29
+ this.varsConfig = translateVarsDescriptionToConfig(varsDescriptions);
30
+
31
+ this.inserter = new Inserter(this.varsConfig);
32
+
33
+ this.suppliedVars = this.inserter.filledVars;
34
+ // console.log(this.filledVariables[0].values);
35
+ }
36
+
37
+ // note: varName has to be unique in comparison to other vars
38
+ insert = (varName: string, varValue: AllowedVariablesType) => {
39
+ const varDescription = this.getVarDescription(varName);
40
+ validateVarValueTypeWithErr(varValue, varDescription.type);
41
+
42
+ this.inserter.insert(varName, varValue);
43
+
44
+ this.suppliedVars = this.inserter.filledVars;
45
+ };
46
+
47
+ inject = async (attributes: IAsyncMapGetter<AllowedVariablesType>) => {
48
+ // note: this might be called as more times as required
49
+ this.suppliedVars = await StaticInjector.inject(
50
+ this.varsConfig,
51
+ this.suppliedVars,
52
+ attributes,
53
+ );
54
+ };
55
+
56
+ importState = (filledValues: SuppliedVariables[]) => {
57
+ this.inserter.import(filledValues);
58
+
59
+ this.suppliedVars = this.inserter.filledVars;
60
+
61
+ return this;
62
+ };
63
+
64
+ dumpState = (): Array<SuppliedVariables> => {
65
+ return this.suppliedVars;
66
+ };
67
+
68
+ // note: actually, not serialized, rather packed to be
69
+ // compatible with "PolicyHandler.evaluate"" method (defined onchaun using Solidity)
70
+ toSerializedVariables = (): Array<VariablesStruct> => {
71
+ // note: only completely supplied variables have to be serialized
72
+ this.validateAllFilled();
73
+
74
+ const solidityPackedVariablesList = this.suppliedVars.map(
75
+ ({ nodeId, values }) => ({
76
+ nodeId,
77
+ values: values.map((val) => solidityEncodeSingleParam(val)),
78
+ }),
79
+ );
80
+
81
+ return solidityPackedVariablesList;
82
+ };
83
+
84
+ getVarDescription(varName: string): VarDescription {
85
+ const variableDescription = this.getVarsDescription().find(
86
+ ({ uniqueName }) => uniqueName == varName,
87
+ );
88
+
89
+ // note: validate variable exists (and has a description)
90
+ if (!variableDescription) throw ErrorFactory.variableNotFound(varName);
91
+
92
+ return variableDescription;
93
+ }
94
+
95
+ getVarsDescription = (): VarDescription[] => {
96
+ // flattened list of each node variables
97
+ const variablesDescriptionList: VarDescription[] = this.varsConfig.flatMap(
98
+ ({ variables }) => variables,
99
+ );
100
+
101
+ return variablesDescriptionList;
102
+ };
103
+
104
+ validateAllFilledExceptInjections = (): void => {
105
+ const isVariableInjectable = (varDescription: VarDescription) => {
106
+ // note: undefined means not injection; defined means injection
107
+ return !!varDescription.injection;
108
+ };
109
+
110
+ validateAllVariablesSupplied(
111
+ this.varsConfig,
112
+ this.suppliedVars,
113
+ isVariableInjectable,
114
+ );
115
+ };
116
+
117
+ validateAllFilled = (): void => {
118
+ validateAllVariablesSupplied(this.varsConfig, this.suppliedVars);
119
+ };
120
+ }
@@ -0,0 +1,53 @@
1
+ import {
2
+ InjectionFormattingError,
3
+ NodeVariablesAreUndefinedErr,
4
+ VariableNodeNotFoundError,
5
+ VariableNotFilledError,
6
+ VariableNotFoundError,
7
+ VariableTypeNotKnownError,
8
+ VariableTypeNotMetError,
9
+ } from './validation-errors';
10
+
11
+ export class ErrorFactory {
12
+ static variableTypeNotMet = (
13
+ ...params: Parameters<typeof VariableTypeNotMetError.create>
14
+ ) => {
15
+ return VariableTypeNotMetError.create(...params);
16
+ };
17
+
18
+ static variableNotFound = (
19
+ ...params: Parameters<typeof VariableNotFoundError.create>
20
+ ) => {
21
+ return VariableNotFoundError.create(...params);
22
+ };
23
+
24
+ static injectionFormatting = (
25
+ ...params: Parameters<typeof InjectionFormattingError.create>
26
+ ) => {
27
+ return InjectionFormattingError.create(...params);
28
+ };
29
+
30
+ static nodeVariablesAreUndefined = (
31
+ ...params: Parameters<typeof NodeVariablesAreUndefinedErr.create>
32
+ ) => {
33
+ return NodeVariablesAreUndefinedErr.create(...params);
34
+ };
35
+
36
+ static variableNodeNotFound = (
37
+ ...params: Parameters<typeof VariableNodeNotFoundError.create>
38
+ ) => {
39
+ return VariableNodeNotFoundError.create(...params);
40
+ };
41
+
42
+ static providedVariableWithNotKnownType = (
43
+ ...params: Parameters<typeof VariableTypeNotKnownError.create>
44
+ ) => {
45
+ return VariableTypeNotKnownError.create(...params);
46
+ };
47
+
48
+ static variableNotFilled = (
49
+ ...params: Parameters<typeof VariableNotFilledError.create>
50
+ ) => {
51
+ return VariableNotFilledError.create(...params);
52
+ };
53
+ }
@@ -0,0 +1 @@
1
+ export * from './ErrorFactory';
@@ -0,0 +1,57 @@
1
+ import { BaseError } from '@guardian-network/shared';
2
+
3
+ export class CannotLookupVariableValueError extends BaseError {
4
+ static create = (varName: string, injectionName: string) => {
5
+ const errorMessage = `No injection or default value for ${varName} (attribute ${injectionName}) is provided`;
6
+ return this.build(errorMessage);
7
+ };
8
+ }
9
+
10
+ export class VariableNotFoundError extends BaseError {
11
+ static create = (varName: string) => {
12
+ const errorMessage = `Cannot find variable ${varName} among known policy variables`;
13
+ return this.build(errorMessage);
14
+ };
15
+ }
16
+
17
+ export class VariableTypeNotMetError extends BaseError {
18
+ static create = (varValue: string, expectedType: string) => {
19
+ const errorMessage = `Variable of value ${varValue} is not one of allowed Solidity types ${expectedType}`;
20
+ return this.build(errorMessage);
21
+ };
22
+ }
23
+
24
+ export class VariableNodeNotFoundError extends BaseError {
25
+ static create = (varName: string) => {
26
+ const errorMessage = `Not found node id for variable ${varName}`;
27
+ return this.build(errorMessage);
28
+ };
29
+ }
30
+
31
+ export class NodeVariablesAreUndefinedErr extends BaseError {
32
+ static create = (targetNode: string) => {
33
+ const errorMessage = `Variables record is missing for node id: ${targetNode}`;
34
+ return this.build(errorMessage);
35
+ };
36
+ }
37
+
38
+ export class InjectionFormattingError extends BaseError {
39
+ static create = (injectionValue: string, injectionIndex: number) => {
40
+ const errorMessage = `Failed formatting variables description onchain output. Problems with injection ${injectionValue}:${injectionIndex}`;
41
+ return this.build(errorMessage);
42
+ };
43
+ }
44
+
45
+ export class VariableNotFilledError extends BaseError {
46
+ static create = (varName: string, injection?: string) => {
47
+ const errorMessage = `Variable ${varName} ${injection ? `(injection: ${injection})` : ''} was not filled`;
48
+ return this.build(errorMessage);
49
+ };
50
+ }
51
+
52
+ export class VariableTypeNotKnownError extends BaseError {
53
+ static create = (varValue: string, expectedType: string) => {
54
+ const errorMessage = `Variable of value ${varValue} type is not allowed Solidity type ${expectedType}`;
55
+ return this.build(errorMessage);
56
+ };
57
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export {
2
+ AllowedVariablesType,
3
+ SuppliedVariables,
4
+ TypedRawOnchainVariablesDescription,
5
+ } from './types';
6
+ export { rawOnchainVariablesDescriptionToOffchainView } from './utils';
7
+ export { VariablesPopulator } from './VariablesPopulator';
@@ -0,0 +1,57 @@
1
+ import {
2
+ AllowedVariablesType,
3
+ IAsyncMapGetter,
4
+ NodeVariablesConfig,
5
+ SuppliedVariables,
6
+ } from '../types';
7
+ import { validateVarValueTypeWithErr } from '../utils';
8
+
9
+ // note: inject variables values
10
+ export class StaticInjector {
11
+ static inject = async <T extends AllowedVariablesType>(
12
+ varsConfig: NodeVariablesConfig[],
13
+ knownVariables: SuppliedVariables[],
14
+ attributesSource: IAsyncMapGetter<T>,
15
+ ): Promise<Array<SuppliedVariables>> => {
16
+ const knownVariablesClone = structuredClone(knownVariables);
17
+
18
+ for (let mayBeSuppliedVariable of knownVariablesClone) {
19
+ // why var-config is unaware of supplied-var-node-id???
20
+ const variableConfig = varsConfig.find(
21
+ (v) => v.nodeId == mayBeSuppliedVariable.nodeId,
22
+ );
23
+
24
+ // note: only for known config with variables.lengt > 0
25
+ if (!variableConfig || variableConfig.variables.length === 0) {
26
+ continue;
27
+ }
28
+
29
+ for (let [index, varDescription] of variableConfig.variables.entries()) {
30
+ // note: skipping when not injectable variable
31
+ if (!varDescription.injection) {
32
+ continue;
33
+ }
34
+
35
+ // note: look up attributes to fill injected variable
36
+ const attribute = await attributesSource.get(varDescription.injection);
37
+ // const defaultValue = mayBeSuppliedVariable.values[index];
38
+ const expectedType = varDescription.type;
39
+
40
+ if (attribute !== undefined) {
41
+ validateVarValueTypeWithErr(attribute, expectedType);
42
+ mayBeSuppliedVariable.values[index] = attribute;
43
+
44
+ // note: ??redundancy. this is validated while insert
45
+ }
46
+ // note: Redundancy? this is validated in VariablesPopulator.validateAllFilled()
47
+ // else
48
+ // throw ErrorFactory.cannotLookupVariableValue(
49
+ // varDescription.uniqueName,
50
+ // varDescription.injection,
51
+ // );
52
+ }
53
+ }
54
+
55
+ return knownVariablesClone;
56
+ };
57
+ }
@@ -0,0 +1 @@
1
+ export { StaticInjector } from './StaticInjector';