def get_wallet(self, key=None):
        """Get the wallet of a user

        Args:
            key (String): the private key of the user

        Return:
             (Wallet): the wallet of the user
        """

        return Wallet(key, self)
Exemple #2
0
def test_wallet_from_key():
    """Test Wallet creation from a key"""

    wallet = Wallet('aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')

    assert wallet.account == \
           '55d83bb921c148822bfe7057604bf3bb6d499976ea1054943f91c9caf28f2717bfcdf5e5b8c1fc0d18d510691765506c'

    assert wallet.public == \
           '55d83bb921c148822bfe7057604bf3bb6d499976ea1054943f91c9caf28f2717bfcdf5e5b8c1fc0d18d510691765506c'

    assert wallet.private == \
           'aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518'

    assert wallet.key == \
           'aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518'

    assert wallet.get_account() == wallet.public

    assert repr(wallet) == \
           '55d83bb921c148822bfe7057604bf3bb6d499976ea1054943f91c9caf28f2717bfcdf5e5b8c1fc0d18d510691765506c (0)'
Exemple #3
0
def test_minimal_cryptocurrency():
    """Test basic process of a create a cryptocurrency"""

    # Generate first user
    wallet = Wallet()

    # Create a new currency with 10 units
    blockchain = BlockChain.new_cryptocurrency(wallet.account, 10, timestamp=datetime(2018, 1, 1, 0, 0, 0),
                                               difficulty=4, mining=True)

    # Get the wallets of Alice and Bob
    alice = blockchain.get_wallet(wallet.key)
    bob = blockchain.get_wallet()

    assert alice.get_balance() == 10
    assert bob.get_balance() == 0

    # Alice transfer to Bob 5 units and get 10 units because she mining the block
    assert blockchain.add_transaction(alice.key, bob.account, 5)

    blockchain.generate_candidate(alice.account, timestamp=datetime(2018, 1, 1, 0, 1, 0))
    blockchain.mining_candidate()

    assert alice.get_balance() == 15
    assert bob.get_balance() == 5

    # New users Carol, Dan and Eve
    carol = blockchain.get_wallet()
    dan = blockchain.get_wallet()
    eve = blockchain.get_wallet()

    # Alice transfer 3 to Carol and 4.5 to Dan
    assert blockchain.add_transaction(alice.key, carol.account, 3)
    assert blockchain.add_transaction(alice.key, dan.account, 4.5)

    # Bon transfer 2.2 to Dan
    assert blockchain.add_transaction(bob.key, dan.account, 2.2)

    # Eve mine the Block
    blockchain.generate_candidate(eve.account, timestamp=datetime(2018, 1, 1, 0, 2, 0))
    blockchain.mining_candidate()

    assert alice.get_balance() == 7.5
    assert bob.get_balance() == 2.8
    assert carol.get_balance() == 3
    assert dan.get_balance() == 6.7
    assert eve.get_balance() == 10
Exemple #4
0
def test_wallet_operation():
    """Test operation with wallets"""

    wallet_1 = Wallet('aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')
    wallet_2 = Wallet('7d6433bcc63f973580dc7562d2ca79fcb12bb4e08c7e7333')
    wallet_3 = Wallet('3d66f0ea52a2c5cf42893560d5522e82621790edeb7f609b')

    # List of unspent transactions
    unspent = UnspentList()

    # Assign 100 coins to the wallet #1
    unspent.unspent.append(UnspentTransaction('d749929fe94b9a37dbe5d74cb297ad24b46e467898545f4e109eb21835046ac4',
                                              0, wallet_1.get_account(), 100))

    assert unspent.address_amount(wallet_1.get_account()) == 100
    assert unspent.address_amount(wallet_2.get_account()) == 0
    assert unspent.address_amount(wallet_3.get_account()) == 0

    # Link unspent to the wallets
    wallet_1.unspent = unspent
    wallet_2.unspent = unspent
    wallet_3.unspent = unspent

    # Validate accounts
    assert wallet_1.get_balance() == 100
    assert wallet_2.get_balance() == 0
    assert wallet_3.get_balance() == 0

    # Try to transfer 900 coins form 1 to 2
    assert wallet_1.generate_transaction_to(wallet_2.get_account(), 900) is None

    # Transfer 90 coins form 1 to 2
    transaction = wallet_1.generate_transaction_to(wallet_2.get_account(), 90)

    assert transaction is not None
    assert transaction.hash_id == transaction.generate_transaction_id()

    # Spend the transaction
    assert unspent.validate_transaction(transaction)
    assert unspent.spend_transaction(transaction)
    assert unspent.validate_transaction(transaction) is False

    assert wallet_1.get_balance() == 10
    assert wallet_2.get_balance() == 90
    assert wallet_3.get_balance() == 0

    # Transfer 50 form 1 to 3
    assert wallet_1.transfer_to(wallet_3.get_account(), 50) is False

    # Transfer 5 form 1 to 3
    assert wallet_1.transfer_to(wallet_3.get_account(), 5)

    assert wallet_1.get_balance() == 5
    assert wallet_2.get_balance() == 90
    assert wallet_3.get_balance() == 5

    # Transfer 0.5 form 2 to 3
    assert wallet_2.transfer_to(wallet_3.get_account(), 0.5)

    assert wallet_1.get_balance() == 5
    assert wallet_2.get_balance() == 89.5
    assert wallet_3.get_balance() == 5.5
Exemple #5
0
def test_wallet_creation():
    """Test Wallet creation"""

    wallet = Wallet()

    assert wallet.public == generate_public_key(wallet.private)
