def _get_script_hash(self) -> bytes: file_path = self._nef_path if file_path.endswith('.nef') and os.path.isfile(file_path): with open(file_path, mode='rb') as nef: file = nef.read() from boa3.neo.contracts.neffile import NefFile return NefFile.deserialize(file).script_hash
def __init__(self, bytecode: bytes, analyser: Analyser, entry_file: str): self._metadata = analyser.metadata self._symbols: Dict[str, ISymbol] = analyser.symbol_table import os self._files: List[str] = [analyser.path.replace(os.sep, '/')] self._nef: NefFile = NefFile(bytecode) self._entry_file = entry_file
def get_output(self, path: str, root_folder: str = None) -> Tuple[bytes, Dict[str, Any]]: nef_output = path.replace('.py', '.nef') if not os.path.isfile(nef_output): return self.compile_and_save(path, root_folder=root_folder) manifest_output = path.replace('.py', '.manifest.json') from boa3.neo.contracts.neffile import NefFile if not os.path.isfile(nef_output): output = bytes() else: with open(nef_output, mode='rb') as nef: file = nef.read() output = NefFile.deserialize(file).script if not os.path.isfile(manifest_output): manifest = {} else: with open(manifest_output) as manifest_output: import json manifest = json.loads(manifest_output.read()) return output, manifest
def test_generate_nef_file(self): path = self.get_contract_path('GenerationWithDecorator.py') expected_nef_output = path.replace('.py', '.nef') Boa3.compile_and_save(path) self.assertTrue(os.path.exists(expected_nef_output)) with open(expected_nef_output, 'rb') as nef_output: magic = nef_output.read(constants.SIZE_OF_INT32) compiler_with_version = nef_output.read(64) compiler, version = compiler_with_version.rsplit(b'-', maxsplit=1) version = version[:32] nef_output.read(2) # reserved nef_output.read(1) # TODO: method tokens nef_output.read(2) # reserved script_size = nef_output.read(1) script = nef_output.read(int.from_bytes(script_size, BYTEORDER)) check_sum = nef_output.read(constants.SIZE_OF_INT32) self.assertEqual(int.from_bytes(script_size, BYTEORDER), len(script)) nef = NefFile(script)._nef self.assertEqual(compiler.decode(ENCODING), nef.compiler) self.assertEqual(check_sum, nef.checksum) self.assertEqual(version, nef.version.to_array())
def __init__(self, file_path: str): self._nef_path: str = file_path script_hash = None if file_path.endswith('.nef') and os.path.isfile(file_path): with open(file_path, mode='rb') as nef: file = nef.read() from boa3.neo.contracts.neffile import NefFile script_hash = NefFile.deserialize(file).script_hash self._script_hash: Optional[bytes] = script_hash
def _get_contract_id(self, contract_path: str) -> int: if path.isfile(contract_path): with open(contract_path, mode='rb') as nef: from boa3.neo.contracts.neffile import NefFile script_hash = NefFile.deserialize(nef.read()).script_hash return self._storage.get_contract_id(script_hash) contracts = self.contracts if contract_path in contracts: return contracts.index(contract_path) return -1
def __init__(self, bytecode: bytes, analyser: Analyser, entry_file: str): import os self._metadata = analyser.metadata self._symbols: Dict[str, ISymbol] = analyser.symbol_table.copy() self._entry_file = entry_file self._entry_file_full_path = analyser.path.replace( os.sep, constants.PATH_SEPARATOR) self._files: List[str] = [self._entry_file_full_path] self._nef: NefFile = NefFile(bytecode) self.__all_imports: List[Import] = None self._all_methods: Dict[str, Method] = None self._all_static_vars: Dict[str, Variable] = None
def compile_and_save(self, path: str, log: bool = True) -> Tuple[bytes, Dict[str, Any]]: nef_output = path.replace('.py', '.nef') manifest_output = path.replace('.py', '.manifest.json') from boa3.boa3 import Boa3 from boa3.neo.contracts.neffile import NefFile Boa3.compile_and_save(path, show_errors=log) with open(nef_output, mode='rb') as nef: file = nef.read() output = NefFile.deserialize(file).script with open(manifest_output) as manifest_output: import json manifest = json.loads(manifest_output.read()) return output, manifest
def test_generate_nef_file(self): path = '%s/boa3_test/test_sc/generation_test/GenerationWithDecorator.py' % self.dirname expected_nef_output = path.replace('.py', '.nef') Boa3.compile_and_save(path) self.assertTrue(os.path.exists(expected_nef_output)) with open(expected_nef_output, 'rb') as nef_output: magic = nef_output.read(constants.SIZE_OF_INT32) compiler = nef_output.read(32) version = nef_output.read(16) hash = nef_output.read(constants.SIZE_OF_INT160) check_sum = nef_output.read(constants.SIZE_OF_INT32) script_size = nef_output.read(1) script = nef_output.read() self.assertEqual(int.from_bytes(script_size, BYTEORDER), len(script)) nef = NefFile(script)._nef self.assertEqual(compiler.decode(ENCODING), nef.compiler) self.assertEqual(hash, nef.script_hash.to_array()) self.assertEqual(check_sum, nef.checksum) self.assertEqual(version, nef.version.to_array())
def create_test_nef(self, test_script): nef = NefFile(test_script) nef._set_version(self.test_version) return nef