Example #1
0
def test_valid_transaction_with_invalid_signature():
    transaction = Transaction(Wallet(), 'recipient', 50)
    transaction.input['signature'] = Wallet().sign(transaction.output)

    with pytest.raises(Exception, match='Invalid signature'):
        Transaction.is_valid_transaction(transaction)
Example #2
0
def blockchain_ten_blocks():
    blockchain = BlockChain()
    for i in range(5):
        blockchain.add_block([Transaction(Wallet(), 'abcd', i).to_json()])
    return blockchain
Example #3
0
def blockchain_three_blocks():
        blockchain = Blockchain()
        for i in range(3):
            blockchain.add_block([Transaction(Wallet(), 'recipient', i).to_json()])
        return blockchain
Example #4
0
def test_transaction_update_exceeds_balance():
    sender_wallet = Wallet()
    transaction = Transaction(sender_wallet, 'recipient', 50)
    with pytest.raises(Exception, match='Amount exceeds balance'):
        transaction.update(sender_wallet, 'new recipient', 9001)
Example #5
0
def test_reward_transaction():
    miner_wallet = Wallet()
    transaction = Transaction.reward_transaction(miner_wallet)

    assert transaction.input == MINING_REWARD_INPUT
    assert transaction.output[miner_wallet.address] == MINING_REWARD
def test_transaction_pool():
    transaction_pool = TransactionPool()
    transaction1 = Transactions(Wallet(), 'recp1', 10)
    transaction_pool.set_transaction(transaction1)
    assert transaction1.id in transaction_pool.transaction_map.keys()
Example #7
0
def test_invalid_reward_transaction_extra_recipient():
    reward_transaction = Transaction.reward_transaction(Wallet())
    reward_transaction.output['extra_recipient'] = 60

    with pytest.raises(Exception, match='Invalid mining reward'):
        Transaction.is_valid_transaction(reward_transaction)
def test_verify_signature_with_valid_signature():
    data = {"foo": "bar"}
    wallet = Wallet()
    sig = wallet.gen_signature(data)

    assert Wallet.verify_signature(wallet.public_key, data, sig)
Example #9
0
def main():
    wallet = Wallet()
    acc = Account(wallet)
    acc.getAcc('rs112', 'rpass')
    print(acc)
Example #10
0
def test_transaction_update_exceeds_balance():
    sender_wallet = Wallet()
    transaction = Transaction(sender_wallet, "recipient", 50)

    with pytest.raises(Exception, match="Amount exceeds balance"):
        transaction.update(sender_wallet, "new_recipient", 9001)
Example #11
0
def test_valid_transaction_with_invalid_signature():
    transaction = Transaction(Wallet(), "recipient", 50)
    transaction.input["signature"] = Wallet().sign(transaction.output)
    with pytest.raises(Exception, match="Invalid signature"):
        Transaction.is_valid_transaction(transaction)
Example #12
0
def test_transaction_exceed_balance():
    with pytest.raises(Exception, match="Amount exceeds balance"):
        Transaction(Wallet(), "recipient", 9001)
Example #13
0
                raise Exception('Invalid mining reward')
            return

        output_total = sum(transaction.output.values())

        if transaction.input['amount'] != output_total:
            raise Exception('Invalid transaction output values')

        if not Wallet.verify(transaction.input['public_key'],
                             transaction.output,
                             transaction.input['signature']):
            raise Exception('Invalid signature')

    @staticmethod
    def reward_transaction(miner_wallet):
        """
        Generate a reward transaction that rewards the miner
        """

        output = {miner_wallet.address: MINING_REWARD}

        return Transaction(input=MINING_REWARD_INPUT, output=output)


if __name__ == '__main__':
    transaction = Transaction(Wallet(), 'recipient', 13)
    print(f'transaction: {transaction.__dict__}')

    transaction_json = transaction.to_json()
    restored_transaction = Transaction.from_json(transaction_json)
    print(f'restored transaction: {restored_transaction.__dict__}')
Example #14
0
from backend.pubsub import PubSub
from django.contrib.auth.decorators import login_required
from backend.wallet.wallet import Wallet
from backend.wallet.transaction import Transaction
from django.views.decorators.csrf import csrf_exempt
from backend.wallet.transaction_pool import TransactionPool
from backend.products.assets import Assets
from backend.products.assets_transact import AssetsTransaction
from backend.products.assets_transpool import AssetsTransPool
from products.models import Products
from django.forms.models import model_to_dict
from django.contrib.auth.models import User

block = BlockChain()
sender_assets = Assets(block)
wallet = Wallet(block)
assets = Assets(block)
transaction_pool = TransactionPool()
assetTranPool = AssetsTransPool()
pubsub = PubSub(block, transaction_pool, assetTranPool)


# Create your views here.
@login_required
def index(request):
    if assets.address != request.user.username:
        assets.address = request.user.username
    return render(request, 'index.html', {'Date': time.time()})


@login_required
Example #15
0
def blockchain_init():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.add_block([Transaction(Wallet(), "recipient", i).to_json()])

    return blockchain
Example #16
0
from random import randint

import requests
from flask import Flask, jsonify, request
from flask_cors import CORS

from backend.blockchain.blockchain import Blockchain
from backend.pubsub import PubSub
from backend.wallet.transaction import Transaction
from backend.wallet.transaction_pool import TransactionPool
from backend.wallet.wallet import Wallet

