def __init__(self, project_dir: str = '', network: str = '', wallet_path: str = '', contract_config_path: str = ''): super().__init__(project_dir) contract_config_file_path = path.join(self.project_dir, 'contracts', 'config.json') old_contract_config_file_path = path.join(self.project_dir, 'contracts', 'default-config.json') if len(contract_config_path) != 0: self._contract_config_file_path = contract_config_path elif path.exists(contract_config_file_path): self._contract_config_file_path = contract_config_file_path elif path.exists(old_contract_config_file_path): self._contract_config_file_path = old_contract_config_file_path else: raise PunicaException(PunicaError.config_file_not_found) try: with open(self._contract_config_file_path, 'r') as f: self._contract_config = json.load(f) except FileNotFoundError: raise PunicaException(PunicaError.config_file_not_found) self._avm_dir = path.join(project_dir, 'build', 'contracts') self._wallet_dir = path.join(project_dir, 'wallet') if len(network) == 0: network = self.default_network self._network = network self._ontology = Ontology() self._ontology.rpc.set_address(self.get_rpc_address(network)) if len(wallet_path) == 0: wallet_path = path.join(self.wallet_dir, self._contract_config.get('defaultWallet', 'wallet.json')) self._wallet_path = wallet_path self._ontology.wallet_manager.open_wallet(self._wallet_path)
async def get_ddo(ont_id): contract_address = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03' version = b'\x00' remote = 'http://dappnode1.ont.io:20336/' sdk = Ontology(rpc_address=remote) args = dict(ontid=ont_id.encode('utf-8')) invoke_code = build_vm.build_native_invoke_code(contract_address, version, 'getDDO', args) tx = Transaction(0, 0xd1, 0, 0, b'', invoke_code) response = await sdk.aio_rpc.send_raw_transaction_pre_exec(tx) serialized_ddo = response['Result'] if len(serialized_ddo) == 0: return dict() if isinstance(serialized_ddo, str): stream = StreamManager.get_stream(bytearray.fromhex(serialized_ddo)) elif isinstance(serialized_ddo, bytes): stream = StreamManager.get_stream(serialized_ddo) else: raise SDKException( ErrorCode.params_type_error('unexpected data type {}'.format( type(serialized_ddo)))) reader = BinaryReader(stream) try: public_key_bytes = reader.read_var_bytes() except SDKException: public_key_bytes = b'' try: attribute_bytes = reader.read_var_bytes() except SDKException: attribute_bytes = b'' try: recovery_bytes = reader.read_var_bytes() except SDKException: recovery_bytes = b'' if len(recovery_bytes) != 0: b58_recovery = Address(recovery_bytes).b58encode() else: b58_recovery = '' try: controller = reader.read_var_bytes().decode('utf-8') except SDKException: controller = '' try: recovery = reader.read_var_bytes().decode('utf-8') except SDKException: recovery = '' pub_keys = ontid.OntId.parse_pub_keys(ont_id, public_key_bytes) attribute_list = ontid.OntId.parse_attributes(attribute_bytes) return dict( id=ont_id, keys=pub_keys, ctrl=controller, attributes=attribute_list, recovery=recovery if recovery else b58_recovery, )
def register_check(): for basename in account_and_ontid.keys(): sdk = Ontology() sdk.rpc.set_address("http://localhost:25768") wallet_path = base_path + basename + "/wallet.dat" sdk.wallet_manager.open_wallet(wallet_path) ontid = sdk.native_vm.ont_id() account_info = account_and_ontid.get(basename) yield (account_info.get("account"), ontid.get_ddo(ont_id=account_info.get("ontid")))
def setUp(self): root_folder = os.path.dirname(os.path.dirname(__file__)) wallet_path = os.path.join(root_folder, 'wallet', 'wallet.json') self.ontology = Ontology() self.ontology.rpc.set_address('http://polaris3.ont.io:20336') self.ontology.wallet_manager.open_wallet(wallet_path) self.acct = self.ontology.wallet_manager.get_account_by_b58_address('AKeDu9QW6hfAhwpvCwNNwkEQt1LkUQpBpW', 'password') self.hex_contract_address = '6fa374c57ea53680f5733d789a4f03b0ea45d82e' self.hello_ontology = InvokeHelloPython(self.ontology, self.hex_contract_address)
def setUp(self): self.sdk = Ontology() self.sdk.rpc.connect_to_test_net() test_dir = path.dirname(__file__) wallet_path = path.join(path.dirname(test_dir), 'wallet', 'wallet.json') self.sdk.wallet_manager.open_wallet(wallet_path) self.gas_price = 500 self.gas_limit = 25000000 self.contract_address = '166c697011bf3d028287849cb20d96eafd43943f' self.ont_id = 'did:ont:AHBB3LQNpqXjCLathy7vTNgmQ1cGSj8S9Z' self.ont_id_ctrl_address = 'AHBB3LQNpqXjCLathy7vTNgmQ1cGSj8S9Z' self.ont_id_ctrl_acct = self.sdk.wallet_manager.get_control_account_by_b58_address(self.ont_id, self.ont_id_ctrl_address, 'password')
async def get_ddo(ont_id): contract_address = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03' version = b'\x00' remote = 'http://dappnode1.ont.io:20336/' sdk = Ontology(rpc_address=remote) args = dict(ontid=ont_id.encode('utf-8')) invoke_code = build_vm.build_native_invoke_code(contract_address, version, 'getDocumentJson', args) tx = Transaction(0, 0xd1, 0, 0, b'', invoke_code) response = await sdk.aio_rpc.send_raw_transaction_pre_exec(tx) ddo = response['Result'] return ddo
def register_ontid(): for basename in account_and_ontid.keys(): sdk = Ontology() sdk.rpc.set_address("http://localhost:25768") wallet_path = base_path + basename + "/wallet.dat" sdk.wallet_manager.open_wallet(wallet_path) ontid = sdk.native_vm.ont_id() account_info = account_and_ontid.get(basename) account = sdk.wallet_manager.get_account_by_b58_address( account_info.get("account"), account_info.get("account_password") ) yield ontid.registry_ont_id( ont_id=account_info.get("ontid"), ctrl_acct=account, payer=account, gas_price=0, gas_limit=50000 )
def __init__(self, mode, *arg, **kwargs): self.sdk = Ontology() if mode == 'main': self.sdk.rpc.connect_to_main_net() self.sdk.restful.connect_to_main_net() self.sdk.websocket.connect_to_main_net() elif mode == 'test': self.sdk.rpc.connect_to_test_net() self.sdk.restful.connect_to_test_net() self.sdk.websocket.connect_to_test_net() elif mode == 'local': self.sdk.rpc.connect_to_localhost() self.sdk.restful.connect_to_localhost() self.sdk.websocket.connect_to_localhost() else: rpc_address = 'http://10.112.104.226:20336' restful_address = 'http://10.112.104.226:20334' websocket_address = 'http://10.112.104.226:20335' self.sdk.rpc.set_address(rpc_address) self.sdk.restful.set_address(restful_address) self.sdk.websocket.set_address(websocket_address)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import json import unittest import binascii from ontology.contract.neo.abi.abi_info import AbiInfo from ontology.contract.neo.invoke_function import InvokeFunction from ontology.sdk import Ontology from ontology.wallet.wallet_manager import WalletManager from src.crypto.ecies import ECIES ontology = Ontology() remote_rpc_address = 'http://polaris3.ont.io:20336' contract_address_hex = 'cf25ea1932ddbc9a03ce62131001d8bcdccc12ea' contract_address_bytearray = bytearray(binascii.a2b_hex(contract_address_hex)) contract_address_bytearray.reverse() ontology.rpc.set_address(remote_rpc_address) root_folder = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) wallet_path = os.path.join(root_folder, 'wallet', 'wallet.json') contracts_folder = os.path.join(root_folder, 'contracts') contracts_path = os.path.join(contracts_folder, 'interplanetary-album.abi.json') with open(contracts_path) as f: contract_abi = json.loads(f.read()) entry_point = contract_abi.get('entrypoint', '') functions = contract_abi['abi']['functions'] events = contract_abi.get('events', list()) abi_info = AbiInfo(contract_address_hex, entry_point, functions, events)
## Default configs ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'bmp'} IPFS_HOST = '127.0.0.1' IPFS_PORT = 5001 GAS_LIMIT = 20000000 GAS_PRICE = 500 ONT_RPC_ADDRESS = 'http://polaris3.ont.io:20336' CONTRACT_ADDRESS_HEX = 'cf25ea1932ddbc9a03ce62131001d8bcdccc12ea' ## load config from punica.json with open(os.path.join(ROOT_FOLDER, 'punica.json')) as f: punicaCfg = json.loads(f.read()) IPFS_HOST = punicaCfg.get('IPFS_HOST', IPFS_HOST) IPFS_PORT = punicaCfg.get('IPFS_PORT', IPFS_PORT) ONT_RPC_ADDRESS = punicaCfg.get('ONT_RPC_ADDRESS', ONT_RPC_ADDRESS) CONTRACT_ADDRESS_HEX = punicaCfg.get('CONTRACT_ADDRESS_HEX', CONTRACT_ADDRESS_HEX) CONTRACT_ADDRESS_BYTEARRAY = bytearray(binascii.a2b_hex(CONTRACT_ADDRESS_HEX)) CONTRACT_ADDRESS_BYTEARRAY.reverse() ONTOLOGY = Ontology() ONTOLOGY.rpc.set_address(ONT_RPC_ADDRESS) with open(os.path.join(CONTRACTS_FOLDER, 'interplanetary-album.abi.json')) as f: CONTRACT_ABI = json.loads(f.read()) entry_point = CONTRACT_ABI.get('entrypoint', '') functions = CONTRACT_ABI['abi']['functions'] events = CONTRACT_ABI.get('events', list()) ABI_INFO = AbiInfo(CONTRACT_ADDRESS_HEX, entry_point, functions, events) WALLET_MANAGER = WalletManager() WALLET_MANAGER.open_wallet(WALLET_PATH)
from ontology.sdk import Ontology from ontology.exception.exception import SDKException from ontology.wallet.wallet_manager import WalletManager static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static') template_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'html') app = Flask('DXToken', static_folder=static_folder, template_folder=template_folder) app.config.from_object('default_settings') jsglue = JSGlue() jsglue.init_app(app) sdk = Ontology() sdk.rpc.set_address(app.config['DEFAULT_REMOTE_RPC_ADDRESS']) oep4 = sdk.neo_vm.oep4() oep4.hex_contract_address = app.config['DEFAULT_CONTRACT_ADDRESS'] gas_price = app.config['GAS_PRICE'] gas_limit = app.config['GAS_LIMIT'] wallet_manager = WalletManager() wallet_path = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'wallet', 'wallet.json') if os.path.isfile(wallet_path): wallet_manager.open_wallet(wallet_path) @app.route('/') def index():
def test_singleton(self): for _ in range(10): new_sdk = Ontology() self.assertEqual(sdk, new_sdk)
def __init__(self, project_dir: str = '', network: str = ''): super().__init__(project_dir) if len(network) == 0: network = self.default_network self._ontology = Ontology() self._ontology.rpc.set_address(self.get_rpc_address(network))