diff --git a/ucoin/hdc/__init__.py b/ucoin/hdc/__init__.py index 18ac7c9e8699e7b69ed9d5a220f17886ca954901..efacc8ef7702dc2dc54b0ff537f4b60e6f55ca3c 100644 --- a/ucoin/hdc/__init__.py +++ b/ucoin/hdc/__init__.py @@ -22,4 +22,4 @@ class HDC(API): def __init__(self, module='hdc'): super().__init__(module) -from . import amendments, transactions +from . import amendments, coins, transactions diff --git a/ucoin/hdc/amendments/__init__.py b/ucoin/hdc/amendments/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..0ecf677d5aefb7ada332f4f98bc5844148cba871 --- /dev/null +++ b/ucoin/hdc/amendments/__init__.py @@ -0,0 +1,94 @@ +# +# 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 HDC + +class Base(HDC): + def __init__(self): + super().__init__('hdc/amendments') + +class Promoted(Base): + """GET the current promoted amendment (amendment which received enough votes to be accepted).""" + + def __init__(self, number=None): + """ + Uses number to fit the result. + + Arguments: + - `number`: amendment number + """ + + super().__init__() + + self.number = number + + def get(self): + if not self.number: + return self.requests_get('/promoted').json() + + return self.requests_get('/promoted/%d' % self.number).json() + +class Current(Promoted): + """Alias of amendments/promoted.""" + + pass + +class List(Base): + """GET the list of amendments through the previousHash value.""" + + def get(self): + """creates a generator with one amendment per iteration.""" + + current = self.requests_get('/promoted').json() + yield current + + while 'previousHash' in current and current['previousHash']: + current = self.requests_get('/promoted/%d' % (current['number']-1)).json() + yield current + +class CurrentVotes(Base): + """GET the votes that legitimate the current amendment.""" + + def get(self): + return self.merkle_easy_parser('/current/votes') + +class Votes(Base): + """GET an index of votes received by this node.""" + + def __init__(self, amendment_id=None): + """ + Uses amendment_id to fit the result. + + Arguments: + - `amendment_id`: amendment id + """ + + super().__init__() + + self.amendment_id = amendment_id + + def get(self): + if not self.amendment_id: + return self.requests_get('/votes').json() + + return self.merkle_easy_parser('/votes/%s' % self.amendment_id) + + def post(self): + pass + +from . import view diff --git a/ucoin/hdc/amendments/view.py b/ucoin/hdc/amendments/view.py new file mode 100644 index 0000000000000000000000000000000000000000..61ac7f63114f4368f43796a0b5c88c9ee058cd73 --- /dev/null +++ b/ucoin/hdc/amendments/view.py @@ -0,0 +1,50 @@ +# +# 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 HDC + +class View(HDC): + def __init__(self, amendment_id): + super().__init__('hdc/amendments/view/%s' % amendment_id) + +class Members(View): + """GET the members present in the Community for this amendment.""" + + def get(self): + return self.merkle_easy_parser('/members') + +class Self(View): + """Shows the raw data of the amendment [AMENDMENT_ID].""" + + def get(self): + return self.requests_get('/self').json() + +class Voters(View): + """GET the voters listed in this amendment.""" + + def get(self): + return self.merkle_easy_parser('/voters') + +class Signatures(View): + """GET the signatures of the Community listed in this amendment. + + This URL should give the same result as hdc/amendments/votes/[PREVIOUS_AMENDEMENT_ID] if all votes present in this URL were taken in count as signatures for this AMENDMENT_ID. + """ + + def get(self): + return self.merkle_easy_parser('/signatures') diff --git a/ucoin/hdc/amendments.py b/ucoin/hdc/coins/__init__.py similarity index 54% rename from ucoin/hdc/amendments.py rename to ucoin/hdc/coins/__init__.py index 7ff6770458867da6a1bb041687d64ebc7313c329..4725095201f3f46ef44c44039188a180351fb111 100644 --- a/ucoin/hdc/amendments.py +++ b/ucoin/hdc/coins/__init__.py @@ -16,22 +16,27 @@ # Caner Candan <caner@candan.fr>, http://caner.candan.fr # -from . import HDC +from .. import HDC -class Base(HDC): - def __init__(self): - super().__init__('hdc/amendments') +class Coins(HDC): + def __init__(self, pgp_fingerprint): + super().__init__('hdc/coins/%s' % pgp_fingerprint) -class List(Base): - """GET the list of amendments through the previousHash value.""" +class List(Coins): + """GET a list of coins owned by the given [PGP_FINGERPRINT].""" def get(self): - """creates a generator with one amendment per iteration.""" + return self.requests_get('/list').json() - current = self.requests_get('/current').json() - yield current +class View(Coins): + """GET the ownership state of the coin [COIN_NUMBER] issued by [PGP_FINGERPRINT].""" - while 'previousHash' in current and current['previousHash']: - current['previousNumber'] = current['number']-1 - current = self.requests_get('/view/%(previousNumber)d-%(previousHash)s/self' % current).json() - yield current + def __init__(self, pgp_fingerprint, coin_number): + super().__init__(pgp_fingerprint) + + self.coin_number = coin_number + + def get(self): + return self.requests_get('/view/%d' % self.coin_number).json() + +from . import view diff --git a/ucoin/hdc/transactions.py b/ucoin/hdc/coins/view.py similarity index 71% rename from ucoin/hdc/transactions.py rename to ucoin/hdc/coins/view.py index 8f25dea4104aa181a0401b0bbbe84fa5e2a692a0..f30f1c65dc9b2083989060a2533821edc970c835 100644 --- a/ucoin/hdc/transactions.py +++ b/ucoin/hdc/coins/view.py @@ -19,13 +19,11 @@ from . import HDC class Base(HDC): - def __init__(self): - super().__init__('hdc/transactions') + def __init__(self, pgp_fingerprint, coin_number): + super().__init__('hdc/coins/%s/view/%d' % (pgp_fingerprint, coin_number)) -class All(Base): - """GET all the transactions stored by this node.""" +class History(Base): + """GET a transaction history of the coin [COIN_NUMBER] issued by [PGP_FINGERPRINT].""" def get(self): - """creates a generator with one transaction per iteration.""" - - return self.merkle_easy_parser('/all') + return self.requests_get('/history').json() diff --git a/ucoin/hdc/transactions/__init__.py b/ucoin/hdc/transactions/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..c43f5bfe86324ced04c3922f485bf5a239ba175a --- /dev/null +++ b/ucoin/hdc/transactions/__init__.py @@ -0,0 +1,126 @@ +# +# 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 HDC + +class Base(HDC): + def __init__(self): + super().__init__('hdc/transactions') + +class Process(Base): + """POST a transaction.""" + + def __init__(self, transaction, signature): + """ + Arguments: + - `transaction`: The raw transaction. + - `signature`: The signature of the transaction. + """ + + super().__init__() + + self.transaction = transaction + self.signature = signature + + def post(self): + pass + +class All(Base): + """GET all the transactions stored by this node.""" + + def get(self): + """creates a generator with one transaction per iteration.""" + + return self.merkle_easy_parser('/all') + +class Keys(Base): + """GET PGP keys for which some transactions have been recoreded by this node (sent and received).""" + + def get(self): + """creates a generator with one key per iteration.""" + + return self.merkle_easy_parser('/keys') + +class Last(Base): + """GET the last received transaction.""" + + def __init__(self, count=None): + """ + Arguments: + - `count`: Integer indicating to retrieve the last [COUNT] transactions. + """ + + super().__init__() + + self.count = count + + def get(self): + if not self.count: + return self.requests_get('/last').json() + + return self.requests_get('/last/%d' % self.count).json() + +class Sender(Base): + """GET all the transactions sent by this sender and stored by this node (should contain all transactions of the sender).""" + + def __init__(self, pgp_fingerprint): + """ + Arguments: + - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions. + """ + + super().__init__() + + self.pgp_fingerprint = pgp_fingerprint + + def get(self): + return self.merkle_easy_parser('/sender/%s' % self.pgp_fingerprint) + +class Recipient(Base): + """GET all the transactions received for this recipient stored by this node.""" + + def __init__(self, pgp_fingerprint): + """ + Arguments: + - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions. + """ + + super().__init__() + + self.pgp_fingerprint = pgp_fingerprint + + def get(self): + return self.merkle_easy_parser('/recipient/%s' % self.pgp_fingerprint) + +class View(Base): + """GET the transaction of given TRANSACTION_ID.""" + + def __init__(self, transaction_id): + """ + Arguments: + - `transaction_id`: The transaction unique identifier. + """ + + super().__init__() + + self.transaction_id = transaction_id + + def get(self): + return self.requests_get('/view/%s' % self.transaction_id).json() + +from . import sender diff --git a/ucoin/hdc/transactions/sender/__init__.py b/ucoin/hdc/transactions/sender/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..5ba9249bb4eaca6cc2cc6bf7a613cc2b380f3bbe --- /dev/null +++ b/ucoin/hdc/transactions/sender/__init__.py @@ -0,0 +1,63 @@ +# +# 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 HDC + +class Base(HDC): + """Get the last received transaction of a PGP key.""" + + def __init__(self, pgp_fingerprint): + """ + Arguments: + - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions. + """ + + super().__init__('hdc/transactions/sender/%s' % pgp_fingerprint) + +class Last(Base): + """Get the last received transaction of a PGP key.""" + + def __init__(self, pgp_fingerprint, count=None): + """ + Arguments: + - `count`: Integer indicating to retrieve the last [COUNT] transactions. + """ + + super().__init__(pgp_fingerprint) + + self.count = count + + def get(self): + if not self.count: + return self.requests_get('/last').json() + + return self.requests_get('/last/%d' % self.count).json() + +class Transfer(Base): + """GET all transfer transactions sent by this sender and stored by this node (should contain all transfert transactions of the sender).""" + + def get(self): + return self.merkle_easy_parser('/transfert') + +class Issuance(Base): + """GET all issuance transactions (forged coins) sent by this sender and stored by this node (should contain all issuance transactions of the sender).""" + + def get(self): + return self.merkle_easy_parser('/issuance') + +from . import issuance diff --git a/ucoin/hdc/transactions/sender/issuance.py b/ucoin/hdc/transactions/sender/issuance.py new file mode 100644 index 0000000000000000000000000000000000000000..f7380df2623036c435861aaa4c03de5114ea5144 --- /dev/null +++ b/ucoin/hdc/transactions/sender/issuance.py @@ -0,0 +1,61 @@ +# +# 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 HDC + +class Base(HDC): + """Get the received issuance transaction of a PGP key.""" + + def __init__(self, pgp_fingerprint): + """ + Arguments: + - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions. + """ + + super().__init__('hdc/transactions/sender/%s/issuance' % pgp_fingerprint) + +class Last(Base): + """GET the last received issuance transaction of a PGP key.""" + + def get(self): + return self.requests_get('/last').json() + +class Fusion(Base): + """GET all fusion transactions sent by this sender and stored by this node (should contain all fusion transactions of the sender).""" + + def get(self): + return self.merkle_easy_parser('/fusion') + +class Dividend(Base): + """GET all dividend transactions (issuance of new coins) sent by this sender and stored by this node (should contain all dividend transactions of the sender).""" + + def __init__(self, pgp_fingerprint, am_number=None): + """ + Arguments: + - `am_number`: amendment number + """ + + super().__init__(pgp_fingerprint) + + self.am_number = am_number + + def get(self): + if not self.am_number: + return self.merkle_easy_parser('/dividend') + + return self.merkle_easy_parser('/dividend/%d' % self.am_number)