diff --git a/src/cutecoin/gui/mainWindow.py b/src/cutecoin/gui/mainWindow.py index 97e32f8296d6ee23dbbebaf774606aae0c0f07ea..84ae40b45a39918c9ec4124fb8f8c7dc52d3ea59 100644 --- a/src/cutecoin/gui/mainWindow.py +++ b/src/cutecoin/gui/mainWindow.py @@ -82,9 +82,12 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.label_account_name.setText( "Current account : " + self.core.current_account.name) - self.list_wallets.setModel( - WalletsListModel( - self.core.current_account)) + + for wallet in self.core.current_account.wallets: + wallet.refresh_coins() + + wallets_list_model = WalletsListModel(self.core.current_account) + self.list_wallets.setModel(wallets_list_model) self.tabs_communities.clear() for community in self.core.current_account.communities: diff --git a/src/cutecoin/gui/processConfigureCommunity.py b/src/cutecoin/gui/processConfigureCommunity.py index 307d78ab4a5dc71ce3f3971da8382e19ab34fe74..4e5e60a489ed7864ac08ad42cf438839f2669684 100644 --- a/src/cutecoin/gui/processConfigureCommunity.py +++ b/src/cutecoin/gui/processConfigureCommunity.py @@ -185,6 +185,7 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog): menu.exec_(self.mapToGlobal(point)) def accept(self): + #TODO: Push wht only if changed for wallet in self.account.wallets: wallet.push_wht() diff --git a/src/cutecoin/models/account/__init__.py b/src/cutecoin/models/account/__init__.py index fb0da9edc3c965fde9a98e28547d53f932c57a1b..f34f993b9816325cbcb61358b35dda5e94f0b3c1 100644 --- a/src/cutecoin/models/account/__init__.py +++ b/src/cutecoin/models/account/__init__.py @@ -13,7 +13,6 @@ from cutecoin.models.account.communities import Communities from cutecoin.models.community import Community from cutecoin.models.transaction import Transaction from cutecoin.models.person import Person -from cutecoin.tools.exceptions import CommunityNotFoundError class Account(object): @@ -113,11 +112,8 @@ class Account(object): return sent def quality(self, community): - if community in self.communities: - wallets = self.wallets.community_wallets(community.currency) - return community.person_quality(wallets, self.fingerprint()) - else: - raise CommunityNotFoundError(self.keyid, community.amendment_id()) + wallets = self.wallets.community_wallets(community.currency) + return community.person_quality(wallets, self.fingerprint()) def jsonify_contacts(self): data = [] diff --git a/src/cutecoin/models/coin/__init__.py b/src/cutecoin/models/coin/__init__.py index 61c7a7ebec807c2097946502270a768e92a22482..22407fbd85a8491de9487b6ab8291cc2693bdd2e 100644 --- a/src/cutecoin/models/coin/__init__.py +++ b/src/cutecoin/models/coin/__init__.py @@ -7,6 +7,7 @@ Created on 2 févr. 2014 import re import math import logging +import ucoin class Coin(object): @@ -15,34 +16,30 @@ class Coin(object): A coin parsing a regex to read its value ''' - def __init__(self, issuer, number, base, power, origin): + def __init__(self, issuer, am_number, coin_number): self.issuer = issuer - self.number = number - self.base = base - self.power = power - self.origin = origin + self.am_number = am_number + self.coin_number = coin_number @classmethod def from_id(cls, coin_id): # Regex to parse the coin id - regex = "^([A-Z\d]{40})-(\d+)-(\d)-(\d+)-((A|F|D)-\d+)$" + regex = "^([A-Z\d]{40})-(\d+)-(\d+)$" m = re.search(regex, coin_id) issuer = m.group(1) - number = int(m.group(2)) - base = int(m.group(3)) - power = int(m.group(4)) - origin = m.group(5) - return cls(issuer, number, base, power, origin) + am_number = int(m.group(2)) + power = int(m.group(3)) + return cls(issuer, am_number, power) def __eq__(self, other): return self.get_id() == other.get_id() - def value(self): - return self.base * math.pow(10, self.power) + def value(self, wallet): + amendment_request = ucoin.hdc.amendments.view.Self(self.am_number) + amendment = wallet.request(amendment_request) + return wallet.coin_algo(amendment, self.coin_number) def get_id(self): return self.issuer + "-" \ - + str(self.number) + "-" \ - + str(self.base) + "-" \ - + str(self.power) + "-" \ - + self.origin + + str(self.am_number) + "-" \ + + str(self.coin_number) diff --git a/src/cutecoin/models/coin/algorithms.py b/src/cutecoin/models/coin/algorithms.py new file mode 100644 index 0000000000000000000000000000000000000000..13a4c7d4bf2911f9f984addd276554003a7b84dd --- /dev/null +++ b/src/cutecoin/models/coin/algorithms.py @@ -0,0 +1,35 @@ +''' +Created on 21 mai 2014 + +@author: inso +''' + +import math + +class Algorithm(object): + ''' + classdocs + ''' + def __init__(self, parameters): + ''' + Constructor + ''' + self.parameters = parameters + + def coin_value(self, amendement, coin_number): + pass + + +class Base2Draft(Algorithm): + + def __init__(self, parameters): + super.__init__(parameters) + + def coin_value(self, amendment, coin_number): + coin_base = amendment['CoinBase'] + coins_list = amendment['CoinsList'] + i = 0 + while coin_number > coins_list[i]: + coin_number -= coins_list[i] + i += 1 + return math.pow(2, coin_base + i) diff --git a/src/cutecoin/models/wallet/__init__.py b/src/cutecoin/models/wallet/__init__.py index b3078a1c450e665e574453d5c15f23b9bf9241ec..18d33999936ebcc134eb868c7485232d91b96ca1 100644 --- a/src/cutecoin/models/wallet/__init__.py +++ b/src/cutecoin/models/wallet/__init__.py @@ -9,10 +9,12 @@ import logging import gnupg import json import time +import importlib from cutecoin.models.coin import Coin +from cutecoin.models.coin import algorithms from cutecoin.models.node import Node from cutecoin.models.transaction import Transaction -from cutecoin.tools.exceptions import CommunityNotFoundError +from cutecoin.tools.exceptions import AlgorithmNotImplemented class Wallet(object): @@ -33,6 +35,7 @@ class Wallet(object): self.name = name self.nodes = nodes self.required_trusts = required_trusts + self.coin_algo = None @classmethod def create(cls, keyid, currency, node, required_trusts, name): @@ -62,9 +65,28 @@ class Wallet(object): def value(self): value = 0 for coin in self.coins: - value += coin.value() + value += coin.value(self.coin_algo) return value + def refresh_coins(self): + coins_list_request = ucoin.hdc.coins.List(self.fingerprint()) + data = self.request(coins_list_request) + for coin_data in data['coins']: + coin = Coin.from_id(coin_data) + self.coins.append(coin) + node_parameters = self.request(ucoin.registry.Parameters()) + + if 'CoinAlgo' in node_parameters: + coin_algo_name = node_parameters['CoinAlgo'] + else: + coin_algo_name = 'Base2Draft' + + try: + module = importlib.import_module("cutecoin.models.coin.algorithm") + self.coin_algo = module.getattr(module, coin_algo_name) + except AttributeError: + raise AlgorithmNotImplemented(coin_algo_name) + def transactions_received(self): received = [] transactions_data = self.request( @@ -208,7 +230,7 @@ Hosters: def jsonify(self): return {'coins': self.jsonify_coins_list(), - 'fingerprint': self.fingerprint(), + 'keyid': self.keyid, 'name': self.name, 'currency': self.currency, 'nodes': self.jsonify_nodes_list()} diff --git a/src/cutecoin/tools/exceptions.py b/src/cutecoin/tools/exceptions.py index eaca9d739c2232e442f6a50d39f33faeb08d77a9..96403b03891404f2fe39c9a24d7c718ccaefe454 100644 --- a/src/cutecoin/tools/exceptions.py +++ b/src/cutecoin/tools/exceptions.py @@ -49,19 +49,18 @@ class PersonNotFoundError(Error): " not found ") -class CommunityNotFoundError(Error): +class AlgorithmNotImplemented(Error): ''' - Exception raised when looking for community in an account list + Exception raised when a coin uses an algorithm not known ''' - def __init__(self, keyid, amendmentid): + def __init__(self, algo_name): ''' Constructor ''' - super(CommunityNotFoundError, self) \ - .__init("Community with amendment " + amendmentid - + " not found in account " + keyid) + super(AlgorithmNotImplemented, self) \ + .__init("Algorithm " + algo_name + " not implemented.") class KeyAlreadyUsed(Error):