@guardian-network/policy-intermediate-representation 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 (137) hide show
  1. package/README.md +36 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +8 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/ir-notation/errors/ErrorFactory.d.ts +7 -0
  7. package/dist/ir-notation/errors/ErrorFactory.d.ts.map +1 -0
  8. package/dist/ir-notation/errors/ErrorFactory.js +17 -0
  9. package/dist/ir-notation/errors/ErrorFactory.js.map +1 -0
  10. package/dist/ir-notation/errors/index.d.ts +2 -0
  11. package/dist/ir-notation/errors/index.d.ts.map +1 -0
  12. package/dist/ir-notation/errors/index.js +18 -0
  13. package/dist/ir-notation/errors/index.js.map +1 -0
  14. package/dist/ir-notation/errors/validation-errors.d.ts +11 -0
  15. package/dist/ir-notation/errors/validation-errors.d.ts.map +1 -0
  16. package/dist/ir-notation/errors/validation-errors.js +26 -0
  17. package/dist/ir-notation/errors/validation-errors.js.map +1 -0
  18. package/dist/ir-notation/helpers/formatters.d.ts +18 -0
  19. package/dist/ir-notation/helpers/formatters.d.ts.map +1 -0
  20. package/dist/ir-notation/helpers/formatters.js +45 -0
  21. package/dist/ir-notation/helpers/formatters.js.map +1 -0
  22. package/dist/ir-notation/helpers/index.d.ts +5 -0
  23. package/dist/ir-notation/helpers/index.d.ts.map +1 -0
  24. package/dist/ir-notation/helpers/index.js +21 -0
  25. package/dist/ir-notation/helpers/index.js.map +1 -0
  26. package/dist/ir-notation/helpers/matchers.d.ts +4 -0
  27. package/dist/ir-notation/helpers/matchers.d.ts.map +1 -0
  28. package/dist/ir-notation/helpers/matchers.js +17 -0
  29. package/dist/ir-notation/helpers/matchers.js.map +1 -0
  30. package/dist/ir-notation/helpers/morphs.d.ts +4 -0
  31. package/dist/ir-notation/helpers/morphs.d.ts.map +1 -0
  32. package/dist/ir-notation/helpers/morphs.js +26 -0
  33. package/dist/ir-notation/helpers/morphs.js.map +1 -0
  34. package/dist/ir-notation/helpers/validation/index.d.ts +2 -0
  35. package/dist/ir-notation/helpers/validation/index.d.ts.map +1 -0
  36. package/dist/ir-notation/helpers/validation/index.js +18 -0
  37. package/dist/ir-notation/helpers/validation/index.js.map +1 -0
  38. package/dist/ir-notation/helpers/validation/typing.validation.d.ts +10 -0
  39. package/dist/ir-notation/helpers/validation/typing.validation.d.ts.map +1 -0
  40. package/dist/ir-notation/helpers/validation/typing.validation.js +41 -0
  41. package/dist/ir-notation/helpers/validation/typing.validation.js.map +1 -0
  42. package/dist/ir-notation/parser/index.d.ts +4 -0
  43. package/dist/ir-notation/parser/index.d.ts.map +1 -0
  44. package/dist/ir-notation/parser/index.js +14 -0
  45. package/dist/ir-notation/parser/index.js.map +1 -0
  46. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.d.ts +14 -0
  47. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.d.ts.map +1 -0
  48. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.js +44 -0
  49. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.js.map +1 -0
  50. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.d.ts +12 -0
  51. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.d.ts.map +1 -0
  52. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.js +27 -0
  53. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.js.map +1 -0
  54. package/dist/ir-notation/parser/parser-contracts/StaticParser.d.ts +7 -0
  55. package/dist/ir-notation/parser/parser-contracts/StaticParser.d.ts.map +1 -0
  56. package/dist/ir-notation/parser/parser-contracts/StaticParser.js +16 -0
  57. package/dist/ir-notation/parser/parser-contracts/StaticParser.js.map +1 -0
  58. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.d.ts +7 -0
  59. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.d.ts.map +1 -0
  60. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.js +40 -0
  61. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.js.map +1 -0
  62. package/dist/ir-notation/parser/parser-contracts/guards.d.ts +5 -0
  63. package/dist/ir-notation/parser/parser-contracts/guards.d.ts.map +1 -0
  64. package/dist/ir-notation/parser/parser-contracts/guards.js +14 -0
  65. package/dist/ir-notation/parser/parser-contracts/guards.js.map +1 -0
  66. package/dist/ir-notation/parser/parser-contracts/index.d.ts +5 -0
  67. package/dist/ir-notation/parser/parser-contracts/index.d.ts.map +1 -0
  68. package/dist/ir-notation/parser/parser-contracts/index.js +13 -0
  69. package/dist/ir-notation/parser/parser-contracts/index.js.map +1 -0
  70. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.d.ts +19 -0
  71. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.d.ts.map +1 -0
  72. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.js +140 -0
  73. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.js.map +1 -0
  74. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.d.ts +7 -0
  75. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.d.ts.map +1 -0
  76. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.js +49 -0
  77. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.js.map +1 -0
  78. package/dist/ir-notation/parser/parser-contracts/tools/index.d.ts +6 -0
  79. package/dist/ir-notation/parser/parser-contracts/tools/index.d.ts.map +1 -0
  80. package/dist/ir-notation/parser/parser-contracts/tools/index.js +25 -0
  81. package/dist/ir-notation/parser/parser-contracts/tools/index.js.map +1 -0
  82. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.d.ts +5 -0
  83. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.d.ts.map +1 -0
  84. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.js +26 -0
  85. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.js.map +1 -0
  86. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.d.ts +10 -0
  87. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.d.ts.map +1 -0
  88. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.js +36 -0
  89. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.js.map +1 -0
  90. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.d.ts +12 -0
  91. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.d.ts.map +1 -0
  92. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.js +39 -0
  93. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.js.map +1 -0
  94. package/dist/ir-notation/parser/parser-contracts/types.d.ts +59 -0
  95. package/dist/ir-notation/parser/parser-contracts/types.d.ts.map +1 -0
  96. package/dist/ir-notation/parser/parser-contracts/types.js +12 -0
  97. package/dist/ir-notation/parser/parser-contracts/types.js.map +1 -0
  98. package/dist/ir-notation/parser/parser.unvalidated.d.ts +6 -0
  99. package/dist/ir-notation/parser/parser.unvalidated.d.ts.map +1 -0
  100. package/dist/ir-notation/parser/parser.unvalidated.js +92 -0
  101. package/dist/ir-notation/parser/parser.unvalidated.js.map +1 -0
  102. package/dist/ir-notation/parser/parser.validated.d.ts +17 -0
  103. package/dist/ir-notation/parser/parser.validated.d.ts.map +1 -0
  104. package/dist/ir-notation/parser/parser.validated.js +37 -0
  105. package/dist/ir-notation/parser/parser.validated.js.map +1 -0
  106. package/dist/ir-notation/parser/types.d.ts +9 -0
  107. package/dist/ir-notation/parser/types.d.ts.map +1 -0
  108. package/dist/ir-notation/parser/types.js +11 -0
  109. package/dist/ir-notation/parser/types.js.map +1 -0
  110. package/package.json +71 -0
  111. package/src/index.ts +5 -0
  112. package/src/ir-notation/errors/ErrorFactory.ts +25 -0
  113. package/src/ir-notation/errors/index.ts +1 -0
  114. package/src/ir-notation/errors/validation-errors.ts +38 -0
  115. package/src/ir-notation/helpers/formatters.ts +63 -0
  116. package/src/ir-notation/helpers/index.ts +4 -0
  117. package/src/ir-notation/helpers/matchers.ts +12 -0
  118. package/src/ir-notation/helpers/morphs.ts +26 -0
  119. package/src/ir-notation/helpers/validation/index.ts +1 -0
  120. package/src/ir-notation/helpers/validation/typing.validation.ts +89 -0
  121. package/src/ir-notation/parser/index.ts +10 -0
  122. package/src/ir-notation/parser/parser-contracts/ParserWithValidation.ts +99 -0
  123. package/src/ir-notation/parser/parser-contracts/SimplifiedParser.ts +41 -0
  124. package/src/ir-notation/parser/parser-contracts/StaticParser.ts +25 -0
  125. package/src/ir-notation/parser/parser-contracts/base/ParserBase.ts +79 -0
  126. package/src/ir-notation/parser/parser-contracts/guards.ts +17 -0
  127. package/src/ir-notation/parser/parser-contracts/index.ts +7 -0
  128. package/src/ir-notation/parser/parser-contracts/tools/ParamsExtractor.ts +251 -0
  129. package/src/ir-notation/parser/parser-contracts/tools/TypesNormalizer.ts +78 -0
  130. package/src/ir-notation/parser/parser-contracts/tools/index.ts +5 -0
  131. package/src/ir-notation/parser/parser-contracts/tools/tools.helper.ts +33 -0
  132. package/src/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.ts +52 -0
  133. package/src/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.ts +76 -0
  134. package/src/ir-notation/parser/parser-contracts/types.ts +78 -0
  135. package/src/ir-notation/parser/parser.unvalidated.ts +200 -0
  136. package/src/ir-notation/parser/parser.validated.ts +67 -0
  137. package/src/ir-notation/parser/types.ts +28 -0
