Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Send complete ast and sources
Browse files Browse the repository at this point in the history
  • Loading branch information
daniyarchambylov committed Apr 22, 2019
1 parent 5898ab8 commit 2d42c39
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
26 changes: 11 additions & 15 deletions lib/mythx.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ const srcmap = require('./srcmap');
// FIXME: Temporary solution, creates an array of objects
// Each can be passed to MythXIssues constructor
const newTruffleObjToOldTruffleByContracts = buildObj => {
const {sources, compiler } = buildObj;

const { sources, compiler } = buildObj;
let allContracts = [];

const allSources = Object.entries(sources).reduce((accum, [sourcePath, data]) => {
const { source, ast, legacyAST } = data;
const key = path.basename(sourcePath)
accum[key] = { ast, legacyAST, source };
return accum;
}, {});

for (const [sourcePath, data] of Object.entries(sources)) {
const contracts = data.contracts.map(contract => ({
contractName: contract.contractName,
bytecode: contract.bytecode,
deployedBytecode: contract.deployedBytecode,
sourceMap: contract.sourceMap,
deployedSourceMap: contract.deployedSourceMap,
ast: data.ast,
legacyAST: data.legacyAST,
source: data.source,
sources: allSources,
compiler,
sourcePath,
}));
Expand All @@ -41,9 +45,7 @@ const truffle2MythXJSON = function(truffleJSON, toolId = 'truffle-security') {
sourceMap,
deployedSourceMap,
sourcePath,
source,
legacyAST,
ast,
sources,
compiler: { version },
} = truffleJSON;

Expand All @@ -63,13 +65,7 @@ const truffle2MythXJSON = function(truffleJSON, toolId = 'truffle-security') {
deployedSourceMap,
mainSource: sourcesKey,
sourceList: [ sourcePath ],
sources: {
[sourcesKey]: {
source,
ast,
legacyAST,
},
},
sources,
toolId,
version,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"updatedAt": "2019-03-06T05:53:44.707Z",
"sources": {
"/home/daniyar/tmp/truffle-next-20/simple_dao/contracts/simple_dao.sol": {
"/test/simple_dao/contracts/simple_dao.sol": {
"contracts": [
{
"contractName": "SimpleDAO",
Expand All @@ -16,7 +16,7 @@
}
],
"ast": {
"absolutePath": "/home/daniyar/tmp/truffle-next-20/simple_dao/contracts/simple_dao.sol",
"absolutePath": "/test/simple_dao/contracts/simple_dao.sol",
"exportedSymbols": {
"SimpleDAO": [
62
Expand Down Expand Up @@ -798,7 +798,7 @@
},
"legacyAST": {
"attributes": {
"absolutePath": "/home/daniyar/tmp/truffle-next-20/simple_dao/contracts/simple_dao.sol",
"absolutePath": "/test/simple_dao/contracts/simple_dao.sol",
"exportedSymbols": {
"SimpleDAO": [
62
Expand Down
44 changes: 27 additions & 17 deletions test/test_mythx.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ const srcmap = require('../lib/srcmap');

describe('mythx.js', () => {
it('should turn truffle contract json to mythx compatible object', done => {
fs.readFile( `${__dirname}/sample-truffle/simple_dao/build/contracts/SimpleDAO.json`, 'utf8', (err, data) => {
const solFilePath = `/test/simple_dao/contracts/simple_dao.sol`
fs.readFile( `${__dirname}/sample-truffle/simple_dao/build/mythx/contracts/simple_dao.json`, 'utf8', (err, data) => {
if (err) return done(err);
const truffleJSON = JSON.parse(data);
const mythXJSON = mythx.truffle2MythXJSON(truffleJSON, 'test-truffle-analyze');
const contracts = mythx.newTruffleObjToOldTruffleByContracts(truffleJSON);
const mythXJSON = mythx.truffle2MythXJSON(contracts[0], 'test-truffle-analyze');

assert.deepEqual(mythXJSON, {
contractName: truffleJSON.contractName,
bytecode: truffleJSON.bytecode,
deployedBytecode: truffleJSON.deployedBytecode,
sourceMap: srcmap.zeroedSourceMap(truffleJSON.sourceMap),
deployedSourceMap: srcmap.zeroedSourceMap(truffleJSON.deployedSourceMap),
contractName: truffleJSON.sources[solFilePath].contracts[0].contractName,
bytecode: truffleJSON.sources[solFilePath].contracts[0].bytecode,
deployedBytecode: truffleJSON.sources[solFilePath].contracts[0].deployedBytecode,
sourceMap: srcmap.zeroedSourceMap(truffleJSON.sources[solFilePath].contracts[0].sourceMap),
deployedSourceMap: srcmap.zeroedSourceMap(truffleJSON.sources[solFilePath].contracts[0].deployedSourceMap),
mainSource: 'simple_dao.sol',
sourceList: [ truffleJSON.sourcePath ],
sourceList: [ solFilePath ],
sources: {
'simple_dao.sol': {
source: truffleJSON.source,
ast: truffleJSON.ast,
legacyAST: truffleJSON.legacyAST,
source: truffleJSON.sources[solFilePath].source,
ast: truffleJSON.sources[solFilePath].ast,
legacyAST: truffleJSON.sources[solFilePath].legacyAST,
},
},
toolId: 'test-truffle-analyze',
Expand Down Expand Up @@ -139,9 +141,13 @@ describe('mythx.js', () => {
"deployedBytecode": "0x",
"sourceMap": "",
"deployedSourceMap": "",
"ast": {},
"legacyAST": {},
"source": "",
"sources": {
"contract.sol": {
"ast": {},
"legacyAST": {},
"source": "",
}
},
"compiler": { "name": "", "version": "" },
"sourcePath": "contract.sol",
},
Expand All @@ -151,11 +157,15 @@ describe('mythx.js', () => {
"deployedBytecode": "0x",
"sourceMap": "",
"deployedSourceMap": "",
"ast": {},
"legacyAST": {},
"source": "",
"compiler": { "name": "", "version": "" },
"sourcePath": "contract.sol",
"sources": {
"contract.sol": {
"ast": {},
"legacyAST": {},
"source": "",
}
},
}
];

Expand Down

0 comments on commit 2d42c39

Please sign in to comment.