def __str__(self): """Create human readable string representation This method returns a string representation of the object such that it is readable by human. The Class attributes will be ordered e.g. ----------------------- Transaction: VaccineTransaction Signature: sfefsdf Timestamp: 1514903576 Vaccine: 61 Version: 0.0.1 ----------------------- """ instance_member_list = [] for item in vars(self).items(): if item[0] == 'validation_text': continue if type(item[1]).__name__ == "bytes": instance_member_list.append( (item[0].title(), key_utils.bytes_to_hex(item[1]))) continue if type(item[1]).__name__ == "list": modified_list = [] for list_elem in item[1]: if type(list_elem).__name__ == "tuple": modified_tuple = [] for tuple_elem in list_elem: if type(tuple_elem).__name__ == "bytes": bytes_tuple_elem = key_utils.bytes_to_hex( tuple_elem) modified_tuple.append(bytes_tuple_elem) else: modified_tuple.append(tuple_elem) new_tuple = tuple(modified_tuple) modified_list.append(new_tuple) else: modified_list.append(list_elem) instance_member_list.append((item[0].title(), modified_list)) continue instance_member_list.append((item[0].title(), item[1])) instance_member_list.sort(key=lambda tup: tup[0]) string = "-----------------------\n" string = string + " Transaction: {}\n".format(type(self).__name__) for tuple_member in instance_member_list: string = string + " {}: {}\n".format(*tuple_member) string = string + "-----------------------" return string
def get_content_for_hashing(self): """Return relevant block information for hashing.""" fields = [str(self.index), self.previous_block, self.version, str(self.timestamp), key_utils.bytes_to_hex(self.public_key), self.signature] content = CONFIG.serializaton["separator"].join(fields) content += CONFIG.serializaton["line_terminator"] for transaction in self.transactions: content += repr(transaction) + CONFIG.serializaton["line_terminator"] return content
def __repr__(self): """Create a string representation of the current block for hashing.""" fields = [str(self.index), self.previous_block, self.version, str(self.timestamp), key_utils.bytes_to_hex(self.public_key)] if self.signature != "": fields.append(self.signature) if self.hash != "": fields.append(self.hash) block = CONFIG.serializaton["separator"].join(fields) block += CONFIG.serializaton["line_terminator"] for transaction in self.transactions: block += repr(transaction) + CONFIG.serializaton["line_terminator"] return block
def _get_content_for_signing(self): """Return relevant block information for signing. This method is needed at two points: 1. Signing of a block 2. Validating a block's signature In both scenarios, we need the block representation without its signature and hash. """ fields = [str(self.index), self.previous_block, self.version, str(self.timestamp), key_utils.bytes_to_hex(self.public_key)] content = CONFIG.serializaton["separator"].join(fields) content += CONFIG.serializaton["line_terminator"] for transaction in self.transactions: content += repr(transaction) + CONFIG.serializaton["line_terminator"] return content
def __init__(self): """Initialize Client Parse neighbor list Load or generate RSA keypair Instantiate all internal data structures Start block creation thread Optionally start user interface """ # Mock nodes by hard coding if os.getenv('NEIGHBORS_HOST_PORT'): neighbors_list = os.getenv('NEIGHBORS_HOST_PORT') neighbors_list = map(str.strip, neighbors_list.split(",")) self.nodes = ["http://" + neighbor for neighbor in neighbors_list] else: self.nodes = ["http://127.0.0.1:9000"] self._setup_public_key() self.chain = Chain() self.transaction_set = TransactionSet() self.invalid_transactions = set() self.creator_election_thread = None if os.getenv('CONFIRM_BLOCKSENDING') == '1': write_logs_to_file() self._start_election_thread() logger.debug("Finished full_client init.") logger.debug("My public key is: {} or {}".format(self.public_key, key_utils.bytes_to_hex(self.public_key))) if os.getenv('START_CLI') == '1': write_logs_to_file() if os.getenv('REGISTER_AS_ADMISSION') == '1': # start block creation cli t = threading.Thread(target=self.start_block_creation_repl, daemon=True, name="admission cli") else: # start transaction creation cli t = threading.Thread(target=self.start_create_transaction_loop, daemon=True, name="doctor cli") time.sleep(0.5) t.start()
def test_bytes_to_hex(public_key_bytes, public_key_hex, private_key_bytes, private_key_hex): hex_object = key_utils.bytes_to_hex(public_key_bytes) assert hex_object == public_key_hex hex_object = key_utils.bytes_to_hex(private_key_bytes) assert hex_object == private_key_hex
def test_transaction_signing(unsigned_tx): unsigned_tx.sign(PRIVATE_KEY) assert bytes_to_hex( unsigned_tx.signature ) == "6fb072caaf1fccf04f295a27662290bf131ac56da976222b05e38fa2d73bb82f8d4b12f70ea35e597bc6b9c91330dcee3f9f625a7fb6596ae4525d2779e99fb1e103499be57be933825b20160127ebc3cb138b567b92a0a4eaf67619fbc8b0ffcfd4c0676b17f6706e68876398aa589f0da7410188340de6d6fbe434bc76035cb8aa1a88f86c9737958629788b58e925320cf09af9264a33f21969eb5d9126c059a4f80c7b9363d4466f6dad1290c7b350f519caa1bac901a7399db7cb8ecf65b4c72a19af84aeb90ff27a1e385f2a785c2a5b95345b7638566e57fe13f3bbdfc6dfff1db7fdbefbd378587237cf2c3e12eec7a26124e5d06835c940da2c5e83"
def sign(self, private_key): """Sign creator's public key, in order to prove identity.""" block_content = str.encode(self._get_content_for_signing()) self.signature = key_utils.bytes_to_hex(crypto.sign(block_content, private_key))