Skip to content

Commit 00371a2

Browse files
committed
feat: add did jwk resolver and support for p256 keys
Signed-off-by: Daniel Bluhm <[email protected]>
1 parent 27d6f23 commit 00371a2

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

didcomm_messaging/crypto/backend/askar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class AskarKey(PublicKey):
3030
"ed25519-pub": KeyAlg.ED25519,
3131
"x25519-pub": KeyAlg.X25519,
3232
"secp256k1-pub": KeyAlg.K256,
33+
"p256-pub": KeyAlg.P256,
3334
}
3435
alg_to_codec = {v: k for k, v in codec_to_alg.items()}
3536

didcomm_messaging/multiformats/multicodec.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SupportedCodecs(Enum):
2222
bls12381g2 = Multicodec("bls12_381-g2-pub", b"\xeb\x01")
2323
bls12381g1g2 = Multicodec("bls12_381-g1g2-pub", b"\xee\x01")
2424
secp256k1_pub = Multicodec("secp256k1-pub", b"\xe7\x01")
25+
p256_pub = Multicodec("p256-pub", b"\x12\x00")
2526

2627
@classmethod
2728
def by_name(cls, name: str) -> Multicodec:
@@ -49,6 +50,7 @@ def for_data(cls, data: bytes) -> Multicodec:
4950
"bls12_381-g2-pub",
5051
"bls12_381-g1g2-pub",
5152
"secp256k1-pub",
53+
"p256-pub",
5254
]
5355

5456

didcomm_messaging/resolver/jwk.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""did:jwk Resolver."""
2+
3+
import re
4+
import json
5+
6+
from didcomm_messaging import DIDResolver
7+
from didcomm_messaging.resolver import DIDResolutionError
8+
from didcomm_messaging.multiformats.multibase import Base64UrlEncoder
9+
10+
b64 = Base64UrlEncoder()
11+
12+
13+
class JWKResolver(DIDResolver):
14+
"""Resolve did:jwk."""
15+
16+
PATTERN = re.compile(r"^did:jwk:(?P<did>.*)$")
17+
18+
async def resolve(self, did: str) -> dict:
19+
"""Resolve a did:jwk."""
20+
if match := self.PATTERN.match(did):
21+
encoded = match.group("did")
22+
else:
23+
raise DIDResolutionError(f"Invalid DID: {did}")
24+
25+
jwk = json.loads(b64.decode(encoded))
26+
doc = {
27+
"@context": [
28+
"https://www.w3.org/ns/did/v1",
29+
"https://w3id.org/security/suites/jws-2020/v1",
30+
],
31+
"id": f"did:jwk:{encoded}",
32+
"verificationMethod": [
33+
{
34+
"id": f"did:jwk:{encoded}#0",
35+
"type": "JsonWebKey2020",
36+
"controller": f"did:jwk:{encoded}",
37+
"publicKeyJwk": jwk,
38+
}
39+
],
40+
}
41+
42+
use = jwk.get("use")
43+
if use == "sig":
44+
doc.update(
45+
{
46+
"assertionMethod": [f"did:jwk:{encoded}#0"],
47+
"authentication": [f"did:jwk:{encoded}#0"],
48+
"capabilityInvocation": [f"did:jwk:{encoded}#0"],
49+
"capabilityDelegation": [f"did:jwk:{encoded}#0"],
50+
}
51+
)
52+
elif use == "enc":
53+
doc.update({"keyAgreement": [f"did:jwk:{encoded}#0"]})
54+
55+
return doc
56+
57+
async def is_resolvable(self, did: str) -> bool:
58+
"""Return if did is resolvable by this resolver."""
59+
return bool(self.PATTERN.match(did))

0 commit comments

Comments
 (0)