@guardian-network/shared 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.
- package/README.md +2 -0
- package/dist/arbitrary-data-artifact/MinArbitraryDataArtifactFactory.d.ts +63 -0
- package/dist/arbitrary-data-artifact/MinArbitraryDataArtifactFactory.d.ts.map +1 -0
- package/dist/arbitrary-data-artifact/MinArbitraryDataArtifactFactory.js +87 -0
- package/dist/arbitrary-data-artifact/MinArbitraryDataArtifactFactory.js.map +1 -0
- package/dist/arbitrary-data-artifact/index.d.ts +3 -0
- package/dist/arbitrary-data-artifact/index.d.ts.map +1 -0
- package/dist/arbitrary-data-artifact/index.js +6 -0
- package/dist/arbitrary-data-artifact/index.js.map +1 -0
- package/dist/arbitrary-data-artifact/method-mutability.types.d.ts +9 -0
- package/dist/arbitrary-data-artifact/method-mutability.types.d.ts.map +1 -0
- package/dist/arbitrary-data-artifact/method-mutability.types.js +3 -0
- package/dist/arbitrary-data-artifact/method-mutability.types.js.map +1 -0
- package/dist/arbitrary-data-artifact/min-arbitrary-data-artifact.interface.d.ts +27 -0
- package/dist/arbitrary-data-artifact/min-arbitrary-data-artifact.interface.d.ts.map +1 -0
- package/dist/arbitrary-data-artifact/min-arbitrary-data-artifact.interface.js +3 -0
- package/dist/arbitrary-data-artifact/min-arbitrary-data-artifact.interface.js.map +1 -0
- package/dist/arbitrary-data-artifact/typed-contract-method.interface.d.ts +21 -0
- package/dist/arbitrary-data-artifact/typed-contract-method.interface.d.ts.map +1 -0
- package/dist/arbitrary-data-artifact/typed-contract-method.interface.js +3 -0
- package/dist/arbitrary-data-artifact/typed-contract-method.interface.js.map +1 -0
- package/dist/errors/BaseError.d.ts +5 -0
- package/dist/errors/BaseError.d.ts.map +1 -0
- package/dist/errors/BaseError.js +13 -0
- package/dist/errors/BaseError.js.map +1 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +18 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/misc-utils/data-structure-transformation.helper.d.ts +2 -0
- package/dist/misc-utils/data-structure-transformation.helper.d.ts.map +1 -0
- package/dist/misc-utils/data-structure-transformation.helper.js +8 -0
- package/dist/misc-utils/data-structure-transformation.helper.js.map +1 -0
- package/dist/misc-utils/find-cycle-node.helper.d.ts +3 -0
- package/dist/misc-utils/find-cycle-node.helper.d.ts.map +1 -0
- package/dist/misc-utils/find-cycle-node.helper.js +40 -0
- package/dist/misc-utils/find-cycle-node.helper.js.map +1 -0
- package/dist/misc-utils/index.d.ts +4 -0
- package/dist/misc-utils/index.d.ts.map +1 -0
- package/dist/misc-utils/index.js +20 -0
- package/dist/misc-utils/index.js.map +1 -0
- package/dist/misc-utils/node-id-tooling.d.ts +5 -0
- package/dist/misc-utils/node-id-tooling.d.ts.map +1 -0
- package/dist/misc-utils/node-id-tooling.js +14 -0
- package/dist/misc-utils/node-id-tooling.js.map +1 -0
- package/dist/solidity-encode-decode/decode.helper.d.ts +8 -0
- package/dist/solidity-encode-decode/decode.helper.d.ts.map +1 -0
- package/dist/solidity-encode-decode/decode.helper.js +33 -0
- package/dist/solidity-encode-decode/decode.helper.js.map +1 -0
- package/dist/solidity-encode-decode/encode.helper.d.ts +17 -0
- package/dist/solidity-encode-decode/encode.helper.d.ts.map +1 -0
- package/dist/solidity-encode-decode/encode.helper.js +73 -0
- package/dist/solidity-encode-decode/encode.helper.js.map +1 -0
- package/dist/solidity-encode-decode/index.d.ts +5 -0
- package/dist/solidity-encode-decode/index.d.ts.map +1 -0
- package/dist/solidity-encode-decode/index.js +14 -0
- package/dist/solidity-encode-decode/index.js.map +1 -0
- package/dist/solidity-types/base/SolidityType.d.ts +11 -0
- package/dist/solidity-types/base/SolidityType.d.ts.map +1 -0
- package/dist/solidity-types/base/SolidityType.js +23 -0
- package/dist/solidity-types/base/SolidityType.js.map +1 -0
- package/dist/solidity-types/base/index.d.ts +2 -0
- package/dist/solidity-types/base/index.d.ts.map +1 -0
- package/dist/solidity-types/base/index.js +18 -0
- package/dist/solidity-types/base/index.js.map +1 -0
- package/dist/solidity-types/constants.d.ts +10 -0
- package/dist/solidity-types/constants.d.ts.map +1 -0
- package/dist/solidity-types/constants.js +21 -0
- package/dist/solidity-types/constants.js.map +1 -0
- package/dist/solidity-types/index.d.ts +5 -0
- package/dist/solidity-types/index.d.ts.map +1 -0
- package/dist/solidity-types/index.js +21 -0
- package/dist/solidity-types/index.js.map +1 -0
- package/dist/solidity-types/native-types/SolidityAddressType.d.ts +6 -0
- package/dist/solidity-types/native-types/SolidityAddressType.d.ts.map +1 -0
- package/dist/solidity-types/native-types/SolidityAddressType.js +15 -0
- package/dist/solidity-types/native-types/SolidityAddressType.js.map +1 -0
- package/dist/solidity-types/native-types/SolidityBytesType.d.ts +6 -0
- package/dist/solidity-types/native-types/SolidityBytesType.d.ts.map +1 -0
- package/dist/solidity-types/native-types/SolidityBytesType.js +15 -0
- package/dist/solidity-types/native-types/SolidityBytesType.js.map +1 -0
- package/dist/solidity-types/native-types/index.d.ts +3 -0
- package/dist/solidity-types/native-types/index.d.ts.map +1 -0
- package/dist/solidity-types/native-types/index.js +19 -0
- package/dist/solidity-types/native-types/index.js.map +1 -0
- package/dist/solidity-types/solidity-types-packing.helper.d.ts +2 -0
- package/dist/solidity-types/solidity-types-packing.helper.d.ts.map +1 -0
- package/dist/solidity-types/solidity-types-packing.helper.js +13 -0
- package/dist/solidity-types/solidity-types-packing.helper.js.map +1 -0
- package/dist/solidity-types/solidity-types-verification.helper.d.ts +6 -0
- package/dist/solidity-types/solidity-types-verification.helper.d.ts.map +1 -0
- package/dist/solidity-types/solidity-types-verification.helper.js +42 -0
- package/dist/solidity-types/solidity-types-verification.helper.js.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityAddressListPacked.d.ts +5 -0
- package/dist/solidity-types/synthetic-types/SolidityAddressListPacked.d.ts.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityAddressListPacked.js +15 -0
- package/dist/solidity-types/synthetic-types/SolidityAddressListPacked.js.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityBytes32Packed.d.ts +5 -0
- package/dist/solidity-types/synthetic-types/SolidityBytes32Packed.d.ts.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityBytes32Packed.js +15 -0
- package/dist/solidity-types/synthetic-types/SolidityBytes32Packed.js.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityBytesListPacked.d.ts +5 -0
- package/dist/solidity-types/synthetic-types/SolidityBytesListPacked.d.ts.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityBytesListPacked.js +15 -0
- package/dist/solidity-types/synthetic-types/SolidityBytesListPacked.js.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityUint24ListTypePacked.d.ts +7 -0
- package/dist/solidity-types/synthetic-types/SolidityUint24ListTypePacked.d.ts.map +1 -0
- package/dist/solidity-types/synthetic-types/SolidityUint24ListTypePacked.js +27 -0
- package/dist/solidity-types/synthetic-types/SolidityUint24ListTypePacked.js.map +1 -0
- package/dist/solidity-types/synthetic-types/index.d.ts +5 -0
- package/dist/solidity-types/synthetic-types/index.d.ts.map +1 -0
- package/dist/solidity-types/synthetic-types/index.js +21 -0
- package/dist/solidity-types/synthetic-types/index.js.map +1 -0
- package/dist/types/compiler.types.d.ts +11 -0
- package/dist/types/compiler.types.d.ts.map +1 -0
- package/dist/types/compiler.types.js +3 -0
- package/dist/types/compiler.types.js.map +1 -0
- package/dist/types/contracts.types.d.ts +52 -0
- package/dist/types/contracts.types.d.ts.map +1 -0
- package/dist/types/contracts.types.js +3 -0
- package/dist/types/contracts.types.js.map +1 -0
- package/dist/types/dsl.types.d.ts +32 -0
- package/dist/types/dsl.types.d.ts.map +1 -0
- package/dist/types/dsl.types.js +3 -0
- package/dist/types/dsl.types.js.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +23 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/intermediate-representation.types.d.ts +18 -0
- package/dist/types/intermediate-representation.types.d.ts.map +1 -0
- package/dist/types/intermediate-representation.types.js +19 -0
- package/dist/types/intermediate-representation.types.js.map +1 -0
- package/dist/types/solidity-decode.types.d.ts +2 -0
- package/dist/types/solidity-decode.types.d.ts.map +1 -0
- package/dist/types/solidity-decode.types.js +3 -0
- package/dist/types/solidity-decode.types.js.map +1 -0
- package/dist/types/solidity-encode.types.d.ts +5 -0
- package/dist/types/solidity-encode.types.d.ts.map +1 -0
- package/dist/types/solidity-encode.types.js +3 -0
- package/dist/types/solidity-encode.types.js.map +1 -0
- package/package.json +67 -0
- package/src/arbitrary-data-artifact/MinArbitraryDataArtifactFactory.ts +97 -0
- package/src/arbitrary-data-artifact/index.ts +2 -0
- package/src/arbitrary-data-artifact/method-mutability.types.ts +24 -0
- package/src/arbitrary-data-artifact/min-arbitrary-data-artifact.interface.ts +32 -0
- package/src/arbitrary-data-artifact/typed-contract-method.interface.ts +57 -0
- package/src/errors/BaseError.ts +12 -0
- package/src/errors/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/misc-utils/data-structure-transformation.helper.ts +3 -0
- package/src/misc-utils/find-cycle-node.helper.ts +53 -0
- package/src/misc-utils/index.ts +3 -0
- package/src/misc-utils/node-id-tooling.ts +15 -0
- package/src/solidity-encode-decode/decode.helper.ts +57 -0
- package/src/solidity-encode-decode/encode.helper.ts +133 -0
- package/src/solidity-encode-decode/index.ts +10 -0
- package/src/solidity-types/base/SolidityType.ts +26 -0
- package/src/solidity-types/base/index.ts +1 -0
- package/src/solidity-types/constants.ts +23 -0
- package/src/solidity-types/index.ts +4 -0
- package/src/solidity-types/native-types/SolidityAddressType.ts +13 -0
- package/src/solidity-types/native-types/SolidityBytesType.ts +15 -0
- package/src/solidity-types/native-types/index.ts +2 -0
- package/src/solidity-types/solidity-types-packing.helper.ts +13 -0
- package/src/solidity-types/solidity-types-verification.helper.ts +52 -0
- package/src/solidity-types/synthetic-types/SolidityAddressListPacked.ts +13 -0
- package/src/solidity-types/synthetic-types/SolidityBytes32Packed.ts +13 -0
- package/src/solidity-types/synthetic-types/SolidityBytesListPacked.ts +13 -0
- package/src/solidity-types/synthetic-types/SolidityUint24ListTypePacked.ts +31 -0
- package/src/solidity-types/synthetic-types/index.ts +4 -0
- package/src/types/compiler.types.ts +12 -0
- package/src/types/contracts.types.ts +58 -0
- package/src/types/dsl.types.ts +39 -0
- package/src/types/index.ts +6 -0
- package/src/types/intermediate-representation.types.ts +18 -0
- package/src/types/solidity-decode.types.ts +5 -0
- package/src/types/solidity-encode.types.ts +14 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { DslNode, NodeWithReference } from '../types/dsl.types';
|
|
2
|
+
|
|
3
|
+
export const findCycle = (nodes: DslNode[]): NodeWithReference | null => {
|
|
4
|
+
const nodeMap = new Map<string, DslNode>();
|
|
5
|
+
nodes.forEach((node) => nodeMap.set(node.id, node));
|
|
6
|
+
|
|
7
|
+
const visited = new Set<string>();
|
|
8
|
+
const explored = new Set<string>();
|
|
9
|
+
|
|
10
|
+
const hasCycle = (
|
|
11
|
+
nodeId: string,
|
|
12
|
+
parentNodeId: string,
|
|
13
|
+
): [/* isCyclic: */ boolean, NodeWithReference?] => {
|
|
14
|
+
if (visited.has(nodeId)) {
|
|
15
|
+
return [true, { nodeId, parentNodeId }];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (explored.has(nodeId)) {
|
|
19
|
+
return [false];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const node = nodeMap.get(nodeId);
|
|
23
|
+
if (!node) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`Node ${nodeId} not found in graph. Probably nodeIds are hashed incorrectly.`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
visited.add(nodeId);
|
|
30
|
+
|
|
31
|
+
for (const referenceId of node.references) {
|
|
32
|
+
const recTest = hasCycle(referenceId, nodeId);
|
|
33
|
+
|
|
34
|
+
if (recTest[0]) {
|
|
35
|
+
return recTest;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
visited.delete(nodeId);
|
|
40
|
+
explored.add(nodeId);
|
|
41
|
+
|
|
42
|
+
return [false];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
for (const node of nodes) {
|
|
46
|
+
const cycleTestResult = hasCycle(node.id, '0');
|
|
47
|
+
if (!explored.has(node.id) && cycleTestResult[0]) {
|
|
48
|
+
return cycleTestResult[1]!;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return null;
|
|
53
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { solidityPackedKeccak256 as keccak256Hash } from 'ethers';
|
|
2
|
+
export { solidityPackedKeccak256 as keccak256Hash } from 'ethers';
|
|
3
|
+
|
|
4
|
+
export class NodeId {
|
|
5
|
+
// note: this takes (intermeditate presentation of artifact + salt = node_id)
|
|
6
|
+
// with significant uniqueness level
|
|
7
|
+
static fromNotation(artifactIntermediateForm: string, salt: number): string {
|
|
8
|
+
// todo: validate 'artifactIntermediateForm' relying on Regex
|
|
9
|
+
const nodeId = keccak256Hash(
|
|
10
|
+
['string', 'uint256'],
|
|
11
|
+
[artifactIntermediateForm.trim(), salt],
|
|
12
|
+
);
|
|
13
|
+
return nodeId;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { DecodedParamType } from '../types/solidity-decode.types';
|
|
2
|
+
import { defaultAbiCoder } from './';
|
|
3
|
+
|
|
4
|
+
type SolidityDecodeReturnType = ReturnType<typeof defaultAbiCoder.decode>;
|
|
5
|
+
type SolidityDecodeInputParamsType = Parameters<typeof defaultAbiCoder.decode>;
|
|
6
|
+
|
|
7
|
+
type PreciseDecodedParamType<T> = T extends 'string'
|
|
8
|
+
? string
|
|
9
|
+
: T extends 'bool'
|
|
10
|
+
? boolean
|
|
11
|
+
: T extends 'uint256'
|
|
12
|
+
? bigint
|
|
13
|
+
: T extends 'bytes' // todo: add support of 'bytes32'
|
|
14
|
+
? string
|
|
15
|
+
: any;
|
|
16
|
+
|
|
17
|
+
export const solidityDecode = (
|
|
18
|
+
...params: SolidityDecodeInputParamsType
|
|
19
|
+
): SolidityDecodeReturnType => {
|
|
20
|
+
return defaultAbiCoder.decode(...params);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const solidityDecodeSingleParam = <T extends DecodedParamType>(
|
|
24
|
+
type: T,
|
|
25
|
+
data: string,
|
|
26
|
+
): PreciseDecodedParamType<T> => {
|
|
27
|
+
const decodedArray = solidityDecode([type], data).toArray();
|
|
28
|
+
|
|
29
|
+
if (decodedArray.length !== 1)
|
|
30
|
+
throw new Error(
|
|
31
|
+
'Decode: Solidity data contains more than single param value',
|
|
32
|
+
);
|
|
33
|
+
const [result] = decodedArray;
|
|
34
|
+
|
|
35
|
+
const resultWithPropperType = inferAndPopulateWithTypescriptType(
|
|
36
|
+
type,
|
|
37
|
+
result,
|
|
38
|
+
);
|
|
39
|
+
return resultWithPropperType;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const inferAndPopulateWithTypescriptType = <T extends DecodedParamType>(
|
|
43
|
+
type: T,
|
|
44
|
+
value: any,
|
|
45
|
+
) => {
|
|
46
|
+
if (type === 'string') {
|
|
47
|
+
return value as PreciseDecodedParamType<T>;
|
|
48
|
+
} else if (type === 'bool') {
|
|
49
|
+
return value as PreciseDecodedParamType<T>;
|
|
50
|
+
} else if (type === 'uint256') {
|
|
51
|
+
return BigInt(value) as PreciseDecodedParamType<T>;
|
|
52
|
+
} else if (type === 'bytes') {
|
|
53
|
+
return value as PreciseDecodedParamType<T>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
throw new Error('Decode: Not supported decoding type');
|
|
57
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SolidityAddressType,
|
|
3
|
+
SolidityBytesType,
|
|
4
|
+
} from '../solidity-types/native-types';
|
|
5
|
+
import {
|
|
6
|
+
EncodedParamType,
|
|
7
|
+
ObjectEncodeParamTypes,
|
|
8
|
+
PrimitiveEncodeParamTypes,
|
|
9
|
+
} from '../types/solidity-encode.types';
|
|
10
|
+
import { defaultAbiCoder } from './';
|
|
11
|
+
|
|
12
|
+
type SolidityEncodeReturnType = ReturnType<typeof defaultAbiCoder.encode>;
|
|
13
|
+
type SolidityEncodeInputParamsType = Parameters<typeof defaultAbiCoder.encode>;
|
|
14
|
+
|
|
15
|
+
type PreciseEncodedParamType<T extends EncodedParamType> = T extends string
|
|
16
|
+
? 'string'
|
|
17
|
+
: T extends boolean
|
|
18
|
+
? 'bool'
|
|
19
|
+
: T extends number
|
|
20
|
+
? 'uint256'
|
|
21
|
+
: T extends SolidityAddressType
|
|
22
|
+
? 'address'
|
|
23
|
+
: T extends SolidityBytesType
|
|
24
|
+
? 'bytes'
|
|
25
|
+
: /* : T extends SolidityUint24ListType
|
|
26
|
+
? 'uint24[]' */
|
|
27
|
+
any;
|
|
28
|
+
|
|
29
|
+
export const solidityEncode = (
|
|
30
|
+
...params: SolidityEncodeInputParamsType
|
|
31
|
+
): SolidityEncodeReturnType => {
|
|
32
|
+
return defaultAbiCoder.encode(...params);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// note: this encode string, number, bool into respective Solidity presentation; type will pick automatically
|
|
36
|
+
// unless ObjectEncodeParamTypes is applied, this will fail with proper encoding when raw string-bytes or string-address supplied
|
|
37
|
+
export const solidityEncodeSingleParam = <T extends EncodedParamType>(
|
|
38
|
+
primitiveTypeOrObject: T,
|
|
39
|
+
): SolidityEncodeReturnType => {
|
|
40
|
+
const { typename, value: unpackedValue } = inferSolidityType(
|
|
41
|
+
primitiveTypeOrObject,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const encodedResult = solidityEncode([typename], [unpackedValue]);
|
|
45
|
+
return encodedResult;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const solidityEncodeMultipleParams = <T extends Array<EncodedParamType>>(
|
|
49
|
+
...primitiveTypeOrObjectList: T
|
|
50
|
+
): SolidityEncodeReturnType => {
|
|
51
|
+
const typesAndValues = primitiveTypeOrObjectList.map((v) =>
|
|
52
|
+
inferSolidityType(v),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const encodedResult = solidityEncode(
|
|
56
|
+
typesAndValues.map((v) => v.typename),
|
|
57
|
+
typesAndValues.map((v) => v.value),
|
|
58
|
+
);
|
|
59
|
+
return encodedResult;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const inferSolidityType = <T extends EncodedParamType>(
|
|
63
|
+
primitiveTypeOrObject: T,
|
|
64
|
+
) => {
|
|
65
|
+
if (isPrimitiveType(primitiveTypeOrObject)) {
|
|
66
|
+
return inferPirimitiveType(primitiveTypeOrObject);
|
|
67
|
+
} else if (isObjectType(primitiveTypeOrObject)) {
|
|
68
|
+
return inferObjectType(primitiveTypeOrObject);
|
|
69
|
+
} else
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Encode: Provide with not supported encoding type ${primitiveTypeOrObject}`,
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const inferPirimitiveType = <T extends PrimitiveEncodeParamTypes>(
|
|
76
|
+
primitiveTypeOrObject: T,
|
|
77
|
+
) => {
|
|
78
|
+
let typename: PreciseEncodedParamType<T>;
|
|
79
|
+
let value = primitiveTypeOrObject;
|
|
80
|
+
|
|
81
|
+
const type = typeof primitiveTypeOrObject;
|
|
82
|
+
if (type === 'boolean') {
|
|
83
|
+
typename = 'bool' as PreciseEncodedParamType<T>;
|
|
84
|
+
} else if (type === 'string') {
|
|
85
|
+
typename = 'string' as PreciseEncodedParamType<T>;
|
|
86
|
+
} else if (type === 'number') {
|
|
87
|
+
typename = 'uint256' as PreciseEncodedParamType<T>;
|
|
88
|
+
} else
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Encode: Not supported encoding type for primitive-type ${type}`,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
return { typename, value };
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const inferObjectType = <T extends ObjectEncodeParamTypes>(
|
|
97
|
+
primitiveTypeOrObject: T,
|
|
98
|
+
) => {
|
|
99
|
+
let typename: PreciseEncodedParamType<T>;
|
|
100
|
+
const value = primitiveTypeOrObject.value;
|
|
101
|
+
|
|
102
|
+
if (primitiveTypeOrObject instanceof SolidityAddressType) {
|
|
103
|
+
typename = 'address' as PreciseEncodedParamType<T>;
|
|
104
|
+
} else if (primitiveTypeOrObject instanceof SolidityBytesType) {
|
|
105
|
+
typename = 'bytes' as PreciseEncodedParamType<T>;
|
|
106
|
+
} /* else if (primitiveTypeOrObject instanceof SolidityUint24ListType) {
|
|
107
|
+
typename = 'uint24[]' as PreciseEncodedParamType<T>;
|
|
108
|
+
} */ else
|
|
109
|
+
throw new Error(
|
|
110
|
+
`Encode: Not supported encoding type for object: ${primitiveTypeOrObject}`,
|
|
111
|
+
);
|
|
112
|
+
return { typename, value };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// guards
|
|
116
|
+
const isPrimitiveType = (
|
|
117
|
+
value: EncodedParamType,
|
|
118
|
+
): value is PrimitiveEncodeParamTypes => {
|
|
119
|
+
const type = typeof value;
|
|
120
|
+
return type === 'string' || type === 'boolean' || type === 'number';
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const isObjectType = (
|
|
124
|
+
value: EncodedParamType,
|
|
125
|
+
): value is ObjectEncodeParamTypes => {
|
|
126
|
+
const type = typeof value;
|
|
127
|
+
return (
|
|
128
|
+
type === 'object' &&
|
|
129
|
+
(value instanceof SolidityAddressType ||
|
|
130
|
+
value instanceof SolidityBytesType) /* ||
|
|
131
|
+
value instanceof SolidityUint24ListType */
|
|
132
|
+
);
|
|
133
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AbiCoder } from 'ethers';
|
|
2
|
+
export { solidityDecode, solidityDecodeSingleParam } from './decode.helper';
|
|
3
|
+
export {
|
|
4
|
+
inferSolidityType,
|
|
5
|
+
solidityEncode,
|
|
6
|
+
solidityEncodeMultipleParams,
|
|
7
|
+
solidityEncodeSingleParam,
|
|
8
|
+
} from './encode.helper';
|
|
9
|
+
|
|
10
|
+
export const defaultAbiCoder = AbiCoder.defaultAbiCoder();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
type Verifier<T> = (value: T) => T;
|
|
2
|
+
|
|
3
|
+
export abstract class SolidityType<T> {
|
|
4
|
+
static build<T, R>(
|
|
5
|
+
this: new (param: T, verifierMethod: Verifier<T>) => R,
|
|
6
|
+
nonVerifiedValue: T,
|
|
7
|
+
verifier: Verifier<T>,
|
|
8
|
+
): R {
|
|
9
|
+
const verified = verifier(nonVerifiedValue);
|
|
10
|
+
|
|
11
|
+
return new this(verified, verifier);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
protected verifiedValue: T,
|
|
16
|
+
protected verifierMethod: Verifier<T>,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
public get value(): T {
|
|
20
|
+
return this.verifiedValue;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected setValue(value: T) {
|
|
24
|
+
this.verifiedValue = this.verifierMethod(value);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './SolidityType';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const SOLIDITY_UINT256 = 'uint256';
|
|
2
|
+
export const SOLIDITY_BOOL = 'bool';
|
|
3
|
+
export const SOLIDITY_ADDRESS = 'address';
|
|
4
|
+
export const SOLIDITY_BYTES32 = 'bytes32'; // NOT SUPPORTED YET, but wraps into SOLIDITY_BYTES offchain
|
|
5
|
+
|
|
6
|
+
export const SOLIDITY_BYTES = 'bytes';
|
|
7
|
+
export const SOLIDITY_STRING = 'string';
|
|
8
|
+
export const SOLIIDTY_UINT24_LIST = 'uint24[]'; // NOT SUPPORTED YET onchain, but wraps into SOLIDITY_BYTES offchain
|
|
9
|
+
|
|
10
|
+
// todo: retrieve at compile-time from '../../contracts/pre-defined/constants/ValueTypeNames.sol'
|
|
11
|
+
export const ALLOWED_SOLIDITY_VALUE_TYPES: Array<string> = [
|
|
12
|
+
SOLIDITY_UINT256,
|
|
13
|
+
SOLIDITY_BOOL,
|
|
14
|
+
SOLIDITY_ADDRESS,
|
|
15
|
+
// SOLIDITY_BYTES32,
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
// todo: retrieve at compile-time from '../../contracts/pre-defined/constants/ReferenceTypeNames.sol'
|
|
19
|
+
export const ALLOWED_SOLIDITY_REFERENCE_TYPES: Array<string> = [
|
|
20
|
+
SOLIDITY_BYTES,
|
|
21
|
+
SOLIDITY_STRING,
|
|
22
|
+
SOLIIDTY_UINT24_LIST,
|
|
23
|
+
];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SolidityType } from '../base';
|
|
2
|
+
import { verifyAddress } from '../solidity-types-verification.helper';
|
|
3
|
+
|
|
4
|
+
// natively supported type in artifacts
|
|
5
|
+
export class SolidityAddressType extends SolidityType<string> {
|
|
6
|
+
static create = (address: string) => {
|
|
7
|
+
return this.build(address, verifyAddress);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
public get address(): string {
|
|
11
|
+
return this.verifiedValue;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SolidityType } from '../base';
|
|
2
|
+
import { verifyBytes } from '../solidity-types-verification.helper';
|
|
3
|
+
|
|
4
|
+
// natively supported type in artifacts
|
|
5
|
+
export class SolidityBytesType extends SolidityType<string> {
|
|
6
|
+
static create = (bytes: string) => {
|
|
7
|
+
return this.build(bytes, verifyBytes);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// 'SolidityBytesType' has to have at least one even private method that differs
|
|
11
|
+
// from 'SolidityAddressType'; otherwise 'PreciseEncodedParamType' won't work properly
|
|
12
|
+
public get bytes(): string {
|
|
13
|
+
return this.verifiedValue;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { solidityEncode } from '../solidity-encode-decode';
|
|
2
|
+
import { SOLIIDTY_UINT24_LIST } from './constants';
|
|
3
|
+
import { verifyUint24Array } from './solidity-types-verification.helper';
|
|
4
|
+
|
|
5
|
+
export const packAndVerifyUint24List = (list: number[]) => {
|
|
6
|
+
const verifiedList = verifyUint24Array(list);
|
|
7
|
+
const bytesPackedList = solidityEncode(
|
|
8
|
+
[SOLIIDTY_UINT24_LIST],
|
|
9
|
+
[verifiedList],
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
return bytesPackedList;
|
|
13
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { getAddress, getBytes } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export const verifyAddress = (value: string) => {
|
|
4
|
+
// validates, normalizes and returns checksumed value
|
|
5
|
+
const address = getAddress(value);
|
|
6
|
+
return address;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const verifyAddressesList = (list: Array<string>) => {
|
|
10
|
+
const verifiedList = list.map((v) => verifyAddress(v));
|
|
11
|
+
|
|
12
|
+
return verifiedList;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const verifyBytesList = (list: Array<string>) => {
|
|
16
|
+
const verifiedList = list.map((v) => verifyBytes(v));
|
|
17
|
+
|
|
18
|
+
return verifiedList;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const verifyBytes = (value: string) => {
|
|
22
|
+
// just a validation; no need to return as Uint8Array
|
|
23
|
+
getBytes(value);
|
|
24
|
+
return value;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const verifyUint24Array = (
|
|
28
|
+
list: Array<number>,
|
|
29
|
+
expectedLength?: number,
|
|
30
|
+
) => {
|
|
31
|
+
// validates each element fits into solidity uint24 type
|
|
32
|
+
const isValidTreshold = (value: number): boolean => {
|
|
33
|
+
const tresholdMin = 0;
|
|
34
|
+
const tresholdMax = Math.pow(2, 24);
|
|
35
|
+
|
|
36
|
+
const result = value >= tresholdMin && value <= tresholdMax;
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
let isValid =
|
|
41
|
+
Array.isArray(list) &&
|
|
42
|
+
list.every((value) => typeof value === 'number' && isValidTreshold(value));
|
|
43
|
+
|
|
44
|
+
// validate: 'list.length' is equal to 'expectedLength'
|
|
45
|
+
isValid = !!expectedLength
|
|
46
|
+
? isValid && list.length === expectedLength
|
|
47
|
+
: isValid;
|
|
48
|
+
|
|
49
|
+
if (!isValid) throw new Error(`Can not satisfy Uint24 array validations`);
|
|
50
|
+
|
|
51
|
+
return list;
|
|
52
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { solidityEncode } from '../../solidity-encode-decode';
|
|
2
|
+
import { SolidityBytesType } from '../native-types';
|
|
3
|
+
import { verifyAddressesList } from '../solidity-types-verification.helper';
|
|
4
|
+
|
|
5
|
+
// todo: refactor as done in 'SolidityUint24ListTypePacked'
|
|
6
|
+
export class SolidityAddressListPacked extends SolidityBytesType {
|
|
7
|
+
static fromList = (list: Array<string>) => {
|
|
8
|
+
const verifiedList = verifyAddressesList(list);
|
|
9
|
+
const toBytes = solidityEncode(['address[]'], [verifiedList]);
|
|
10
|
+
|
|
11
|
+
return this.create(toBytes);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { solidityEncode } from '../../solidity-encode-decode';
|
|
2
|
+
import { SolidityBytesType } from '../native-types';
|
|
3
|
+
import { verifyBytes } from '../solidity-types-verification.helper';
|
|
4
|
+
|
|
5
|
+
// todo: refactor as done in 'SolidityUint24ListTypePacked'
|
|
6
|
+
export class SolidityBytes32TypePacked extends SolidityBytesType {
|
|
7
|
+
static fromString = (bytes32Value: string) => {
|
|
8
|
+
verifyBytes(bytes32Value);
|
|
9
|
+
const toBytes = solidityEncode(['bytes32'], [bytes32Value]);
|
|
10
|
+
|
|
11
|
+
return this.create(toBytes);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { solidityEncode } from '../../solidity-encode-decode';
|
|
2
|
+
import { SolidityBytesType } from '../native-types';
|
|
3
|
+
import { verifyBytesList } from '../solidity-types-verification.helper';
|
|
4
|
+
|
|
5
|
+
// todo: refactor as done in 'SolidityUint24ListTypePacked'
|
|
6
|
+
export class SolidityBytesListPacked extends SolidityBytesType {
|
|
7
|
+
static fromList = (list: Array<string>) => {
|
|
8
|
+
verifyBytesList(list);
|
|
9
|
+
const toBytes = solidityEncode(['bytes[]'], [list]);
|
|
10
|
+
|
|
11
|
+
return this.create(toBytes);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { solidityDecode } from '../../solidity-encode-decode';
|
|
2
|
+
import { SOLIIDTY_UINT24_LIST } from '../constants';
|
|
3
|
+
import { SolidityBytesType } from '../native-types';
|
|
4
|
+
import { packAndVerifyUint24List } from '../solidity-types-packing.helper';
|
|
5
|
+
import { verifyBytes } from '../solidity-types-verification.helper';
|
|
6
|
+
|
|
7
|
+
export class SolidityUint24ListTypePacked extends SolidityBytesType {
|
|
8
|
+
static fromUint24List = (list: number[]) => {
|
|
9
|
+
const bytesPackedList = packAndVerifyUint24List(list);
|
|
10
|
+
|
|
11
|
+
return this.build(bytesPackedList, verifyBytes);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
public get uintArray(): Array<bigint> {
|
|
15
|
+
const [decoded] = solidityDecode(
|
|
16
|
+
[SOLIIDTY_UINT24_LIST],
|
|
17
|
+
this.verifiedValue,
|
|
18
|
+
);
|
|
19
|
+
const result = [...(decoded as Array<bigint>)];
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
setValueAtPos = (pos: number, value: number) => {
|
|
24
|
+
const toNumbersList = this.uintArray.map((v) => Number(v)); // should be safe since it is in range of uint24
|
|
25
|
+
toNumbersList[pos] = value;
|
|
26
|
+
|
|
27
|
+
const bytesPackedList = packAndVerifyUint24List(toNumbersList);
|
|
28
|
+
|
|
29
|
+
this.setValue(bytesPackedList);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ContractRunner } from 'ethers';
|
|
2
|
+
import { OnchainPresentation } from './contracts.types';
|
|
3
|
+
|
|
4
|
+
export type ICompiler = {
|
|
5
|
+
compile(): Promise<OnchainPresentation>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export interface LacLangCompilerOptions {
|
|
9
|
+
checkTypesAgainstOnchainDescriptors?: boolean;
|
|
10
|
+
checkTypesAgainstDslDeclarations?: boolean;
|
|
11
|
+
provider?: ContractRunner;
|
|
12
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// note: this file mostly duplicates types located in packages/contratcs/src/typechain/**
|
|
2
|
+
import { AddressLike, BigNumberish, BytesLike } from 'ethers';
|
|
3
|
+
|
|
4
|
+
type InjectionMetadataStruct = { value: string; index: BigNumberish };
|
|
5
|
+
|
|
6
|
+
type SubstitutionArgumentStruct = {
|
|
7
|
+
supplierNodeId: BytesLike; // bytes32 encoded constant value
|
|
8
|
+
index: BigNumberish; // POSITION index in generic arguments list of particular node
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type ConstantArgumentStruct = {
|
|
12
|
+
value: BytesLike; // bytes encoded constant value
|
|
13
|
+
index: BigNumberish; // POSITION index in generic arguments list of particular node
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type VariablesStruct = { nodeId: BytesLike; values: BytesLike[] };
|
|
17
|
+
|
|
18
|
+
export type NodeInitDataStruct = {
|
|
19
|
+
id: BytesLike;
|
|
20
|
+
initData: BytesLike;
|
|
21
|
+
needsInitialization: boolean;
|
|
22
|
+
artifactAddress: AddressLike;
|
|
23
|
+
argsCount: BigNumberish;
|
|
24
|
+
constantExecArgs: ConstantArgumentStruct[];
|
|
25
|
+
substitutedExecArgs: SubstitutionArgumentStruct[];
|
|
26
|
+
variableExecArgs: BigNumberish[];
|
|
27
|
+
injections: InjectionMetadataStruct[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type GraphInitParamsStruct = {
|
|
31
|
+
rootNode: BytesLike;
|
|
32
|
+
nodes: NodeInitDataStruct[];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type OnchainPresentation = GraphInitParamsStruct;
|
|
36
|
+
|
|
37
|
+
export type NodeTreeInitData = NodeInitDataStruct;
|
|
38
|
+
|
|
39
|
+
// taken from 'packages/contracts/src/typechain/contracts/PolicyHandler.ts
|
|
40
|
+
type ExecVarsMetadataStructOutputBase = {
|
|
41
|
+
nodeId: string;
|
|
42
|
+
nodeIndex: bigint;
|
|
43
|
+
artifactAddress: string;
|
|
44
|
+
descriptions: ArgumentDescriptionStructOutput[];
|
|
45
|
+
injections: StringAndIndexStructOutput[];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type OnchainVariablesDescription = ExecVarsMetadataStructOutputBase;
|
|
49
|
+
|
|
50
|
+
type ArgumentDescriptionStructOutput = {
|
|
51
|
+
name: string;
|
|
52
|
+
typename: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type StringAndIndexStructOutput = {
|
|
56
|
+
value: string;
|
|
57
|
+
index: bigint;
|
|
58
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type MinTypedValue = {
|
|
2
|
+
type: string;
|
|
3
|
+
value: string;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export interface DslNode {
|
|
7
|
+
id: string;
|
|
8
|
+
references: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type TypedValue = MinTypedValue & {
|
|
12
|
+
substitution?: boolean;
|
|
13
|
+
constant?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type InstanceConfigArgumentsOnly = {
|
|
17
|
+
execArguments: TypedValue[];
|
|
18
|
+
initArguments: TypedValue[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type InstanceConfigNameAndAddressOnly = {
|
|
22
|
+
name?: string;
|
|
23
|
+
artifactAddress: string;
|
|
24
|
+
needsInitialization: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type InstanceConfig = InstanceConfigArgumentsOnly &
|
|
28
|
+
InstanceConfigNameAndAddressOnly;
|
|
29
|
+
|
|
30
|
+
export type TranspilerOutput = {
|
|
31
|
+
ir: string;
|
|
32
|
+
rootNode: string;
|
|
33
|
+
typings: InstanceConfig[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type NodeWithReference = {
|
|
37
|
+
nodeId: string;
|
|
38
|
+
parentNodeId: string;
|
|
39
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
enum SupportedSolidityTypesEnum {
|
|
2
|
+
Uint256 = 'uint256',
|
|
3
|
+
String = 'string',
|
|
4
|
+
Bool = 'bool',
|
|
5
|
+
Bytes = 'bytes',
|
|
6
|
+
Address = 'address',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
enum DSLTypesEnum {
|
|
10
|
+
Number = 'number',
|
|
11
|
+
String = 'string',
|
|
12
|
+
Bool = 'bool',
|
|
13
|
+
Bytes = 'bytes',
|
|
14
|
+
Address = 'address',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type SupportedSolidityType = `${SupportedSolidityTypesEnum}`;
|
|
18
|
+
export type DSLType = `${DSLTypesEnum}`;
|