diff --git a/ucoinpy/documents/__init__.py b/ucoinpy/documents/__init__.py index 0fe4cc12ddd034b516ba535eee18d8414a236cae..d8d1a59a056c7e17dd43425075ba9ab0d304658d 100644 --- a/ucoinpy/documents/__init__.py +++ b/ucoinpy/documents/__init__.py @@ -4,11 +4,13 @@ Created on 3 déc. 2014 @author: inso ''' import base58 -import time +import re from ..key import Base58Encoder class Document: + RE_VERSION = re.compile("Version: ([0-9]+)\n") + def __init__(self, version): self.version = version diff --git a/ucoinpy/documents/block.py b/ucoinpy/documents/block.py index c3fc78a82a51d300d7721f588d50034d7825c31f..1f02736ec9e02551d5bd515fa655a2756e3a7161 100644 --- a/ucoinpy/documents/block.py +++ b/ucoinpy/documents/block.py @@ -6,6 +6,11 @@ Created on 2 déc. 2014 from .. import PROTOCOL_VERSION from . import Document +from .certification import SelfCertification, Certification +from .membership import Membership +from .transaction import Transaction + +import re class Block(Document): @@ -48,7 +53,27 @@ COMPACT_TRANSACTION BOTTOM_SIGNATURE ''' - def __init__(self, currency, noonce, number, powmin, time, + re_currency = re.compile("Currency: ([0-9a-zA-Z_\-]+)\n") + re_noonce = re.compile("Nonce: ([0-9]+)\n") + re_number = re.compile("Number: ([0-9]+)\n") + re_powmin = re.compile("PoWMin: ([0-9]+)\n") + re_time = re.compile("Time: ([0-9]+)\n") + re_mediantime = re.compile("MedianTime: ([0-9]+)\n") + re_universaldividend = re.compile("UniversalDividend: ([0-9]+)\n") + re_issuer = re.compile("Issuer: ([1-9A-Za-z][^OIl]{43,45})\n") + re_previoushash = re.compile("PreviousHash: ([0-9a-fA-F]{5,40})\n") + re_previousissuer = re.compile("PreviousIssuer: ([1-9A-Za-z][^OIl]{43,45})\n") + re_parameters = re.compile("Parameters: ([0-9]+\.[0-9]+)(:[0-9]+){10}\n") + re_memberscount = re.compile("MembersCount: ([0-9]+)\n") + re_identities = re.compile("Identities:\n") + re_joiners = re.compile("Joiners:\n") + re_actives = re.compile("Actives:\n") + re_leavers = re.compile("Leavers:\n") + re_excluded = re.compile("Excluded:\n") + re_certifications = re.compile("Certifications:\n") + re_transactions = re.compile("Transactions:\n") + + def __init__(self, version, currency, noonce, number, powmin, time, mediantime, ud, issuer, prev_hash, prev_issuer, parameters, members_count, identities, joiners, actives, leavers, excluded, certifications, @@ -56,6 +81,7 @@ BOTTOM_SIGNATURE ''' Constructor ''' + super(version) self.currency = currency self.noonce = noonce self.number = number @@ -75,67 +101,114 @@ BOTTOM_SIGNATURE self.excluded = excluded self.certifications = certifications self.transactions = transactions - + @classmethod def from_raw(cls, raw): #TODO : Parsing lines = raw.splitlines(True) - + n = 0 - version_re = re.compile("Version: ([0-9]+)\n") - version = version_re.match(lines[n]) + + version = Block.RE_VERSION.match(lines[n]) n = 1 - currency_re = re.compile("Currency: ([0-9a-zA-Z_\-]+)\n" - currency = currency_re.match(lines[n]) - + + currency = Block.re_currency.match(lines[n]) n = 2 - noonce_re = re.compile("Nonce: ([0-9]+)\n") - noonce = nonce_re.match(lines[n]) - + + noonce = int(Block.re_noonce.match(lines[n])) n = 3 - number_re = re.compile("Number: ([0-9]+)\n") - number = number_re.match(lines[n]) - + + number = int(Block.re_number.match(lines[n])) n = 4 - powmin_re = re.compile("PoWMin: ([0-9]+)\n") - powmin = powmin.match(lines[n]) - + + powmin = int(Block.re_powmin.match(lines[n])) n = 5 - time_re = re.compile("Time: ([0-9]+)\n") - time = time.match(lines[n]) - + + time = int(Block.re_time.match(lines[n])) n = 6 - mediantime_re = re.compile("MedianTime: ([0-9]+)\n") - mediantime = mediantime_re.match(line[n]) - + + mediantime = int(Block.re_mediantime.match(lines[n])) n = 7 - ud_re = re.compile("UniversalDividend: ([0-9]+)\n") - ud = ud_re.match(line[n]) - - n = 8 - issuer_re = re.compile("Issuer: ([1-9A-Za-z][^OIl]{43,45})\n") - issuer = issuer_re.match(line[n]) - - n = 9 - previoushash_re = re.compile("PreviousHash: ([0-9a-fA-F]{5,40})\n") - prev_hash = previoushash_re.match(line[n]) - - n = 10 - previousissuer_re = re.compile("PreviousIssuer: ([1-9A-Za-z][^OIl]{43,45})\n") - prev_issuer = previousissuer_re.match(line[n]) - - parameters = "" - members_count = "" - identities = "" - joiners = "" - actives = "" - leavers = "" - excluded = "" - certifications = "" - transactions = "" - - return cls([]) - + + ud = Block.re_universaldividend.match(lines[n]) + if ud is not None: + ud = int(ud) + n = n + 1 + + issuer = Block.re_issuer.match(lines[n]) + n = n + 1 + + prev_hash = Block.re_previoushash.match(lines[n]) + n = n + 1() + + prev_issuer = Block.re_previousissuer.match(lines[n]) + n = n + 1 + + if number == 0: + parameters = Block.re_parameters.match(lines[n]) + n = n + 1 + + members_count = int(Block.re_memberscount.match(lines[n])) + n = n + 1 + + identities = [] + joiners = [] + actives = [] + leavers = [] + excluded = [] + certifications = [] + transactions = [] + + if Block.re_identities.match(lines[n]) is not None: + while Block.re_joiners.match(lines[n]) is None: + selfcert = SelfCertification.from_inline(lines[n]) + if selfcert is None: + return None + else: + identities.append(selfcert) + lines = lines + 1 + + if Block.re_joiners.match(lines[n]) is not None: + while Block.re_actives.match(lines[n]) is None: + membership = Membership.from_inline(lines[n]) + joiners.append(membership) + lines = lines + 1 + + if Block.re_actives.match(lines[n]) is not None: + while Block.re_leavers.match(lines[n]) is None: + membership = Membership.from_inline(lines[n]) + actives.append(membership) + lines = lines + 1 + + if Block.re_leavers.match(lines[n]) is not None: + while Block.re_excluded.match(lines[n]) is None: + membership = Membership.from_inline(lines[n]) + leavers.append(membership) + lines = lines + 1 + + if Block.re_excluded.match(lines[n]) is not None: + while Block.re_certifications.match(lines[n]) is None: + membership = Membership.from_inline(lines[n]) + excluded.append(membership) + lines = lines + 1 + + if Block.re_certifications.match(lines[n]) is not None: + while Block.re_transactions.match(lines[n]) is None: + certification = Certification.from_inline(lines[n]) + certifications.append(certification) + lines = lines + 1 + + if Block.re_transactions.match(lines[n]) is not None: + while Block.re_transactions.match(lines[n]) is None: + transaction = Transaction.from_compact(lines[n]) + transactions.append(transaction) + lines = lines + 1 + + return cls(version, currency, noonce, number, powmin, time, + mediantime, ud, issuer, prev_hash, prev_issuer, + parameters, members_count, identities, joiners, + actives, leavers, excluded, certifications, transactions) + def content(self): doc = """ Version: {0} diff --git a/ucoinpy/documents/certification.py b/ucoinpy/documents/certification.py index fc8d9461afa2d5cc5c2de17745358c5f7efc4ada..c9cdca44c454daad1e39242a409f776e852d13c3 100644 --- a/ucoinpy/documents/certification.py +++ b/ucoinpy/documents/certification.py @@ -11,18 +11,22 @@ class SelfCertification(Document): A document discribing a self certification. ''' def __init__(self, ts, identifier): - super(ts) self.identifier = identifier - self.timestamp = timestamp - + self.timestamp = ts + + @classmethod + def from_inline(cls, inline): + #TODO : Parsing + return cls() + @classmethod def from_raw(cls, raw): #TODO : Parsing return cls() - def ts(self): + def ts(self): return "META:TS:{0}".format(self.timestamp) - + def uid(self): return "UID:{0}".format(self.identifier) @@ -42,7 +46,10 @@ class Certification(Document): self.selfcert = selfcert self.blockid = blockid - + @classmethod + def from_inline(cls, inline): + return cls() + @classmethod def from_raw(cls, raw): #TODO : Parsing diff --git a/ucoinpy/documents/membership.py b/ucoinpy/documents/membership.py index 05ddb7df53b4ccc23df7279a6fe05afbcf19ef23..fa76d4280e354e8f039e94072ece3783d01645c5 100644 --- a/ucoinpy/documents/membership.py +++ b/ucoinpy/documents/membership.py @@ -32,14 +32,16 @@ class Membership(object): self.userid = userid self.cert_ts = cert_ts - + @classmethod + def from_inline(cls, inline): + #TODO: Parsing + return None + @classmethod def from_raw(cls, raw): #TODO : Parsing return cls() - - def content(self): return """ Version: {0} diff --git a/ucoinpy/documents/transaction.py b/ucoinpy/documents/transaction.py index beaf476469305ad99bbc9f2edc88c06c3cdb49dd..ee5a824bef806e643243815f7bbaaca0157f5b1f 100644 --- a/ucoinpy/documents/transaction.py +++ b/ucoinpy/documents/transaction.py @@ -37,6 +37,10 @@ SIGNATURES self.outputs = outputs self.comment = comment + @classmethod + def from_compact(cls, compact): + return None + @classmethod def from_raw(cls, raw): #TODO : Parsing