Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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()
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 def verify_transaction(self, transaction, sender_pk):
     hashed_transaction = object_hash(transaction)
     truthy = verifier(sender_pk, transaction.signature, hashed_transaction)
     return truthy