async def pack(wallet_handle: int, my_vk: str, their_vk: str, msg: Message) -> bytes: return await crypto.pack_message( wallet_handle, Serializer.pack(msg), [their_vk], my_vk )
async def test_got_hello_world(config, transport): msg = Message({ 'type': TESTING_MESSAGE.SEND_MESSAGE, 'to': 'http://localhost:3000/indy', 'content': { 'type': 'hello_world', 'message': 'Hello, world!' } }) await transport.send(config.tested_agent, Serializer.pack(msg)) msg_bytes = await expect_message(transport, 5) msg = Serializer.unpack(msg_bytes) assert msg.type == 'hello_world' assert msg.message == 'Hello, world!'
async def handle_send_message(msg: Message, **kwargs): """ Message handler for send_message_command. """ transport = kwargs['transport'] if is_valid_send_message(msg): await transport.send(msg.to, Serializer.pack(msg.content)) return print('invalid send message command dropped')
def parse(invite_url: str) -> Message: matches = re.match('(.+)?c_i=(.+)', invite_url) assert matches, 'Improperly formatted invite url!' invite_msg = Serializer.unpack( base64.urlsafe_b64decode(matches.group(2)).decode('ascii')) validate_message([('@type', Connection.Message.INVITE), 'label', 'recipientKeys', 'serviceEndpoint'], invite_msg) return invite_msg
def build(label: str, connection_key: str, endpoint: str) -> str: msg = Message({ '@type': Connection.Message.INVITE, 'label': label, 'recipientKeys': [connection_key], 'serviceEndpoint': endpoint, # routing_keys not specified, but here is where they would be put in the invite. }) b64_invite = base64.urlsafe_b64encode( bytes(Serializer.pack(msg), 'ascii')).decode('ascii') return '{}?c_i={}'.format(endpoint, b64_invite)
async def unpack(wallet_handle: int, wire_msg_bytes: bytes, **kwargs) -> Message: __tracebackhide__ = True wire_msg = json.loads(await crypto.unpack_message(wallet_handle, wire_msg_bytes)) if 'expected_to_vk' in kwargs: assert kwargs['expected_to_vk'] == wire_msg['recipient_verkey'], \ 'Message is not for the expected verkey!' if 'expected_from_vk' in kwargs: assert kwargs['expected_from_vk'] == wire_msg['sender_verkey'], \ 'Message is not from the expected verkey!' return Serializer.unpack(wire_msg['message'])
async def message_process(config, transport, router): """ """ # Initialization steps # -- Create wallet print('Creating wallet: {}'.format(config.wallet_name)) try: await wallet.create_wallet( json.dumps({ 'id': config.wallet_name, 'storage_config': { 'path': config.wallet_path } }), json.dumps({'key': 'test-agent'})) except: pass # -- Open a wallet print('Opening wallet: {}'.format(config.wallet_name)) config.wallet_handle = await wallet.open_wallet( json.dumps({ 'id': config.wallet_name, 'storage_config': { 'path': config.wallet_path } }), json.dumps({'key': 'test-agent'})) await TRANSPORT.create_transport_key(config.wallet_handle) # Register Routes await testing_routes(router) while True: msg_bytes = await transport.recv() print('Got message: {}'.format(msg_bytes)) try: msg = Serializer.unpack(msg_bytes) except Exception as e: print('Failed to unpack message: {}\n\nError: {}'.format( msg_bytes, e)) continue await router.route(msg, config=config, transport=transport)
async def test_connection_started_by_tested_agent(config, wallet_handle, transport): invite_url = input('Input generated connection invite: ') matches = re.match('(.+)?c_i=(.+)', invite_url) assert matches, 'Improperly formatted invite url!' invite_msg = Serializer.unpack( base64.urlsafe_b64decode(matches.group(2)).decode('utf-8')) validate_message(['@type', 'label', 'key', 'endpoint'], invite_msg) # Create my information for connection (my_did, my_vk) = await did.create_and_store_my_did(wallet_handle, '{}') # Send Connection Request to inviter request = Message({ '@type': Connection.REQUEST, 'label': 'testing-agent', 'DID': my_did, 'DIDDoc': { 'key': my_vk, 'endpoint': config.endpoint, } }) await transport.send( invite_msg['endpoint'], await pack(wallet_handle, my_vk, invite_msg['key'], request)) # Wait for response response_bytes = await expect_message(transport, 60) response = await unpack(wallet_handle, response_bytes, expected_to_vk=my_vk) validate_message(['@type', 'DID', 'DIDDoc'], response) validate_message(['key', 'endpoint'], response['DIDDoc'])
YOUR_ORCA_SCRIPTS_FOLDER = os.path.expanduser( "~/.local/share/orca/orca-scripts") YOUR_NVDAREMOTE_SERVER_ADDRESS = "host" YOUR_NVDAREMOTE_SERVER_PORT = 6837 YOUR_NVDAREMOTE_KEY = "key" # Make the NVDA Remote modules importable sys.path.insert(0, YOUR_ORCA_SCRIPTS_FOLDER) from orca.speechdispatcherfactory import SpeechServer from serializer import JSONSerializer from transport import RelayTransport import traceback import threading mySerializer = JSONSerializer() transport = RelayTransport( mySerializer, (YOUR_NVDAREMOTE_SERVER_ADDRESS, YOUR_NVDAREMOTE_SERVER_PORT), channel=YOUR_NVDAREMOTE_KEY, connection_type="slave") def try_run_thread(): try: transport.run() except Exception: print("Error in thread") traceback.print_exc()
def main(): engine = Engine.create() serializer = JSONSerializer() engine_controller = EngineController(engine, serializer) server = STDIOServer(engine_controller.request_handler) server.listen()