Commit 8d65c5c1 authored by inso's avatar inso

Parse Certification and Identity

parent 24c9080e
......@@ -3,7 +3,7 @@ import base64
import logging
from .document import Document, MalformedDocumentError
from .constants import pubkey_regex, signature_regex, block_id_regex, block_uid_regex
from .constants import pubkey_regex, signature_regex, block_id_regex, block_uid_regex, uid_regex
class Identity(Document):
......@@ -15,8 +15,19 @@ class Identity(Document):
.format(pubkey_regex=pubkey_regex,
signature_regex=signature_regex,
block_uid_regex=block_uid_regex))
re_type = re.compile("Type: (Identity)")
re_issuer = re.compile("Issuer: ({pubkey_regex})\n".format(pubkey_regex=pubkey_regex))
re_unique_id = re.compile("UniqueID: ({uid_regex})\n".format(uid_regex=uid_regex))
re_uid = re.compile("UID:([^\n]+)\n")
re_timestamp = re.compile("META:TS:({block_uid_regex})\n".format(block_uid_regex=block_uid_regex))
re_meta_ts = re.compile("META:TS:({block_uid_regex})\n".format(block_uid_regex=block_uid_regex))
re_timestamp = re.compile("Timestamp: ({block_uid_regex})\n".format(block_uid_regex=block_uid_regex))
fields_parsers = {**Document.fields_parsers, **{
"Type": re_type,
"UniqueID": re_unique_id,
"Issuer": re_issuer,
"Timestamp": re_timestamp
}}
def __init__(self, version, currency, pubkey, uid, ts, signature):
"""
......@@ -51,6 +62,35 @@ class Identity(Document):
return cls(version, currency, pubkey, uid, ts, signature)
@classmethod
def from_signed_raw(cls, signed_raw):
from .block import BlockUID
n = 0
lines = signed_raw.splitlines(True)
version = int(Identity.parse_field("Version", lines[n]))
n += 1
Identity.parse_field("Type", lines[n])
n += 1
currency = Identity.parse_field("Currency", lines[n])
n += 1
pubkey = Identity.parse_field("Issuer", lines[n])
n += 1
uid = Identity.parse_field("UniqueID", lines[n])
n += 1
ts = BlockUID.from_str(Identity.parse_field("Timestamp", lines[n]))
n += 1
signature = Identity.parse_field("Signature", lines[n])
return cls(version, currency, pubkey, uid, ts, signature)
def raw(self):
return """Version: {version}
Type: Identity
......@@ -84,6 +124,23 @@ class Certification(Document):
signature_regex=signature_regex
))
re_timestamp = re.compile("META:TS:({block_uid_regex})\n".format(block_uid_regex=block_uid_regex))
re_type = re.compile("Type: (Certification)")
re_issuer = re.compile("Issuer: ({pubkey_regex})\n".format(pubkey_regex=pubkey_regex))
re_idty_issuer = re.compile("IdtyIssuer: ({pubkey_regex})\n".format(pubkey_regex=pubkey_regex))
re_idty_unique_id = re.compile("IdtyUniqueID: ({uid_regex})\n".format(uid_regex=uid_regex))
re_idty_timestamp = re.compile("IdtyTimestamp: ({block_uid_regex})\n".format(block_uid_regex=block_uid_regex))
re_idty_signature = re.compile("IdtySignature: ({signature_regex})\n".format(signature_regex=signature_regex))
re_cert_timestamp = re.compile("CertTimestamp: ({block_uid_regex})\n".format(block_uid_regex=block_uid_regex))
fields_parsers = {**Document.fields_parsers, **{
"Type": re_type,
"Issuer": re_issuer,
"CertTimestamp": re_cert_timestamp,
"IdtyIssuer": re_idty_issuer,
"IdtyUniqueID": re_idty_unique_id,
"IdtySignature": re_idty_signature,
"IdtyTimestamp": re_idty_timestamp
}}
def __init__(self, version, currency, pubkey_from, pubkey_to,
timestamp, signature):
......@@ -102,6 +159,44 @@ class Certification(Document):
self.pubkey_to = pubkey_to
self.timestamp = timestamp
@classmethod
def from_signed_raw(cls, signed_raw):
from .block import BlockUID
n = 0
lines = signed_raw.splitlines(True)
version = int(Identity.parse_field("Version", lines[n]))
n += 1
Certification.parse_field("Type", lines[n])
n += 1
currency = Certification.parse_field("Currency", lines[n])
n += 1
pubkey_from = Certification.parse_field("Issuer", lines[n])
n += 1
pubkey_to = Certification.parse_field("IdtyIssuer", lines[n])
n += 1
Certification.parse_field("IdtyUniqueID", lines[n])
n += 1
BlockUID.from_str(Certification.parse_field("IdtyTimestamp", lines[n]))
n += 1
Certification.parse_field("IdtySignature", lines[n])
n += 1
timestamp = BlockUID.from_str(Certification.parse_field("CertTimestamp", lines[n]))
n += 1
signature = Certification.parse_field("Signature", lines[n])
return cls(version, currency, pubkey_from, pubkey_to, timestamp, signature)
@classmethod
def from_inline(cls, version, currency, blockhash, inline):
"""
......
uid_regex = "[A-Za-z0-9_-]{2,100}"
pubkey_regex = "(?![OIl])[1-9A-Za-z]{42,45}"
signature_regex = "[A-Za-z0-9+/]+(?:=|==)?"
block_hash_regex = "[0-9a-fA-F]{5,64}"
......
......@@ -63,6 +63,9 @@ J3G9oM5AKYZNLAB5Wx499w61NuUoS57JVccTShUbGpCMjCqj9yXXqNq7dyZpDWA6BxipsiaMZhujMeBf
"""
self.assertEqual(selfcert.signed_raw(), result)
from_raw = Identity.from_signed_raw(result)
self.assertEqual(from_raw.signed_raw(), result)
def test_certifications_from_inline(self):
version = 2
currency = "zeta_brousouf"
......@@ -106,7 +109,8 @@ SoKwoa8PFfCDJWZ6dNCv7XstezHcc2BbKiJgVDXv82R5zYR83nis9dShLgWJ5w48noVUHimdngzYQneN
"""
self.assertEqual(certification.signed_raw(selfcert), result)
from_raw = Certification.from_signed_raw(certification.signed_raw(selfcert))
self.assertEqual(from_raw.signed_raw(selfcert), result)
def test_revokation_from_inline(self):
version = 2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment