Skip to content

Commit

Permalink
Merge pull request #1986 from sicpa-dlab/feature/did-method-key-type-…
Browse files Browse the repository at this point in the history
…registry

did method & key type registry
  • Loading branch information
swcurran authored Oct 25, 2022
2 parents 1fb0bca + 5d42eac commit d0c4742
Show file tree
Hide file tree
Showing 68 changed files with 1,079 additions and 1,183 deletions.
20 changes: 11 additions & 9 deletions aries_cloudagent/config/default_context.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
"""Classes for configuring the default injection context."""

from .base_context import ContextBuilder
from .injection_context import InjectionContext
from .provider import CachedProvider, ClassProvider

from ..cache.base import BaseCache
from ..cache.in_memory import InMemoryCache
from ..core.event_bus import EventBus
from ..core.goal_code_registry import GoalCodeRegistry
from ..core.plugin_registry import PluginRegistry
from ..core.profile import ProfileManager, ProfileManagerProvider
from ..core.protocol_registry import ProtocolRegistry
from ..core.goal_code_registry import GoalCodeRegistry
from ..resolver.did_resolver import DIDResolver
from ..tails.base import BaseTailsServer

from ..protocols.actionmenu.v1_0.base_service import BaseMenuService
from ..protocols.actionmenu.v1_0.driver_service import DriverMenuService
from ..protocols.didcomm_prefix import DIDCommPrefix
from ..protocols.introduction.v0_1.base_service import BaseIntroductionService
from ..protocols.introduction.v0_1.demo_service import DemoIntroductionService
from ..resolver.did_resolver import DIDResolver
from ..tails.base import BaseTailsServer
from ..transport.wire_format import BaseWireFormat
from ..utils.stats import Collector
from ..utils.dependencies import is_indy_sdk_module_installed
from ..utils.stats import Collector
from ..wallet.did_method import DIDMethods
from ..wallet.key_type import KeyTypes
from .base_context import ContextBuilder
from .injection_context import InjectionContext
from .provider import CachedProvider, ClassProvider


class DefaultContextBuilder(ContextBuilder):
Expand Down Expand Up @@ -51,6 +51,8 @@ async def build_context(self) -> InjectionContext:

# Global did resolver
context.injector.bind_instance(DIDResolver, DIDResolver([]))
context.injector.bind_instance(DIDMethods, DIDMethods())
context.injector.bind_instance(KeyTypes, KeyTypes())

await self.bind_providers(context)
await self.load_plugins(context)
Expand Down
16 changes: 8 additions & 8 deletions aries_cloudagent/config/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from ..wallet.base import BaseWallet
from ..wallet.crypto import seed_to_did
from ..wallet.did_info import DIDInfo
from ..wallet.did_method import DIDMethod
from ..wallet.key_type import KeyType
from ..wallet.did_method import SOV
from ..wallet.key_type import ED25519
from .base import ConfigError
from .injection_context import InjectionContext

