|
13 | 13 | from ...storage.error import StorageError, StorageNotFoundError
|
14 | 14 | from ...storage.record import StorageRecord
|
15 | 15 | from ...wallet.base import BaseWallet, DIDInfo
|
| 16 | +from ...wallet.crypto import create_keypair, seed_to_did |
16 | 17 | from ...wallet.error import WalletNotFoundError
|
| 18 | +from ...wallet.util import bytes_to_b58 |
17 | 19 |
|
18 | 20 | from ..message_delivery import MessageDelivery
|
19 | 21 | from ..responder import BaseResponder
|
@@ -68,7 +70,7 @@ async def create_invitation(
|
68 | 70 | accept: str = None,
|
69 | 71 | public: bool = False,
|
70 | 72 | multi_use: bool = False,
|
71 |
| - alias: str = None |
| 73 | + alias: str = None, |
72 | 74 | ) -> Tuple[ConnectionRecord, ConnectionInvitation]:
|
73 | 75 | """
|
74 | 76 | Generate new connection invitation.
|
@@ -161,7 +163,7 @@ async def create_invitation(
|
161 | 163 | state=ConnectionRecord.STATE_INVITATION,
|
162 | 164 | accept=accept,
|
163 | 165 | invitation_mode=invitation_mode,
|
164 |
| - alias=alias |
| 166 | + alias=alias, |
165 | 167 | )
|
166 | 168 |
|
167 | 169 | await connection.save(self.context, reason="Created new invitation")
|
@@ -217,7 +219,7 @@ async def receive_invitation(
|
217 | 219 | their_role=their_role,
|
218 | 220 | state=ConnectionRecord.STATE_INVITATION,
|
219 | 221 | accept=accept,
|
220 |
| - alias=alias |
| 222 | + alias=alias, |
221 | 223 | )
|
222 | 224 |
|
223 | 225 | await connection.save(
|
@@ -276,8 +278,10 @@ async def create_request(
|
276 | 278 | connection.my_did = my_info.did
|
277 | 279 |
|
278 | 280 | # Create connection request message
|
| 281 | + if not my_endpoint: |
| 282 | + my_endpoint = self.context.settings.get("default_endpoint") |
279 | 283 | did_doc = await self.create_did_document(
|
280 |
| - my_info, connection.inbound_connection_id |
| 284 | + my_info, connection.inbound_connection_id, my_endpoint |
281 | 285 | )
|
282 | 286 | if not my_label:
|
283 | 287 | my_label = self.context.settings.get("default_label")
|
@@ -357,7 +361,7 @@ async def receive_request(
|
357 | 361 |
|
358 | 362 | await new_connection.save(
|
359 | 363 | self.context,
|
360 |
| - reason="Received connection request from multi-use invitation DID" |
| 364 | + reason="Received connection request from multi-use invitation DID", |
361 | 365 | )
|
362 | 366 | connection = new_connection
|
363 | 367 |
|
@@ -461,6 +465,8 @@ async def create_response(
|
461 | 465 | connection.my_did = my_info.did
|
462 | 466 |
|
463 | 467 | # Create connection response message
|
| 468 | + if not my_endpoint: |
| 469 | + my_endpoint = self.context.settings.get("default_endpoint") |
464 | 470 | did_doc = await self.create_did_document(
|
465 | 471 | my_info, connection.inbound_connection_id, my_endpoint
|
466 | 472 | )
|
@@ -565,6 +571,70 @@ async def accept_response(
|
565 | 571 |
|
566 | 572 | return connection
|
567 | 573 |
|
| 574 | + async def create_static_connection( |
| 575 | + self, |
| 576 | + my_did: str = None, |
| 577 | + my_seed: str = None, |
| 578 | + their_did: str = None, |
| 579 | + their_seed: str = None, |
| 580 | + their_verkey: str = None, |
| 581 | + their_endpoint: str = None, |
| 582 | + their_role: str = None, |
| 583 | + alias: str = None, |
| 584 | + ) -> ConnectionRecord: |
| 585 | + """ |
| 586 | + Register a new static connection (for use by the test suite). |
| 587 | +
|
| 588 | + Args: |
| 589 | + my_did: override the DID used in the connection |
| 590 | + my_seed: provide a seed used to generate our DID and keys |
| 591 | + their_did: provide the DID used by the other party |
| 592 | + their_seed: provide a seed used to generate their DID and keys |
| 593 | + their_verkey: provide the verkey used by the other party |
| 594 | + their_endpoint: their URL endpoint for routing messages |
| 595 | + their_role: their role in this connection |
| 596 | + alias: an alias for this connection record |
| 597 | +
|
| 598 | + Returns: |
| 599 | + The new `ConnectionRecord` instance |
| 600 | +
|
| 601 | + """ |
| 602 | + wallet: BaseWallet = await self.context.inject(BaseWallet) |
| 603 | + |
| 604 | + # seed and DID optional |
| 605 | + my_info = await wallet.create_local_did(my_seed, my_did) |
| 606 | + |
| 607 | + # must provide their DID and verkey if the seed is not known |
| 608 | + if (not their_did or not their_verkey) and not their_seed: |
| 609 | + raise ConnectionManagerError( |
| 610 | + "Either a verkey or seed must be provided for the other party" |
| 611 | + ) |
| 612 | + if not their_did: |
| 613 | + their_did = seed_to_did(their_seed) |
| 614 | + if not their_verkey: |
| 615 | + their_verkey_bin, _ = create_keypair(their_seed) |
| 616 | + their_verkey = bytes_to_b58(their_verkey_bin) |
| 617 | + their_info = DIDInfo(their_did, their_verkey, {}) |
| 618 | + |
| 619 | + print(their_info, my_info) |
| 620 | + |
| 621 | + # Create connection record |
| 622 | + connection = ConnectionRecord( |
| 623 | + initiator=ConnectionRecord.INITIATOR_SELF, |
| 624 | + my_did=my_info.did, |
| 625 | + their_did=their_info.did, |
| 626 | + their_role=their_role, |
| 627 | + state=ConnectionRecord.STATE_ACTIVE, |
| 628 | + alias=alias, |
| 629 | + ) |
| 630 | + await connection.save(self.context, reason="Created new static connection") |
| 631 | + |
| 632 | + # Synthesize their DID doc |
| 633 | + did_doc = await self.create_did_document(their_info, None, their_endpoint) |
| 634 | + await self.store_did_document(did_doc) |
| 635 | + |
| 636 | + return connection |
| 637 | + |
568 | 638 | async def find_connection(
|
569 | 639 | self,
|
570 | 640 | their_did: str,
|
@@ -675,27 +745,27 @@ async def find_message_connection(
|
675 | 745 |
|
676 | 746 | async def create_did_document(
|
677 | 747 | self,
|
678 |
| - my_info: DIDInfo, |
| 748 | + did_info: DIDInfo, |
679 | 749 | inbound_connection_id: str = None,
|
680 |
| - my_endpoint: str = None, |
| 750 | + svc_endpoint: str = None, |
681 | 751 | ) -> DIDDoc:
|
682 | 752 | """Create our DID document for a given DID.
|
683 | 753 |
|
684 | 754 | Args:
|
685 |
| - my_info: The DID I am using in this connection |
686 |
| - inbound_connection_id: The inbound routing connection id to use |
687 |
| - my_endpoint: A custom endpoint for the DID Document |
| 755 | + did_info: The DID information (DID and verkey) used in the connection |
| 756 | + inbound_connection_id: The ID of the inbound routing connection to use |
| 757 | + svc_endpoint: A custom endpoint for the DID Document |
688 | 758 |
|
689 | 759 | Returns:
|
690 | 760 | The prepared `DIDDoc` instance
|
691 | 761 |
|
692 | 762 | """
|
693 | 763 |
|
694 |
| - did_doc = DIDDoc(did=my_info.did) |
695 |
| - did_controller = my_info.did |
696 |
| - did_key = my_info.verkey |
| 764 | + did_doc = DIDDoc(did=did_info.did) |
| 765 | + did_controller = did_info.did |
| 766 | + did_key = did_info.verkey |
697 | 767 | pk = PublicKey(
|
698 |
| - my_info.did, |
| 768 | + did_info.did, |
699 | 769 | "1",
|
700 | 770 | did_key,
|
701 | 771 | PublicKeyType.ED25519_SIG_2018,
|
@@ -729,24 +799,23 @@ async def create_did_document(
|
729 | 799 | "Routing DIDDoc service has no recipient key(s)"
|
730 | 800 | )
|
731 | 801 | rk = PublicKey(
|
732 |
| - my_info.did, |
| 802 | + did_info.did, |
733 | 803 | f"routing-{router_idx}",
|
734 | 804 | service.recip_keys[0].value,
|
735 | 805 | PublicKeyType.ED25519_SIG_2018,
|
736 | 806 | did_controller,
|
737 | 807 | True,
|
738 | 808 | )
|
739 | 809 | routing_keys.append(rk)
|
740 |
| - my_endpoint = service.endpoint |
| 810 | + svc_endpoint = service.endpoint |
741 | 811 | break
|
742 | 812 | router_id = router.inbound_connection_id
|
743 | 813 |
|
744 |
| - if not my_endpoint: |
745 |
| - my_endpoint = self.context.settings.get("default_endpoint") |
746 |
| - service = Service( |
747 |
| - my_info.did, "indy", "IndyAgent", [pk], routing_keys, my_endpoint |
748 |
| - ) |
749 |
| - did_doc.set(service) |
| 814 | + if svc_endpoint: |
| 815 | + service = Service( |
| 816 | + did_info.did, "indy", "IndyAgent", [pk], routing_keys, svc_endpoint |
| 817 | + ) |
| 818 | + did_doc.set(service) |
750 | 819 |
|
751 | 820 | return did_doc
|
752 | 821 |
|
|
0 commit comments