def test_their_vk_and_recipients_raises_error(my_test_info, their_test_info): """Test specifying both their_vk and recipients raises an error.""" with pytest.raises(ValueError): Connection.from_parts( my_test_info.keys, their_vk=their_test_info.keys.verkey, recipients=[their_test_info.keys.verkey], )
def main(): """Send message from cron job.""" keys, target, _args = config() conn = Connection(keys, target) conn.send({ "@type": "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0/message", "~l10n": { "locale": "en" }, "sent_time": utils.timestamp(), "content": "The Cron script was executed.", })
def test_byte_inputs_without_their_info(my_test_info): """Test that valid byte inputs yield expected values.""" conn = Connection.from_parts(my_test_info.keys) assert conn.verkey == my_test_info.keys.verkey assert conn.sigkey == my_test_info.keys.sigkey assert conn.verkey_b58 == my_test_info.keys.verkey_b58 assert conn.did == my_test_info.did
def main(): """Start a server with a static connection.""" their_vk, _ = crypto.create_keypair( seed=hashlib.sha256(b"client").digest()) conn = Connection.from_seed( hashlib.sha256(b"server").digest(), Target(their_vk=their_vk)) @conn.route("https://didcomm.org/basicmessage/1.0/message") async def basic_message_auto_responder(msg, conn): await conn.send_async({ "@type": "https://didcomm.org/" "basicmessage/1.0/message", "~l10n": { "locale": "en" }, "sent_time": utils.timestamp(), "content": "You said: {}".format(msg["content"]), }) async def handle(request): """aiohttp handle POST.""" response = [] with conn.session(response.append) as session: await conn.handle(await request.read(), session) if response: return web.Response(body=response.pop()) raise web.HTTPAccepted() app = web.Application() app.add_routes([web.post("/", handle)]) web.run_app(app, port=os.environ.get("PORT", 3000))
def test_give_recipients_b58(my_test_info, their_test_info): """Test recipients set when specified.""" conn = Connection.from_parts( my_test_info.keys, recipients=[their_test_info.keys.verkey_b58], ) assert conn.target.recipients == [their_test_info.keys.verkey]
def main(): """Create Connection and start web server.""" keys, target, args = config() conn = Connection(keys, target) @conn.route("https://didcomm.org/basicmessage/1.0/message") async def basic_message(msg, conn): """Respond to a basic message.""" await conn.send_async({ "@type": "https://didcomm.org/" "basicmessage/1.0/message", "~l10n": { "locale": "en" }, "sent_time": utils.timestamp(), "content": "You said: {}".format(msg["content"]), }) async def handle(request): """aiohttp handle POST.""" response = [] with conn.session(response.append) as session: await conn.handle(await request.read(), session) if response: return web.Response(text=response.pop()) raise web.HTTPAccepted() app = web.Application() app.add_routes([web.post("/", handle)]) web.run_app(app, port=args.port)
def _gen(send=None, dispatcher=None): return Connection.from_parts( bob_keys, their_vk=alice_keys.verkey, endpoint="asdf", send=send, dispatcher=dispatcher, )
def main(): """Create connection and start web server.""" keys, target, args = config() conn = Connection(keys, target) bmc = BasicMessageCounter() conn.route_module(bmc) async def handle(request): """aiohttp handle POST.""" await conn.handle(await request.read()) return web.Response(status=202) app = web.Application() app.add_routes([web.post("/", handle)]) web.run_app(app, port=args.port)
def test_give_routing_keys(my_test_info, their_test_info): """Test routing_keys set when specified.""" conn = Connection.from_parts( my_test_info.keys, their_vk=their_test_info.keys.verkey, routing_keys=[their_test_info.keys.verkey], ) assert conn.target.recipients == [their_test_info.keys.verkey] assert conn.target.routing_keys == [their_test_info.keys.verkey]
def test_b58_inputs_with_their_info(my_test_info, their_test_info): """Test that valid b58 inputs yield expected values.""" conn = Connection.from_parts(my_test_info.keys, their_vk=their_test_info.keys.verkey_b58) assert conn.verkey == my_test_info.keys.verkey assert conn.sigkey == my_test_info.keys.sigkey assert conn.verkey_b58 == my_test_info.keys.verkey_b58 assert conn.did == my_test_info.did assert conn.target.recipients == [their_test_info.keys.verkey]
def test_pack_unpack_plaintext(alice: Connection, bob): """Test pack/unpack in plaintext.""" msg = {"@type": "doc;protocol/1.0/name"} packed_msg = alice.pack(msg, plaintext=True) assert isinstance(packed_msg, bytes) unpacked_msg = bob.unpack(packed_msg) assert isinstance(unpacked_msg, Message) assert hasattr(unpacked_msg, "mtc") assert unpacked_msg.mtc.is_plaintext() assert unpacked_msg.mtc.sender is None assert unpacked_msg.mtc.recipient is None
def main(): """Create Connection and start web server.""" keys, target, args = config() conn = Connection(keys, target) @conn.route("https://didcomm.org/basicmessage/1.0/message") async def basic_message(msg, conn): """Respond to a basic message.""" await conn.send_async({ "@type": "https://didcomm.org/" "basicmessage/1.0/message", "~l10n": { "locale": "en" }, "sent_time": utils.timestamp(), "content": "You said: {}".format(msg["content"]), }) async def ws_handle(request): """Handle WS requests.""" sock = web.WebSocketResponse() await sock.prepare(request) with conn.session(sock.send_bytes) as session: async for msg in sock: if msg.type == aiohttp.WSMsgType.BINARY: await session.handle(msg.data) elif msg.type == aiohttp.WSMsgType.ERROR: print("ws connection closed with exception %s" % sock.exception()) if not session.should_return_route(): await sock.close() return sock async def post_handle(request): """Handle posted messages.""" response = [] with conn.session(response.append) as session: await session.handle(await request.read()) if response: return web.Response(text=response.pop()) raise web.HTTPAccepted() app = web.Application() app.add_routes([web.get("/", ws_handle), web.post("/", post_handle)]) web.run_app(app, port=args.port)
def test_pack_unpack_with_routing_keys(alice, bob): """Test packing for a connection with routing keys.""" route1 = Connection.from_parts(crypto.create_keypair()) route2 = Connection.from_parts(crypto.create_keypair()) alice.target.update(routing_keys=[route1.verkey, route2.verkey]) packed_message = alice.pack({"@type": "doc;protocol/1.0/name"}) route2_msg = route2.unpack(packed_message) assert route2_msg.type == utils.FORWARD assert route2_msg["to"] == route1.verkey_b58 assert route2_msg.mtc.is_anoncrypted() assert route2_msg.mtc.sender is None route1_msg = route1.unpack(route2_msg["msg"]) assert route1_msg.type == utils.FORWARD assert route1_msg["to"] == bob.verkey_b58 assert route1_msg.mtc.is_anoncrypted() assert route1_msg.mtc.sender is None bob_msg = bob.unpack(route1_msg["msg"]) assert bob_msg.type == "doc;protocol/1.0/name" assert bob_msg.mtc.is_authcrypted() assert bob_msg.mtc.sender == alice.verkey_b58 assert bob_msg.mtc.recipient == bob.verkey_b58
def test_update(my_test_info, their_test_info): their_new_info = generate_test_info() conn = Connection.from_parts( my_test_info.keys, their_vk=their_test_info.keys.verkey, routing_keys=[their_test_info.keys.verkey], endpoint="endpoint1", ) assert conn.verkey == my_test_info.keys.verkey assert conn.sigkey == my_test_info.keys.sigkey assert conn.verkey_b58 == my_test_info.keys.verkey_b58 assert conn.did == my_test_info.did assert conn.target.recipients == [their_test_info.keys.verkey] assert conn.target.routing_keys == [their_test_info.keys.verkey] assert conn.target.endpoint == "endpoint1" with pytest.raises(ValueError): conn.target.update(their_vk=their_new_info.keys.verkey, recipients=[their_new_info.keys.verkey]) conn.target.update( their_vk=their_new_info.keys.verkey, routing_keys=[their_new_info.keys.verkey], endpoint="endpoint2", ) assert conn.target.recipients == [their_new_info.keys.verkey] assert conn.target.routing_keys == [their_new_info.keys.verkey] assert conn.target.endpoint == "endpoint2" conn.target.update( their_vk=their_test_info.keys.verkey, routing_keys=[their_test_info.keys.verkey], ) assert conn.target.recipients == [their_test_info.keys.verkey] assert conn.target.routing_keys == [their_test_info.keys.verkey] assert conn.target.endpoint == "endpoint2" conn.target.update(recipients=[their_new_info.keys.verkey]) assert conn.target.recipients == [their_new_info.keys.verkey] conn.target.update(recipients=[their_test_info.keys.verkey_b58]) assert conn.target.recipients == [their_test_info.keys.verkey]
def main(): """Send a message and await the reply.""" their_vk, _ = crypto.create_keypair( seed=hashlib.sha256(b"server").digest()) conn = Connection.from_seed( hashlib.sha256(b"client").digest(), Target( their_vk=their_vk, endpoint="http://*****:*****@type": "https://didcomm.org/basicmessage/1.0/message", "~l10n": { "locale": "en" }, "sent_time": utils.timestamp(), "content": "The Cron script has been executed.", }, return_route="all", ) print("Msg from conn:", reply and reply.pretty_print())
def connection_ws(example_keys, test_keys): """Connection fixture with ws send.""" return Connection.from_parts(test_keys, their_vk=example_keys.verkey, send=utils.ws_send)
def connection(example_keys, test_keys): """Connection fixture.""" return Connection.from_parts(test_keys, their_vk=example_keys.verkey)
def bob(bob_keys, alice_keys): """Create Bob's Connection.""" yield Connection.from_parts(bob_keys, their_vk=alice_keys.verkey)
def alice(alice_keys, bob_keys): """Create Alice's Connection.""" yield Connection.from_parts(alice_keys, their_vk=bob_keys.verkey)
def conn(dispatcher): """Function scoped static connection fixture. This connection isn't actually connected to anything. """ yield Connection.from_parts((b"", b""), dispatcher=dispatcher)
def test_bad_inputs(args): """Test that bad inputs raise an error.""" with pytest.raises(TypeError): Connection.from_parts(*args)