siphon-library 1.0.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/out/code_reference.d.ts +11 -0
- package/out/code_reference.js +56 -0
- package/out/code_reference.js.map +1 -0
- package/out/pattern.d.ts +17 -0
- package/out/pattern.js +41 -0
- package/out/pattern.js.map +1 -0
- package/package.json +20 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface CodeReference {
|
|
2
|
+
fileName: string;
|
|
3
|
+
contractName: string;
|
|
4
|
+
functionName: string;
|
|
5
|
+
lines?: {
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
declare function createCodeReference(code: string, fileName: string, contractName: string, functionName: string): CodeReference;
|
|
11
|
+
export { createCodeReference, CodeReference };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCodeReference = createCodeReference;
|
|
4
|
+
function createCodeReference(code, fileName, contractName, functionName) {
|
|
5
|
+
const linesArray = code.split("\n");
|
|
6
|
+
let contractLine = -1;
|
|
7
|
+
for (let i = 0; i < linesArray.length; i++) {
|
|
8
|
+
if (linesArray[i].includes(`contract ${contractName}`)) {
|
|
9
|
+
contractLine = i + 1;
|
|
10
|
+
break;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
if (contractLine === -1) {
|
|
14
|
+
console.log(`Contract "${contractName}" not found in the code.`);
|
|
15
|
+
return { fileName, contractName, functionName };
|
|
16
|
+
}
|
|
17
|
+
let functionStartLine = -1;
|
|
18
|
+
let functionEndLine = -1;
|
|
19
|
+
for (let i = contractLine; i < linesArray.length; i++) {
|
|
20
|
+
if (linesArray[i].includes(`function ${functionName}(`)) {
|
|
21
|
+
functionStartLine = i + 1;
|
|
22
|
+
let openBraces = 0;
|
|
23
|
+
let foundOpening = false;
|
|
24
|
+
for (let j = i; j < linesArray.length; j++) {
|
|
25
|
+
const line = linesArray[j];
|
|
26
|
+
for (const char of line) {
|
|
27
|
+
if (char === "{") {
|
|
28
|
+
openBraces++;
|
|
29
|
+
foundOpening = true;
|
|
30
|
+
}
|
|
31
|
+
else if (char === "}") {
|
|
32
|
+
openBraces--;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (foundOpening && openBraces === 0) {
|
|
36
|
+
functionEndLine = j + 1;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const codeReference = {
|
|
44
|
+
fileName,
|
|
45
|
+
contractName,
|
|
46
|
+
functionName,
|
|
47
|
+
};
|
|
48
|
+
if (functionEndLine !== -1) {
|
|
49
|
+
codeReference.lines = {
|
|
50
|
+
start: functionStartLine,
|
|
51
|
+
end: functionEndLine,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return codeReference;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=code_reference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code_reference.js","sourceRoot":"","sources":["../src/code_reference.ts"],"names":[],"mappings":";;AA2ES,kDAAmB;AAjE5B,SAAS,mBAAmB,CAC1B,IAAY,EACZ,QAAgB,EAChB,YAAoB,EACpB,YAAoB;IAEpB,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAEpC,IAAI,YAAY,GAAG,CAAC,CAAC,CAAC;IACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3C,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,YAAY,YAAY,EAAE,CAAC,EAAE,CAAC;YACvD,YAAY,GAAG,CAAC,GAAG,CAAC,CAAC;YACrB,MAAM;QACR,CAAC;IAC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ACb,CAAC;IAEF,IAAI,eAAe,KAAK,CAAC,CAAC,EAAE,CAAC;QAC3B,aAAa,CAAC,KAAK,GAAG;YACpB,KAAK,EAAE,iBAAiB;YACxB,GAAG,EAAE,eAAe;SACrB,CAAC;IACJ,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC"}
|
package/out/pattern.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare enum PatternType {
|
|
2
|
+
RedundantCode = "REDUNDANT_CODE",
|
|
3
|
+
OpaquePredicates = "OPAQUE_PREDICATES",
|
|
4
|
+
ExpensiveOperationsInsideLoop = "EXPENSIVE_OPERATIONS_INSIDE_LOOP",
|
|
5
|
+
LoopInvariantOperations = "LOOP_INVARIANT_OPERATIONS",
|
|
6
|
+
LoopInvariantConditions = "LOOP_INVARIANT_CONDITIONS"
|
|
7
|
+
}
|
|
8
|
+
export type Severity = "low" | "medium" | "high" | "critical";
|
|
9
|
+
export interface PatternInfo {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
severity: Severity;
|
|
13
|
+
}
|
|
14
|
+
export declare const PatternDefinitions: Record<PatternType, PatternInfo>;
|
|
15
|
+
export interface DetectedPattern extends PatternInfo {
|
|
16
|
+
type: PatternType;
|
|
17
|
+
}
|
package/out/pattern.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PatternDefinitions = exports.PatternType = void 0;
|
|
4
|
+
// Define an enum for pattern types.
|
|
5
|
+
var PatternType;
|
|
6
|
+
(function (PatternType) {
|
|
7
|
+
PatternType["RedundantCode"] = "REDUNDANT_CODE";
|
|
8
|
+
PatternType["OpaquePredicates"] = "OPAQUE_PREDICATES";
|
|
9
|
+
PatternType["ExpensiveOperationsInsideLoop"] = "EXPENSIVE_OPERATIONS_INSIDE_LOOP";
|
|
10
|
+
PatternType["LoopInvariantOperations"] = "LOOP_INVARIANT_OPERATIONS";
|
|
11
|
+
PatternType["LoopInvariantConditions"] = "LOOP_INVARIANT_CONDITIONS";
|
|
12
|
+
})(PatternType || (exports.PatternType = PatternType = {}));
|
|
13
|
+
// This constant maps each PatternType to its corresponding info.
|
|
14
|
+
exports.PatternDefinitions = {
|
|
15
|
+
[PatternType.RedundantCode]: {
|
|
16
|
+
name: "Redundant Code",
|
|
17
|
+
description: "Identifies unreachable code branches that can be eliminated to reduce bytecode size.",
|
|
18
|
+
severity: "low",
|
|
19
|
+
},
|
|
20
|
+
[PatternType.OpaquePredicates]: {
|
|
21
|
+
name: "Opaque Predicates",
|
|
22
|
+
description: "Detects conditions that always evaluate to the same value regardless of input.",
|
|
23
|
+
severity: "low",
|
|
24
|
+
},
|
|
25
|
+
[PatternType.ExpensiveOperationsInsideLoop]: {
|
|
26
|
+
name: "Expensive Operations Inside Loop",
|
|
27
|
+
description: "Flags high-cost operations (e.g., storage accesses) performed inside loops.",
|
|
28
|
+
severity: "high",
|
|
29
|
+
},
|
|
30
|
+
[PatternType.LoopInvariantOperations]: {
|
|
31
|
+
name: "Loop Invariant Operations",
|
|
32
|
+
description: "Identifies operations inside loops that yield the same result on every iteration.",
|
|
33
|
+
severity: "medium",
|
|
34
|
+
},
|
|
35
|
+
[PatternType.LoopInvariantConditions]: {
|
|
36
|
+
name: "Loop Invariant Conditions",
|
|
37
|
+
description: "Recognizes condition checks in loops that remain constant across iterations.",
|
|
38
|
+
severity: "medium",
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=pattern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pattern.js","sourceRoot":"","sources":["../src/pattern.ts"],"names":[],"mappings":";;;AAAA,oCAAoC;AACpC,IAAY,WAMX;AAND,WAAY,WAAW;IACrB,+CAAgC,CAAA;IAChC,qDAAsC,CAAA;IACtC,iFAAkE,CAAA;IAClE,oEAAqD,CAAA;IACrD,oEAAqD,CAAA;AACvD,CAAC,EANW,WAAW,2BAAX,WAAW,QAMtB;AAYD,iEAAiE;AACpD,QAAA,kBAAkB,GAAqC;IAClE,CAAC,WAAW,CAAC,aAAa,CAAC,EAAE;QAC3B,IAAI,EAAE,gBAAgB;QACtB,WAAW,EACT,sFAAsF;QACxF,QAAQ,EAAE,KAAK;KAChB;IACD,CAAC,WAAW,CAAC,gBAAgB,CAAC,EAAE;QAC9B,IAAI,EAAE,mBAAmB;QACzB,WAAW,EACT,gFAAgF;QAClF,QAAQ,EAAE,KAAK;KAChB;IACD,CAAC,WAAW,CAAC,6BAA6B,CAAC,EAAE;QAC3C,IAAI,EAAE,kCAAkC;QACxC,WAAW,EACT,6EAA6E;QAC/E,QAAQ,EAAE,MAAM;KACjB;IACD,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE;QACrC,IAAI,EAAE,2BAA2B;QACjC,WAAW,EACT,mFAAmF;QACrF,QAAQ,EAAE,QAAQ;KACnB;IACD,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE;QACrC,IAAI,EAAE,2BAA2B;QACjC,WAAW,EACT,8EAA8E;QAChF,QAAQ,EAAE,QAAQ;KACnB;CACF,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "siphon-library",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A library for ...",
|
|
5
|
+
"main": "out/index.js",
|
|
6
|
+
"types": "out/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"out"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "jest"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/jest": "^29.0.0",
|
|
16
|
+
"jest": "^29.0.0",
|
|
17
|
+
"ts-jest": "^29.0.0",
|
|
18
|
+
"typescript": "5.8.2"
|
|
19
|
+
}
|
|
20
|
+
}
|