export default source => { // parse the Solidity source const ast = solparser.parse(source) // get a list of all function nodes const functionNodes = flatten(ast).filter(propEquals('type', 'FunctionDeclaration')) // analyze the security of the functions const analyzedNodes = functionNodes.map(node => { const functionCallees = callees(node).map(node => node.callee) return { name: graphNodeName(node.name), callees:functionCallees, send: functionCallees.some(callee => { return (callee.name || callee.property && callee.property.name) === 'send' }), constant: node.modifiers && node.modifiers.some(propEquals('name', 'constant')), internal: node.modifiers && node.modifiers.some(propEquals('name', 'internal')) } }) // console.log(JSON.stringify(ast, null, 2)) // console.log(JSON.stringify(analyzedNodes, null, 2)) // generate a graph var digraph = new Graph() analyzedNodes.forEach(({ name, callees, send, constant, internal }) => { // node digraph.setNode(graphNodeName(name), send ? { color: COLORS.SEND } : constant ? { color: COLORS.CONSTANT } : internal ? { color: COLORS.INTERNAL } : {} ) // edge callees.forEach(callee => { const calleeName = callee.property && callee.property.name || callee.name digraph.setEdge(name, graphNodeName(calleeName)) }) }) // add send node if(analyzedNodes.some(prop('send'))) { digraph.setNode(SEND_NODE_NAME, SEND_NODE_STYLE) } return dot.write(digraph) }
function toDot(graph) { var graphlibGraph = toGraph(graph); // Stringify node metadata graphlibGraph.nodes().forEach(function (name) { var node = graphlibGraph.node(name); if (node.metadata) node.metadata = JSON.stringify(node.metadata); }); // Stringify edge metadata graphlibGraph.edges().forEach(function (name) { var edge = graphlibGraph.edge(name); if (edge.metadata) edge.metadata = JSON.stringify(edge.metadata); }); // Convert to dot and return return dot.write(graphlibGraph); }
var graphlib = require("graphlib"); var dot = require("graphlib-dot"); var g = new dot.DotDigraph(); var parent; for (var i=0; i<5; i++){ id = g.addNode('cluster'+i,{ label: i }); if (i!=0) { g.parent(id, parent); } parent=id; } for (var i=6; i<10; i++){ id = g.addNode('cluster'+i,{ label: i }); if (i==6){ g.parent(id, 'cluster2'); } else { g.parent(id, parent); } parent=id; } console.log(dot.write(g));