Skip to content

Commit fad23a9

Browse files
committed
create statement: Issuer as public key using did:key if not given
Signed-off-by: John Andersen <[email protected]>
1 parent a0e179b commit fad23a9

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

scitt_emulator/create_statement.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22
# Licensed under the MIT License.
33
import pathlib
44
import argparse
5-
from typing import Optional
5+
from typing import Union, Optional
66

77
import cwt
88
import pycose
99
import pycose.headers
1010
import pycose.messages
1111
import pycose.keys.ec2
12+
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
13+
from cryptography.hazmat.primitives.serialization import load_pem_private_key
14+
15+
# NOTE These are unmaintained but the
16+
# https://github.com/hashberg-io/multiformats stuff and base58 modules don't
17+
# produce the same results:
18+
# https://grotto-networking.com/blog/posts/DID_Key.html#bug-in-multibase-library
19+
import multibase
20+
import multicodec
1221

1322
# TODO jwcrypto is LGPLv3, is there another option with a permissive licence?
1423
import jwcrypto.jwk
1524

25+
from scitt_emulator.did_helpers import DID_KEY_METHOD, MULTICODEC_HEX_P384_PUBLIC_KEY
26+
1627

1728
@pycose.headers.CoseHeaderAttribute.register_attribute()
1829
class CWTClaims(pycose.headers.CoseHeaderAttribute):
@@ -40,7 +51,7 @@ class TBD(pycose.headers.CoseHeaderAttribute):
4051

4152
def create_claim(
4253
claim_path: pathlib.Path,
43-
issuer: str,
54+
issuer: Union[str, None],
4455
subject: str,
4556
content_type: str,
4657
payload: str,
@@ -91,6 +102,23 @@ def create_claim(
91102
cwt_cose_key_to_cose_key = cwt_cose_key.to_dict()
92103
sign1_message_key = pycose.keys.ec2.EC2Key.from_dict(cwt_cose_key_to_cose_key)
93104

105+
# If issuer was not given used did:key of public key
106+
if issuer is None:
107+
multicodec_prefix_p_384 = "p384-pub"
108+
multicodec.constants.NAME_TABLE[multicodec_prefix_p_384] = MULTICODEC_HEX_P384_PUBLIC_KEY
109+
issuer = (
110+
DID_KEY_METHOD
111+
+ multibase.encode(
112+
"base58btc",
113+
multicodec.add_prefix(
114+
multicodec_prefix_p_384,
115+
load_pem_private_key(key_as_pem_bytes, password=None)
116+
.public_key()
117+
.public_bytes(Encoding.X962, PublicFormat.CompressedPoint),
118+
),
119+
).decode()
120+
)
121+
94122
# CWT_Claims (label: 14 pending [CWT_CLAIM_COSE]): A CWT representing
95123
# the Issuer (iss) making the statement, and the Subject (sub) to
96124
# correlate a collection of statements about an Artifact. Additional
@@ -163,7 +191,7 @@ def create_claim(
163191
def cli(fn):
164192
p = fn("create-claim", description="Create a fake SCITT claim")
165193
p.add_argument("--out", required=True, type=pathlib.Path)
166-
p.add_argument("--issuer", required=True, type=str)
194+
p.add_argument("--issuer", required=False, type=str, default=None)
167195
p.add_argument("--subject", required=True, type=str)
168196
p.add_argument("--content-type", required=True, type=str)
169197
p.add_argument("--payload", required=True, type=str)

tests/test_cli.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from scitt_emulator import cli, server
1313
from scitt_emulator.oidc import OIDCAuthMiddleware
1414

15-
issuer = "did:web:example.com"
1615
content_type = "application/json"
1716
payload = '{"foo": "bar"}'
1817

@@ -72,8 +71,6 @@ def test_client_cli(use_lro: bool, tmp_path):
7271
"create-claim",
7372
"--out",
7473
claim_path,
75-
"--issuer",
76-
issuer,
7774
"--subject",
7875
"test",
7976
"--content-type",
@@ -265,8 +262,6 @@ def test_client_cli_token(tmp_path):
265262
"create-claim",
266263
"--out",
267264
claim_path,
268-
"--issuer",
269-
issuer,
270265
"--subject",
271266
"test",
272267
"--content-type",

tests/test_docs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ def test_docs_registration_policies(tmp_path):
293293
]
294294
execute_cli(command)
295295
assert os.path.exists(receipt_path)
296+
receipt_path.unlink()
296297
assert os.path.exists(entry_id_path)
298+
receipt_path.unlink(entry_id_path)
297299

298300
# TODO Switch back on the OIDC routes
299301
# submit accepted claim using OIDC -> jwks lookup
@@ -311,4 +313,6 @@ def test_docs_registration_policies(tmp_path):
311313
]
312314
execute_cli(command)
313315
assert os.path.exists(receipt_path)
316+
receipt_path.unlink()
314317
assert os.path.exists(entry_id_path)
318+
receipt_path.unlink(entry_id_path)

0 commit comments

Comments
 (0)