Expand Down Expand Up @@ -79,7 +79,7 @@ async def wallet_config(
if wallet_seed and seed_to_did(wallet_seed) != public_did:
if context.settings.get("wallet.replace_public_did"):
replace_did_info = await wallet.create_local_did(
method=DIDMethod.SOV, key_type=KeyType.ED25519, seed=wallet_seed
method=SOV, key_type=ED25519, seed=wallet_seed
)
public_did = replace_did_info.did
await wallet.set_public_did(public_did)
Expand All @@ -99,8 +99,8 @@ async def wallet_config(
metadata = {"endpoint": endpoint} if endpoint else None

local_did_info = await wallet.create_local_did(
method=DIDMethod.SOV,
key_type=KeyType.ED25519,
method=SOV,
key_type=ED25519,
seed=wallet_seed,
metadata=metadata,
)
Expand All @@ -110,7 +110,7 @@ async def wallet_config(
print(f"Verkey: {local_did_info.verkey}")
else:
public_did_info = await wallet.create_public_did(
method=DIDMethod.SOV, key_type=KeyType.ED25519, seed=wallet_seed
method=SOV, key_type=ED25519, seed=wallet_seed
)
public_did = public_did_info.did
if provision:
Expand All @@ -128,8 +128,8 @@ async def wallet_config(
test_seed = "testseed000000000000000000000001"
if test_seed:
await wallet.create_local_did(
method=DIDMethod.SOV,
key_type=KeyType.ED25519,
method=SOV,
key_type=ED25519,
seed=test_seed,
metadata={"endpoint": "1.2.3.4:8021"},
)
Expand Down
46 changes: 20 additions & 26 deletions aries_cloudagent/core/tests/test_conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
from ...config.injection_context import InjectionContext
from ...connections.models.conn_record import ConnRecord
from ...connections.models.connection_target import ConnectionTarget
from ...connections.models.diddoc import (
DIDDoc,
PublicKey,
PublicKeyType,
Service,
)
from ...connections.models.diddoc import DIDDoc, PublicKey, PublicKeyType, Service
from ...core.event_bus import EventBus, MockEventBus
from ...core.in_memory import InMemoryProfileManager
from ...core.profile import ProfileManager
from ...core.protocol_registry import ProtocolRegistry
from ...multitenant.base import BaseMultitenantManager
from ...multitenant.manager import MultitenantManager
from ...protocols.coordinate_mediation.mediation_invite_store import (
MediationInviteRecord,
)
Expand All @@ -26,8 +23,6 @@
)
from ...protocols.out_of_band.v1_0.models.oob_record import OobRecord
from ...resolver.did_resolver import DIDResolver
from ...multitenant.base import BaseMultitenantManager
from ...multitenant.manager import MultitenantManager
from ...storage.base import BaseStorage
from ...storage.error import StorageNotFoundError
from ...transport.inbound.message import InboundMessage
Expand All @@ -36,14 +31,13 @@
from ...transport.outbound.manager import QueuedOutboundMessage
from ...transport.outbound.message import OutboundMessage
from ...transport.outbound.status import OutboundSendStatus
from ...transport.wire_format import BaseWireFormat
from ...transport.pack_format import PackWireFormat
from ...transport.wire_format import BaseWireFormat
from ...utils.stats import Collector
from ...version import __version__
from ...wallet.base import BaseWallet
from ...wallet.key_type import KeyType
from ...wallet.did_method import DIDMethod

from ...wallet.did_method import SOV
from ...wallet.key_type import ED25519
from .. import conductor as test_module


Expand Down Expand Up @@ -132,8 +126,8 @@ async def test_startup(self):

wallet = session.inject(BaseWallet)
await wallet.create_public_did(
DIDMethod.SOV,
KeyType.ED25519,
SOV,
ED25519,
)

mock_inbound_mgr.return_value.setup.assert_awaited_once()
Expand Down Expand Up @@ -601,8 +595,8 @@ async def test_admin(self):
session = await conductor.root_profile.session()
wallet = session.inject(BaseWallet)
await wallet.create_public_did(
DIDMethod.SOV,
KeyType.ED25519,
SOV,
ED25519,
)

with async_mock.patch.object(
Expand Down Expand Up @@ -645,8 +639,8 @@ async def test_admin_startx(self):
session = await conductor.root_profile.session()
wallet = session.inject(BaseWallet)
await wallet.create_public_did(
DIDMethod.SOV,
KeyType.ED25519,
SOV,
ED25519,
)

with async_mock.patch.object(
Expand Down Expand Up @@ -717,8 +711,8 @@ async def test_start_static(self):
session = await conductor.root_profile.session()
wallet = session.inject(BaseWallet)
await wallet.create_public_did(
DIDMethod.SOV,
KeyType.ED25519,
SOV,
ED25519,
)

mock_mgr.return_value.create_static_connection = async_mock.AsyncMock()
Expand Down Expand Up @@ -887,8 +881,8 @@ async def test_print_invite_connection(self):
session = await conductor.root_profile.session()
wallet = session.inject(BaseWallet)
await wallet.create_public_did(
DIDMethod.SOV,
KeyType.ED25519,
SOV,
ED25519,
)

await conductor.start()
Expand Down Expand Up @@ -1401,8 +1395,8 @@ async def test_startup_x_version_mismatch(self):

wallet = session.inject(BaseWallet)
await wallet.create_public_did(
DIDMethod.SOV,
KeyType.ED25519,
SOV,
ED25519,
)

mock_inbound_mgr.return_value.setup.assert_awaited_once()
Expand Down Expand Up @@ -1438,8 +1432,8 @@ async def test_startup_x_no_storage_version(self):

wallet = session.inject(BaseWallet)
await wallet.create_public_did(
DIDMethod.SOV,
KeyType.ED25519,
SOV,
ED25519,
)

mock_inbound_mgr.return_value.setup.assert_awaited_once()
Expand Down
32 changes: 21 additions & 11 deletions aries_cloudagent/did/did_key.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
"""DID Key class and resolver methods."""

from ..wallet.crypto import ed25519_pk_to_curve25519
from ..wallet.key_type import KeyType
from ..wallet.key_type import (
BLS12381G1G2,
ED25519,
KeyType,
BLS12381G1,
X25519,
BLS12381G2,
KeyTypes,
)
from ..wallet.util import b58_to_bytes, bytes_to_b58

from ..vc.ld_proofs.constants import DID_V1_CONTEXT_URL
Expand Down Expand Up @@ -31,7 +39,7 @@ def from_public_key_b58(cls, public_key: str, key_type: KeyType) -> "DIDKey":
return cls.from_public_key(public_key_bytes, key_type)

@classmethod
def from_fingerprint(cls, fingerprint: str) -> "DIDKey":
def from_fingerprint(cls, fingerprint: str, key_types=None) -> "DIDKey":
"""Initialize new DIDKey instance from multibase encoded fingerprint.
The fingerprint contains both the public key and key type.
Expand All @@ -43,7 +51,9 @@ def from_fingerprint(cls, fingerprint: str) -> "DIDKey":
key_bytes_with_prefix = b58_to_bytes(fingerprint[1:])

# Get associated key type with prefixed bytes
key_type = KeyType.from_prefixed_bytes(key_bytes_with_prefix)
if not key_types:
key_types = KeyTypes()
key_type = key_types.from_prefixed_bytes(key_bytes_with_prefix)

if not key_type:
raise Exception(
Expand Down Expand Up @@ -169,8 +179,8 @@ def construct_did_key_bls12381g1g2(did_key: "DIDKey") -> dict:
g1_public_key = did_key.public_key[:48]
g2_public_key = did_key.public_key[48:]

bls12381g1_key = DIDKey.from_public_key(g1_public_key, KeyType.BLS12381G1)
bls12381g2_key = DIDKey.from_public_key(g2_public_key, KeyType.BLS12381G2)
bls12381g1_key = DIDKey.from_public_key(g1_public_key, BLS12381G1)
bls12381g2_key = DIDKey.from_public_key(g2_public_key, BLS12381G2)

bls12381g1_key_id = f"{did_key.did}#{bls12381g1_key.fingerprint}"
bls12381g2_key_id = f"{did_key.did}#{bls12381g2_key.fingerprint}"
Expand Down Expand Up @@ -241,7 +251,7 @@ def construct_did_key_ed25519(did_key: "DIDKey") -> dict:
"""
curve25519 = ed25519_pk_to_curve25519(did_key.public_key)
x25519 = DIDKey.from_public_key(curve25519, KeyType.X25519)
x25519 = DIDKey.from_public_key(curve25519, X25519)

did_doc = construct_did_signature_key_base(
id=did_key.did,
Expand Down Expand Up @@ -289,9 +299,9 @@ def construct_did_signature_key_base(


DID_KEY_RESOLVERS = {
KeyType.ED25519: construct_did_key_ed25519,
KeyType.X25519: construct_did_key_x25519,
KeyType.BLS12381G2: construct_did_key_bls12381g2,
KeyType.BLS12381G1: construct_did_key_bls12381g1,
KeyType.BLS12381G1G2: construct_did_key_bls12381g1g2,
ED25519: construct_did_key_ed25519,
X25519: construct_did_key_x25519,
BLS12381G2: construct_did_key_bls12381g2,
BLS12381G1: construct_did_key_bls12381g1,
BLS12381G1G2: construct_did_key_bls12381g1g2,
}
14 changes: 6 additions & 8 deletions aries_cloudagent/did/tests/test_did_key_bls12381g1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase


from ...wallet.key_type import KeyType
from ...wallet.key_type import BLS12381G1
from ...wallet.util import b58_to_bytes
from ..did_key import DIDKey, DID_KEY_RESOLVERS
from .test_dids import (
Expand All @@ -24,14 +24,12 @@
class TestDIDKey(TestCase):
def test_bls12381g1_from_public_key(self):
key_bytes = b58_to_bytes(TEST_BLS12381G1_BASE58_KEY)
did_key = DIDKey.from_public_key(key_bytes, KeyType.BLS12381G1)
did_key = DIDKey.from_public_key(key_bytes, BLS12381G1)

assert did_key.did == TEST_BLS12381G1_DID

def test_bls12381g1_from_public_key_b58(self):
did_key = DIDKey.from_public_key_b58(
TEST_BLS12381G1_BASE58_KEY, KeyType.BLS12381G1
)
did_key = DIDKey.from_public_key_b58(TEST_BLS12381G1_BASE58_KEY, BLS12381G1)

assert did_key.did == TEST_BLS12381G1_DID

Expand All @@ -53,20 +51,20 @@ def test_bls12381g1_properties(self):
assert did_key.did == TEST_BLS12381G1_DID
assert did_key.public_key_b58 == TEST_BLS12381G1_BASE58_KEY
assert did_key.public_key == b58_to_bytes(TEST_BLS12381G1_BASE58_KEY)
assert did_key.key_type == KeyType.BLS12381G1
assert did_key.key_type == BLS12381G1
assert did_key.key_id == TEST_BLS12381G1_KEY_ID
assert did_key.prefixed_public_key == TEST_BLS12381G1_PREFIX_BYTES

def test_bls12381g1_diddoc(self):
did_key = DIDKey.from_did(TEST_BLS12381G1_DID)

resolver = DID_KEY_RESOLVERS[KeyType.BLS12381G1]
resolver = DID_KEY_RESOLVERS[BLS12381G1]

assert resolver(did_key) == did_key.did_doc

def test_bls12381g1_resolver(self):
did_key = DIDKey.from_did(TEST_BLS12381G1_DID)
resolver = DID_KEY_RESOLVERS[KeyType.BLS12381G1]
resolver = DID_KEY_RESOLVERS[BLS12381G1]
did_doc = resolver(did_key)

assert (
Expand Down
Loading

0 comments on commit d0c4742

Please sign in to comment.