@@ -0,0 +1,78 @@
1
+ import { solidityEncode } from '@guardian-network/shared';
2
+ import { getAddress, getBytes } from 'ethers';
3
+ import { isSupportedSolidityType } from '../guards';
4
+ import {
5
+ KnownTSType,
6
+ SolidityType,
7
+ SupportedSolidityTypesEnum,
8
+ } from '../types';
9
+
10
+ export class TypesNormalizer {
11
+ // note: can take multiple type/value pairs, then encode inputs to Solidity-bytes
12
+ static toSolidityEncodedValueFromString(
13
+ soldityTypes: string[], // taken from onchain declaration
14
+ valuesAsStringList: string[],
15
+ ): string {
16
+ // note: Convert each argument to its respective TS type
17
+ const convertedValues = this.toTsArrayFromStringArray(
18
+ soldityTypes,
19
+ valuesAsStringList,
20
+ );
21
+
22
+ // note: Encode the types and values using EthersV6-Solidity's encoding function
23
+ return solidityEncode(soldityTypes, convertedValues);
24
+ }
25
+
26
+ // note: Normalizes argument supplied as string (defined in this way in intermediate presentation) to the respective typescript type
27
+ static toTsValueFromString(
28
+ type: SolidityType,
29
+ valueAsString: string,
30
+ ): KnownTSType {
31
+ const getBool = (value: string): boolean => {
32
+ return value === 'true' ? true : false;
33
+ };
34
+
35
+ switch (type) {
36
+ case SupportedSolidityTypesEnum.Address:
37
+ return getAddress(valueAsString); // string to a valid Ethereum address
38
+ case SupportedSolidityTypesEnum.Bool:
39
+ return getBool(valueAsString); // 'true'/'false' to boolean
40
+ case SupportedSolidityTypesEnum.String:
41
+ return valueAsString.replace(/^"(.*)"$/, '$1'); // Removes quotes around a string if present
42
+ case SupportedSolidityTypesEnum.Bytes:
43
+ return getBytes(valueAsString); // string to bytes (expects a hex string)
44
+ case SupportedSolidityTypesEnum.Uint256:
45
+ return BigInt(valueAsString); // string to BigInt (for 'uint256' type)
46
+ default:
47
+ // Type is known for sure, because there is a guard in previous calls
48
+ throw new Error(
49
+ `Can not normalize. Provided Solidity type ${type} is known yet unsupported`,
50
+ );
51
+ }
52
+ }
53
+
54
+ // note: This takes raw values (defined as string in intermediate presentation), then converts them to respective typesript types.
55
+ // While converting it relies on solidity-types fetched from onchain declaration of Artifact.
56
+ static toTsArrayFromStringArray = (
57
+ soldityTypes: string[], // taken from onchain declaration
58
+ valuesAsStringList: string[],
59
+ ): Array<KnownTSType> => {
60
+ if (soldityTypes.length !== valuesAsStringList.length) {
61
+ throw new Error(
62
+ `Can not convert to Typescript value: Solidity-Types ${soldityTypes.length} and Values ${valuesAsStringList.length} arrays lengths not match`,
63
+ );
64
+ }
65
+
66
+ const typedValuesList = valuesAsStringList.map((value, index) => {
67
+ const solidityTypeName = soldityTypes[index];
68
+ if (isSupportedSolidityType(solidityTypeName)) {
69
+ // note: converts each argument to its respective type
70
+ return this.toTsValueFromString(solidityTypeName, value);
71
+ } else {
72
+ throw new Error(`Supplied unknown Solidity type ${solidityTypeName}`);
73
+ }
74
+ });
75
+
76
+ return typedValuesList;
77
+ };
78
+ }
@@ -0,0 +1,5 @@
1
+ export * from './ParamsExtractor';
2
+ export * from './tools.helper';
3
+ export { DSLConfigArgsTypesSource } from './types-source/DSLConfigArgsTypesSource';
4
+ export { OnchainDescriptorArgsTypesSource } from './types-source/OnchainDescriptorArgsTypesSource';
5
+ export * from './TypesNormalizer';
@@ -0,0 +1,33 @@
1
+ import { ContractRunner } from 'ethers';
2
+ import { isContractProviderType } from '../guards';
3
+ import { GetTypesValues, IArgsTypesSource } from '../types';
4
+ import { OnchainDescriptorArgsTypesSource } from './types-source/OnchainDescriptorArgsTypesSource';
5
+
6
+ // note: this extracts raw artifacts (list: Array<string>) – as they declared in intermeditate presentation
7
+ export const toUnprocessedArtifactsList = (
8
+ intermediatePresentation: string,
9
+ ): Array<string> => {
10
+ // note: no extra whitespaces + split input (by lines count) into individual artifact strings
11
+ const list = intermediatePresentation
12
+ .trim() // remove surrounding whitespaces in declaration (intermediate-presentation)
13
+ .split(/\r?\n/) // split by each line
14
+ .map((v) => v.trim()); // remove surrounding whitespaces in each artifact
15
+
16
+ // note: list has no validations, because validations are applied in respective methods for each list entry
17
+ return list;
18
+ };
19
+
20
+ export const createOrInferTypesSource = (
21
+ providerOrSource: ContractRunner | IArgsTypesSource,
22
+ ): GetTypesValues => {
23
+ let getTypesSource: GetTypesValues;
24
+
25
+ if (isContractProviderType(providerOrSource)) {
26
+ getTypesSource = new OnchainDescriptorArgsTypesSource(providerOrSource)
27
+ .getTypes;
28
+ } else {
29
+ getTypesSource = providerOrSource.getTypes;
30
+ }
31
+
32
+ return getTypesSource;
33
+ };
@@ -0,0 +1,52 @@
1
+ import { InstanceConfig } from '@guardian-network/shared';
2
+ import { DSLTypesToIRTypes } from '../../../../helpers';
3
+ import { ExecInitArtifactTypes, IArgsTypesSource } from '../../types';
4
+
5
+ export class DSLConfigArgsTypesSource implements IArgsTypesSource {
6
+ static build = (
7
+ ipArtifactInstanceConfigList: InstanceConfig[],
8
+ ): DSLConfigArgsTypesSource => {
9
+ return new DSLConfigArgsTypesSource(ipArtifactInstanceConfigList);
10
+ };
11
+
12
+ constructor(private configList: InstanceConfig[]) {}
13
+
14
+ private getArtifactConfiguration = (
15
+ instanceAddress: string,
16
+ pos?: number,
17
+ ): InstanceConfig => {
18
+ const isPosProvided = typeof pos !== 'undefined';
19
+ if (!isPosProvided) {
20
+ throw new Error('Config index in index list is not provided');
21
+ }
22
+
23
+ const config = this.configList.find(
24
+ (value, index) =>
25
+ value.artifactAddress === instanceAddress && index == pos,
26
+ );
27
+
28
+ if (!config) {
29
+ throw new Error(
30
+ `The artifact config of ${instanceAddress} is not found within config list at pos ${pos}`,
31
+ );
32
+ }
33
+ return config;
34
+ };
35
+
36
+ getTypes = async (
37
+ instanceAddress: string,
38
+ posAtConfigList?: number,
39
+ ): Promise<ExecInitArtifactTypes> => {
40
+ const { initArguments: initArgsRaw, execArguments: execArgsRaw } =
41
+ this.getArtifactConfiguration(instanceAddress, posAtConfigList);
42
+
43
+ const initParamsTypes = initArgsRaw
44
+ .map((v) => v.type)
45
+ .map(DSLTypesToIRTypes);
46
+ const execParamsTypes = execArgsRaw
47
+ .map((v) => v.type)
48
+ .map(DSLTypesToIRTypes);
49
+
50
+ return { initParamsTypes, execParamsTypes };
51
+ };
52
+ }
@@ -0,0 +1,76 @@
1
+ import {
2
+ IMinArbitraryDataArtifact,
3
+ MinArbitraryDataArtifactFactory,
4
+ } from '@guardian-network/shared';
5
+ import { ContractRunner } from 'ethers';
6
+ import {
7
+ ExecInitArtifactTypes,
8
+ ExecInitArtifactTypesAndNames,
9
+ IArgsTypesAndNamesSource,
10
+ IArgsTypesSource,
11
+ } from '../../types';
12
+
13
+ export class OnchainDescriptorArgsTypesSource
14
+ implements IArgsTypesAndNamesSource, IArgsTypesSource
15
+ {
16
+ private artifactInstance!: IMinArbitraryDataArtifact;
17
+
18
+ constructor(private provider: ContractRunner) {}
19
+
20
+ protected connectInstance = (artifactAddress: string) => {
21
+ this.artifactInstance = MinArbitraryDataArtifactFactory.connect(
22
+ artifactAddress,
23
+ this.provider,
24
+ );
25
+
26
+ return this;
27
+ };
28
+
29
+ private getTypesValues = async (instanceAddress: string) => {
30
+ this.connectInstance(instanceAddress);
31
+
32
+ const execDescriptorValue = await this.artifactInstance.getExecDescriptor();
33
+
34
+ const initDescriptorValue = await this.artifactInstance.getInitDescriptor();
35
+
36
+ return { execDescriptorValue, initDescriptorValue };
37
+ };
38
+
39
+ getTypes = async (
40
+ instanceAddress: string,
41
+ ): Promise<ExecInitArtifactTypes> => {
42
+ const {
43
+ execDescriptorValue: { argsTypes: execParamsTypes },
44
+ initDescriptorValue: { argsTypes: initParamsTypes },
45
+ } = await this.getTypesValues(instanceAddress);
46
+
47
+ // note: this requires spread to omit wrappers
48
+ return {
49
+ initParamsTypes: [...initParamsTypes],
50
+ execParamsTypes: [...execParamsTypes],
51
+ };
52
+ };
53
+
54
+ getTypesAndNames = async (
55
+ instanceAddress: string,
56
+ ): Promise<ExecInitArtifactTypesAndNames> => {
57
+ const {
58
+ execDescriptorValue: {
59
+ argsTypes: execParamsTypes,
60
+ argsNames: execParamsNames,
61
+ },
62
+ initDescriptorValue: {
63
+ argsTypes: initParamsTypes,
64
+ argsNames: initParamsNames,
65
+ },
66
+ } = await this.getTypesValues(instanceAddress);
67
+
68
+ // note: this requires spread to omit wrappers
69
+ return {
70
+ initParamsTypes: [...initParamsTypes],
71
+ initParamsNames: [...initParamsNames],
72
+ execParamsTypes: [...execParamsTypes],
73
+ execParamsNames: [...execParamsNames],
74
+ };
75
+ };
76
+ }
@@ -0,0 +1,78 @@
1
+ export type GetTypesValues = (
2
+ artifactAddress: string,
3
+ posAtConfigList?: number,
4
+ ) => Promise<ExecInitArtifactTypes>;
5
+
6
+ export interface IArgsTypesSource {
7
+ getTypes: GetTypesValues;
8
+ }
9
+
10
+ export interface IArgsTypesAndNamesSource {
11
+ getTypesAndNames(
12
+ instanceAddress: string,
13
+ ): Promise<ExecInitArtifactTypesAndNames>;
14
+ }
15
+
16
+ export interface IParamsExtractor {
17
+ artifactData: (rawArtifact?: string) => Promise<ArtifactData>;
18
+ }
19
+
20
+ export type ArtifactData = {
21
+ artifactAddress: string;
22
+ argsCount: number;
23
+ execRuntimeVariablesIndices: number[];
24
+ execRuntimeVariablesInjectionsWithIndices: Parameter[];
25
+ execSubstitutionParamsList: SubstitutingParameter[];
26
+ execKnownParamsList: Parameter[];
27
+ initDataParamsSolidityPacked: string;
28
+ needsInitialization: boolean;
29
+ };
30
+
31
+ export type ExecInitArtifactTypes = {
32
+ execParamsTypes: string[];
33
+ initParamsTypes: string[];
34
+ };
35
+
36
+ export type ExecInitArtifactTypesAndNames = ExecInitArtifactTypes & {
37
+ execParamsNames: string[];
38
+ initParamsNames: string[];
39
+ };
40
+
41
+ export type KnownTSType = string | bigint | boolean | Uint8Array;
42
+
43
+ export type SolidityType = `${SupportedSolidityTypesEnum}`;
44
+
45
+ export enum SupportedSolidityTypesEnum {
46
+ Uint256 = 'uint256',
47
+ String = 'string',
48
+ Bool = 'bool',
49
+ Bytes = 'bytes',
50
+ Address = 'address',
51
+ }
52
+
53
+ type AddressComponent = {
54
+ addressClause: string;
55
+ };
56
+
57
+ type UnprocessedInitDataAndExecParams = {
58
+ initClause: string | null;
59
+ paramsClause: string;
60
+ };
61
+
62
+ export type RawArtifactComponents = AddressComponent &
63
+ UnprocessedInitDataAndExecParams;
64
+
65
+ export type NormalizedExecParameter = {
66
+ supplierNodeId: string;
67
+ index: number;
68
+ };
69
+
70
+ export type SubstitutingParameter = {
71
+ from: string; // external node-id to expect data from in run-time
72
+ atPos: number; // index in general params list of this node-id
73
+ };
74
+
75
+ export type Parameter = {
76
+ value: string;
77
+ index: number;
78
+ };
@@ -0,0 +1,200 @@
1
+ import {
2
+ InstanceConfig,
3
+ NodeId,
4
+ SupportedSolidityType,
5
+ TranspilerOutput,
6
+ } from '@guardian-network/shared';
7
+ import { ContractRunner } from 'ethers/providers';
8
+ import {
9
+ bytesEncodeArgs,
10
+ DSLTypesToIRTypes,
11
+ extractArguments,
12
+ extractComponents,
13
+ extractInjection,
14
+ indexConstants,
15
+ isConstant,
16
+ strIsSubst,
17
+ strIsVar,
18
+ } from '../helpers';
19
+ import { OnchainDescriptorArgsTypesSource } from './parser-contracts/tools';
20
+ import { ParsingResult, ValidationMiddlware } from './types';
21
+
22
+ type Parameter = {
23
+ value: string;
24
+ index: number;
25
+ };
26
+
27
+ type KnownParamsAndInitData = {
28
+ knownExecParams: Parameter[];
29
+ initDataEncoded: string;
30
+ };
31
+
32
+ type RetrieveExecAndInitData = (
33
+ parameters: Array<Parameter>,
34
+ initArgs: string[],
35
+ instanceConfig: InstanceConfig,
36
+ artifactAddress: string,
37
+ provider?: ContractRunner,
38
+ ) => KnownParamsAndInitData | Promise<KnownParamsAndInitData>;
39
+
40
+ const getKnownExecDataAndInitDataFromDslTypes = (
41
+ parameters: Array<Parameter>,
42
+ initArgs: string[],
43
+ instanceConfig: InstanceConfig,
44
+ /* artifactAddress: string,
45
+ provider?: ContractRunner, */
46
+ ): KnownParamsAndInitData => {
47
+ const execConstTypes = instanceConfig.execArguments
48
+ .filter((el) => el.constant)
49
+ .map((el) => el.type);
50
+
51
+ const knownExecParams = indexConstants(
52
+ parameters.filter((arg) => isConstant(arg.value)),
53
+ execConstTypes.map(DSLTypesToIRTypes),
54
+ );
55
+
56
+ const initTypes = instanceConfig.initArguments.map((el) => el.type);
57
+ const initDataEncoded = bytesEncodeArgs(
58
+ initArgs,
59
+ initTypes.map(DSLTypesToIRTypes),
60
+ );
61
+
62
+ return {
63
+ knownExecParams,
64
+ initDataEncoded,
65
+ };
66
+ };
67
+
68
+ const getKnownExecDataAndInitDataFromOnchainTypes = async (
69
+ parameters: Array<Parameter>,
70
+ initArgs: string[],
71
+ instanceConfig: InstanceConfig,
72
+ artifactAddress: string,
73
+ provider?: ContractRunner,
74
+ ): Promise<KnownParamsAndInitData> => {
75
+ const onchain = new OnchainDescriptorArgsTypesSource(provider!);
76
+
77
+ const { execParamsTypes, initParamsTypes } =
78
+ await onchain.getTypes(artifactAddress);
79
+
80
+ const knownExecParams = indexConstants(
81
+ parameters.filter((arg) => isConstant(arg.value)),
82
+ <SupportedSolidityType[]>execParamsTypes,
83
+ );
84
+ const initDataEncoded = bytesEncodeArgs(
85
+ initArgs,
86
+ <SupportedSolidityType[]>initParamsTypes,
87
+ );
88
+
89
+ return {
90
+ knownExecParams,
91
+ initDataEncoded,
92
+ };
93
+ };
94
+
95
+ // note: types dsl-inferred-first
96
+ export const parseIRByDSLTypesWithInterceptor = async (
97
+ { ir, typings }: TranspilerOutput,
98
+ middleware?: ValidationMiddlware,
99
+ ) => {
100
+ return parse(
101
+ { ir, typings },
102
+ getKnownExecDataAndInitDataFromDslTypes,
103
+ middleware,
104
+ );
105
+ };
106
+
107
+ // note: types onchain-inferred-first
108
+ export const parseIRByOnchainTypesWithInterceptor = async (
109
+ { ir, typings }: TranspilerOutput,
110
+ provider: ContractRunner,
111
+ middleware?: ValidationMiddlware,
112
+ ) => {
113
+ return parse(
114
+ { ir, typings },
115
+ getKnownExecDataAndInitDataFromOnchainTypes,
116
+ middleware,
117
+ provider,
118
+ );
119
+ };
120
+
121
+ const parse = async (
122
+ transpileOutput: Pick<TranspilerOutput, 'ir' | 'typings'>,
123
+ exectAndInitData: RetrieveExecAndInitData,
124
+ middleware?: ValidationMiddlware,
125
+ provider?: ContractRunner,
126
+ ) => {
127
+ const res: ParsingResult[] = [];
128
+
129
+ const artifacts = transpileOutput.ir.trim().split(/\r?\n/);
130
+ for (let [index, artifact] of artifacts.entries()) {
131
+ artifact = artifact.trim();
132
+ const { addressClause, paramsClause, initClause } =
133
+ extractComponents(artifact);
134
+
135
+ const artifactAddress = addressClause;
136
+ const parameters = extractArguments(paramsClause).map((value, index) => ({
137
+ value,
138
+ index,
139
+ }));
140
+
141
+ const initArgs = extractArguments(initClause);
142
+
143
+ const variables = parameters
144
+ .filter((val) => strIsVar(val.value))
145
+ .map((val) => val.index);
146
+ const substitutions = parameters
147
+ .filter((val) => strIsSubst(val.value))
148
+ .map((val) => ({
149
+ supplierNodeId: val.value.replace(/\|/g, ''),
150
+ index: val.index,
151
+ }));
152
+
153
+ const currentInstanceConfig = transpileOutput.typings[index];
154
+
155
+ if (middleware) {
156
+ await middleware.innerValidations(artifactAddress, currentInstanceConfig);
157
+ }
158
+
159
+ const { knownExecParams: partialExecData, initDataEncoded: initData } =
160
+ await exectAndInitData(
161
+ parameters,
162
+ initArgs,
163
+ currentInstanceConfig,
164
+ artifactAddress,
165
+ provider,
166
+ );
167
+
168
+ const injections = parameters
169
+ .filter((val) => strIsVar(val.value))
170
+ .map((el, index) => {
171
+ const injection = extractInjection(el.value);
172
+
173
+ if (injection && injection != '') {
174
+ return {
175
+ value: el.value,
176
+ index,
177
+ };
178
+ }
179
+ })
180
+ .filter((el) => !!el);
181
+
182
+ res.push({
183
+ id: NodeId.fromNotation(artifact, index),
184
+ artifactAddress,
185
+ constantExecArgs: partialExecData,
186
+ variableExecArgs: variables,
187
+ injections,
188
+ argsCount: parameters.length,
189
+ substitutedExecArgs: substitutions,
190
+ initData,
191
+ needsInitialization: initArgs.length != 0,
192
+ });
193
+ }
194
+
195
+ if (middleware) {
196
+ await middleware.outerValidations(res);
197
+ }
198
+
199
+ return res;
200
+ };
@@ -0,0 +1,67 @@
1
+ import { TranspilerOutput } from '@guardian-network/shared';
2
+ import { ContractRunner } from 'ethers';
3
+ import {
4
+ dslTypesToOnchainTypesParamsValidation,
5
+ onchainSubstitutionToReturnTypesValidation,
6
+ } from '../helpers';
7
+ import { ParserWithValidation } from './parser-contracts';
8
+
9
+ export const TypingsValidator = (provider: ContractRunner) => ({
10
+ innerValidations: dslTypesToOnchainTypesParamsValidation(provider),
11
+ outerValidations: onchainSubstitutionToReturnTypesValidation(provider),
12
+ });
13
+
14
+ export const getIRParser = (
15
+ input: TranspilerOutput,
16
+ provider: ContractRunner,
17
+ ) => {
18
+ // const middlerware = TypingsValidator(provider);
19
+
20
+ return {
21
+ validated: {
22
+ DSL_TYPING: async () => {
23
+ const parser = ParserWithValidation.fromDSLBasedConfig(
24
+ input.ir,
25
+ input.typings,
26
+ provider,
27
+ );
28
+ const parsed = await parser.process();
29
+ return parsed;
30
+ // const parsed = await parseIRByDSLTypesWithInterceptor(
31
+ // input,
32
+ // TypingsValidator(provider),
33
+ // );
34
+ // return parsed;
35
+ },
36
+ ONCHAIN_TYPING: async () => {
37
+ const parser = ParserWithValidation.fromOnchainSource(
38
+ input.ir,
39
+ provider,
40
+ );
41
+ const parsed = await parser.process();
42
+ return parsed;
43
+ // const parsed = await parseIRByOnchainTypesWithInterceptor(
44
+ // input,
45
+ // provider!,
46
+ // TypingsValidator(provider!),
47
+ // )
48
+ // return parsed;
49
+ },
50
+ },
51
+ };
52
+ };
53
+
54
+ export const getIRParserUnvalidate = (input: TranspilerOutput) => {
55
+ const unvalidateParser = async () => {
56
+ const parser = ParserWithValidation.fromDSLBasedConfig(
57
+ input.ir,
58
+ input.typings,
59
+ );
60
+ const parsed = await parser.process();
61
+ return parsed;
62
+ // const parsed = await parseIRByDSLTypesWithInterceptor(input);
63
+ // return parsed;
64
+ };
65
+
66
+ return unvalidateParser;
67
+ };
@@ -0,0 +1,28 @@
1
+ import {
2
+ DSLType,
3
+ InstanceConfig,
4
+ NodeTreeInitData,
5
+ SupportedSolidityType,
6
+ } from '@guardian-network/shared';
7
+
8
+ export type ParsingResult = NodeTreeInitData;
9
+
10
+ export type DslTypeToSolidityType = Record<DSLType, SupportedSolidityType>;
11
+
12
+ export const DSLTypesMapping: DslTypeToSolidityType = {
13
+ // SAFE KEYS
14
+ // UNSAFE VALUES
15
+ number: 'uint256',
16
+ address: 'address',
17
+ bool: 'bool',
18
+ string: 'string',
19
+ bytes: 'bytes',
20
+ };
21
+
22
+ export type ValidationMiddlware = {
23
+ innerValidations: (
24
+ artifactAddress: string,
25
+ currentInstanceConfig: InstanceConfig,
26
+ ) => Promise<void>;
27
+ outerValidations: (output: NodeTreeInitData[]) => Promise<void>;
28
+ };