app = Flask(__name__)
CORS(app, resources={r'/*': {'origins': 'http://localhost:3000'}})
blockchain = Blockchain()
wallet = Wallet(blockchain)
transaction_pool = TransactionPool()
pubsub = PubSub(blockchain, transaction_pool)

# for i in range(3):
#     blockchain.add_block(i)
# @blueprint.after_request # blueprint can also be app~~
# def after_request(response):
#     header = response.headers
#     header['Access-Control-Allow-Origin'] = '*'
#     return response


@app.route('/')
def default():
    return 'Welcome to the blockchain'
Example #17
0
def test_invalid_transaction_chain_duplicate_transactions(blockchain_init):
    transaction = Transaction(Wallet(), "recipient", 1).to_json()
    blockchain_init.add_block([transaction, transaction])

    with pytest.raises(Exception, match="is not unique."):
        Blockchain.is_valid_transaction_chain(blockchain_init.chain)
Example #18
0
def test_is_valid_transaction_chain_bad_transaction(blockchain_three_blocks):
    bad_transaction = Transaction(Wallet(), 'recipient', 1)
    bad_transaction.input['signature'] = Wallet().sign(bad_transaction.output)
    blockchain_three_blocks.add_block([bad_transaction.to_json()])
    with pytest.raises(Exception):
        Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
Example #19
0
def main():
    transaction = Transaction(Wallet(), 'recipient', 15)
    print(f'transaction.__dict__: {transaction.__dict__}')
def main():
    transaction = Transaction(Wallet(), 'recipient', 109)
    print(f'\nTransaction.__dict__: {transaction.__dict__}')
    transaction_json = transaction.to_json()
    restored_transaction = Transaction.from_json(transaction_json)
    print(f'\nrestored_transaction.__dict__: {restored_transaction.__dict__}')
Example #21
0
def test_transaction_exceeds_balance():
    with pytest.raises(Exception, match='Amount exceeds balance'):
        Transaction(Wallet(), 'recipient', 9001)
    def is_valid_transaction(transaction):

        if not Wallet.verify(transaction.input['public_key'],
                             transaction.output,
                             transaction.input['signature']):
            raise Exception('Invalid signature')
Example #23
0
def test_valid_transaction():
    Transaction.is_valid_transaction(Transaction(Wallet(), 'recipient', 50))
Example #24
0
from flask import Flask, jsonify, request
from backend.blockchain.blockchain import Blockchain
from backend.pubsub import PubSub
from backend.wallet.wallet import Wallet
from backend.wallet.transaction import Transaction
from backend.wallet.transaction_pool import TransactionPool
import os
import requests
import random
app = Flask(__name__)
blockchain = Blockchain()
wallet = Wallet(blockchain)
transaction_pool = TransactionPool()
pubsub = PubSub(blockchain, transaction_pool)


@app.route('/')
def default():
    return 'Blockchain 2.0v'


@app.route('/blockchain')
def route_blockchain():
    return jsonify(blockchain.to_json())


@app.route('/blockchain/mine')
def route_blockchain_mine():
    #'Stubbed_transaction_data'
    transaction_data = transaction_pool.transaction_data()
    transaction_data.append(Transaction.reward_transaction(wallet).to_json())
Example #25
0
def test_valid_reward_transaction():
    reward_transaction = Transaction.reward_transaction(Wallet())
    Transaction.is_valid_transaction(reward_transaction)
def test_set_transaction():
    transaction_pool = TransactionPool()
    transaction = Transaction(Wallet(), 'recipient', 1)
    transaction_pool.set_transaction(transaction)

    assert transaction_pool.transaction_map[transaction.id] == transaction

def post_wallet_transact(recipient, amount):
    return requests.post(f'{BASE_URL}/wallet/transact',
                         json={
                             'recipient': recipient,
                             'amount': amount
                         }).json()


def get_wallet_info():
    return requests.get(f'{BASE_URL}/wallet/info').json()


start_blockchain = get_blockchain()
print(f'start blockchain: {start_blockchain}')

recipient = Wallet().address
post_wallet_transact_1 = post_wallet_transact(recipient, 21)
time.sleep(1)
post_wallet_transact_2 = post_wallet_transact(recipient, 13)
print(f'\npost_wallet_transact_1: {post_wallet_transact_1}')
print(f'\npost_wallet_transact_2: {post_wallet_transact_2}')

time.sleep(1)
mined_block = get_blockchain_mine()
print(f'\nmined_block: {mined_block}')

wallet_info = get_wallet_info()
print(f'\nwallet_info: {wallet_info}')
Example #28
0
def test_verify_valid_signature():
    data = {'foo': 'test_data'}
    wallet = Wallet()
    signature = wallet.sign(data)
    assert Wallet.verify(wallet.public_key, data, signature)
Example #29
0
def test_valid_transaction_chain_duplicate_transaction(blockchain_three_blocks):
    transaction = Transaction(Wallet(), 'recipient', 1).to_json()
    blockchain_three_blocks.add_block([transaction, transaction])
    
    with pytest.raises(Exception, match='is not unique'):
        Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_transaction_exceeds_balance():
    with pytest.raises(Exception):
        Transaction(Wallet(), 'recipient', 9001)