From 7a4639c011703c3eafd80b62d6ae66374f25ea01 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Tue, 7 Jul 2015 19:21:32 +0200 Subject: [PATCH] Upgrade to last version --- ucoinpy/__init__.py | 2 ++ ucoinpy/api/bma/__init__.py | 2 +- ucoinpy/api/bma/blockchain/__init__.py | 9 +++++- ucoinpy/api/bma/node/__init__.py | 37 +++++++++++++++++++++ ucoinpy/api/bma/wot/__init__.py | 10 ++++++ ucoinpy/documents/__init__.py | 12 +++---- ucoinpy/documents/block.py | 18 +++++------ ucoinpy/documents/certification.py | 45 ++++++++++++++++++++------ ucoinpy/documents/membership.py | 12 +++---- ucoinpy/documents/peer.py | 4 +-- ucoinpy/documents/status.py | 16 ++++----- ucoinpy/documents/transaction.py | 35 ++++++++++---------- ucoinpy/key/__init__.py | 4 +-- ucoinpy/key/hdwallet.py | 8 ++--- 14 files changed, 148 insertions(+), 66 deletions(-) create mode 100644 ucoinpy/api/bma/node/__init__.py diff --git a/ucoinpy/__init__.py b/ucoinpy/__init__.py index 354362cd..3b1345bc 100644 --- a/ucoinpy/__init__.py +++ b/ucoinpy/__init__.py @@ -19,3 +19,5 @@ PROTOCOL_VERSION="1" MANAGED_API=["BASIC_MERKLED_API"] + +from . import api, documents, key \ No newline at end of file diff --git a/ucoinpy/api/bma/__init__.py b/ucoinpy/api/bma/__init__.py index 768bd447..d126a82a 100644 --- a/ucoinpy/api/bma/__init__.py +++ b/ucoinpy/api/bma/__init__.py @@ -141,4 +141,4 @@ class API(object): for leaf in root['leaves'][begin:end]: yield self.requests_get(path, leaf=leaf).json()['leaf'] -from . import network, blockchain, tx, wot +from . import network, blockchain, tx, wot, node diff --git a/ucoinpy/api/bma/blockchain/__init__.py b/ucoinpy/api/bma/blockchain/__init__.py index 03890265..f594d7f1 100644 --- a/ucoinpy/api/bma/blockchain/__init__.py +++ b/ucoinpy/api/bma/blockchain/__init__.py @@ -34,13 +34,20 @@ class Parameters(Blockchain): class Membership(Blockchain): - """POST a Membership document.""" + """GET/POST a Membership document.""" + def __init__(self, connection_handler, search=None): + super().__init__(connection_handler) + self.search = search def __post__(self, **kwargs): assert 'membership' in kwargs return self.requests_post('/membership', **kwargs).json() + def __get__(self, **kwargs): + assert self.search is not None + return self.requests_get('/memberships/%s' % self.search, **kwargs).json() + class Block(Blockchain): """GET/POST a block from/to the blockchain.""" diff --git a/ucoinpy/api/bma/node/__init__.py b/ucoinpy/api/bma/node/__init__.py new file mode 100644 index 00000000..fac217c6 --- /dev/null +++ b/ucoinpy/api/bma/node/__init__.py @@ -0,0 +1,37 @@ +# +# This program 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. +# +# This program 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/>. +# +# Authors: +# Caner Candan <caner@candan.fr>, http://caner.candan.fr +# + +from .. import API, logging + +logger = logging.getLogger("ucoin/node") + +class Node(API): + def __init__(self, connection_handler, module='node'): + super(Node, self).__init__(connection_handler, module) + + +class Summary(Node): + """GET Certification data over a member.""" + + def __init__(self, connection_handler, module='node'): + super(Summary, self).__init__(connection_handler, module) + + + def __get__(self, **kwargs): + return self.requests_get('/summary', **kwargs).json() + diff --git a/ucoinpy/api/bma/wot/__init__.py b/ucoinpy/api/bma/wot/__init__.py index 0de3bfc8..ea9b9a23 100644 --- a/ucoinpy/api/bma/wot/__init__.py +++ b/ucoinpy/api/bma/wot/__init__.py @@ -37,6 +37,16 @@ class Add(WOT): return self.requests_post('/add', **kwargs).json() +class Revoke(WOT): + """POST Public key data.""" + + def __post__(self, **kwargs): + assert 'pubkey' in kwargs + assert 'self_' in kwargs + + return self.requests_post('/revoke', **kwargs).json() + + class Lookup(WOT): """GET Public key data.""" diff --git a/ucoinpy/documents/__init__.py b/ucoinpy/documents/__init__.py index 198b62b4..9763c366 100644 --- a/ucoinpy/documents/__init__.py +++ b/ucoinpy/documents/__init__.py @@ -1,8 +1,8 @@ -''' +""" Created on 3 déc. 2014 @author: inso -''' +""" import base58 import base64 import re @@ -23,10 +23,10 @@ class Document: self.signatures = [] def sign(self, keys): - ''' + """ Sign the current document. Warning : current signatures will be replaced with the new ones. - ''' + """ self.signatures = [] for key in keys: signing = base64.b64encode(key.signature(bytes(self.raw(), 'ascii'))) @@ -34,10 +34,10 @@ class Document: self.signatures.append(signing.decode("ascii")) def signed_raw(self): - ''' + """ If keys are None, returns the raw + current signatures If keys are present, returns the raw signed by these keys - ''' + """ raw = self.raw() signed = "\n".join(self.signatures) signed_raw = raw + signed + "\n" diff --git a/ucoinpy/documents/block.py b/ucoinpy/documents/block.py index 6a023cd6..a02a550d 100644 --- a/ucoinpy/documents/block.py +++ b/ucoinpy/documents/block.py @@ -1,8 +1,8 @@ -''' +""" Created on 2 déc. 2014 @author: inso -''' +""" from .. import PROTOCOL_VERSION from . import Document @@ -15,7 +15,7 @@ import logging class Block(Document): - ''' + """ Version: VERSION Type: Block Currency: CURRENCY @@ -52,7 +52,7 @@ Transactions: COMPACT_TRANSACTION ... BOTTOM_SIGNATURE - ''' + """ re_type = re.compile("Type: (Block)\n") re_noonce = re.compile("Nonce: ([0-9]+)\n") @@ -82,9 +82,9 @@ BOTTOM_SIGNATURE parameters, members_count, identities, joiners, actives, leavers, excluded, certifications, transactions, signature): - ''' + """ Constructor - ''' + """ super().__init__(version, currency, [signature]) self.noonce = noonce self.number = number @@ -224,7 +224,7 @@ BOTTOM_SIGNATURE for i in range(n, tx_max): tx_lines += lines[n] n = n + 1 - transaction = Transaction.from_compact(version, tx_lines) + transaction = Transaction.from_compact(currency, tx_lines) transactions.append(transaction) signature = Block.re_signature.match(lines[n]).group(1) @@ -291,6 +291,6 @@ PreviousIssuer: {1}\n".format(self.prev_hash, self.prev_issuer) doc += "Transactions:\n" for transaction in self.transactions: - doc += "{0}\n".format(transaction.inline()) + doc += "{0}\n".format(transaction.compact()) - return doc + return doc \ No newline at end of file diff --git a/ucoinpy/documents/certification.py b/ucoinpy/documents/certification.py index ce5eff0e..d7e79adb 100644 --- a/ucoinpy/documents/certification.py +++ b/ucoinpy/documents/certification.py @@ -1,8 +1,8 @@ -''' +""" Created on 2 déc. 2014 @author: inso -''' +""" import re import base64 import logging @@ -11,9 +11,9 @@ from . import Document class SelfCertification(Document): - ''' + """ A document discribing a self certification. - ''' + """ re_inline = re.compile("([1-9A-Za-z][^OIl]{42,45}):([A-Za-z0-9+/]+(?:=|==)?):([0-9]+):([^\n]+)\n") re_uid = re.compile("UID:([^\n]+)\n") @@ -48,9 +48,9 @@ META:TS:{1} class Certification(Document): - ''' + """ A document describing a certification. - ''' + """ re_inline = re.compile("([1-9A-Za-z][^OIl]{42,45}):\ ([1-9A-Za-z][^OIl]{42,45}):([0-9]+):([A-Za-z0-9+/]+(?:=|==)?)\n") @@ -58,9 +58,9 @@ class Certification(Document): def __init__(self, version, currency, pubkey_from, pubkey_to, blocknumber, blockhash, signature): - ''' + """ Constructor - ''' + """ super().__init__(version, currency, [signature]) self.pubkey_from = pubkey_from self.pubkey_to = pubkey_to @@ -85,10 +85,10 @@ class Certification(Document): def sign(self, selfcert, keys): - ''' + """ Sign the current document. Warning : current signatures will be replaced with the new ones. - ''' + """ self.signatures = [] for key in keys: signing = base64.b64encode(key.signature(bytes(self.raw(selfcert), 'ascii'))) @@ -105,3 +105,28 @@ class Certification(Document): return "{0}:{1}:{2}:{3}".format(self.pubkey_from, self.pubkey_to, self.blocknumber, self.signatures[0]) + +class Revocation(Document): + """ + A document describing a self-revocation. + """ + def __init__(self, version, currency, signature): + """ + Constructor + """ + super().__init__(version, currency, [signature]) + + def raw(self, selfcert): + return """{0}META:REVOKE +""".format(selfcert.signed_raw()) + + def sign(self, selfcert, keys): + """ + Sign the current document. + Warning : current signatures will be replaced with the new ones. + """ + self.signatures = [] + for key in keys: + signing = base64.b64encode(key.signature(bytes(self.raw(selfcert), 'ascii'))) + self.signatures.append(signing.decode("ascii")) + diff --git a/ucoinpy/documents/membership.py b/ucoinpy/documents/membership.py index af71a3a0..570110e2 100644 --- a/ucoinpy/documents/membership.py +++ b/ucoinpy/documents/membership.py @@ -1,8 +1,8 @@ -''' +""" Created on 2 déc. 2014 @author: inso -''' +""" from .. import PROTOCOL_VERSION from . import Document @@ -10,7 +10,7 @@ import re class Membership(Document): - ''' + """ This is a utility class to generate membership documents : Version: VERSION Type: Membership @@ -20,7 +20,7 @@ class Membership(Document): Membership: MEMBERSHIP_TYPE UserID: USER_ID CertTS: CERTIFICATION_TS - ''' + """ # PUBLIC_KEY:SIGNATURE:NUMBER:HASH:TIMESTAMP:USER_ID re_inline = re.compile("([1-9A-Za-z][^OIl]{42,45}):([A-Za-z0-9+/]+(?:=|==)?):\ @@ -36,9 +36,9 @@ class Membership(Document): def __init__(self, version, currency, issuer, block_number, block_hash, membership_type, uid, cert_ts, signature): - ''' + """ Constructor - ''' + """ super().__init__(version, currency, [signature]) self.issuer = issuer self.block_number = block_number diff --git a/ucoinpy/documents/peer.py b/ucoinpy/documents/peer.py index 6b437a6d..6ca7606c 100644 --- a/ucoinpy/documents/peer.py +++ b/ucoinpy/documents/peer.py @@ -1,8 +1,8 @@ -''' +""" Created on 2 déc. 2014 @author: inso -''' +""" import re diff --git a/ucoinpy/documents/status.py b/ucoinpy/documents/status.py index 2ba47ea8..f11e7542 100644 --- a/ucoinpy/documents/status.py +++ b/ucoinpy/documents/status.py @@ -1,15 +1,15 @@ -''' +""" Created on 2 déc. 2014 @author: inso -''' +""" import re from . import Document class Status(Document): - ''' + """ Version: VERSION Type: Status Currency: CURRENCY_NAME @@ -17,7 +17,7 @@ class Status(Document): Block: BLOCK From: SENDER To: RECIPIENT - ''' + """ re_type = re.compile("Type: (Status)") re_status = re.compile("Status: (NEW|NEW_BACK|UP|UP_BACK|DOWN)") @@ -27,9 +27,9 @@ class Status(Document): def __init__(self, version, currency, status, blockid, sender, recipient, signature): - ''' + """ Constructor - ''' + """ super().__init__(version, currency, [signature]) self.status = status @@ -70,12 +70,12 @@ class Status(Document): sender, recipient, signature) def raw(self): - return '''Version: {0} + return """Version: {0} Type: Status Currency: {1} Status: {2} Block: {3} From: {4} To: {5} -'''.format(self.version, self.currency, self.status, +""".format(self.version, self.currency, self.status, self.blockid, self.sender, self.recipient) diff --git a/ucoinpy/documents/transaction.py b/ucoinpy/documents/transaction.py index cd42c610..0a9d40d2 100644 --- a/ucoinpy/documents/transaction.py +++ b/ucoinpy/documents/transaction.py @@ -1,14 +1,15 @@ -''' +""" Created on 2 déc. 2014 @author: inso -''' +""" from . import Document import re +import logging class Transaction(Document): - ''' + """ Document format : Version: VERSION Type: Transaction @@ -37,10 +38,10 @@ PUBLIC_KEY:AMOUNT COMMENT SIGNATURE ... - ''' + """ re_type = re.compile("Type: (Transaction)\n") - re_header = re.compile("TX:([0-9])+:([0-9])+:([0-9])+:([0-9])+:(0|1)\n") + re_header = re.compile("TX:([0-9]+):([0-9]+):([0-9]+):([0-9]+):(0|1)\n") re_issuers = re.compile("Issuers:\n") re_inputs = re.compile("Inputs:\n") re_outputs = re.compile("Outputs:\n") @@ -50,9 +51,9 @@ SIGNATURE def __init__(self, version, currency, issuers, inputs, outputs, comment, signatures): - ''' + """ Constructor - ''' + """ super().__init__(version, currency, signatures) self.issuers = issuers @@ -180,9 +181,9 @@ Issuers: return doc def compact(self): - ''' + """ Return a transaction in its compact format. - ''' + """ """TX:VERSION:NB_ISSUERS:NB_INPUTS:NB_OUTPUTS:HAS_COMMENT PUBLIC_KEY:INDEX ... @@ -212,26 +213,26 @@ COMMENT class SimpleTransaction(Transaction): - ''' + """ As transaction class, but for only one issuer. ... - ''' + """ def __init__(self, version, currency, issuer, single_input, outputs, comment, signature): - ''' + """ Constructor - ''' + """ super().__init__(version, currency, [issuer], [single_input], outputs, comment, [signature]) class InputSource(): - ''' + """ A Transaction INPUT Compact : INDEX:SOURCE:FINGERPRINT:AMOUNT - ''' + """ re_inline = re.compile("([0-9]+):(D|T):([0-9]+):\ ([0-9a-fA-F]{5,40}):([0-9]+)\n") re_compact = re.compile("([0-9]+):(D|T):([0-9a-fA-F]{5,40}):([0-9]+)\n") @@ -277,9 +278,9 @@ class InputSource(): class OutputSource(): - ''' + """ A Transaction OUTPUT - ''' + """ re_inline = re.compile("([1-9A-Za-z][^OIl]{42,45}):([0-9]+)") def __init__(self, pubkey, amount): diff --git a/ucoinpy/key/__init__.py b/ucoinpy/key/__init__.py index 191bf378..f0813ef8 100644 --- a/ucoinpy/key/__init__.py +++ b/ucoinpy/key/__init__.py @@ -1,8 +1,8 @@ -''' +""" Ucoin public and private keys @author: inso -''' +""" import base58 import base64 diff --git a/ucoinpy/key/hdwallet.py b/ucoinpy/key/hdwallet.py index dffb5fff..1a135d7d 100644 --- a/ucoinpy/key/hdwallet.py +++ b/ucoinpy/key/hdwallet.py @@ -1,9 +1,9 @@ -''' +""" HD Wallet inspired from Bip32 wallets. @author: inso -''' -''' +""" +""" import os import hmac import hashlib @@ -375,4 +375,4 @@ if __name__ == "__main__": print "* [Chain m/0/2147483647h/1/2147483646h/2]" m = m.ChildKey(2) m.dump() -''' \ No newline at end of file +""" \ No newline at end of file -- GitLab