-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconnections.py
301 lines (263 loc) · 9.67 KB
/
connections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
""" Connections protocol from Indy HIPE 0031
https://github.com/hyperledger/indy-hipe/tree/master/text/0031-connection-protocol
"""
from enum import Enum, auto
from aries_staticagent import (
StaticConnection,
Module,
route,
crypto,
Message
)
from . import ProtocolStateMachine
class States(Enum):
""" Possible states for connection protocol. """
NULL = auto()
INVITED = auto()
REQUESTED = auto()
RESPONDED = auto()
COMPLETE = auto()
class Events(Enum):
""" Possible events for connection protocol. """
# Inviter Role
SEND_INVITE = auto()
RECV_REQ = auto()
SEND_RESP = auto()
RECV_ACK = auto()
# Invitee Role
RECV_INVITE = auto()
SEND_REQ = auto()
RECV_RESP = auto()
SEND_ACK = auto()
# Either
SEND_ERR = auto()
RECV_ERR = auto()
class Roles(Enum):
""" Possible roles for connection protocol. """
NULL = auto()
INVITER = auto()
INVITEE = auto()
class ConnectionState(ProtocolStateMachine):
""" State object of connection. Defines the state transitions for the
protocol.
"""
transitions = {
Roles.INVITER: {
States.NULL: {
Events.SEND_INVITE: States.INVITED,
},
States.INVITED: {
Events.SEND_INVITE: States.INVITED,
Events.RECV_REQ: States.REQUESTED
},
States.REQUESTED: {
Events.RECV_REQ: States.REQUESTED,
Events.SEND_RESP: States.RESPONDED
},
States.RESPONDED: {
Events.RECV_REQ: States.REQUESTED,
Events.SEND_RESP: States.RESPONDED,
Events.RECV_ACK: States.COMPLETE
},
States.COMPLETE: {
Events.RECV_ACK: States.COMPLETE
}
},
Roles.INVITEE: {
States.NULL: {
Events.RECV_INVITE: States.INVITED,
},
States.INVITED: {
Events.RECV_INVITE: States.INVITED,
Events.SEND_REQ: States.REQUESTED
},
States.REQUESTED: {
Events.SEND_REQ: States.REQUESTED,
Events.RECV_RESP: States.RESPONDED
},
States.RESPONDED: {
Events.SEND_REQ: States.REQUESTED,
Events.RECV_RESP: States.RESPONDED,
Events.SEND_ACK: States.COMPLETE
},
States.COMPLETE: {
Events.SEND_ACK: States.COMPLETE
}
}
}
def __init__(self):
# Starting state for this protocol
super().__init__()
self.state = States.NULL
self.role = Roles.NULL
class Connections(Module):
""" Module for Connection Protocol """
DOC_URI = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/'
PROTOCOL = 'connections'
VERSION = '1.0'
def __init__(self, endpoint=None, dispatcher=None, connections=None):
super().__init__()
self.endpoint = endpoint
self.connections = connections if connections else {}
# This isn't a hack per se but it does allow us to have multiple
# Connections with the same underlying routing which is helpful for
# testing the connections protocol.
self.dispatcher = dispatcher
def create_invitation(self):
""" Create and return an invite. """
conn_vk, conn_sk = crypto.create_keypair()
connection = StaticConnection(
conn_vk,
conn_sk,
b'',
'',
dispatcher=self.dispatcher
)
conn_vk_b58 = crypto.bytes_to_b58(conn_vk)
self.connections[conn_vk_b58] = connection
connection.state = ConnectionState()
connection.state.role = Roles.INVITER
connection.state.transition(Events.SEND_INVITE)
invitation = Message({
'@type': self.type('invitation'),
'label': 'static-iiw',
'recipientKeys': [conn_vk_b58],
'serviceEndpoint': self.endpoint,
'routingKeys': []
})
invitation_url = '{}?c_i={}'.format(
self.endpoint,
crypto.bytes_to_b64(invitation.serialize().encode())
)
return connection, invitation_url
@route
async def invitation(self, msg, _agent):
""" Process an invitation. """
print(msg.pretty_print())
their_conn_key = msg['recipientKeys'][0]
my_vk, my_sk = crypto.create_keypair()
new_connection = StaticConnection(
my_vk,
my_sk,
msg['recipientKeys'][0],
msg['serviceEndpoint'],
dispatcher=self.dispatcher
)
new_connection.did = crypto.bytes_to_b58(my_vk[:16])
new_connection.vk_b58 = crypto.bytes_to_b58(my_vk)
new_connection.state = ConnectionState()
new_connection.state.role = Roles.INVITEE
new_connection.state.transition(Events.RECV_INVITE)
self.connections[their_conn_key] = new_connection
await new_connection.send_async({
'@type': self.type('request'),
'label': 'apts-demo-agent-as-invitee',
'connection': {
'DID': new_connection.did,
'DIDDoc': {
"@context": "https://w3id.org/did/v1",
"id": new_connection.did,
"publicKey": [{
"id": new_connection.did + "#keys-1",
"type": "Ed25519VerificationKey2018",
"controller": new_connection.did,
"publicKeyBase58": new_connection.vk_b58
}],
"service": [{
"id": new_connection.did + ";indy",
"type": "IndyAgent",
"recipientKeys": [new_connection.vk_b58],
"routingKeys": [],
"serviceEndpoint": self.endpoint,
}],
}
}
})
new_connection.state.transition(Events.SEND_REQ)
@route
async def request(self, msg, _agent):
""" Process a request. """
print(msg.pretty_print())
connection = self.connections[msg.mtc.ad['recip_vk']]
connection.state.transition(Events.RECV_REQ)
# Old connection keys, need to sign new keys with these
conn_vk, conn_sk = connection.my_vk, connection.my_sk
# Relationship keys, replacing connection keys
my_vk, my_sk = crypto.create_keypair()
# Update connection
connection.my_vk, connection.my_sk = my_vk, my_sk
connection.did = crypto.bytes_to_b58(my_vk[:16])
connection.vk_b58 = crypto.bytes_to_b58(my_vk)
connection.their_did = msg['connection']['DIDDoc']['publicKey'][0]['controller']
connection.their_vk = crypto.b58_to_bytes(
msg['connection']['DIDDoc']['publicKey'][0]['publicKeyBase58']
)
connection.endpoint = msg['connection']['DIDDoc']['service'][0]['serviceEndpoint']
del self.connections[msg.mtc.ad['recip_vk']]
self.connections[connection.vk_b58] = connection
# Prepare response
connection_block = {
'DID': connection.did,
'DIDDoc': {
"@context": "https://w3id.org/did/v1",
"id": connection.did,
"publicKey": [{
"id": connection.did + "#keys-1",
"type": "Ed25519VerificationKey2018",
"controller": connection.did,
"publicKeyBase58": connection.vk_b58
}],
"service": [{
"id": connection.did + ";indy",
"type": "IndyAgent",
"recipientKeys": [connection.vk_b58],
"routingKeys": [],
"serviceEndpoint": self.endpoint,
}],
}
}
connection.state.transition(Events.SEND_RESP)
await connection.send_async({
'@type': self.type('response'),
'~thread': {
'thid': msg.id,
'sender_order': 0
},
'connection~sig': crypto.sign_message_field(
connection_block,
crypto.bytes_to_b58(conn_vk),
conn_sk
)
})
@route
async def response(self, msg, _agent):
""" Process a response. """
print("Got response:", msg.pretty_print())
their_conn_key = msg['connection~sig']['signer']
connection = self.connections[their_conn_key]
connection.state.transition(Events.RECV_RESP)
msg['connection'] = crypto.verify_signed_message_field(msg['connection~sig'])
del msg['connection~sig']
print("Got response (sig unpacked)", msg.pretty_print())
# Update connection
del self.connections[their_conn_key]
connection.their_did = msg['connection']['DIDDoc']['publicKey'][0]['controller']
connection.their_vk = crypto.b58_to_bytes(
msg['connection']['DIDDoc']['publicKey'][0]['publicKeyBase58']
)
connection.endpoint = msg['connection']['DIDDoc']['service'][0]['serviceEndpoint']
self.connections[connection.vk_b58] = connection
connection.state.transition(Events.SEND_ACK)
await connection.send_async({
'@type': self.type('ack'),
'status': 'OK',
'~thread': {
'thid': msg.id
}
})
@route
async def ack(self, msg, _agent):
""" Process an ack. """
print(msg.pretty_print())
connection = self.connections[msg.mtc.ad['recip_vk']]
connection.state.transition(Events.RECV_ACK)