Skip to content

Commit

Permalink
Update texts, add imports to sourceList
Browse files Browse the repository at this point in the history
  • Loading branch information
muellerberndt committed Mar 25, 2019
1 parent 4005931 commit 5934c5b
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions sabre.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ if (process.argv.length != 3) {

let ethAddress = process.env.MYTHX_ETH_ADDRESS;
let password = process.env.MYTHX_PASSWORD;

const solidity_file = process.argv[2];
let sourceList = [solidity_file];

if (!(ethAddress && password)) {
ethAddress = '0x0000000000000000000000000000000000000000';
Expand Down Expand Up @@ -53,28 +55,36 @@ const input = {
};

const solidity_file_dir = path.dirname(solidity_file).split(path.sep).pop();
const import_paths = helpers.getImportPaths(solidity_code);

const getFileContent = filepath => {
try {
filepath = path.join(solidity_file_dir, import_paths.find(p => p.indexOf(filepath) > -1));
filepath = path.join(solidity_file_dir, filepath);
const stats = fs.statSync(filepath);

if (fs.existsSync(filepath)) {
return fs.readFileSync(filepath).toString();
}
} catch(err) {
throw new Error(`Import ${filepath} not found`);
if (stats.isFile()) {
return fs.readFileSync(filepath).toString();
} else {
throw new Error `File ${filepath} not found`;
}
};

const findImports = pathName => {
try {
sourceList.push(pathName);
return { contents: getFileContent(pathName) };
} catch (e) {
return { error: e.message };
}
};

/* Dynamic linking is not supported. */

const regex = new RegExp(/__\$\w+\$__/,'g');
const address = '0000000000000000000000000000000000000000';

const replaceLinkedLibs = byteCode => {
return byteCode.replace(regex, address);
};

const getMythXReport = solidityCompiler => {
const compiled = JSON.parse(solidityCompiler.compile(JSON.stringify(input), findImports));

Expand Down Expand Up @@ -114,13 +124,15 @@ const getMythXReport = solidityCompiler => {

/* Format data for MythX API */

// console.log(contract);

const data = {
contractName: contractName,
bytecode: contract.evm.bytecode.object,
bytecode: replaceLinkedLibs(contract.evm.bytecode.object),
sourceMap: contract.evm.deployedBytecode.sourceMap,
deployedBytecode: contract.evm.deployedBytecode.object,
deployedBytecode: replaceLinkedLibs(contract.evm.deployedBytecode.object),
deployedSourceMap: contract.evm.deployedBytecode.sourceMap,
sourceList: [solidity_file],
sourceList: sourceList,
analysisMode: 'quick',
sources: {}
};
Expand All @@ -136,7 +148,7 @@ const getMythXReport = solidityCompiler => {
}
);

const mythxSpinner = ora({ text: 'Fetching analysis', color: 'yellow', spinner: 'bouncingBar' }).start();
const mythxSpinner = ora({ text: 'Analyzing ' + contractName, color: 'yellow', spinner: 'bouncingBar' }).start();

client.analyzeWithStatus({ data, timeout: 300000, clientToolName: 'sabre' })
.then(result => {
Expand All @@ -163,7 +175,7 @@ const version = helpers.getSolidityVersion(solidity_code);
if (version !== releases.latest) {
/* Get the solc remote version snapshot of the specified version in the contract */

const solcSpinner = ora({ text: `Downloading solc v${version}`, color: 'yellow', spinner: 'bouncingBar' }).start();
const solcSpinner = ora({ text: `Compiling with solc v${version}`, color: 'yellow', spinner: 'bouncingBar' }).start();

solc.loadRemoteVersion(releases[version], function (err, solcSnapshot) {

Expand All @@ -181,4 +193,4 @@ if (version !== releases.latest) {
/* Use `solc`, if the specified version in the contract matches it's latest version */

getMythXReport(solc);
}
}

0 comments on commit 5934c5b

Please sign in to comment.