def test_did_validate(): test_did = did_generate_random() did_id = did_to_id(test_did) did_validate(test_did) did_validate(f'{test_did}/test') did_validate(f'{test_did}01021A2A/test') test_did = (did_id, did_id) with pytest.raises(TypeError): did_validate(test_did) assert (not is_did(test_did)) test_did = 'test-text' with pytest.raises(ValueError, match='must start'): did_validate(test_did) assert (not is_did(test_did)) test_did = 'did' with pytest.raises(ValueError, match='must start'): did_validate(test_did) assert (not is_did(test_did)) test_did = f'did:extra-long:{did_id}' with pytest.raises(ValueError, match='"id" must have only'): did_validate(test_did) assert (not is_did(test_did)) test_did = f'did:valid:0x01A2B3C' with pytest.raises(ValueError, match='path should only have hex characters'): did_validate(test_did) assert (not is_did(test_did))
def test_did_parse(): test_did = did_generate_random() did_id = did_to_id(test_did) # basic parse value = did_parse(test_did) assert (value['method'] == 'dep') assert (f'0x{value["id"]}' == did_id) assert (value['id_hex'] == did_id) assert (value['fragment'] is None) assert (value['path'] is None) # test path test_path = secrets.token_hex(32) value = did_parse(f'{test_did}/{test_path}') assert (value['method'] == 'dep') assert (f'0x{value["id"]}' == did_id) assert (value['id_hex'] == did_id) assert (value['fragment'] == '') assert (value['path'] == test_path) # test fragment test_path = secrets.token_hex(32) test_fragment = secrets.token_urlsafe(32) value = did_parse(f'{test_did}/{test_path}#{test_fragment}') assert (value['method'] == 'dep') assert (f'0x{value["id"]}' == did_id) assert (value['id_hex'] == did_id) assert (value['fragment'] == test_fragment) assert (value['path'] == test_path)
def get_block_number(self, did: str) -> int: block_number = None did_id = did_to_id(did) if did_id: did_id = self._web3.toBytes(hexstr=did_id) block_number = self.call('getBlockNumberUpdated', did_id) return block_number
def is_match(self, name_did_url): """ Is this AgentAccess object matches the name, DID or URL. :param str name_did_url: Name, DID or URL to match :returns: True if match """ if self._name == name_did_url: return True if self._url == name_did_url: return True if self._did and is_did(self._did) and is_did(name_did_url): if did_to_id(self._did) == did_to_id(name_did_url): return True return False
def register_did(self, did: str, ddo_text: str, account: ConvexAccount): encode_ddo_text = ContractBase.escape_string(ddo_text) did_id = did_to_id(did).lower() command = f'(register {did_id} "{encode_ddo_text}")' result = self.send(command, account) if result and 'value' in result: return id_to_did(result['value']) return result
def register(self, account: EthereumAccount, did: str, ddo_text: str) -> str: tx_hash = None did_id = did_to_id(did) if did_id: did_id = self._web3.toBytes(hexstr=did_id) if len(ddo_text) > MAX_DDO_TEXT_SIZE: logger.error(f'ddo test is to large {len(ddo_text)}') tx_hash = self.call('registerDID', (did_id, ddo_text), account) return tx_hash
def owner(self, did: str, account_address: AccountAddress): did_id = did_to_id(did).lower() command = f'(owner {did_id})' if is_address(account_address): address = account_address else: address = account_address.address result = self.query(command, address) if result and 'value' in result: return to_address(result['value']) return result
def get_value(self, did: str) -> str: block_number = self.get_block_number(did) if block_number: did_id = self._web3.toBytes(hexstr=did_to_id(did)) event_filter = self.create_event_filter( 'DIDRegistered', None, from_block=block_number, argument_filters={'_did': did_id}) if event_filter: for event_log in event_filter.get_all_entries(): event_args = event_log.args return event_args['_value'] return None
def test_did_to_id(): test_did = did_generate_random() did_id = did_to_id(test_did) value = did_parse(test_did) assert (value['id_hex'] == did_id)
def test_asset_store_and_download_command(config, convex_network, convex_accounts): args = Mock() test_upload_filename = create_random_filename() create_test_file(test_upload_filename) args.url = config['convex']['network']['url'] if 'contract_names' in config['convex']['network']: args.contract_name = config['convex']['network']['contract_names'].get( 'DIDContract', None) local_agent = config['agents']['surfer'] args.username = local_agent['username'] args.password = local_agent['password'] args.agent = local_agent['url'] args.name = 'Test Asset Store' args.description = None args.date_created = None args.author = None args.license = None args.copyright_holder = None args.in_language = None args.tags = None args.filename = test_upload_filename asset_store = AssetStoreCommand() output = Output() asset_store.execute(args, output) assert (output.values['asset_did']) upload_asset_did = output.values['asset_did'] # register agent in network using it's did ddo = convex_network.resolve_agent(args.agent, username=args.username, password=args.password) register_account = convex_accounts[0] did_id = remove_0x_prefix(did_to_id(upload_asset_did)) try: convex_network.register_did(register_account, f'did:dep:{did_id}', ddo.as_text) except ConvexAPIError as e: pass args = Mock() test_download_filename = create_random_filename() args.url = config['convex']['network']['url'] if 'contract_names' in config['convex']['network']: args.contract_name = config['convex']['network']['contract_names'].get( 'DIDContract', None) local_agent = config['agents']['surfer'] args.username = local_agent['username'] args.password = local_agent['password'] args.description = None args.date_created = None args.created_now = True args.author = None args.license = None args.copyright_holder = None args.in_language = None args.tags = None args.asset_did = upload_asset_did args.filename = test_download_filename asset_download = AssetDownloadCommand() output = Output() asset_download.execute(args, output) print(output.lines) assert (output.values['asset_did']) assert (os.path.exists(test_download_filename)) if os.path.exists(test_download_filename): os.unlink(test_download_filename) os.unlink(test_upload_filename)