def send_funds_from_to(self, sender_public_key_str: str, sender_private_key_str: str, recipient_public_key_str: str, value: float): if self.get_balance_for_public_key(sender_public_key_str) < value: print("Not enough balance, transaction discarded") return None if value <= 0: print("Value should be positive, transaction discarded") return None inputs = [] total = 0 for transaction_id, utxo in self.all_utxos.items(): if utxo.is_mine(sender_public_key_str): total += utxo.value inp = TransactionInput(transaction_id) inputs.append(inp) if total >= value + self.fee: break transaction = Transaction(sender_public_key_str, recipient_public_key_str, value, inputs) encoded = base64.b64decode(sender_private_key_str.encode('utf8')) sender_private_formated = PEM.encode(encoded, 'RSA PRIVATE KEY') transaction.generate_signature(import_key(sender_private_formated.encode('utf8'))) transaction.process_transaction(self.all_utxos, self.minimum_transaction, self.fee) return transaction
def verify_signature(self): if not self.signature: return False message = self.sender + self.recipient + str(self.value) sender_public_key = import_key(self.sender) return verify(message.encode(), self.signature, sender_public_key)
def decrypt(self, password): cipher = AESCipher(password) try: private_key_str = cipher.decrypt(self.private_key_encrypted) self.__private_key = import_key(pem_format(private_key_str)) except UnicodeDecodeError: raise Exception('Password was invalid to decode private key')
def verify_signature(self): if self.sender == '': return True if not self.signature: return False message = self.sender + self.recipient + str(self.value) encoded = base64.b64decode(self.sender.encode('utf8')) sender_public_formated = PEM.encode(encoded, 'RSA PUBLIC KEY') sender_public_key = import_key(sender_public_formated) return verify(message.encode(), self.signature, sender_public_key)
def get_public_key(self): return import_key(pem_format(self.public_key_str))