1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
# Copyright 2014-2022 Vincent Texier <vit@free.fr>
#
# DuniterPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DuniterPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
from typing import Any, Optional, Type, TypeVar, Union
from ..constants import (
BLOCK_ID_REGEX,
BLOCK_NUMBER_REGEX,
G1_CURRENCY_CODENAME,
PUBKEY_REGEX,
SIGNATURE_REGEX,
)
# required to type hint cls in classmethod
from ..key import SigningKey
from .block_id import BlockID
from .document import Document, MalformedDocumentError
from .identity import Identity
CertificationType = TypeVar("CertificationType", bound="Certification")
VERSION = 10
class Certification(Document):
"""
A document describing a certification.
"""
re_inline = re.compile(
f"({PUBKEY_REGEX}):({PUBKEY_REGEX}):({BLOCK_NUMBER_REGEX}):({SIGNATURE_REGEX})\n"
)
re_type = re.compile("Type: (Certification)")
re_issuer = re.compile(f"Issuer: ({PUBKEY_REGEX})\n")
re_cert_block_id = re.compile(f"CertTimestamp: ({BLOCK_ID_REGEX})\n")
fields_parsers = {
**Document.fields_parsers,
**{"Type": re_type, "Issuer": re_issuer, "CertTimestamp": re_cert_block_id},
}
def __init__(
self,
pubkey_from: str,
identity: Union[Identity, str],
block_id: BlockID,
signing_key: SigningKey = None,
version: int = VERSION,
currency: str = G1_CURRENCY_CODENAME,
) -> None:
"""
Constructor
:param pubkey_from: Pubkey of the certifier
:param identity: Document instance of the certified identity or identity pubkey string
:param block_id: Current BlockID instance
:param signing_key: SigningKey instance to sign the document (default=None)
:param version: Document version (default=certification.VERSION)
:param currency: Currency codename (default=constants.CURRENCY_CODENAME_G1)
"""
super().__init__(version, currency)
self.pubkey_from = pubkey_from
self.identity = identity if isinstance(identity, Identity) else None
self.pubkey_to = identity.pubkey if isinstance(identity, Identity) else identity
self.block_id = block_id
if signing_key is not None:
self.sign(signing_key)
def __eq__(self, other: Any) -> bool:
"""
Check Certification instances equality
"""
if not isinstance(other, Certification):
return NotImplemented
return (
super().__eq__(other)
and self.pubkey_from == other.pubkey_from
and self.identity == other.identity
and self.block_id == other.block_id
)
def __hash__(self) -> int:
return hash(
(
self.pubkey_from,
self.identity,
self.block_id,
self.version,
self.currency,
self.signature,
)
)
@classmethod
def from_signed_raw(
cls: Type[CertificationType], signed_raw: str
) -> CertificationType:
"""
Return Certification instance from signed raw document
:param signed_raw: Signed raw document
:return:
"""
n = 0
lines = signed_raw.splitlines(True)
version = int(Certification.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 += 5
block_id = BlockID.from_str(
Certification.parse_field("CertTimestamp", lines[n])
)
n += 1
signature = Certification.parse_field("Signature", lines[n])
identity = Identity.from_certification_raw(signed_raw)
certification = cls(
pubkey_from, identity, block_id, version=version, currency=currency
)
# return certification with signature
certification.signature = signature
return certification
@classmethod
def from_inline(
cls: Type[CertificationType],
block_hash: Optional[str],
inline: str,
version: int = VERSION,
currency: str = G1_CURRENCY_CODENAME,
) -> CertificationType:
"""
Return Certification instance from inline document
Only self.pubkey_to is populated.
You must populate self.identity with an Identity instance to use raw/sign/signed_raw methods
:param block_hash: Hash of the block
:param inline: Inline document
:param version: Document version (default=certification.VERSION)
:param currency: Currency codename (default=constants.CURRENCY_CODENAME_G1)
:return:
"""
cert_data = Certification.re_inline.match(inline)
if cert_data is None:
raise MalformedDocumentError(f"Certification ({inline})")
pubkey_from = cert_data.group(1)
pubkey_to = cert_data.group(2)
block_number = int(cert_data.group(3))
if block_number == 0 or block_hash is None:
block_id = BlockID.empty()
else:
block_id = BlockID(block_number, block_hash)
signature = cert_data.group(4)
certification = cls(
pubkey_from, pubkey_to, block_id, version=version, currency=currency
)
# return certification with signature
certification.signature = signature
return certification
def raw(self) -> str:
"""
Return a raw document of the certification
"""
if not isinstance(self.identity, Identity):
raise MalformedDocumentError(
"Can not return full certification document created from inline"
)
return f"Version: {self.version}\n\
Type: Certification\n\
Currency: {self.currency}\n\
Issuer: {self.pubkey_from}\n\
IdtyIssuer: {self.identity.pubkey}\n\
IdtyUniqueID: {self.identity.uid}\n\
IdtyTimestamp: {self.identity.block_id}\n\
IdtySignature: {self.identity.signature}\n\
CertTimestamp: {self.block_id}\n"
def sign(self, key: SigningKey) -> None:
"""
Sign the current document with the key for the certified Identity given
:param key: Libnacl key instance
"""
if not isinstance(self.identity, Identity):
raise MalformedDocumentError(
"Can not return full certification document created from inline"
)
super().sign(key)
def signed_raw(self) -> str:
"""
Return signed raw document of the certification for the certified Identity instance
:return:
"""
if not isinstance(self.identity, Identity):
raise MalformedDocumentError(
"Identity is not defined or properly defined. Can not create raw format"
)
if self.signature is None:
raise MalformedDocumentError(
"Signature is not defined, can not create signed raw format"
)
return f"{self.raw()}{self.signature}\n"
def inline(self) -> str:
"""
Return inline document string
:return:
"""
return f"{self.pubkey_from}:{self.pubkey_to}:{self.block_id.number}:{self.signature}"
|