|
6 | 6 | from acapy_agent.resolver.base import DIDNotFound |
7 | 7 | from acapy_agent.wallet.base import BaseWallet |
8 | 8 | from acapy_agent.wallet.crypto import validate_seed |
| 9 | +from acapy_agent.wallet.did_info import DIDInfo |
9 | 10 | from acapy_agent.wallet.did_method import DIDMethods |
10 | 11 | from acapy_agent.wallet.did_parameters_validation import DIDParametersValidation |
11 | 12 | from acapy_agent.wallet.error import WalletError |
12 | | -from acapy_agent.wallet.key_type import ED25519 |
| 13 | +from acapy_agent.wallet.key_type import BLS12381G2, ED25519, P256 |
| 14 | +from acapy_agent.wallet.keys.manager import multikey_to_verkey |
| 15 | +from acapy_agent.wallet.routes import format_did_info |
13 | 16 | from acapy_agent.wallet.util import b58_to_bytes, bytes_to_b64 |
14 | 17 | from aiohttp import web |
15 | 18 |
|
16 | | -from .base import ( |
17 | | - DidUpdateRequestOptions, |
18 | | - SubmitSignatureOptions, |
19 | | - DIDDocumentSchema, |
20 | | - DidActionState, |
21 | | -) |
22 | | -from .helpers import ( |
23 | | - create_verification_keys, |
24 | | - create_did_verification_method, |
25 | | - VerificationMethods, |
26 | | - create_did_payload, |
27 | | - CheqdNetwork, |
28 | | -) |
29 | 19 | from ..did.base import ( |
30 | 20 | BaseDIDManager, |
31 | 21 | CheqdDIDManagerError, |
32 | | - Secret, |
33 | | - DidDeactivateRequestOptions, |
34 | 22 | DidCreateRequestOptions, |
| 23 | + DidDeactivateRequestOptions, |
35 | 24 | Options, |
| 25 | + Secret, |
36 | 26 | ) |
37 | 27 | from ..did_method import CHEQD |
38 | 28 | from ..resolver.resolver import CheqdDIDResolver |
| 29 | +from .base import ( |
| 30 | + DidActionState, |
| 31 | + DIDDocumentSchema, |
| 32 | + DidUpdateRequestOptions, |
| 33 | + SubmitSignatureOptions, |
| 34 | +) |
| 35 | +from .helpers import ( |
| 36 | + CheqdNetwork, |
| 37 | + VerificationMethods, |
| 38 | + create_did_payload, |
| 39 | + create_did_verification_method, |
| 40 | + create_verification_keys, |
| 41 | +) |
39 | 42 | from .registrar import DIDRegistrar |
40 | 43 |
|
41 | 44 | LOGGER = logging.getLogger(__name__) |
@@ -86,9 +89,11 @@ async def create( |
86 | 89 | verkey = key.verkey |
87 | 90 | verkey_bytes = b58_to_bytes(verkey) |
88 | 91 | public_key_b64 = bytes_to_b64(verkey_bytes) |
89 | | - verification_method = ( |
90 | | - options.get("verification_method") or VerificationMethods.Ed255192020 |
91 | | - ) |
| 92 | + verification_method = VerificationMethods.Ed255192020 |
| 93 | + if options.get("verification_method"): |
| 94 | + verification_method = VerificationMethods( |
| 95 | + options.get("verification_method") |
| 96 | + ) |
92 | 97 |
|
93 | 98 | if did_doc is None: |
94 | 99 | # generate payload |
@@ -159,7 +164,7 @@ async def create( |
159 | 164 | return { |
160 | 165 | "did": did, |
161 | 166 | "verkey": verkey, |
162 | | - "didDocument": publish_did_state.didDocument.dict(), |
| 167 | + "didDocument": publish_did_state.didDocument.model_dump(), |
163 | 168 | } |
164 | 169 |
|
165 | 170 | async def update(self, did: str, did_doc: dict, options: dict = None) -> dict: |
@@ -218,7 +223,7 @@ async def update(self, did: str, did_doc: dict, options: dict = None) -> dict: |
218 | 223 | except Exception as ex: |
219 | 224 | raise ex |
220 | 225 |
|
221 | | - return {"did": did, "didDocument": publish_did_state.didDocument.dict()} |
| 226 | + return {"did": did, "didDocument": publish_did_state.didDocument.model_dump()} |
222 | 227 |
|
223 | 228 | async def deactivate(self, did: str, options: dict = None) -> dict: |
224 | 229 | """Deactivate a Cheqd DID.""" |
@@ -277,6 +282,77 @@ async def deactivate(self, did: str, options: dict = None) -> dict: |
277 | 282 | raise ex |
278 | 283 | return { |
279 | 284 | "did": did, |
280 | | - "didDocument": publish_did_state.didDocument.dict(), |
| 285 | + "didDocument": publish_did_state.didDocument.model_dump(), |
281 | 286 | "didDocumentMetadata": metadata, |
282 | 287 | } |
| 288 | + |
| 289 | + async def import_did(self, did_document: dict, metadata: dict = None) -> dict: |
| 290 | + """Import a DID.""" |
| 291 | + metadata = metadata or {} |
| 292 | + |
| 293 | + async with self.profile.session() as session: |
| 294 | + try: |
| 295 | + wallet = session.inject(BaseWallet) |
| 296 | + if not wallet: |
| 297 | + raise WalletError(reason="No wallet available") |
| 298 | + |
| 299 | + did = did_document.get("id") |
| 300 | + if not did: |
| 301 | + raise WalletError(reason="DID document must contain an 'id' field") |
| 302 | + |
| 303 | + # Extract verification method (assuming first one for simplicity) |
| 304 | + verification_methods = did_document.get("verificationMethod", []) |
| 305 | + if not verification_methods: |
| 306 | + raise WalletError( |
| 307 | + reason="DID document must contain verification methods" |
| 308 | + ) |
| 309 | + # Get the first verification method's public key |
| 310 | + first_vm = verification_methods[0] |
| 311 | + # Handle both publicKeyBase58 and publicKeyMultibase formats |
| 312 | + public_key_base58 = first_vm.get("publicKeyBase58") |
| 313 | + public_key_multibase = first_vm.get("publicKeyMultibase") |
| 314 | + if public_key_base58: |
| 315 | + # If we have publicKeyBase58, use it directly |
| 316 | + verkey = public_key_base58 |
| 317 | + elif public_key_multibase: |
| 318 | + # If we have publicKeyMultibase, convert it to verkey format |
| 319 | + verkey = multikey_to_verkey(public_key_multibase) |
| 320 | + else: |
| 321 | + raise WalletError( |
| 322 | + reason=( |
| 323 | + "Verification method must contain either " |
| 324 | + "'publicKeyBase58' or 'publicKeyMultibase'" |
| 325 | + ) |
| 326 | + ) |
| 327 | + # Determine key type from verification method |
| 328 | + key_type = ED25519 # Default fallback |
| 329 | + |
| 330 | + vm_type = first_vm.get("type", "") |
| 331 | + if "Ed25519" in vm_type: |
| 332 | + key_type = ED25519 |
| 333 | + elif "P256" in vm_type or "secp256r1" in vm_type: |
| 334 | + key_type = P256 |
| 335 | + elif "BLS" in vm_type: |
| 336 | + key_type = BLS12381G2 |
| 337 | + LOGGER.debug("Detected key type %s for DID %s", key_type.key_type, did) |
| 338 | + # Determine and validate DID method |
| 339 | + did_methods: DIDMethods = self.profile.inject(DIDMethods) |
| 340 | + method = did_methods.from_did(did) |
| 341 | + did_validation = DIDParametersValidation(did_methods) |
| 342 | + did_validation.validate_key_type(method, key_type) |
| 343 | + |
| 344 | + # Create DIDInfo object |
| 345 | + did_info = DIDInfo( |
| 346 | + did=did, |
| 347 | + verkey=verkey, |
| 348 | + metadata=metadata, |
| 349 | + method=method, |
| 350 | + key_type=key_type, |
| 351 | + ) |
| 352 | + stored_info = await wallet.store_did(did_info) |
| 353 | + |
| 354 | + LOGGER.info("Successfully imported DID: %s", did) |
| 355 | + except Exception as ex: |
| 356 | + raise ex |
| 357 | + |
| 358 | + return {"result": format_did_info(stored_info)} |
0 commit comments