def get_money(self, public_key): """ Determines if the owner of this public key has gets anything from this transaction. :param public_key: the public key of the owner to check. :return: int, returns 0 it is not his transaction, negative number if he is the sender and positive number if he is a recipient. """ total = 0 if hash_it(self.public_key) == hash_it(public_key): total -= self.value elif hash_it(public_key) == self.recipient: total += self.value return total
def hash(self): """ Hashes the transaction. :return: the hash of this transaction. """ header = self.recipient + str(self.value) + str(self.public_key) + str( self.signature) return hash_it(header)
def __init__(self, blockchain, private_key=None): if not private_key: self.public_key, self.__private_key = rsa.newkeys(512) else: self.public_key = rsa.PublicKey(private_key[0], private_key[1]) self.__private_key = rsa.PrivateKey(private_key[0], private_key[1], private_key[2], private_key[3], private_key[4]) self.address = hash_it(pub_to_json(self.public_key)) self.blockchain = blockchain
def hash_block(self): """ Creates a hash from the block, using a sha256 algorithm. :return: str, representing this hash. """ from util import hash_it header = self.prev_hash + str(self.index)\ + str(self.timestamp) + str(self.data) \ + str(self.nonce) + str(self.owner_address) return hash_it(header)
def get_node_address(id): """ Responds with all the nodes the networks has currently. :return: """ node_public_key = nodes_public_keys[id] try: node_address = hash_it(node_public_key) return jsonify(node_address), 200 except: return 'No node with such id in the database', 409
def balance(self, public_key): """ Checks how much money does the owner of this public key has in this current block. :return: int. """ total = 0 for transaction in self.data: total += transaction.get_money(public_key) if self.owner_address == hash_it(pub_to_json(public_key)): total += 100 return total
def get_owner(self): """ Gets the address of the owner of this transaction. :return: owner address. """ return hash_it(self.public_key)