@@ -127,10 +127,42 @@ async def create_invitation(
127127 or_default = True ,
128128 )
129129 image_url = self .profile .context .settings .get ("image_url" )
130+ invitation = None
131+ connection = None
132+
133+ invitation_mode = ConnRecord .INVITATION_MODE_ONCE
134+ if multi_use :
135+ invitation_mode = ConnRecord .INVITATION_MODE_MULTI
130136
131137 if not my_label :
132138 my_label = self .profile .settings .get ("default_label" )
133139
140+ accept = (
141+ ConnRecord .ACCEPT_AUTO
142+ if (
143+ auto_accept
144+ or (
145+ auto_accept is None
146+ and self .profile .settings .get ("debug.auto_accept_requests" )
147+ )
148+ )
149+ else ConnRecord .ACCEPT_MANUAL
150+ )
151+
152+ if recipient_keys :
153+ # TODO: register recipient keys for relay
154+ # TODO: check that recipient keys are in wallet
155+ invitation_key = recipient_keys [0 ] # TODO first key appropriate?
156+ else :
157+ # Create and store new invitation key
158+ async with self .profile .session () as session :
159+ wallet = session .inject (BaseWallet )
160+ invitation_signing_key = await wallet .create_signing_key (
161+ key_type = ED25519
162+ )
163+ invitation_key = invitation_signing_key .verkey
164+ recipient_keys = [invitation_key ]
165+
134166 if public :
135167 if not self .profile .settings .get ("public_invites" ):
136168 raise ConnectionManagerError ("Public invitations are not enabled" )
@@ -143,89 +175,64 @@ async def create_invitation(
143175 "Cannot create public invitation with no public DID"
144176 )
145177
146- if multi_use :
147- raise ConnectionManagerError (
148- "Cannot use public and multi_use at the same time"
149- )
150-
151- if metadata :
152- raise ConnectionManagerError (
153- "Cannot use public and set metadata at the same time"
154- )
155-
156178 # FIXME - allow ledger instance to format public DID with prefix?
157179 invitation = ConnectionInvitation (
158180 label = my_label , did = f"did:sov:{ public_did .did } " , image_url = image_url
159181 )
160182
183+ connection = ConnRecord ( # create connection record
184+ invitation_key = public_did .verkey ,
185+ invitation_msg_id = invitation ._id ,
186+ invitation_mode = invitation_mode ,
187+ their_role = ConnRecord .Role .REQUESTER .rfc23 ,
188+ state = ConnRecord .State .INVITATION .rfc23 ,
189+ accept = accept ,
190+ alias = alias ,
191+ connection_protocol = CONN_PROTO ,
192+ )
193+
194+ async with self .profile .session () as session :
195+ await connection .save (session , reason = "Created new invitation" )
196+
161197 # Add mapping for multitenant relaying.
162198 # Mediation of public keys is not supported yet
163199 await self ._route_manager .route_public_did (self .profile , public_did .verkey )
164200
165- return None , invitation
166-
167- invitation_mode = ConnRecord .INVITATION_MODE_ONCE
168- if multi_use :
169- invitation_mode = ConnRecord .INVITATION_MODE_MULTI
170-
171- if recipient_keys :
172- # TODO: register recipient keys for relay
173- # TODO: check that recipient keys are in wallet
174- invitation_key = recipient_keys [0 ] # TODO first key appropriate?
175201 else :
176- # Create and store new invitation key
202+ # Create connection record
203+ connection = ConnRecord (
204+ invitation_key = invitation_key , # TODO: determine correct key to use
205+ their_role = ConnRecord .Role .REQUESTER .rfc160 ,
206+ state = ConnRecord .State .INVITATION .rfc160 ,
207+ accept = accept ,
208+ invitation_mode = invitation_mode ,
209+ alias = alias ,
210+ connection_protocol = CONN_PROTO ,
211+ )
177212 async with self .profile .session () as session :
178- wallet = session .inject (BaseWallet )
179- invitation_signing_key = await wallet .create_signing_key (
180- key_type = ED25519
181- )
182- invitation_key = invitation_signing_key .verkey
183- recipient_keys = [invitation_key ]
213+ await connection .save (session , reason = "Created new invitation" )
184214
185- accept = (
186- ConnRecord .ACCEPT_AUTO
187- if (
188- auto_accept
189- or (
190- auto_accept is None
191- and self .profile .settings .get ("debug.auto_accept_requests" )
192- )
215+ await self ._route_manager .route_invitation (
216+ self .profile , connection , mediation_record
217+ )
218+ routing_keys , my_endpoint = await self ._route_manager .routing_info (
219+ self .profile ,
220+ my_endpoint or cast (str , self .profile .settings .get ("default_endpoint" )),
221+ mediation_record ,
193222 )
194- else ConnRecord .ACCEPT_MANUAL
195- )
196-
197- # Create connection record
198- connection = ConnRecord (
199- invitation_key = invitation_key , # TODO: determine correct key to use
200- their_role = ConnRecord .Role .REQUESTER .rfc160 ,
201- state = ConnRecord .State .INVITATION .rfc160 ,
202- accept = accept ,
203- invitation_mode = invitation_mode ,
204- alias = alias ,
205- connection_protocol = CONN_PROTO ,
206- )
207- async with self .profile .session () as session :
208- await connection .save (session , reason = "Created new invitation" )
209223
210- await self ._route_manager .route_invitation (
211- self .profile , connection , mediation_record
212- )
213- routing_keys , my_endpoint = await self ._route_manager .routing_info (
214- self .profile ,
215- my_endpoint or cast (str , self .profile .settings .get ("default_endpoint" )),
216- mediation_record ,
217- )
224+ # Create connection invitation message
225+ # Note: Need to split this into two stages
226+ # to support inbound routing of invites
227+ # Would want to reuse create_did_document and convert the result
228+ invitation = ConnectionInvitation (
229+ label = my_label ,
230+ recipient_keys = recipient_keys ,
231+ routing_keys = routing_keys ,
232+ endpoint = my_endpoint ,
233+ image_url = image_url ,
234+ )
218235
219- # Create connection invitation message
220- # Note: Need to split this into two stages to support inbound routing of invites
221- # Would want to reuse create_did_document and convert the result
222- invitation = ConnectionInvitation (
223- label = my_label ,
224- recipient_keys = recipient_keys ,
225- routing_keys = routing_keys ,
226- endpoint = my_endpoint ,
227- image_url = image_url ,
228- )
229236 async with self .profile .session () as session :
230237 await connection .attach_invitation (session , invitation )
231238
@@ -529,6 +536,11 @@ async def receive_request(
529536 their_role = ConnRecord .Role .REQUESTER .rfc160 ,
530537 )
531538 if not connection :
539+ if not self .profile .settings .get ("requests_through_public_did" ):
540+ raise ConnectionManagerError (
541+ "Unsolicited connection requests to "
542+ "public DID is not enabled"
543+ )
532544 connection = ConnRecord ()
533545 connection .invitation_key = connection_key
534546 connection .my_did = my_info .did
0 commit comments