def test_save_nonce(): w = wallet.generate_new_wallet() wallet_database.save_wallet(w) assert wallet_database.load_wallet().get_nonce() == 0 wallet_database.save_nonce(10) assert wallet_database.load_wallet().get_nonce() == 10 assert wallet_database.get_nonce() == 10
def test_nonce_in_tx_correct(): # test if the nonce is correct in the tx w = wallet.generate_new_wallet() t = Transaction(sender=w.get_address(), receivers=["01"], amounts=[1], nonce=w.get_nonce(), fee=100) assert t.nonce == w.get_nonce() tx_hash_old = "" tx_sig_old = "" for i in range(10): # sign tx w.sign_transaction(t) # get sig signature = t.get_signature() # nonce gets bigger every signed tx assert t.nonce == w.get_nonce() - 1 assert t.nonce == i # make sure tx_signature changes assert signature != tx_sig_old # make sure tx_hash changes assert t.hash() != tx_hash_old # finally test if verify works assert t.verify() # save values for next iteration tx_hash_old = t.hash() tx_sig_old = signature # assert that we counted correctly assert w.get_nonce() == 10
def test_save_load_wallet(): # saving and loading wallet w1 = wallet.generate_new_wallet() wallet_database.save_wallet(w1) w2 = wallet_database.load_wallet() assert w1.get_address() == w2.get_address() assert w1.get_private_key().to_der() == w2.get_private_key().to_der() assert w1.get_nonce() == w2.get_nonce()
def test_sign_vote_invalid_address(): prev_hash = "bc10b849a535b5be9f65fffb2e9c8809da75ff86590dd02e3ba26daf0c18b738" sender = "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P" v = Vote(prev_hash, sender) w = wallet.generate_new_wallet() with pytest.raises(ValueError): w.sign_vote(v)
def test_sign_vote_valid(): w = wallet.generate_new_wallet() prev_hash = "0" sender = w.get_address() v = Vote(prev_hash, sender) w.sign_vote(v) assert cryptoutil.verify_vote_sig(v, v.signature, v.sender) with pytest.raises(AssertionError): cryptoutil.verify_vote_sig(v, "")
def test_sign_unvalid_tx(): # test if signing unvalid transaction is rejected w = wallet.generate_new_wallet() t = Transaction(sender="different sender than wallet", receivers=["01"], amounts=[1], nonce=1, fee=100) with pytest.raises(ValueError): w.sign_transaction(t)
def test_sign_transaction(): # test if transactions can be signed w = wallet.generate_new_wallet() t = Transaction(sender=w.get_address(), receivers=["01"], amounts=[1], nonce=1, fee=100) w.sign_transaction(t) assert t.verify()
def gen_wallet(self): # we only want to generate a new wallet if there is none # (todo make it better, if input throws an exception the old wallet is gone) try: wallet_database.load_wallet() y = input( "There is already a wallet with address:\n " + wallet_database.get_address() + "\nGenerating a new wallet would override Your old wallet.\n\n" + "You would loose access to the old wallet.\n\nAre You Sure? (y/N)" ) if y == "y" or y == "Y": wallet_database.save_wallet(wallet.generate_new_wallet()) print("\nGenerated wallet with address:\n" + wallet_database.get_address()) except: wallet_database.save_wallet(wallet.generate_new_wallet()) print("generated wallet with address:" + wallet_database.get_address())
def test_nonce_correctly(): # test if the nonce increases when signing a tx # test if loaded wallet (w_copy) has correct nonce w = wallet.generate_new_wallet() t = Transaction(sender=w.get_address(), receivers=["01"], amounts=[1], nonce=1, fee=100) assert w.get_nonce() == 0 w.sign_transaction(t) assert w.get_nonce() == 1
def test_verify_tx(): """ tests if a signed transaction can be validated we just need the address for this, because address=public_key """ w = wallet.generate_new_wallet() sender = w.get_address() receivers = ["fe"] amounts = [1] fee = 100 t = Transaction(sender, receivers, amounts, fee) w.sign_transaction(t) assert cryptoutil.verify_transaction_sig(t, t.get_signature()) assert t.verify()
def test_verify_vtx_signature(): """ tests if sender and receiver sigs can be validated seperately and together """ sender_wallet = wallet.generate_new_wallet() receiver_wallet = wallet.generate_new_wallet() sender = sender_wallet.get_address() receiver = receiver_wallet.get_address() vt_amount = 100 nto_amount = 100 t = VotingTokenTransaction(sender, receiver, vt_amount, nto_amount) sender_wallet.sign_vtx_sender(t) # verify sender signature assert cryptoutil.verify_transaction_sig( t, t.get_sender_signature(), t.get_sender()) # tx not valid because receiver did not sign assert False == t.verify() # sign with receiver receiver_wallet.sign_vtx_receiver(t) # should work assert cryptoutil.verify_transaction_sig( t, t.get_receiver_signature(), t.get_receiver()) # should also work assert t.verify()
def test_generate_wallet(): # test generating new wallet w = wallet.generate_new_wallet() assert w is not None assert w.get_address() is not None assert w.get_nonce() == 0
def test_get_nonce(): w = wallet.generate_new_wallet() wallet_database.save_wallet(w) assert w.get_nonce() == wallet_database.get_nonce()