@stacks/rendezvous 0.1.3 → 0.3.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/dist/traits.js ADDED
@@ -0,0 +1,389 @@
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 a 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 function names mapped to their trait reference
11
+ * parameter paths.
12
+ * @param functionInterfaceList The list of function interfaces for a contract.
13
+ * @param targetContractId The contract ID to enrich with trait reference data.
14
+ * @returns The contract IDs mapped to a list of enriched function interfaces.
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 nesting path of the trait reference in the function parameter
237
+ * list.
238
+ * @param functionInterfaces The list of function interfaces for a contract.
239
+ * @returns The function names mapped to their trait reference parameter paths.
240
+ */
241
+ const buildTraitReferenceMap = (functionInterfaces) => {
242
+ const traitReferenceMap = new Map();
243
+ const findTraitReferences = (args) => {
244
+ const traitReferences = {};
245
+ args.forEach((arg) => {
246
+ if (arg.type && arg.type.tuple) {
247
+ const nestedTraitReferences = findTraitReferences(arg.type.tuple);
248
+ if (Object.keys(nestedTraitReferences).length > 0) {
249
+ traitReferences[arg.name] = { tuple: nestedTraitReferences };
250
+ }
251
+ }
252
+ else if (arg.type && arg.type.list) {
253
+ const nestedTraitReferences = findTraitReferences([arg.type.list]);
254
+ if (Object.keys(nestedTraitReferences).length > 0) {
255
+ traitReferences[arg.name] = {
256
+ list: nestedTraitReferences["undefined"],
257
+ };
258
+ }
259
+ }
260
+ else if (arg.type && arg.type.response) {
261
+ const okTraitReferences = findTraitReferences([arg.type.response.ok]);
262
+ const errorTraitReferences = findTraitReferences([
263
+ arg.type.response.error,
264
+ ]);
265
+ const responseTraitReferences = {};
266
+ if (Object.keys(okTraitReferences).length > 0) {
267
+ responseTraitReferences.ok =
268
+ okTraitReferences[arg.name] || "trait_reference";
269
+ }
270
+ if (Object.keys(errorTraitReferences).length > 0) {
271
+ responseTraitReferences.error =
272
+ errorTraitReferences[arg.name] || "trait_reference";
273
+ }
274
+ if (Object.keys(responseTraitReferences).length > 0) {
275
+ traitReferences[arg.name] = { response: responseTraitReferences };
276
+ }
277
+ }
278
+ else if (arg.type && arg.type.optional) {
279
+ const nestedTraitReferences = findTraitReferences([arg.type.optional]);
280
+ if (Object.keys(nestedTraitReferences).length > 0) {
281
+ traitReferences[arg.name] = {
282
+ optional: nestedTraitReferences[arg.name] || "trait_reference",
283
+ };
284
+ }
285
+ }
286
+ else if (arg.type === "trait_reference") {
287
+ traitReferences[arg.name] = "trait_reference";
288
+ }
289
+ else if (arg === "trait_reference") {
290
+ traitReferences[arg.name] = "trait_reference";
291
+ }
292
+ });
293
+ return traitReferences;
294
+ };
295
+ functionInterfaces.forEach((fn) => {
296
+ const traitReferences = findTraitReferences(fn.args);
297
+ if (Object.keys(traitReferences).length > 0) {
298
+ traitReferenceMap.set(fn.name, traitReferences);
299
+ }
300
+ });
301
+ return traitReferenceMap;
302
+ };
303
+ exports.buildTraitReferenceMap = buildTraitReferenceMap;
304
+ /**
305
+ * Retrieves the contract IDs that implement a given trait.
306
+ * @param trait The trait to search for.
307
+ * @param projectTraitImplementations The record of the project contract IDs to
308
+ * their implemented traits.
309
+ * @returns An array of contract IDs that implement the trait.
310
+ */
311
+ const getContractIdsImplementingTrait = (trait, projectTraitImplementations) => {
312
+ const contracts = Object.keys(projectTraitImplementations);
313
+ const filteredContracts = contracts.filter((contractId) => {
314
+ var _a;
315
+ const traitImplemented = (_a = projectTraitImplementations[contractId]) === null || _a === void 0 ? void 0 : _a.some((implementedTrait) => {
316
+ var _a, _b;
317
+ const isTraitNamesMatch = implementedTrait.name === ((_a = trait.import.Imported) === null || _a === void 0 ? void 0 : _a.name);
318
+ const isTraitIssuersMatch = JSON.stringify(implementedTrait.contract_identifier.issuer) ===
319
+ JSON.stringify((_b = trait.import.Imported) === null || _b === void 0 ? void 0 : _b.contract_identifier.issuer);
320
+ return isTraitNamesMatch && isTraitIssuersMatch;
321
+ });
322
+ return traitImplemented;
323
+ });
324
+ return filteredContracts;
325
+ };
326
+ exports.getContractIdsImplementingTrait = getContractIdsImplementingTrait;
327
+ /**
328
+ * Checks if any parameter of the function contains a `trait_reference` type.
329
+ * @param fn The function interface.
330
+ * @returns Boolean - true if the function contains a trait reference, false
331
+ * otherwise.
332
+ */
333
+ const isTraitReferenceFunction = (fn) => {
334
+ const hasTraitReference = (type) => {
335
+ if (typeof type === "string") {
336
+ // The type is a base type.
337
+ return type === "trait_reference";
338
+ }
339
+ else {
340
+ // The type is a complex type.
341
+ if ("buffer" in type)
342
+ return false;
343
+ if ("string-ascii" in type)
344
+ return false;
345
+ if ("string-utf8" in type)
346
+ return false;
347
+ if ("list" in type)
348
+ return hasTraitReference(type.list.type);
349
+ if ("tuple" in type)
350
+ return type.tuple.some((item) => hasTraitReference(item.type));
351
+ if ("optional" in type)
352
+ return hasTraitReference(type.optional);
353
+ if ("response" in type)
354
+ return (hasTraitReference(type.response.ok) ||
355
+ hasTraitReference(type.response.error));
356
+ // Default to false for unexpected types.
357
+ return false;
358
+ }
359
+ };
360
+ return fn.args.some((arg) => hasTraitReference(arg.type));
361
+ };
362
+ exports.isTraitReferenceFunction = isTraitReferenceFunction;
363
+ /**
364
+ * Iterates over all project contracts's ASTs excluding the boot ones and
365
+ * extracts a record of contract IDs to their implemented traits.
366
+ * @param simnet The Simnet instance.
367
+ * @returns The contract IDs mapped to their implemented traits.
368
+ */
369
+ const extractProjectTraitImplementations = (simnet) => {
370
+ const allProjectContracts = [
371
+ ...simnet.getContractsInterfaces().keys(),
372
+ ].filter((contractId) => {
373
+ const contractDeployer = contractId.split(".")[0];
374
+ return ![
375
+ "SP000000000000000000002Q6VF78",
376
+ "ST000000000000000000002AMW42H",
377
+ ].includes(contractDeployer);
378
+ });
379
+ const projectTraitImplementations = allProjectContracts.reduce((acc, contractId) => {
380
+ const ast = simnet.getContractAST(contractId);
381
+ const implementedTraits = ast.implemented_traits;
382
+ if (implementedTraits.length > 0) {
383
+ acc[contractId] = implementedTraits;
384
+ }
385
+ return acc;
386
+ }, {});
387
+ return projectTraitImplementations;
388
+ };
389
+ exports.extractProjectTraitImplementations = extractProjectTraitImplementations;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacks/rendezvous",
3
- "version": "0.1.3",
3
+ "version": "0.3.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 tsc --project tsconfig.json && npx jest",
11
+ "test": "npx jest",
12
12
  "test:coverage": "npx tsc --project tsconfig.json && npx jest --coverage"
13
13
  },
14
14
  "keywords": [
@@ -25,6 +25,10 @@
25
25
  "README.md",
26
26
  "LICENSE"
27
27
  ],
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/stacks-network/rendezvous.git"
31
+ },
28
32
  "dependencies": {
29
33
  "@hirosystems/clarinet-sdk": "2.12.0",
30
34
  "@stacks/transactions": "^6.16.1",
@@ -36,7 +40,7 @@
36
40
  "@hirosystems/clarinet-sdk-wasm": "2.12.0",
37
41
  "@types/jest": "^29.5.12",
38
42
  "jest": "^29.7.0",
39
- "ts-jest": "^29.2.3",
43
+ "ts-jest": "^29.2.5",
40
44
  "typescript": "^5.5.4"
41
45
  }
42
46
  }