Exemplo n.º 1
0
    def __init__(self,
                 input_file,
                 name=None,
                 solc_args=None,
                 solc_binary="solc"):

        data = get_solc_json(input_file,
                             solc_args=solc_args,
                             solc_binary=solc_binary)

        self.solidity_files = []

        for filename in data["sourceList"]:
            with open(filename, "r", encoding="utf-8") as file:
                code = file.read()
                self.solidity_files.append(SolidityFile(filename, code))

        has_contract = False

        # If a contract name has been specified, find the bytecode of that specific contract
        srcmap_constructor = []
        srcmap = []
        if name:
            for key, contract in sorted(data["contracts"].items()):
                filename, _name = key.split(":")

                if (filename == input_file and name == _name
                        and len(contract["bin-runtime"])):
                    code = contract["bin-runtime"]
                    creation_code = contract["bin"]
                    srcmap = contract["srcmap-runtime"].split(";")
                    srcmap_constructor = contract["srcmap"].split(";")
                    has_contract = True
                    break

        # If no contract name is specified, get the last bytecode entry for the input file

        else:
            for key, contract in sorted(data["contracts"].items()):
                filename, name = key.split(":")

                if filename == input_file and len(contract["bin-runtime"]):
                    code = contract["bin-runtime"]
                    creation_code = contract["bin"]
                    srcmap = contract["srcmap-runtime"].split(";")
                    srcmap_constructor = contract["srcmap"].split(";")
                    has_contract = True

        if not has_contract:
            raise NoContractFoundError

        self.mappings = []

        self.constructor_mappings = []

        self._get_solc_mappings(srcmap)
        self._get_solc_mappings(srcmap_constructor, constructor=True)

        super().__init__(code, creation_code, name=name)
Exemplo n.º 2
0
def get_contracts_from_file(input_file, solc_args=None, solc_binary="solc"):
    data = get_solc_json(input_file,
                         solc_args=solc_args,
                         solc_binary=solc_binary)

    try:
        for key, contract in data["contracts"].items():
            filename, name = key.split(":")
            if filename == input_file and len(contract["bin-runtime"]):
                yield SolidityContract(
                    input_file=input_file,
                    name=name,
                    solc_args=solc_args,
                    solc_binary=solc_binary,
                )
    except KeyError:
        raise NoContractFoundError
Exemplo n.º 3
0
def _compile_to_code(input_file):
    compiled = util.get_solc_json(str(input_file))
    code = list(compiled['contracts'].values())[0]['bin-runtime']
    return code
Exemplo n.º 4
0
    def __init__(self, input_file, name=None, solc_args=None):

        data = get_solc_json(input_file, solc_args=solc_args)

        self.solidity_files = []

        for filename in data['sourceList']:
            with open(filename, 'r', encoding='utf-8') as file:
                code = file.read()
                self.solidity_files.append(SolidityFile(filename, code))

        has_contract = False

        # If a contract name has been specified, find the bytecode of that specific contract

        if name:
            for key, contract in sorted(data['contracts'].items()):
                filename, _name = key.split(":")

                if filename == input_file and name == _name and len(
                        contract['bin-runtime']):
                    code = contract['bin-runtime']
                    creation_code = contract['bin']
                    srcmap = contract['srcmap-runtime'].split(";")
                    has_contract = True
                    break

        # If no contract name is specified, get the last bytecode entry for the input file

        else:
            for key, contract in sorted(data['contracts'].items()):
                filename, name = key.split(":")

                if filename == input_file and len(contract['bin-runtime']):
                    code = contract['bin-runtime']
                    creation_code = contract['bin']
                    srcmap = contract['srcmap-runtime'].split(";")
                    has_contract = True

        if not has_contract:
            raise NoContractFoundError

        self.mappings = []

        for item in srcmap:
            mapping = item.split(":")

            if len(mapping) > 0 and len(mapping[0]) > 0:
                offset = int(mapping[0])

            if len(mapping) > 1 and len(mapping[1]) > 0:
                length = int(mapping[1])

            if len(mapping) > 2 and len(mapping[2]) > 0:
                idx = int(mapping[2])
            lineno = self.solidity_files[idx].data.encode(
                'utf-8')[0:offset].count('\n'.encode('utf-8')) + 1

            self.mappings.append(SourceMapping(idx, offset, length, lineno))

        super().__init__(code, creation_code, name=name)
Exemplo n.º 5
0
def get_contracts_from_file(input_file, solc_args=None):
    data = get_solc_json(input_file, solc_args=solc_args)
    for key, contract in data['contracts'].items():
        filename, name = key.split(":")
        if filename == input_file and len(contract['bin-runtime']):
            yield SolidityContract(input_file, name, solc_args)