0

I am learning Python following an online course and I am having trouble with the pycrytodome .verify() method being not callable (as per pylint). I will bold the exact line where the error occurs. I am posting the entire class. Any help would be greatly appreciated.

The first verify_transaction() follows the instructor example exactly. I restructured the method after reading documentation (seen below a second time) but am still getting the same "not callable" error. I am baffled, as .verify() is used in generate_keys() with no problem.

I am using VS Code on a Windows 10 machine.

'''

from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import PKCS1_v1_5
from Cryptodome.Hash import SHA256
import Cryptodome.Random
import binascii


class Wallet:
    def __init__(self):
        self.private_key = None
        self.public_key = None

    def create_keys(self):
        private_key, public_key = self.generate_keys()
        self.private_key = private_key
        self.public_key = public_key

    def save_keys(self):
        if self.public_key != None and self.private_key != None:
            try:
                with open('wallet.txt', mode='w') as f:
                    f.write(self.public_key)
                    f.write('\n')
                    f.write(self.private_key)
            except (IOError, IndexError):
                print('I left my wallet in El Segundo...')

    def load_keys(self):
        try:
            with open('wallet.txt', mode='r') as f:
                keys = f.readlines()
                public_key = keys[0][:-1]
                private_key = keys[1]
                self.public_key = public_key
                self.private_key = private_key
        except (IOError, IndexError):
            print('Loading wallet failed...')

    def generate_keys(self):
        private_key = RSA.generate(1024, Cryptodome.Random.new().read)
        public_key = private_key.publickey()
        return (binascii.hexlify(private_key.exportKey(format='DER')).decode('ascii'), 
         binascii.hexlify(public_key.exportKey(format='DER')).decode('ascii'))

    def sign_transaction(self, sender, recipient, amount):
        signer =  PKCS1_v1_5.new(RSA.importKey(binascii.unhexlify(self.private_key)))
        h = SHA256.new((str(sender) + str(recipient) + str(amount)).encode('utf8'))
        signature = signer.sign(h)
        return binascii.hexlify(signature).decode('ascii')

    @staticmethod
    def verify_transaction(transaction):
        if transaction.sender == 'MINING':
            return True
        public_key = RSA.import_key(binascii.unhexlify(transaction.sender))
        h = SHA256.new((str(transaction.sender) + str(transaction.recipient) + 
        str(transaction.amount)).encode('utf8'))    
        **verifier** = PKCS1_v1_5.new(public_key)
        return verifier.verify(h, binascii.unhexlify(transaction.signature))

    @staticmethod
    def other_verify_transaction(transaction):
        if transaction.sender == 'MINING':
            return True
        public_key = RSA.import_key(binascii.unhexlify(transaction.sender))
        h = SHA256.new((str(transaction.sender) + str(transaction.recipient) + 
        str(transaction.amount)).encode('utf8'))    
        verifier = PKCS1_v1_5.new(public_key)
        try:
            **verifier**.verify(h, binascii.unhexlify(transaction.signature))
            valid = True
        except ValueError:
            valid = False
        return valid 

'''

Monjoie11
  • 33
  • 5
  • Try `def other_verify_transaction(self, transaction):` – Eric Jin May 14 '20 at 19:59
  • Thanks Eric. I tried it as an instance method as per your suggestion and I'm still getting the verifier not callable error. Any other ideas? I'm totally stumped – Monjoie11 May 14 '20 at 21:36

2 Answers2

1

Instead of unhexlify try using a2bhex

0

This is actually an error with VS Code's linter. Coming from a Java background, I was taking the red-error-line's word for it as a compilation error (but python is a scripting language), and after a week of sadness and frustration I just pressed run and the code ran great, despite the "error". So, newbie lesson here, run the code before you take a linter's word for it.

I have since come across a similar vs code "error" in c++

Monjoie11
  • 33
  • 5