|
| 1 | +import * as armlet from "armlet"; |
| 2 | +import * as path from "path"; |
| 3 | +import User from "../../Models/User"; |
| 4 | +import {IGitMythXConfig} from "../GitMythXConfig"; |
| 5 | +import {GitRepo} from "../GitRepo"; |
| 6 | +import logger from "../Logger"; |
| 7 | +import {SolidityUtils} from "../SolidityUtils"; |
| 8 | +import {AnalysisReport} from "./AnalysisReport"; |
| 9 | + |
| 10 | +export class Analysis { |
| 11 | + |
| 12 | + private readonly config: IGitMythXConfig; |
| 13 | + |
| 14 | + constructor(private readonly repo: GitRepo) { |
| 15 | + this.config = repo.getMythXConfigFile(); |
| 16 | + } |
| 17 | + |
| 18 | + public async run(): Promise<AnalysisReport[] | AnalysisReport> { |
| 19 | + if (!this.config) { |
| 20 | + logger.error("Missing gitmythx.json config in repo"); |
| 21 | + return new AnalysisReport( |
| 22 | + false, |
| 23 | + "Missing gitmythx.json config in repo", |
| 24 | + ); |
| 25 | + } |
| 26 | + return await Promise.all( |
| 27 | + this.config.contracts.map((contractConfig) => { |
| 28 | + return this.runAnalyses(contractConfig.name, this.repo.getFile(contractConfig.path)); |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + private async runAnalyses(contractFilePath: string, contractSource: string): Promise<AnalysisReport> { |
| 35 | + const contractFileName = path.basename(contractFilePath); |
| 36 | + const solcInput = { |
| 37 | + language: "Solidity", |
| 38 | + sources: { |
| 39 | + [contractFileName]: { |
| 40 | + content: contractSource, |
| 41 | + }, |
| 42 | + }, |
| 43 | + settings: { |
| 44 | + outputSelection: { |
| 45 | + "*": { |
| 46 | + "*": ["*"], |
| 47 | + "": ["ast"], |
| 48 | + }, |
| 49 | + }, |
| 50 | + optimizer: { |
| 51 | + enabled: true, |
| 52 | + runs: 200, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }; |
| 56 | + logger.info("Processing contract import paths"); |
| 57 | + const importPaths = SolidityUtils.getImportPaths(contractSource); |
| 58 | + let sourceList = {}; |
| 59 | + importPaths.forEach((importPath) => { |
| 60 | + const newSourceList = SolidityUtils.parseImports(path.dirname(contractFilePath), importPath, false); |
| 61 | + sourceList = { |
| 62 | + ...sourceList, |
| 63 | + ...newSourceList, |
| 64 | + }; |
| 65 | + }); |
| 66 | + solcInput.sources = { |
| 67 | + ...solcInput.sources, |
| 68 | + ...sourceList, |
| 69 | + }; |
| 70 | + const mythxSourceList = Object.keys(sourceList); |
| 71 | + mythxSourceList.push(contractFileName); |
| 72 | + const compiled = await SolidityUtils.compile(solcInput, this.config.solidityVersion); |
| 73 | + if (!compiled.contracts || !Object.keys(compiled.contracts).length) { |
| 74 | + if (compiled.errors) { |
| 75 | + for (const compiledError of compiled.errors) { |
| 76 | + logger.error(compiledError.formattedMessage); |
| 77 | + } |
| 78 | + } |
| 79 | + return new AnalysisReport( |
| 80 | + false, |
| 81 | + "Failed to compšile contracts", |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + const inputfile = compiled.contracts[contractFileName]; |
| 86 | + let contract; |
| 87 | + let contractName; |
| 88 | + |
| 89 | + if (inputfile.length === 0) { |
| 90 | + logger.error("✖ No contracts found"); |
| 91 | + return new AnalysisReport( |
| 92 | + false, |
| 93 | + "✖ No contracts found", |
| 94 | + ); |
| 95 | + } else if (inputfile.length === 1) { |
| 96 | + contractName = Object.keys(inputfile)[0]; |
| 97 | + contract = inputfile[contractName]; |
| 98 | + } else { |
| 99 | + |
| 100 | + /* |
| 101 | + * Get the contract with largest bytecode object to generate MythX analysis report. |
| 102 | + * If inheritance is used, the main contract is the largest as it contains the bytecode of all others. |
| 103 | + */ |
| 104 | + |
| 105 | + const bytecodes = {}; |
| 106 | + |
| 107 | + for (const key in inputfile) { |
| 108 | + if (inputfile.hasOwnProperty(key)) { |
| 109 | + bytecodes[inputfile[key].evm.bytecode.object.length] = key; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + const largestBytecodeKey = Object.keys(bytecodes).reverse()[0]; |
| 114 | + contractName = bytecodes[largestBytecodeKey]; |
| 115 | + contract = inputfile[contractName]; |
| 116 | + } |
| 117 | + |
| 118 | + /* Bytecode would be empty if contract is only an interface */ |
| 119 | + |
| 120 | + if (!contract.evm.bytecode.object) { |
| 121 | + logger.error( |
| 122 | + "✖ Compiling the Solidity code did not return any bytecode." + |
| 123 | + " Note that abstract contracts cannot be analyzed.", |
| 124 | + ); |
| 125 | + return new AnalysisReport( |
| 126 | + false, |
| 127 | + "✖ Compiling the Solidity code did not return any bytecode." + |
| 128 | + " Note that abstract contracts cannot be analyzed."); |
| 129 | + } |
| 130 | + |
| 131 | + /* Format data for MythX API */ |
| 132 | + logger.info("Formatting mythx request..."); |
| 133 | + const data = { |
| 134 | + contractName, |
| 135 | + bytecode: SolidityUtils.replaceLinkedLibs(contract.evm.bytecode.object), |
| 136 | + sourceMap: contract.evm.deployedBytecode.sourceMap, |
| 137 | + deployedBytecode: SolidityUtils.replaceLinkedLibs(contract.evm.deployedBytecode.object), |
| 138 | + deployedSourceMap: contract.evm.deployedBytecode.sourceMap, |
| 139 | + mythxSourceList, |
| 140 | + analysisMode: "quick", |
| 141 | + sources: {}, |
| 142 | + mainSource: contractFileName, |
| 143 | + } as any; |
| 144 | + |
| 145 | + // tslint:disable-next-line:forin |
| 146 | + for (const key in solcInput.sources) { |
| 147 | + data.sources[key] = { source: solcInput.sources[key].content }; |
| 148 | + } |
| 149 | + logger.info(`Fetching mythx credentials for user ${this.repo.getOwner()}`); |
| 150 | + const mythxUser = await User.findOne({ where: { id: this.repo.getOwner() }}); |
| 151 | + if (!mythxUser) { |
| 152 | + logger.error(`Missing mythx credentials for user ${this.repo.getOwner()}`); |
| 153 | + return new AnalysisReport(false, `Missing mythx credentials for user ${this.repo.getOwner()}`); |
| 154 | + } |
| 155 | + const client = new armlet.Client({ |
| 156 | + ethAddress: "0x", |
| 157 | + password: "sample", |
| 158 | + }); |
| 159 | + client.accessToken = mythxUser.accessToken; |
| 160 | + client.refreshToken = mythxUser.refreshToken; |
| 161 | + logger.info("Submitting contracts to mythx analysis"); |
| 162 | + try { |
| 163 | + const result = await client.analyzeWithStatus({ |
| 164 | + data, |
| 165 | + timeout: 300000, |
| 166 | + clientToolName: "GitMythX", |
| 167 | + }); |
| 168 | + |
| 169 | + /* Add `solidity_file_path` to display the result in the ESLint format with the provided input path */ |
| 170 | + data.filePath = contractFilePath; |
| 171 | + |
| 172 | + /* Add all the imported contracts source code to the `data` to sourcemap the issue location */ |
| 173 | + data.sources = { ...solcInput.sources }; |
| 174 | + logger.info("Creating analysis report..."); |
| 175 | + const report = new AnalysisReport(true, "", data, result); |
| 176 | + logger.info("Created report!"); |
| 177 | + logger.info(JSON.stringify(report)); |
| 178 | + return report; |
| 179 | + } catch (e) { |
| 180 | + logger.error(e.message); |
| 181 | + } |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments