def create_coins(self, amount, wallet_id): coins = [] for _ in range(amount): coin = ScroogeCoin(wallet_id) hashed_coin = object_hash(coin) signature = signer(self.__private_key, hashed_coin) coin.sign(signature) coins.append(coin) return coins
def before_block_append(self, genesis): if not genesis: for transaction in self.transactions_cache: hashed_transaction = object_hash(self.last_transaction) previous_transaction_hash = hashed_transaction transaction.previous_transaction_hash = previous_transaction_hash transaction.used_coins = [*transaction.coins] transaction.coins = self.create_coins(len(transaction.coins), transaction.receiver) self.last_transaction = transaction self.blockchain.append_block(self.transactions_cache) self.after_block_append()
def genesis(self): previous_transaction_hash = None for wallet in self.wallets: coins = self.create_coins(10, wallet.id) transaction = Transaction( coins, wallet.id, type="create", previous_transaction_hash=previous_transaction_hash) hashed_transaction = object_hash(transaction) previous_transaction_hash = hashed_transaction signature = signer(self.__private_key, hashed_transaction) transaction.sign_transaction(signature) self.transactions_cache.append(transaction) self.last_transaction = transaction self.check_for_new_block(True)
def create_transaction(self, amount, receiver, scrooge): owned_coins = self.get_balance(scrooge.blockchain) transfer_coins = owned_coins[:amount] if len(owned_coins) < amount: transaction = Transaction(transfer_coins, receiver.public_key) printer(bcolors.FAIL, bcolors.BOLD, bcolors.UNDERLINE, BALANCE_ERROR_MESSAGE, bcolors.ENDC, bcolors.ENDC, bcolors.ENDC) printer(bcolors.FAIL, transaction, bcolors.ENDC) transfer_coins = owned_coins[:amount] transaction = Transaction(transfer_coins, receiver.public_key) hashed_transaction = object_hash(transaction) signature = signer(self.__private_key, hashed_transaction) transaction.sign_transaction(signature) scrooge.handle_transaction(transaction, self.public_key)
def append_block(self, transactions): previous_hash = None if len(self.chain) == 0 else object_hash( self.chain[-1]) block = Block(len(self.chain), previous_hash, transactions) self.chain.append(block) printer(self)
def after_block_append(self): last_block = self.blockchain.chain[-1] hashed_block = object_hash(last_block) signature = signer(self.__private_key, hashed_block) last_block.sign(signature)
def verify_transaction(self, transaction, sender_pk): hashed_transaction = object_hash(transaction) truthy = verifier(sender_pk, transaction.signature, hashed_transaction) return truthy