def compile_solidity(file, solc_binary="solc"): try: p = Popen([solc_binary, "--bin-runtime", '--allow-paths', ".", file], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() ret = p.returncode if ret < 0: raise CompilerError("The Solidity compiler experienced a fatal error (code %d). Please check the Solidity compiler." % ret) except FileNotFoundError: raise CompilerError("Compiler not found. Make sure that solc is installed and in PATH, or set the SOLC environment variable.") out = stdout.decode("UTF-8") if out == "": err = "Error compiling input file. Solc returned:\n" + stderr.decode("UTF-8") raise CompilerError(err) m = re.search(r":(.*?) =======\nBinary of the runtime part:", out) contract_name = m.group(1) if m: m = re.search(r"runtime part: \n([0-9a-f]+)\n", out) if (m): return [contract_name, m.group(1)] else: return [contract_name, "0x00"] else: err = "Could not retrieve bytecode from solc output. Solc returned:\n" + stdout.decode("UTF-8") raise CompilerError(err)
def compile_solidity(solc_binary, file): try: p = Popen([solc_binary, "--bin-runtime", file], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() ret = p.returncode if ret < 0: raise CompilerError( "The Solidity compiler experienced a fatal error (code %d). Please check the Solidity compiler." % ret) except FileNotFoundError: raise CompilerError( "Compiler not found. Make sure that solc is installed and in PATH, or set the SOLC environment variable." ) out = stdout.decode("UTF-8") if out == "": err = "Error compiling input file. Solc returned:\n" + stderr.decode( "UTF-8") raise CompilerError(err) m = re.search( r":(.*?) =======\nBinary of the runtime part: \n([0-9a-f]+)\n", out) return [m.group(1), m.group(2)]
def get_sigs_from_file(file_name, solc_binary="solc", solc_args=None): """ :param file_name: accepts a filename :return: their signature mappings """ sigs = {} cmd = [solc_binary, "--hashes", file_name] if solc_args: cmd.extend(solc_args.split()) try: p = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() ret = p.returncode if ret != 0: raise CompilerError( "Solc experienced a fatal error (code %d).\n\n%s" % (ret, stderr.decode("UTF-8"))) except FileNotFoundError: raise CompilerError( "Compiler not found. Make sure that solc is installed and in PATH, or set the SOLC environment variable." ) stdout = stdout.decode("unicode_escape").split("\n") for line in stdout: if ("(" in line and ")" in line and ":" in line ): # the ':' need not be checked but just to be sure sigs["0x" + line.split(":")[0]] = [line.split(":")[1].strip()] logging.debug("Signatures: found %d signatures after parsing" % len(sigs)) return sigs
def get_solc_json(file, solc_binary="solc"): try: p = Popen([ solc_binary, "--combined-json", "bin,bin-runtime,srcmap-runtime", '--allow-paths', ".", file ], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() ret = p.returncode if ret != 0: raise CompilerError( "Solc experienced a fatal error (code %d).\n\n%s" % (ret, stderr.decode('UTF-8'))) except FileNotFoundError: raise CompilerError( "Compiler not found. Make sure that solc is installed and in PATH, or set the SOLC environment variable." ) out = stdout.decode("UTF-8") if not len(out): raise CompilerError("Compilation failed.") return json.loads(out)
def get_solc_json(file, solc_binary="solc", solc_settings_json=None): """ :param file: :param solc_binary: :param solc_settings_json: :return: """ cmd = [solc_binary, "--optimize", "--standard-json", "--allow-paths", "."] settings = json.loads(solc_settings_json) if solc_settings_json else {} settings.update({ "outputSelection": { "*": { "": ["ast"], "*": [ "metadata", "evm.bytecode", "evm.deployedBytecode", "evm.methodIdentifiers", ], } } }) input_json = json.dumps({ "language": "Solidity", "sources": { file: { "urls": [file] } }, "settings": settings, }) try: p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate(bytes(input_json, "utf8")) except FileNotFoundError: raise CompilerError( "Compiler not found. Make sure that solc is installed and in PATH, or set the SOLC environment variable." ) out = stdout.decode("UTF-8") result = json.loads(out) for error in result.get("errors", []): if error["severity"] == "error": raise CompilerError("Solc experienced a fatal error.\n\n%s" % error["formattedMessage"]) return result
def get_solc_json(file, solc_binary="solc", solc_args=None): """ :param file: :param solc_binary: :param solc_args: :return: """ cmd = [ solc_binary, "--combined-json", "bin,bin-runtime,srcmap,srcmap-runtime,ast" ] if solc_args: cmd.extend(solc_args.split()) if not "--allow-paths" in cmd: cmd.extend(["--allow-paths", "."]) else: for i, arg in enumerate(cmd): if arg == "--allow-paths": cmd[i + 1] += ",." cmd.append(file) try: p = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() ret = p.returncode if ret != 0: raise CompilerError( "Solc experienced a fatal error (code %d).\n\n%s" % (ret, stderr.decode("UTF-8"))) except FileNotFoundError: raise CompilerError( "Compiler not found. Make sure that solc is installed and in PATH, or set the SOLC environment variable." ) out = stdout.decode("UTF-8") if not len(out): raise CompilerError("Compilation failed.") return json.loads(out)
def import_solidity_file(self, file_path: str, solc_binary: str = "solc", solc_args: str = None): """Import Function Signatures from solidity source files. :param solc_binary: :param solc_args: :param file_path: solidity source code file path :return: """ cmd = [solc_binary, "--hashes", file_path] if solc_args: cmd.extend(solc_args.split()) try: p = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() ret = p.returncode if ret != 0: raise CompilerError( "Solc has experienced a fatal error (code {}).\n\n{}". format(ret, stderr.decode("utf-8"))) except FileNotFoundError: raise CompilerError(( "Compiler not found. Make sure that solc is installed and in PATH, " "or the SOLC environment variable is set.")) stdout = stdout.decode("unicode_escape").split("\n") for line in stdout: # the ':' need not be checked but just to be sure if all(map(lambda x: x in line, ["(", ")", ":"])): solc_bytes = "0x" + line.split(":")[0] solc_text = line.split(":")[1].strip() self.solidity_sigs[solc_bytes].append(solc_text) log.debug("Signatures: found %d signatures after parsing" % len(self.solidity_sigs)) # update DB with what we've found for byte_sig, text_sigs in self.solidity_sigs.items(): for text_sig in text_sigs: self.add(byte_sig, text_sig)
def compile_solidity(solc_binary, file): try: p = Popen(["solc", "--bin-runtime", file], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() except FileNotFoundError: raise CompilerError( "Compiler not found. Make sure that solc is installed and in PATH, or set the SOLC environment variable." ) out = stdout.decode("UTF-8") if out == "": err = "Error compiling input file. Solc returned:\n" + stderr.decode( "UTF-8") raise CompilerError(err) # out = out.replace("[\n\s]", "") m = re.search( r":(.*?) =======\nBinary of the runtime part: \n([0-9a-f]+)\n", out) return [m.group(1), m.group(2)]