-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to decode the event back to the original scitt signed state…
…ment payload (#17) * Add a way to decode the event back to the original scitt signed statement payload --------- Co-authored-by: jgough <[email protected]>
- Loading branch information
1 parent
2d43e1b
commit 6f7bbc9
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" Module for decoding the event """ | ||
|
||
import argparse | ||
|
||
import json | ||
import base64 | ||
|
||
from pprint import pprint | ||
|
||
from pycose.messages import Sign1Message | ||
|
||
|
||
def open_event_json(event_json_file: str) -> bytes: | ||
""" | ||
opens the event json | ||
""" | ||
with open(event_json_file, "rb") as file: | ||
event_json = file.read() | ||
return event_json | ||
|
||
|
||
def get_base64_statement(event_json: bytes) -> str: | ||
""" | ||
gets the base64 encoded signed statement from | ||
the datatrails event | ||
""" | ||
|
||
event = json.loads(event_json) | ||
|
||
base64_signed_statement = event["event_attributes"]["signed_statement"] | ||
|
||
return base64_signed_statement | ||
|
||
|
||
def decode_base64_statement(base64_statement: str) -> bytes: | ||
""" | ||
decodes the base64 encoded signed statement | ||
into a cbor cose sign1 statement | ||
""" | ||
signed_statement = base64.b64decode(base64_statement) | ||
return signed_statement | ||
|
||
|
||
def decode_statement(receipt: bytes): | ||
""" | ||
decodes the signed statement | ||
""" | ||
|
||
# decode the cbor encoded cose sign1 message | ||
message = Sign1Message.decode(receipt) | ||
|
||
return message | ||
|
||
|
||
def main(): | ||
"""Verifies a counter signed receipt signature""" | ||
|
||
parser = argparse.ArgumentParser( | ||
description="Verify a counter signed receipt signature." | ||
) | ||
|
||
# signing key file | ||
parser.add_argument( | ||
"--event-json-file", | ||
type=str, | ||
help="filepath to the stored event, in json format.", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
event_json = open_event_json(args.event_json_file) | ||
|
||
base64_signed_statement = get_base64_statement(event_json) | ||
print(f"\nbase64 encoded signed statement: \n\n{base64_signed_statement}") | ||
|
||
signed_statement = decode_base64_statement(base64_signed_statement) | ||
print(f"\ncbor encoded signed statement: \n\n{signed_statement}") | ||
|
||
decoded_statement = decode_statement(signed_statement) | ||
|
||
print("\ncbor decoded cose sign1 statement:\n") | ||
print("protected headers:") | ||
pprint(decoded_statement.phdr) | ||
print("\nunprotected headers: ") | ||
pprint(decoded_statement.uhdr) | ||
print("\npayload: ", decoded_statement.payload) | ||
print("payload hex: ", decoded_statement.payload.hex()) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |