def test_transaction_verify_transaction_signature_too_large_signature(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) transaction.add_signature("3379cc2f08e4cde5d24af02611c32693b18f406d4b58fbcd2bbd0acc67b1d") verify = transaction.verify_transaction_signature() self.assertFalse(verify)
def test_transaction_verify_transaction_signature(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) verify = transaction.verify_transaction_signature() self.assertTrue(verify)
def add_new_transaction(self, transaction: Transaction) -> bool: ''' add a new transaction to the block after checking balance ''' if transaction.verify_transaction_signature(): # Check balance and fee before confirming a transaction total_charge = float(transaction.value) + float(transaction.fee) if transaction.sender != "Block_Reward" and \ self.check_balance(transaction.sender) >= total_charge: self.unconfirmed_transactions.append(transaction.to_json()) return True return False
def valid_chain(self, chain: List[str]) -> bool: '''check if a blockchain (all blocks) is valid''' current_index = 0 chain = json.loads(chain) # load and check every block from the blockchain while current_index < len(chain): block = json.loads(chain[current_index]) current_block = Block(block['index'], block['transaction'], block['timestamp'], block['previous_hash']) current_block.merkle_root = block['merkle_root'] current_block.nonce = block['nonce'] current_block.difficulty = block['difficulty'] # if the current block is NOT the last block if current_index + 1 < len(chain): # if the hash value of the current block != previous block's hash value of the next block, then reject if current_block.compute_hash() != json.loads( chain[current_index + 1])['previous_hash']: return False # check if the current block is an instance from the list of chain if isinstance(current_block.transaction, list): for transaction in current_block.transaction: transaction = json.loads(transaction) # Skip block reward because it does not have signature if transaction['sender'] == 'Block_Reward': continue current_transaction = Transaction(transaction['sender'], transaction['recipient'], transaction['value']) current_transaction.signature = transaction['signature'] # check if digital signature of each transaction is valid, if not then reject if not current_transaction.verify_transaction_signature(): return False # check if hash value of the current block is not valid, if yes then reject if not self.is_valid_proof(current_block, block['hash']): return False current_index += 1 return True
def test_transaction_verify_transaction_signature_no_signature(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) verify = transaction.verify_transaction_signature() self.assertFalse(verify)