Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
before('Deploy Sample.sol', async () => {
try{
contractName = 'Sample';
network = 'rinkeby';
path = __dirname + '/contracts/'+ contractName +'.sol';
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler);
await sleep(30000); // To make sure that contractCode is stored
}catch(err){
throw err;
}
});
it('Verifies Sample.sol contract successfully', async () => {
function importPathsFromFile(filePath, paths = new Set()) {
let content
try {
content = fs.readFileSync(filePath).toString('utf-8')
} catch (e) {
if (e.code === 'EISDIR') {
console.error(`Skipping directory ${filePath}`)
return paths // empty Set
} else throw e;
}
// const content = fs.readFileSync(filePath).toString('utf-8')
const ast = parser.parse(content, {tolerant: true})
parser.visit(ast, {
ImportDirective(node) {
let importPath = resolveImportPath(filePath, node.path)
paths.add(importPath)
importPathsFromFile(importPath, paths)
}
})
return paths
}
// for draggable
const definition = { "contracts": new Array(), "inheritances": new Array() }
for (let file of files) {
let content
try {
content = fs.readFileSync(file).toString('utf-8')
} catch (e) {
if (e.code === 'EISDIR') {
console.error(`Skipping directory ${file}`)
continue
} else throw e;
}
const ast = parser.parse(content)
let contractName = null
let cluster = null
let dependencies = {}
parser.visit(ast, {
ContractDefinition(node) {
contractName = node.name
if (!digraph.getNode(contractName)) {
let opts = {
label: contractName,
color: 'gray'
}
digraph.addNode(contractName)
module.exports.verify = async (data, cli = false) => {
try{
const { key, path, contractAddress, network, contractName, cvalues, evmVersion } = data;
let { runs, licenseType, optimizationFlag } = data;
if(Object.keys(map.urls).indexOf(network) > -1){
let name;
let abiEncodedParams;
let parsedData;
const contractSource = await straightener.straighten(path);
const ast = parser.parse(contractSource);
const nodes = ast.children;
const compiler = await solReleases.getCompilerVersion(nodes, map);
const availableContracts = nodes.filter(e => (e.type == 'ContractDefinition' && e.kind == 'contract'));
if( availableContracts.length == 1){
name = availableContracts[0].name;
parsedData = availableContracts[0];
}else if(availableContracts.length > 1){
if(contractName){
name = contractName;
parsedData = availableContracts.filter(e => (e.name == contractName))[0];
}else
throw new Error('More Than One Contracts in File!!! Please Provide --contractName Option');
}
const constructorNode = parsedData.subNodes.filter(obj => (obj.type == 'FunctionDefinition' && obj.isConstructor == true)); // eslint-disable-line max-len
solidityVersion(fileContents: string): string | undefined {
let ast
try {
ast = parser.parse(fileContents, {})
// @ts-ignore
for (let n of ast.children) {
if ((n.name === 'solidity') && (n.type === 'PragmaDirective')) {
return n.value
}
}
} catch (error) {
for (let err of error.errors) {
cli.error(err.message)
}
}
}
contract.source = contractSource;
contract.instrumented = contractSource;
this._initializeCoverageFields(contract);
// First, we run over the original contract to get the source mapping.
let ast = SolidityParser.parse(contract.source, {range: true});
parse[ast.type](contract, ast);
const retValue = JSON.parse(JSON.stringify(contract)); // ?????
// Now, we reset almost everything and use the preprocessor to increase our effectiveness.
this._initializeCoverageFields(contract);
contract.instrumented = preprocess(contract.source);
// Walk the AST, recording injection points
ast = SolidityParser.parse(contract.instrumented, {range: true});
contract.contractName = ast.children.filter(node => this._isRootNode(node))[0].name;
parse[ast.type](contract, ast);
// We have to iterate through these points in descending order
const sortedPoints = Object.keys(contract.injectionPoints).sort((a, b) => b - a);
sortedPoints.forEach(injectionPoint => {
// Line instrumentation has to happen first
contract.injectionPoints[injectionPoint].sort((a, b) => {
const injections = ['injectBranch', 'injectEmptyBranch', 'injectLine'];
return injections.indexOf(b.type) - injections.indexOf(a.type);
});
contract.injectionPoints[injectionPoint].forEach(injection => {
this.injector[injection.type](
it(' trying to get compiler version for Sample.sol (should pass)', async () => {
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
compiler.should.equal('v0.5.1+commit.c8a2cb62');
});
});
function analyze(file) {
let content
let dependencies = {}
try {
content = fs.readFileSync(file).toString('utf-8')
} catch (e) {
if (e.code === 'EISDIR') {
console.error(`Skipping directory ${file}`)
return false
} else throw e
}
const ast = parser.parse(content)
let imports = new Map()
parser.visit(ast, {
ImportDirective(node) {
let contractName = path.parse(node.path).name
let absPath
if (node.path.startsWith(".")) {
let currentDir = path.resolve(path.parse(file).dir)
absPath = path.resolve(path.join(currentDir, node.path))
} else {
let modulesInstalledPath = getModulesInstalledPath(node.path)
let absPathObj = path.parse(modulesInstalledPath + node.path)
absPath = absPathObj.dir + path.sep + absPathObj.base
}
imports.set(contractName, absPath)
parseSourceUnit(input) {
var ast;
try {
ast = parser.parse(input, {loc:true, tolerant:true});
} catch (e) {
if (e instanceof parser.ParserError) {
console.log(e.errors);
return;
}
}
if(typeof ast==="undefined"){
console.error("solidity-parser-antlr - failed to parse input");
}
var sourceUnit = {
contracts:{},
pragmas:[],
imports:[],
hash:null
};
const generateMarkdown = ({ contractFile, templateFile, outputFile }) => {
const contractFileString = fs.readFileSync(contractFile).toString();
const templateFileString = fs.readFileSync(templateFile).toString();
const ast = parser.parse(contractFileString);
const contract = ast.children.find(child => {
return child.type === "ContractDefinition";
});
const contractFileArray = contractFileString.split("\n");
function isValidAdditionalLine(index) {
return (
contractFileArray[index] &&
contractFileArray[index].includes("///") &&
!contractFileArray[index].includes(" @dev ") &&
!contractFileArray[index].includes(" @param ") &&
!contractFileArray[index].includes(" @return ")
);
}