@stacks/rendezvous 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -23
- package/dist/citizen.js +1 -1
- package/dist/invariant.js +40 -22
- package/dist/package.json +2 -2
- package/dist/property.js +19 -9
- package/dist/shared.js +47 -74
- package/dist/traits.js +388 -0
- package/dist/traits.types.js +2 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -28,16 +28,11 @@ root
|
|
|
28
28
|
|
|
29
29
|
### Installation
|
|
30
30
|
|
|
31
|
-
---
|
|
32
|
-
|
|
33
|
-
**Install the package locally**
|
|
34
|
-
|
|
35
31
|
```
|
|
36
|
-
npm install
|
|
37
|
-
npm run build
|
|
32
|
+
npm install @stacks/rendezvous
|
|
38
33
|
```
|
|
39
34
|
|
|
40
|
-
|
|
35
|
+
### Usage
|
|
41
36
|
|
|
42
37
|
```
|
|
43
38
|
npx rv <path-to-clarinet-project> <contract-name> <type>
|
|
@@ -45,22 +40,6 @@ npx rv <path-to-clarinet-project> <contract-name> <type>
|
|
|
45
40
|
|
|
46
41
|
---
|
|
47
42
|
|
|
48
|
-
**Install the package globally**
|
|
49
|
-
|
|
50
|
-
```
|
|
51
|
-
git clone https://github.com/stacks-network/rendezvous
|
|
52
|
-
npm install
|
|
53
|
-
npm install --global .
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Run the fuzzer from anywhere on your system:
|
|
57
|
-
|
|
58
|
-
```
|
|
59
|
-
rv <path-to-clarinet-project> <contract-name> <type>
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
43
|
### Configuration
|
|
65
44
|
|
|
66
45
|
**Positional arguments:**
|
package/dist/citizen.js
CHANGED
|
@@ -87,7 +87,7 @@ const groupContractsByEpochFromSimnetPlan = (simnetPlan) => {
|
|
|
87
87
|
exports.groupContractsByEpochFromSimnetPlan = groupContractsByEpochFromSimnetPlan;
|
|
88
88
|
/**
|
|
89
89
|
* Deploy the contracts to the simnet in the correct order.
|
|
90
|
-
* @param simnet
|
|
90
|
+
* @param simnet The simnet instance.
|
|
91
91
|
* @param contractsByEpoch - The record of contracts by epoch.
|
|
92
92
|
* @param getContractSourceFn - The function to retrieve the contract source.
|
|
93
93
|
*/
|
package/dist/invariant.js
CHANGED
|
@@ -9,6 +9,7 @@ const transactions_1 = require("@stacks/transactions");
|
|
|
9
9
|
const heatstroke_1 = require("./heatstroke");
|
|
10
10
|
const fast_check_1 = __importDefault(require("fast-check"));
|
|
11
11
|
const ansicolor_1 = require("ansicolor");
|
|
12
|
+
const traits_1 = require("./traits");
|
|
12
13
|
const checkInvariants = (simnet, sutContractName, rendezvousList, rendezvousAllFunctions, seed, path, runs, radio) => {
|
|
13
14
|
// A map where the keys are the Rendezvous identifiers and the values are
|
|
14
15
|
// arrays of their SUT (System Under Test) functions. This map will be used
|
|
@@ -18,37 +19,54 @@ const checkInvariants = (simnet, sutContractName, rendezvousList, rendezvousAllF
|
|
|
18
19
|
// arrays of their invariant functions. This map will be used to access the
|
|
19
20
|
// invariant functions for each Rendezvous contract afterwards.
|
|
20
21
|
const rendezvousInvariantFunctions = filterInvariantFunctions(rendezvousAllFunctions);
|
|
22
|
+
// The Rendezvous identifier is the first one in the list. Only one contract
|
|
23
|
+
// can be fuzzed at a time.
|
|
24
|
+
const rendezvousContractId = rendezvousList[0];
|
|
25
|
+
const traitReferenceSutFunctions = rendezvousSutFunctions
|
|
26
|
+
.get(rendezvousContractId)
|
|
27
|
+
.filter((fn) => (0, traits_1.isTraitReferenceFunction)(fn));
|
|
28
|
+
const traitReferenceInvariantFunctions = rendezvousInvariantFunctions
|
|
29
|
+
.get(rendezvousContractId)
|
|
30
|
+
.filter((fn) => (0, traits_1.isTraitReferenceFunction)(fn));
|
|
31
|
+
const projectTraitImplementations = (0, traits_1.extractProjectTraitImplementations)(simnet);
|
|
32
|
+
if (Object.entries(projectTraitImplementations).length === 0 &&
|
|
33
|
+
(traitReferenceSutFunctions.length > 0 ||
|
|
34
|
+
traitReferenceInvariantFunctions.length > 0)) {
|
|
35
|
+
const foundTraitReferenceMessage = traitReferenceSutFunctions.length > 0 &&
|
|
36
|
+
traitReferenceInvariantFunctions.length > 0
|
|
37
|
+
? "public functions and invariants"
|
|
38
|
+
: traitReferenceSutFunctions.length > 0
|
|
39
|
+
? "public functions"
|
|
40
|
+
: "invariants";
|
|
41
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`\nFound ${foundTraitReferenceMessage} referencing traits, but no trait implementations were found in the project.
|
|
42
|
+
\nNote: You can add contracts implementing traits either as project contracts or as Clarinet requirements. For more details, visit: https://www.hiro.so/clarinet/.
|
|
43
|
+
\n`));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const enrichedSutFunctionsInterfaces = traitReferenceSutFunctions.length > 0
|
|
47
|
+
? (0, traits_1.enrichInterfaceWithTraitData)(simnet.getContractAST(sutContractName), (0, traits_1.buildTraitReferenceMap)(rendezvousSutFunctions.get(rendezvousContractId)), rendezvousSutFunctions.get(rendezvousContractId), rendezvousContractId)
|
|
48
|
+
: rendezvousSutFunctions;
|
|
49
|
+
const enrichedInvariantFunctionsInterfaces = traitReferenceInvariantFunctions.length > 0
|
|
50
|
+
? (0, traits_1.enrichInterfaceWithTraitData)(simnet.getContractAST(sutContractName), (0, traits_1.buildTraitReferenceMap)(rendezvousInvariantFunctions.get(rendezvousContractId)), rendezvousInvariantFunctions.get(rendezvousContractId), rendezvousContractId)
|
|
51
|
+
: rendezvousInvariantFunctions;
|
|
21
52
|
// Set up local context to track SUT function call counts.
|
|
22
|
-
const localContext = (0, exports.initializeLocalContext)(
|
|
53
|
+
const localContext = (0, exports.initializeLocalContext)(enrichedSutFunctionsInterfaces);
|
|
23
54
|
// Set up context in simnet by initializing state for SUT.
|
|
24
|
-
(0, exports.initializeClarityContext)(simnet,
|
|
55
|
+
(0, exports.initializeClarityContext)(simnet, enrichedSutFunctionsInterfaces);
|
|
25
56
|
radio.emit("logMessage", `\nStarting invariant testing type for the ${sutContractName} contract...\n`);
|
|
26
57
|
const simnetAccounts = simnet.getAccounts();
|
|
27
58
|
const eligibleAccounts = new Map([...simnetAccounts].filter(([key]) => key !== "faucet"));
|
|
28
59
|
const simnetAddresses = Array.from(simnetAccounts.values());
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const rendezvousContractId = rendezvousList[0];
|
|
32
|
-
const functions = (0, shared_1.getFunctionsListForContract)(rendezvousSutFunctions, rendezvousContractId);
|
|
33
|
-
const invariantFunctions = (0, shared_1.getFunctionsListForContract)(rendezvousInvariantFunctions, rendezvousContractId);
|
|
60
|
+
const functions = (0, shared_1.getFunctionsListForContract)(enrichedSutFunctionsInterfaces, rendezvousContractId);
|
|
61
|
+
const invariants = (0, shared_1.getFunctionsListForContract)(enrichedInvariantFunctionsInterfaces, rendezvousContractId);
|
|
34
62
|
if ((functions === null || functions === void 0 ? void 0 : functions.length) === 0) {
|
|
35
63
|
radio.emit("logMessage", (0, ansicolor_1.red)(`No public functions found for the "${sutContractName}" contract. Without public functions, no state transitions can happen inside the contract, and the invariant test is not meaningful.\n`));
|
|
36
64
|
return;
|
|
37
65
|
}
|
|
38
|
-
if ((
|
|
66
|
+
if ((invariants === null || invariants === void 0 ? void 0 : invariants.length) === 0) {
|
|
39
67
|
radio.emit("logMessage", (0, ansicolor_1.red)(`No invariant functions found for the "${sutContractName}" contract. Beware, for your contract may be exposed to unforeseen issues.\n`));
|
|
40
68
|
return;
|
|
41
69
|
}
|
|
42
|
-
const eligibleFunctions = functions.filter((fn) => !(0, shared_1.isTraitReferenceFunction)(fn));
|
|
43
|
-
const eligibleInvariants = invariantFunctions.filter((fn) => !(0, shared_1.isTraitReferenceFunction)(fn));
|
|
44
|
-
if (eligibleFunctions.length === 0) {
|
|
45
|
-
radio.emit("logMessage", (0, ansicolor_1.red)(`No eligible public functions found for the "${sutContractName}" contract. Note: trait references are not supported.\n`));
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (eligibleInvariants.length === 0) {
|
|
49
|
-
radio.emit("logMessage", (0, ansicolor_1.red)(`No eligible invariant functions found for the "${sutContractName}" contract. Note: trait references are not supported.\n`));
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
70
|
const radioReporter = (runDetails) => {
|
|
53
71
|
(0, heatstroke_1.reporter)(runDetails, radio, "invariant");
|
|
54
72
|
};
|
|
@@ -64,14 +82,14 @@ const checkInvariants = (simnet, sutContractName, rendezvousList, rendezvousAllF
|
|
|
64
82
|
})
|
|
65
83
|
.chain((r) => fast_check_1.default
|
|
66
84
|
.record({
|
|
67
|
-
selectedFunction: fast_check_1.default.constantFrom(...
|
|
68
|
-
selectedInvariant: fast_check_1.default.constantFrom(...
|
|
85
|
+
selectedFunction: fast_check_1.default.constantFrom(...functions),
|
|
86
|
+
selectedInvariant: fast_check_1.default.constantFrom(...invariants),
|
|
69
87
|
})
|
|
70
88
|
.map((selectedFunctions) => (Object.assign(Object.assign({}, r), selectedFunctions))))
|
|
71
89
|
.chain((r) => fast_check_1.default
|
|
72
90
|
.record({
|
|
73
|
-
functionArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedFunction, simnetAddresses)),
|
|
74
|
-
invariantArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedInvariant, simnetAddresses)),
|
|
91
|
+
functionArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedFunction, simnetAddresses, projectTraitImplementations)),
|
|
92
|
+
invariantArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedInvariant, simnetAddresses, projectTraitImplementations)),
|
|
75
93
|
})
|
|
76
94
|
.map((args) => (Object.assign(Object.assign({}, r), args))))
|
|
77
95
|
.chain((r) => fast_check_1.default
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacks/rendezvous",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Meet your contract's vulnerabilities head-on.",
|
|
5
5
|
"main": "app.js",
|
|
6
6
|
"bin": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "npx -p typescript tsc --project tsconfig.json && node -e \"if (process.platform !== 'win32') require('fs').chmodSync('./dist/app.js', 0o755);\"",
|
|
11
|
-
"test": "npx
|
|
11
|
+
"test": "npx jest",
|
|
12
12
|
"test:coverage": "npx tsc --project tsconfig.json && npx jest --coverage"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
package/dist/property.js
CHANGED
|
@@ -9,11 +9,27 @@ const transactions_1 = require("@stacks/transactions");
|
|
|
9
9
|
const heatstroke_1 = require("./heatstroke");
|
|
10
10
|
const shared_1 = require("./shared");
|
|
11
11
|
const ansicolor_1 = require("ansicolor");
|
|
12
|
+
const traits_1 = require("./traits");
|
|
12
13
|
const checkProperties = (simnet, sutContractName, rendezvousList, rendezvousAllFunctions, seed, path, runs, radio) => {
|
|
14
|
+
const testContractId = rendezvousList[0];
|
|
13
15
|
// A map where the keys are the test contract identifiers and the values are
|
|
14
16
|
// arrays of their test functions. This map will be used to access the test
|
|
15
17
|
// functions for each test contract in the property-based testing routine.
|
|
16
18
|
const testContractsTestFunctions = filterTestFunctions(rendezvousAllFunctions);
|
|
19
|
+
const traitReferenceFunctions = testContractsTestFunctions
|
|
20
|
+
.get(testContractId)
|
|
21
|
+
.filter((fn) => (0, traits_1.isTraitReferenceFunction)(fn));
|
|
22
|
+
const projectTraitImplementations = (0, traits_1.extractProjectTraitImplementations)(simnet);
|
|
23
|
+
if (Object.entries(projectTraitImplementations).length === 0 &&
|
|
24
|
+
traitReferenceFunctions.length > 0) {
|
|
25
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`\nFound test functions referencing traits, but no trait implementations were found in the project.
|
|
26
|
+
\nNote: You can add contracts implementing traits either as project contracts or as Clarinet requirements. For more details, visit: https://www.hiro.so/clarinet/.
|
|
27
|
+
\n`));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const enrichedTestFunctionsInterfaces = traitReferenceFunctions.length > 0
|
|
31
|
+
? (0, traits_1.enrichInterfaceWithTraitData)(simnet.getContractAST(sutContractName), (0, traits_1.buildTraitReferenceMap)(testContractsTestFunctions.get(testContractId)), testContractsTestFunctions.get(testContractId), testContractId)
|
|
32
|
+
: testContractsTestFunctions;
|
|
17
33
|
radio.emit("logMessage", `\nStarting property testing type for the ${sutContractName} contract...\n`);
|
|
18
34
|
// Search for discard functions, for each test function. This map will
|
|
19
35
|
// be used to pair the test functions with their corresponding discard
|
|
@@ -43,17 +59,11 @@ const checkProperties = (simnet, sutContractName, rendezvousList, rendezvousAllF
|
|
|
43
59
|
const simnetAccounts = simnet.getAccounts();
|
|
44
60
|
const eligibleAccounts = new Map([...simnetAccounts].filter(([key]) => key !== "faucet"));
|
|
45
61
|
const simnetAddresses = Array.from(simnetAccounts.values());
|
|
46
|
-
const
|
|
47
|
-
const testFunctions = (0, shared_1.getFunctionsListForContract)(testContractsTestFunctions, testContractId);
|
|
62
|
+
const testFunctions = (0, shared_1.getFunctionsListForContract)(enrichedTestFunctionsInterfaces, testContractId);
|
|
48
63
|
if ((testFunctions === null || testFunctions === void 0 ? void 0 : testFunctions.length) === 0) {
|
|
49
64
|
radio.emit("logMessage", (0, ansicolor_1.red)(`No test functions found for the "${sutContractName}" contract.\n`));
|
|
50
65
|
return;
|
|
51
66
|
}
|
|
52
|
-
const eligibleTestFunctions = testFunctions.filter((fn) => !(0, shared_1.isTraitReferenceFunction)(fn));
|
|
53
|
-
if (eligibleTestFunctions.length === 0) {
|
|
54
|
-
radio.emit("logMessage", (0, ansicolor_1.red)(`No eligible test functions found for the "${sutContractName}" contract. Note: trait references are not supported.\n`));
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
67
|
const radioReporter = (runDetails) => {
|
|
58
68
|
(0, heatstroke_1.reporter)(runDetails, radio, "test");
|
|
59
69
|
};
|
|
@@ -65,12 +75,12 @@ const checkProperties = (simnet, sutContractName, rendezvousList, rendezvousAllF
|
|
|
65
75
|
})
|
|
66
76
|
.chain((r) => fast_check_1.default
|
|
67
77
|
.record({
|
|
68
|
-
selectedTestFunction: fast_check_1.default.constantFrom(...
|
|
78
|
+
selectedTestFunction: fast_check_1.default.constantFrom(...testFunctions),
|
|
69
79
|
})
|
|
70
80
|
.map((selectedTestFunction) => (Object.assign(Object.assign({}, r), selectedTestFunction))))
|
|
71
81
|
.chain((r) => fast_check_1.default
|
|
72
82
|
.record({
|
|
73
|
-
functionArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedTestFunction, simnetAddresses)),
|
|
83
|
+
functionArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedTestFunction, simnetAddresses, projectTraitImplementations)),
|
|
74
84
|
})
|
|
75
85
|
.map((args) => (Object.assign(Object.assign({}, r), args))))
|
|
76
86
|
.chain((r) => fast_check_1.default
|
package/dist/shared.js
CHANGED
|
@@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getContractNameFromContractId = exports.
|
|
6
|
+
exports.getContractNameFromContractId = exports.argsToCV = exports.hexaString = exports.functionToArbitrary = exports.getFunctionsListForContract = exports.getFunctionsFromContractInterfaces = exports.getSimnetDeployerContractsInterfaces = void 0;
|
|
7
7
|
const fast_check_1 = __importDefault(require("fast-check"));
|
|
8
8
|
const transactions_1 = require("@stacks/transactions");
|
|
9
|
+
const traits_1 = require("./traits");
|
|
9
10
|
/**
|
|
10
11
|
* Get the interfaces of contracts deployed by the specified deployer from the
|
|
11
12
|
* simnet.
|
|
@@ -31,14 +32,14 @@ exports.getFunctionsListForContract = getFunctionsListForContract;
|
|
|
31
32
|
* @param fn The function interface.
|
|
32
33
|
* @returns Array of fast-check arbitraries.
|
|
33
34
|
*/
|
|
34
|
-
const functionToArbitrary = (fn, addresses) => fn.args.map((arg) => parameterTypeToArbitrary(arg.type, addresses));
|
|
35
|
+
const functionToArbitrary = (fn, addresses, projectTraitImplementations) => fn.args.map((arg) => parameterTypeToArbitrary(arg.type, addresses, projectTraitImplementations));
|
|
35
36
|
exports.functionToArbitrary = functionToArbitrary;
|
|
36
37
|
/**
|
|
37
38
|
* For a given type, generate a fast-check arbitrary.
|
|
38
39
|
* @param type The parameter type.
|
|
39
40
|
* @returns Fast-check arbitrary.
|
|
40
41
|
*/
|
|
41
|
-
const parameterTypeToArbitrary = (type, addresses) => {
|
|
42
|
+
const parameterTypeToArbitrary = (type, addresses, projectTraitImplementations) => {
|
|
42
43
|
if (typeof type === "string") {
|
|
43
44
|
// The type is a base type.
|
|
44
45
|
if (type === "principal") {
|
|
@@ -46,9 +47,6 @@ const parameterTypeToArbitrary = (type, addresses) => {
|
|
|
46
47
|
throw new Error("No addresses could be retrieved from the simnet instance!");
|
|
47
48
|
return baseTypesToArbitrary.principal(addresses);
|
|
48
49
|
}
|
|
49
|
-
else if (type === "trait_reference") {
|
|
50
|
-
throw new Error("Unsupported parameter type: trait_reference");
|
|
51
|
-
}
|
|
52
50
|
else
|
|
53
51
|
return baseTypesToArbitrary[type];
|
|
54
52
|
}
|
|
@@ -64,16 +62,19 @@ const parameterTypeToArbitrary = (type, addresses) => {
|
|
|
64
62
|
return complexTypesToArbitrary["string-utf8"](type["string-utf8"].length);
|
|
65
63
|
}
|
|
66
64
|
else if ("list" in type) {
|
|
67
|
-
return complexTypesToArbitrary["list"](type.list.type, type.list.length, addresses);
|
|
65
|
+
return complexTypesToArbitrary["list"](type.list.type, type.list.length, addresses, projectTraitImplementations);
|
|
68
66
|
}
|
|
69
67
|
else if ("tuple" in type) {
|
|
70
|
-
return complexTypesToArbitrary["tuple"](type.tuple, addresses);
|
|
68
|
+
return complexTypesToArbitrary["tuple"](type.tuple, addresses, projectTraitImplementations);
|
|
71
69
|
}
|
|
72
70
|
else if ("optional" in type) {
|
|
73
|
-
return complexTypesToArbitrary["optional"](type.optional, addresses);
|
|
71
|
+
return complexTypesToArbitrary["optional"](type.optional, addresses, projectTraitImplementations);
|
|
74
72
|
}
|
|
75
73
|
else if ("response" in type) {
|
|
76
|
-
return complexTypesToArbitrary.response(type.response.ok, type.response.error, addresses);
|
|
74
|
+
return complexTypesToArbitrary.response(type.response.ok, type.response.error, addresses, projectTraitImplementations);
|
|
75
|
+
}
|
|
76
|
+
else if ("trait_reference" in type) {
|
|
77
|
+
return complexTypesToArbitrary.trait_reference(type.trait_reference, projectTraitImplementations);
|
|
77
78
|
}
|
|
78
79
|
else {
|
|
79
80
|
throw new Error(`Unsupported complex type: ${JSON.stringify(type)}`);
|
|
@@ -88,28 +89,7 @@ const baseTypesToArbitrary = {
|
|
|
88
89
|
uint128: fast_check_1.default.nat(),
|
|
89
90
|
bool: fast_check_1.default.boolean(),
|
|
90
91
|
principal: (addresses) => fast_check_1.default.constantFrom(...addresses),
|
|
91
|
-
trait_reference: undefined,
|
|
92
92
|
};
|
|
93
|
-
/**
|
|
94
|
-
* Custom hexadecimal string generator. The `hexaString` generator from
|
|
95
|
-
* fast-check has been deprecated. This generator is implemented to precisely
|
|
96
|
-
* match the behavior of the deprecated generator.
|
|
97
|
-
*
|
|
98
|
-
* @param constraints Fast-check string constraints.
|
|
99
|
-
* @returns Fast-check arbitrary for hexadecimal strings.
|
|
100
|
-
*
|
|
101
|
-
* Reference for the proposed replacement of the deprecated `hexaString`
|
|
102
|
-
* generator:
|
|
103
|
-
* https://github.com/dubzzz/fast-check/commit/3f4f1203a8863c07d22b45591bf0de1fac02b948
|
|
104
|
-
*/
|
|
105
|
-
const hexaString = (constraints = {}) => {
|
|
106
|
-
const hexa = () => {
|
|
107
|
-
const hexCharSet = "0123456789abcdef";
|
|
108
|
-
return fast_check_1.default.integer({ min: 0, max: 15 }).map((n) => hexCharSet[n]);
|
|
109
|
-
};
|
|
110
|
-
return fast_check_1.default.string(Object.assign(Object.assign({}, constraints), { unit: hexa() }));
|
|
111
|
-
};
|
|
112
|
-
exports.hexaString = hexaString;
|
|
113
93
|
/**
|
|
114
94
|
* Complex types to fast-check arbitraries mapping.
|
|
115
95
|
*/
|
|
@@ -125,23 +105,48 @@ const complexTypesToArbitrary = {
|
|
|
125
105
|
maxLength: length,
|
|
126
106
|
}),
|
|
127
107
|
"string-utf8": (length) => fast_check_1.default.string({ maxLength: length }),
|
|
128
|
-
list: (type, length, addresses) => fast_check_1.default.array(parameterTypeToArbitrary(type, addresses), {
|
|
129
|
-
|
|
108
|
+
list: (type, length, addresses, projectTraitImplementations) => fast_check_1.default.array(parameterTypeToArbitrary(type, addresses, projectTraitImplementations), {
|
|
109
|
+
maxLength: length,
|
|
110
|
+
}),
|
|
111
|
+
tuple: (items, addresses, projectTraitImplementations) => {
|
|
130
112
|
const tupleArbitraries = {};
|
|
131
113
|
items.forEach((item) => {
|
|
132
|
-
tupleArbitraries[item.name] = parameterTypeToArbitrary(item.type, addresses);
|
|
114
|
+
tupleArbitraries[item.name] = parameterTypeToArbitrary(item.type, addresses, projectTraitImplementations);
|
|
133
115
|
});
|
|
134
116
|
return fast_check_1.default.record(tupleArbitraries);
|
|
135
117
|
},
|
|
136
|
-
optional: (type, addresses) => fast_check_1.default.option(parameterTypeToArbitrary(type, addresses)),
|
|
137
|
-
response: (okType, errType, addresses) => fast_check_1.default.oneof(fast_check_1.default.record({
|
|
118
|
+
optional: (type, addresses, projectTraitImplementations) => fast_check_1.default.option(parameterTypeToArbitrary(type, addresses, projectTraitImplementations)),
|
|
119
|
+
response: (okType, errType, addresses, projectTraitImplementations) => fast_check_1.default.oneof(fast_check_1.default.record({
|
|
138
120
|
status: fast_check_1.default.constant("ok"),
|
|
139
|
-
value: parameterTypeToArbitrary(okType, addresses),
|
|
121
|
+
value: parameterTypeToArbitrary(okType, addresses, projectTraitImplementations),
|
|
140
122
|
}), fast_check_1.default.record({
|
|
141
123
|
status: fast_check_1.default.constant("error"),
|
|
142
|
-
value: parameterTypeToArbitrary(errType, addresses),
|
|
124
|
+
value: parameterTypeToArbitrary(errType, addresses, projectTraitImplementations),
|
|
143
125
|
})),
|
|
126
|
+
trait_reference: (traitData, projectTraitImplementations) => {
|
|
127
|
+
return fast_check_1.default.constantFrom(...(0, traits_1.getContractIdsImplementingTrait)(traitData, projectTraitImplementations));
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Custom hexadecimal string generator. The `hexaString` generator from
|
|
132
|
+
* fast-check has been deprecated. This generator is implemented to precisely
|
|
133
|
+
* match the behavior of the deprecated generator.
|
|
134
|
+
*
|
|
135
|
+
* @param constraints Fast-check string constraints.
|
|
136
|
+
* @returns Fast-check arbitrary for hexadecimal strings.
|
|
137
|
+
*
|
|
138
|
+
* Reference for the proposed replacement of the deprecated `hexaString`
|
|
139
|
+
* generator:
|
|
140
|
+
* https://github.com/dubzzz/fast-check/commit/3f4f1203a8863c07d22b45591bf0de1fac02b948
|
|
141
|
+
*/
|
|
142
|
+
const hexaString = (constraints = {}) => {
|
|
143
|
+
const hexa = () => {
|
|
144
|
+
const hexCharSet = "0123456789abcdef";
|
|
145
|
+
return fast_check_1.default.integer({ min: 0, max: 15 }).map((n) => hexCharSet[n]);
|
|
146
|
+
};
|
|
147
|
+
return fast_check_1.default.string(Object.assign(Object.assign({}, constraints), { unit: hexa() }));
|
|
144
148
|
};
|
|
149
|
+
exports.hexaString = hexaString;
|
|
145
150
|
/** The character set used for generating ASCII strings.*/
|
|
146
151
|
const charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
|
147
152
|
/**
|
|
@@ -205,6 +210,9 @@ const argToCV = (arg, type) => {
|
|
|
205
210
|
const responseValue = argToCV(arg.value, branchType);
|
|
206
211
|
return complexTypesToCV.response(status, responseValue);
|
|
207
212
|
}
|
|
213
|
+
else if ("trait_reference" in type) {
|
|
214
|
+
return complexTypesToCV.trait_reference(arg);
|
|
215
|
+
}
|
|
208
216
|
else {
|
|
209
217
|
throw new Error(`Unsupported complex parameter type: ${JSON.stringify(type)}`);
|
|
210
218
|
}
|
|
@@ -241,45 +249,10 @@ const complexTypesToCV = {
|
|
|
241
249
|
else
|
|
242
250
|
throw new Error(`Unsupported response status: ${status}`);
|
|
243
251
|
},
|
|
252
|
+
trait_reference: (traitImplementation) => (0, transactions_1.principalCV)(traitImplementation),
|
|
244
253
|
};
|
|
245
254
|
const isBaseType = (type) => {
|
|
246
255
|
return ["int128", "uint128", "bool", "principal"].includes(type);
|
|
247
256
|
};
|
|
248
|
-
/**
|
|
249
|
-
* Checks if any parameter of the function contains a `trait_reference` type.
|
|
250
|
-
* @param fn The function interface.
|
|
251
|
-
* @returns Boolean - true if the function contains a trait reference, false
|
|
252
|
-
* otherwise.
|
|
253
|
-
*/
|
|
254
|
-
const isTraitReferenceFunction = (fn) => {
|
|
255
|
-
const hasTraitReference = (type) => {
|
|
256
|
-
if (typeof type === "string") {
|
|
257
|
-
// The type is a base type.
|
|
258
|
-
return type === "trait_reference";
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
// The type is a complex type.
|
|
262
|
-
if ("buffer" in type)
|
|
263
|
-
return false;
|
|
264
|
-
if ("string-ascii" in type)
|
|
265
|
-
return false;
|
|
266
|
-
if ("string-utf8" in type)
|
|
267
|
-
return false;
|
|
268
|
-
if ("list" in type)
|
|
269
|
-
return hasTraitReference(type.list.type);
|
|
270
|
-
if ("tuple" in type)
|
|
271
|
-
return type.tuple.some((item) => hasTraitReference(item.type));
|
|
272
|
-
if ("optional" in type)
|
|
273
|
-
return hasTraitReference(type.optional);
|
|
274
|
-
if ("response" in type)
|
|
275
|
-
return (hasTraitReference(type.response.ok) ||
|
|
276
|
-
hasTraitReference(type.response.error));
|
|
277
|
-
// Default to false for unexpected types.
|
|
278
|
-
return false;
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
return fn.args.some((arg) => hasTraitReference(arg.type));
|
|
282
|
-
};
|
|
283
|
-
exports.isTraitReferenceFunction = isTraitReferenceFunction;
|
|
284
257
|
const getContractNameFromContractId = (contractId) => contractId.split(".")[1];
|
|
285
258
|
exports.getContractNameFromContractId = getContractNameFromContractId;
|
package/dist/traits.js
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractProjectTraitImplementations = exports.isTraitReferenceFunction = exports.getContractIdsImplementingTrait = exports.buildTraitReferenceMap = exports.getTraitReferenceData = exports.enrichInterfaceWithTraitData = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Enriches contract interface with trait reference data. Before enrichment,
|
|
6
|
+
* the contract interface lacks trait reference data for parameters. This
|
|
7
|
+
* function constructs a copy of the contract interface with trait reference
|
|
8
|
+
* data for parameters that are trait references.
|
|
9
|
+
* @param ast The contract AST.
|
|
10
|
+
* @param traitReferenceMap The map of function names to trait reference paths.
|
|
11
|
+
* @param functionInterfaceList The list of function interfaces for a contract.
|
|
12
|
+
* @param targetContractId The contract ID to enrich with trait reference data.
|
|
13
|
+
* @returns A map of contract IDs to a list of enriched contract interface
|
|
14
|
+
* functions.
|
|
15
|
+
*/
|
|
16
|
+
const enrichInterfaceWithTraitData = (ast, traitReferenceMap, functionInterfaceList, targetContractId) => {
|
|
17
|
+
const enriched = new Map();
|
|
18
|
+
const enrichArgs = (args, functionName, traitReferenceMap, path = []) => {
|
|
19
|
+
return args.map((arg) => {
|
|
20
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
21
|
+
const listNested = !arg.name;
|
|
22
|
+
const currentPath = listNested ? path : [...path, arg.name];
|
|
23
|
+
if (arg.type && arg.type.tuple) {
|
|
24
|
+
return Object.assign(Object.assign({}, arg), { type: {
|
|
25
|
+
tuple: enrichArgs(arg.type.tuple, functionName, listNested
|
|
26
|
+
? traitReferenceMap.tuple
|
|
27
|
+
: (_a = traitReferenceMap[arg.name]) === null || _a === void 0 ? void 0 : _a.tuple, currentPath),
|
|
28
|
+
} });
|
|
29
|
+
}
|
|
30
|
+
else if (arg.type && arg.type.list) {
|
|
31
|
+
return Object.assign(Object.assign({}, arg), { type: {
|
|
32
|
+
list: enrichArgs([arg.type.list], functionName, listNested
|
|
33
|
+
? traitReferenceMap.list
|
|
34
|
+
: (_b = traitReferenceMap[arg.name]) === null || _b === void 0 ? void 0 : _b.list, arg.type.list.type.tuple
|
|
35
|
+
? [...currentPath, "tuple"]
|
|
36
|
+
: arg.type.list.type.response
|
|
37
|
+
? [...currentPath, "response"]
|
|
38
|
+
: arg.type.list.type.optional
|
|
39
|
+
? [...currentPath, "optional"]
|
|
40
|
+
: [...currentPath, "list"])[0],
|
|
41
|
+
} });
|
|
42
|
+
}
|
|
43
|
+
else if (arg.type && arg.type.response) {
|
|
44
|
+
const okPath = listNested ? currentPath : [...currentPath, "ok"];
|
|
45
|
+
const errorPath = listNested ? currentPath : [...currentPath, "error"];
|
|
46
|
+
const okTraitReference = enrichArgs([{ name: "ok", type: arg.type.response.ok }], functionName, {
|
|
47
|
+
ok: listNested
|
|
48
|
+
? (_c = traitReferenceMap.response) === null || _c === void 0 ? void 0 : _c.ok
|
|
49
|
+
: (_e = (_d = traitReferenceMap[arg.name]) === null || _d === void 0 ? void 0 : _d.response) === null || _e === void 0 ? void 0 : _e.ok,
|
|
50
|
+
}, okPath)[0];
|
|
51
|
+
const errorTraitReference = enrichArgs([{ name: "error", type: arg.type.response.error }], functionName, {
|
|
52
|
+
error: listNested
|
|
53
|
+
? (_f = traitReferenceMap.response) === null || _f === void 0 ? void 0 : _f.error
|
|
54
|
+
: (_h = (_g = traitReferenceMap[arg.name]) === null || _g === void 0 ? void 0 : _g.response) === null || _h === void 0 ? void 0 : _h.error,
|
|
55
|
+
}, errorPath)[0];
|
|
56
|
+
return Object.assign(Object.assign({}, arg), { type: {
|
|
57
|
+
response: {
|
|
58
|
+
ok: okTraitReference.type,
|
|
59
|
+
error: errorTraitReference.type,
|
|
60
|
+
},
|
|
61
|
+
} });
|
|
62
|
+
}
|
|
63
|
+
else if (arg.type && arg.type.optional) {
|
|
64
|
+
const optionalPath = [...currentPath, "optional"];
|
|
65
|
+
const optionalTraitReference = enrichArgs([{ name: "optional", type: arg.type.optional }], functionName, {
|
|
66
|
+
optional: listNested
|
|
67
|
+
? traitReferenceMap.optional
|
|
68
|
+
: (_j = traitReferenceMap[arg.name]) === null || _j === void 0 ? void 0 : _j.optional,
|
|
69
|
+
}, optionalPath)[0];
|
|
70
|
+
return Object.assign(Object.assign({}, arg), { type: {
|
|
71
|
+
optional: optionalTraitReference.type,
|
|
72
|
+
} });
|
|
73
|
+
}
|
|
74
|
+
else if (traitReferenceMap && traitReferenceMap[arg.name]) {
|
|
75
|
+
const [traitReferenceName, traitReferenceImport] = (0, exports.getTraitReferenceData)(ast, functionName, currentPath.filter((x) => x !== undefined));
|
|
76
|
+
if (traitReferenceName && traitReferenceImport) {
|
|
77
|
+
return Object.assign(Object.assign({}, arg), { type: {
|
|
78
|
+
trait_reference: {
|
|
79
|
+
name: traitReferenceName,
|
|
80
|
+
import: traitReferenceImport,
|
|
81
|
+
},
|
|
82
|
+
} });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else if (traitReferenceMap === "trait_reference") {
|
|
86
|
+
const [traitReferenceName, traitReferenceImport] = (0, exports.getTraitReferenceData)(ast, functionName, path);
|
|
87
|
+
if (traitReferenceName && traitReferenceImport) {
|
|
88
|
+
return Object.assign(Object.assign({}, arg), { type: {
|
|
89
|
+
trait_reference: {
|
|
90
|
+
name: traitReferenceName,
|
|
91
|
+
import: traitReferenceImport,
|
|
92
|
+
},
|
|
93
|
+
} });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return arg;
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
const enrichedFunctions = functionInterfaceList.map((f) => {
|
|
100
|
+
return Object.assign(Object.assign({}, f), { args: enrichArgs(f.args, f.name, traitReferenceMap.get(f.name)) });
|
|
101
|
+
});
|
|
102
|
+
enriched.set(targetContractId, enrichedFunctions);
|
|
103
|
+
return enriched;
|
|
104
|
+
};
|
|
105
|
+
exports.enrichInterfaceWithTraitData = enrichInterfaceWithTraitData;
|
|
106
|
+
/**
|
|
107
|
+
* Searches for a trait reference in the contract AST, given the function name
|
|
108
|
+
* and the nesting path of the trait reference.
|
|
109
|
+
* @param ast The contract AST.
|
|
110
|
+
* @param functionName The name of the function to search for trait references
|
|
111
|
+
* import data.
|
|
112
|
+
* @param parameterPath The path to search for the trait reference. The path is
|
|
113
|
+
* an array of strings that represent the nested location of the trait
|
|
114
|
+
* reference in the contract AST.
|
|
115
|
+
* @returns A tuple containing the `trait reference name` and the `imported
|
|
116
|
+
* trait data` if the trait reference is found. Otherwise, returns a tuple of
|
|
117
|
+
* `undefined` values.
|
|
118
|
+
*/
|
|
119
|
+
const getTraitReferenceData = (ast, functionName, parameterPath) => {
|
|
120
|
+
/**
|
|
121
|
+
* Recursively searches for a trait reference import details in the contract
|
|
122
|
+
* parameter nodes, part of the contract AST.
|
|
123
|
+
* @param functionParameterNodes The list of function parameter nodes from
|
|
124
|
+
* the AST. This is the list of nodes following the function name node.
|
|
125
|
+
* @param path The path to search for the trait reference. The path is an
|
|
126
|
+
* array of strings that represent the nested location of the trait reference
|
|
127
|
+
* in the function parameter nodes.
|
|
128
|
+
* @returns A tuple containing the `trait reference name` and the `imported
|
|
129
|
+
* trait data` if the trait reference is found. Otherwise, returns a tuple of
|
|
130
|
+
* `undefined` values.
|
|
131
|
+
*/
|
|
132
|
+
const findTraitReference = (functionParameterNodes, path) => {
|
|
133
|
+
for (const parameterNode of functionParameterNodes) {
|
|
134
|
+
// Check if the current parameter node is a trait reference in the first
|
|
135
|
+
// level of the function parameter nodes.
|
|
136
|
+
if (parameterNode.expr &&
|
|
137
|
+
parameterNode.expr.TraitReference) {
|
|
138
|
+
const [name, importData] = parameterNode.expr
|
|
139
|
+
.TraitReference;
|
|
140
|
+
return [name, importData];
|
|
141
|
+
}
|
|
142
|
+
if (!parameterNode.expr || !parameterNode.expr.List) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
// The parameter name node is the first node in the parameter node list.
|
|
146
|
+
const parameterNameNode = parameterNode.expr.List[0];
|
|
147
|
+
if (!parameterNameNode || !parameterNameNode.expr.Atom) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const currentParameterName = parameterNameNode.expr.Atom.toString();
|
|
151
|
+
// Check the first item in the path list to see if it matches the current
|
|
152
|
+
// parameter name. If it does, we are on the right track.
|
|
153
|
+
if (currentParameterName === path[0]) {
|
|
154
|
+
// If the path only has one item left, the trait reference should be
|
|
155
|
+
// right under our noses, in the next node.
|
|
156
|
+
if (path.length === 1) {
|
|
157
|
+
const traitReferenceNode = parameterNode.expr.List[1];
|
|
158
|
+
if (traitReferenceNode &&
|
|
159
|
+
traitReferenceNode.expr.TraitReference) {
|
|
160
|
+
const [name, importData] = traitReferenceNode.expr.TraitReference;
|
|
161
|
+
return [name, importData];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
// If the path has more than one item left, we need to traverse down
|
|
166
|
+
// the expression list to find the nested trait reference.
|
|
167
|
+
if (parameterNode.expr.List[1] &&
|
|
168
|
+
parameterNode.expr.List[1].expr) {
|
|
169
|
+
const nestedParameterList = parameterNode.expr.List[1]
|
|
170
|
+
.expr;
|
|
171
|
+
if (nestedParameterList.TraitReference) {
|
|
172
|
+
const [name, importData] = nestedParameterList
|
|
173
|
+
.TraitReference;
|
|
174
|
+
return [name, importData];
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// Recursively search for the trait reference in the nested
|
|
178
|
+
// parameter list.
|
|
179
|
+
const result = findTraitReference(nestedParameterList.List, path.slice(1));
|
|
180
|
+
if (result[0] !== undefined)
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return [undefined, undefined];
|
|
188
|
+
};
|
|
189
|
+
for (const node of ast.expressions) {
|
|
190
|
+
if (!node.expr || !node.expr.List) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
// Traverse down the expression.
|
|
194
|
+
const expressionList = node.expr.List;
|
|
195
|
+
// Extract the first atom in the expression list to determine if it is a
|
|
196
|
+
// function definition.
|
|
197
|
+
const potentialFunctionDefinitionAtom = expressionList[0];
|
|
198
|
+
// Check if the potential function definition atom is an actual function
|
|
199
|
+
// definition.
|
|
200
|
+
if (!potentialFunctionDefinitionAtom ||
|
|
201
|
+
!["define-public", "define-read-only"].includes(potentialFunctionDefinitionAtom.expr.Atom.toString())) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
// The current expression is a function definition. Extract the function
|
|
205
|
+
// name node, which is the second node in the expression list.
|
|
206
|
+
const functionNameNode = expressionList[1];
|
|
207
|
+
// Check if the function name node exists and if it is a list.
|
|
208
|
+
if (!functionNameNode || !functionNameNode.expr.List) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
// Extract the function definition list.
|
|
212
|
+
const functionDefinitionList = functionNameNode.expr.List;
|
|
213
|
+
const functionNameAtom = functionDefinitionList[0];
|
|
214
|
+
// Check if the function name atom exists.
|
|
215
|
+
if (!functionNameAtom || !functionNameAtom.expr.Atom) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const currentFunctionName = functionNameAtom.expr.Atom.toString();
|
|
219
|
+
// Check if the current function name matches the function name we are
|
|
220
|
+
// looking for.
|
|
221
|
+
if (currentFunctionName !== functionName) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
// Bingo! Found the function definition. The function parameters are the
|
|
225
|
+
// nodes following the function name node.
|
|
226
|
+
const functionParameterNodes = functionDefinitionList.slice(1);
|
|
227
|
+
const traitReferenceImportData = findTraitReference(functionParameterNodes, parameterPath);
|
|
228
|
+
if (traitReferenceImportData[0] !== undefined)
|
|
229
|
+
return traitReferenceImportData;
|
|
230
|
+
}
|
|
231
|
+
return [undefined, undefined];
|
|
232
|
+
};
|
|
233
|
+
exports.getTraitReferenceData = getTraitReferenceData;
|
|
234
|
+
/**
|
|
235
|
+
* Builds a map of function names to trait reference paths. The trait reference
|
|
236
|
+
* path is the path to the trait reference in the function parameter list.
|
|
237
|
+
* @param functionInterfaces The list of function interfaces for a contract.
|
|
238
|
+
* @returns A map of function names to trait reference paths.
|
|
239
|
+
*/
|
|
240
|
+
const buildTraitReferenceMap = (functionInterfaces) => {
|
|
241
|
+
const traitReferenceMap = new Map();
|
|
242
|
+
const findTraitReferences = (args) => {
|
|
243
|
+
const traitReferences = {};
|
|
244
|
+
args.forEach((arg) => {
|
|
245
|
+
if (arg.type && arg.type.tuple) {
|
|
246
|
+
const nestedTraitReferences = findTraitReferences(arg.type.tuple);
|
|
247
|
+
if (Object.keys(nestedTraitReferences).length > 0) {
|
|
248
|
+
traitReferences[arg.name] = { tuple: nestedTraitReferences };
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else if (arg.type && arg.type.list) {
|
|
252
|
+
const nestedTraitReferences = findTraitReferences([arg.type.list]);
|
|
253
|
+
if (Object.keys(nestedTraitReferences).length > 0) {
|
|
254
|
+
traitReferences[arg.name] = {
|
|
255
|
+
list: nestedTraitReferences["undefined"],
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else if (arg.type && arg.type.response) {
|
|
260
|
+
const okTraitReferences = findTraitReferences([arg.type.response.ok]);
|
|
261
|
+
const errorTraitReferences = findTraitReferences([
|
|
262
|
+
arg.type.response.error,
|
|
263
|
+
]);
|
|
264
|
+
const responseTraitReferences = {};
|
|
265
|
+
if (Object.keys(okTraitReferences).length > 0) {
|
|
266
|
+
responseTraitReferences.ok =
|
|
267
|
+
okTraitReferences[arg.name] || "trait_reference";
|
|
268
|
+
}
|
|
269
|
+
if (Object.keys(errorTraitReferences).length > 0) {
|
|
270
|
+
responseTraitReferences.error =
|
|
271
|
+
errorTraitReferences[arg.name] || "trait_reference";
|
|
272
|
+
}
|
|
273
|
+
if (Object.keys(responseTraitReferences).length > 0) {
|
|
274
|
+
traitReferences[arg.name] = { response: responseTraitReferences };
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else if (arg.type && arg.type.optional) {
|
|
278
|
+
const nestedTraitReferences = findTraitReferences([arg.type.optional]);
|
|
279
|
+
if (Object.keys(nestedTraitReferences).length > 0) {
|
|
280
|
+
traitReferences[arg.name] = {
|
|
281
|
+
optional: nestedTraitReferences[arg.name] || "trait_reference",
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
else if (arg.type === "trait_reference") {
|
|
286
|
+
traitReferences[arg.name] = "trait_reference";
|
|
287
|
+
}
|
|
288
|
+
else if (arg === "trait_reference") {
|
|
289
|
+
traitReferences[arg.name] = "trait_reference";
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
return traitReferences;
|
|
293
|
+
};
|
|
294
|
+
functionInterfaces.forEach((fn) => {
|
|
295
|
+
const traitReferences = findTraitReferences(fn.args);
|
|
296
|
+
if (Object.keys(traitReferences).length > 0) {
|
|
297
|
+
traitReferenceMap.set(fn.name, traitReferences);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
return traitReferenceMap;
|
|
301
|
+
};
|
|
302
|
+
exports.buildTraitReferenceMap = buildTraitReferenceMap;
|
|
303
|
+
/**
|
|
304
|
+
* Retrieves the contract IDs that implement a given trait.
|
|
305
|
+
* @param trait The trait to search for.
|
|
306
|
+
* @param projectTraitImplementations The record of the project contract IDs to
|
|
307
|
+
* their implemented traits.
|
|
308
|
+
* @returns An array of contract IDs that implement the trait.
|
|
309
|
+
*/
|
|
310
|
+
const getContractIdsImplementingTrait = (trait, projectTraitImplementations) => {
|
|
311
|
+
const contracts = Object.keys(projectTraitImplementations);
|
|
312
|
+
const filteredContracts = contracts.filter((contractId) => {
|
|
313
|
+
var _a;
|
|
314
|
+
const traitImplemented = (_a = projectTraitImplementations[contractId]) === null || _a === void 0 ? void 0 : _a.some((implementedTrait) => {
|
|
315
|
+
var _a, _b;
|
|
316
|
+
const isTraitNamesMatch = implementedTrait.name === ((_a = trait.import.Imported) === null || _a === void 0 ? void 0 : _a.name);
|
|
317
|
+
const isTraitIssuersMatch = JSON.stringify(implementedTrait.contract_identifier.issuer) ===
|
|
318
|
+
JSON.stringify((_b = trait.import.Imported) === null || _b === void 0 ? void 0 : _b.contract_identifier.issuer);
|
|
319
|
+
return isTraitNamesMatch && isTraitIssuersMatch;
|
|
320
|
+
});
|
|
321
|
+
return traitImplemented;
|
|
322
|
+
});
|
|
323
|
+
return filteredContracts;
|
|
324
|
+
};
|
|
325
|
+
exports.getContractIdsImplementingTrait = getContractIdsImplementingTrait;
|
|
326
|
+
/**
|
|
327
|
+
* Checks if any parameter of the function contains a `trait_reference` type.
|
|
328
|
+
* @param fn The function interface.
|
|
329
|
+
* @returns Boolean - true if the function contains a trait reference, false
|
|
330
|
+
* otherwise.
|
|
331
|
+
*/
|
|
332
|
+
const isTraitReferenceFunction = (fn) => {
|
|
333
|
+
const hasTraitReference = (type) => {
|
|
334
|
+
if (typeof type === "string") {
|
|
335
|
+
// The type is a base type.
|
|
336
|
+
return type === "trait_reference";
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
// The type is a complex type.
|
|
340
|
+
if ("buffer" in type)
|
|
341
|
+
return false;
|
|
342
|
+
if ("string-ascii" in type)
|
|
343
|
+
return false;
|
|
344
|
+
if ("string-utf8" in type)
|
|
345
|
+
return false;
|
|
346
|
+
if ("list" in type)
|
|
347
|
+
return hasTraitReference(type.list.type);
|
|
348
|
+
if ("tuple" in type)
|
|
349
|
+
return type.tuple.some((item) => hasTraitReference(item.type));
|
|
350
|
+
if ("optional" in type)
|
|
351
|
+
return hasTraitReference(type.optional);
|
|
352
|
+
if ("response" in type)
|
|
353
|
+
return (hasTraitReference(type.response.ok) ||
|
|
354
|
+
hasTraitReference(type.response.error));
|
|
355
|
+
// Default to false for unexpected types.
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
return fn.args.some((arg) => hasTraitReference(arg.type));
|
|
360
|
+
};
|
|
361
|
+
exports.isTraitReferenceFunction = isTraitReferenceFunction;
|
|
362
|
+
/**
|
|
363
|
+
* Iterates over all project contracts's ASTs excluding the boot ones and
|
|
364
|
+
* extracts a record of contract IDs to their implemented traits.
|
|
365
|
+
* @param simnet The Simnet instance.
|
|
366
|
+
* @returns A record of contract IDs to their implemented traits.
|
|
367
|
+
*/
|
|
368
|
+
const extractProjectTraitImplementations = (simnet) => {
|
|
369
|
+
const allProjectContracts = [
|
|
370
|
+
...simnet.getContractsInterfaces().keys(),
|
|
371
|
+
].filter((contractId) => {
|
|
372
|
+
const contractDeployer = contractId.split(".")[0];
|
|
373
|
+
return ![
|
|
374
|
+
"SP000000000000000000002Q6VF78",
|
|
375
|
+
"ST000000000000000000002AMW42H",
|
|
376
|
+
].includes(contractDeployer);
|
|
377
|
+
});
|
|
378
|
+
const projectTraitImplementations = allProjectContracts.reduce((acc, contractId) => {
|
|
379
|
+
const ast = simnet.getContractAST(contractId);
|
|
380
|
+
const implementedTraits = ast.implemented_traits;
|
|
381
|
+
if (implementedTraits.length > 0) {
|
|
382
|
+
acc[contractId] = implementedTraits;
|
|
383
|
+
}
|
|
384
|
+
return acc;
|
|
385
|
+
}, {});
|
|
386
|
+
return projectTraitImplementations;
|
|
387
|
+
};
|
|
388
|
+
exports.extractProjectTraitImplementations = extractProjectTraitImplementations;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacks/rendezvous",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Meet your contract's vulnerabilities head-on.",
|
|
5
5
|
"main": "app.js",
|
|
6
6
|
"bin": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "npx -p typescript tsc --project tsconfig.json && node -e \"if (process.platform !== 'win32') require('fs').chmodSync('./dist/app.js', 0o755);\"",
|
|
11
|
-
"test": "npx
|
|
11
|
+
"test": "npx jest",
|
|
12
12
|
"test:coverage": "npx tsc --project tsconfig.json && npx jest --coverage"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|