def test_minimal_cryptocurrency():
    """Test the operation with a minimal cryptocurrency"""

    wallet_1 = Wallet('aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')

    # Launch a new currency
    blockchain = BlockChain.new_cryptocurrency(wallet_1.public,
                                               100,
                                               timestamp=datetime(
                                                   2000, 1, 1, 0, 0, 0),
                                               difficulty=4,
                                               mining=True)

    # Open the wallets
    wallet_1 = blockchain.get_wallet(
        'aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')
    wallet_2 = blockchain.get_wallet(
        '7d6433bcc63f973580dc7562d2ca79fcb12bb4e08c7e7333')
    wallet_3 = blockchain.get_wallet(
        '3d66f0ea52a2c5cf42893560d5522e82621790edeb7f609b')

    # Ask for the accounts
    assert wallet_1.get_balance() == 100
    assert wallet_2.get_balance() == 0
    assert wallet_3.get_balance() == 0

    # Generate an unspent transaction
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public,
                                      190) is False
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public, 90)
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public,
                                      90) is False

    # Generate a candidate to mining
    blockchain.generate_candidate(wallet_1.public,
                                  timestamp=datetime(2000, 1, 1, 0, 1, 0))
    blockchain.mining_candidate()

    # Ask for the accounts
    assert wallet_1.get_balance() == 110
    assert wallet_2.get_balance() == 90
    assert wallet_3.get_balance() == 0

    # Move more operations
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public, 10)
    assert blockchain.add_transaction(wallet_2.private, wallet_3.public, 20)

    # Generate a candidate to mining
    blockchain.generate_candidate(wallet_1.public,
                                  timestamp=datetime(2000, 1, 1, 0, 2, 0))
    blockchain.mining_candidate()

    # Ask for the accounts
    assert wallet_1.get_balance() == 200
    assert wallet_2.get_balance() == 80
    assert wallet_3.get_balance() == 20

    # Move more operations
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public, 25)
    assert blockchain.add_transaction(wallet_1.private, wallet_3.public, 25)
    assert blockchain.add_transaction(wallet_2.private, wallet_3.public, 50)

    # Generate a candidate to mining
    blockchain.generate_candidate(wallet_2.public,
                                  timestamp=datetime(2000, 1, 1, 0, 3, 0))
    blockchain.mining_candidate()

    # Ask for the accounts
    assert wallet_1.get_balance() == 150
    assert wallet_2.get_balance() == 155
    assert wallet_3.get_balance() == 95
Exemple #7
0
def test_unconfirmed_minner():
    """Test operation with wallets"""

    # List of unspent transactions
    unspent = UnspentList()

    # Wallets
    wallet_1 = Wallet('aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')
    wallet_2 = Wallet('7d6433bcc63f973580dc7562d2ca79fcb12bb4e08c7e7333')
    wallet_3 = Wallet('3d66f0ea52a2c5cf42893560d5522e82621790edeb7f609b')

    # Link unspent to the wallets
    wallet_1.unspent = unspent
    wallet_2.unspent = unspent

    assert wallet_1.get_balance() == 0
    assert wallet_2.get_balance() == 0

    # Add transaction for miner
    inputs = datetime(2000, 1, 1, 0, 0, 0)
    outputs = OutputTransaction(wallet_1.public, 5)
    transaction = Transaction(inputs, outputs, wallet_1.private)

    assert unspent.append_unconfirmed(transaction)
    assert unspent.append_unconfirmed(transaction) is False

    assert unspent.confirm_unconfirmed()
    assert wallet_1.get_balance() == 5
    assert wallet_2.get_balance() == 0
Exemple #8
0
def test_unconfirmed():
    """Test operation with wallets"""

    wallet_1 = Wallet('aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')
    wallet_2 = Wallet('7d6433bcc63f973580dc7562d2ca79fcb12bb4e08c7e7333')

    # List of unspent transactions
    unspent = UnspentList()

    # Assign 250 coins to the wallet #1
    unspent.unspent.append(
        UnspentTransaction(
            'd749929fe94b9a37dbe5d74cb297ad24b46e467898545f4e109eb21835046ac4',
            0, wallet_1.get_account(), 250))

    assert unspent.address_amount(wallet_1.get_account()) == 250
    assert unspent.address_amount(wallet_2.get_account()) == 0

    # Link unspent to the wallets
    wallet_1.unspent = unspent
    wallet_2.unspent = unspent

    # Validate accounts
    assert wallet_1.get_balance() == 250
    assert wallet_2.get_balance() == 0

    # Try to transfer 900 coins form 1 to 2
    assert wallet_1.generate_transaction_to(wallet_2.get_account(),
                                            900) is None

    # Generate a transfer of 200 coins form 1 to 2
    transaction = wallet_1.generate_transaction_to(wallet_2.get_account(), 200)

    # The operation can be append to the unconfirmed transactions
    assert unspent.append_unconfirmed(transaction)

    # Cannot add to the unconfirmed list again
    assert unspent.append_unconfirmed(transaction) is False

    # Validate accounts (unconfirmed cannot be spend again)
    assert wallet_1.get_balance() == 0
    assert wallet_2.get_balance() == 0

    # Confirm unconfirmed transactions
    assert unspent.confirm_unconfirmed()

    # Validate accounts
    assert wallet_1.get_balance() == 50
    assert wallet_2.get_balance() == 200

    # Spend more transactions
    assert unspent.append_unconfirmed(
        wallet_1.generate_transaction_to(wallet_2.get_account(), 25))
    assert unspent.append_unconfirmed(
        wallet_2.generate_transaction_to(wallet_1.get_account(), 10))
    assert unspent.confirm_unconfirmed()

    # Validate accounts
    assert wallet_1.get_balance() == 35
    assert wallet_2.get_balance() == 215