From 9df7c70720faab20d1fb56148cfba4127098c20b Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 28 Sep 2016 12:03:54 +0200 Subject: [PATCH] Add basic Asset model --- transaction.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/transaction.py b/transaction.py index 6ac39b2f..cf0d3e94 100644 --- a/transaction.py +++ b/transaction.py @@ -10,7 +10,8 @@ from cryptoconditions.exceptions import ParsingError from bigchaindb_common.crypto import SigningKey, hash_data from bigchaindb_common.exceptions import (KeypairMismatchException, - InvalidHash, InvalidSignature) + InvalidHash, InvalidSignature, + AmountError) from bigchaindb_common.util import serialize, gen_timestamp @@ -236,6 +237,45 @@ class Condition(object): return cls(fulfillment, cond['owners_after']) +class Asset(object): + def __init__(self, data=None, data_id=None, divisible=False, + updatable=False, refillable=False): + if data is not None and not isinstance(data, dict): + raise TypeError('`data` must be a dict instance or None') + else: + self.data = data + + # TODO: Add ID method here I guess + self.data_id = data_id if data_id is not None else self.to_hash() + self.divisible = divisible + self.updatable = updatable + self.refillable = refillable + + def __eq__(self, other): + try: + other_dict = other.to_dict() + except AttributeError: + return False + return self.to_dict() == other_dict + + def to_dict(self): + return { + 'id': self.data_id, + 'divisible': self.divisible, + 'updatable': self.updatable, + 'refillable': self.refillable, + 'data': self.data, + } + + @classmethod + def from_dict(cls, asset): + return cls(asset['data'], asset['id'], asset['divisible'], + asset['updatable'], asset['refillable']) + + def to_hash(self): + return str(uuid4()) + + class Metadata(object): def __init__(self, data=None, data_id=None): if data_id is not None: