Example #1
0
def info(dic):
    state=state_library.current_state()
    chain=blockchain.load_chain()
    if dic['type']=='blockCount':
        if len(chain)>0:
            return package({'length':state['length'], 'recent_hash':state['recent_hash']})
        else:
            return package({'length':0, 'recent_hash':0})
    if dic['type']=='rangeRequest':
        ran=dic['range']
        if ran[0]==0:
            ran[0]=1
        print('ran: ' +str(ran))
        if len(chain)>=int(ran[1]):
            print('$$$$$$$$$$dic: ' +str(dic))
            return package(chain[ran[0]:ran[1]+1])
        else:
            return package({'error':'oops'})
    if dic['type']=='transactions':
        return package(blockchain.load_transactions())
    if dic['type']=='backup_states':
        backups=state_library.fs_load(state_library.backup_db,{})
        out={}
        for i in backups.keys():
            find_biggest=0
            if int(i)<int(dic['start']) and int(i)>int(find_biggest):
                find_biggest=int(i)
        return package(backups[str(find_biggest)])
    if dic['type']=='pushtx':
        blockchain.add_transaction(dic['tx'])
    if dic['type']=='pushblock':
        blockchain.chain_push(dic['block'])
Example #2
0
def tradeChain(dic):
    print('dic: '+str(dic))
    if dic['type']=='tx':
         if blockchain.verify_transaction_pool(dic['transaction']):
            blockchain.add_transaction(dic['transaction'])
            return package({'response':'Recieved transaction'})
    print('ERROR--YOU WERE SENT A BAD TX!!')
    print(dic)
    return package({'error':'bad tx'})
Example #3
0
def handle_transaction():
    request_json = request.json
    if request.method == 'POST':
        sender_blockchain_address = request_json['sender_blockchain_address']
        recipient_blockchain_address = request_json[
            'recipient_blockchain_address']
        value = float(request_json['value'])
        sender_private_key = request_json['sender_private_key']
        sender_public_key = request_json['sender_public_key']

        transaction_to_add = transaction.Transaction(
            sender_blockchain_address,
            recipient_blockchain_address,
            value,
            sender_private_key,
            sender_public_key,
        )
        is_added = blockchain.add_transaction(
            sender_blockchain_address=sender_blockchain_address,
            recipient_blockchain_address=recipient_blockchain_address,
            value=value,
            sender_public_key=sender_public_key,
            signature=transaction_to_add.generate_signature())
        if is_added:
            return jsonify({'message': 'success'}), 200
        return jsonify({'message': 'fail'}), 400

    if request.method == 'GET':
        response = blockchain.transaction_pool
        return jsonify(response), 200
Example #4
0
def add_transaction():
    json = request.get_json()
    transaction_keys = ['sender', 'receiver', 'amount']
    if not all(key in json for key in transaction_keys):
        return 'Some elements of the transaction are missing', 400

    index = blockchain.add_transaction(json['sender'], json['receiver'],
                                       json['amount'])
    response = {'message': f'This transaction will be add to block {index}'}
    return jsonify(response), 201
Example #5
0
def easy_add_transaction(tx_orig, sign_over, privkey):
    state = state_library.current_state()
    pubkey = pt.privtopub(privkey)
    if pubkey not in state or "count" not in state[pubkey]:
        my_count = 1
    else:
        my_count = state[pubkey]["count"]
    txs = blockchain.load_transactions()
    my_txs = filter(lambda x: x["id"] == pubkey, txs)
    tx = copy.deepcopy(tx_orig)
    tx["count"] = len(my_txs) + my_count
    tx["signature"] = pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey)
    print (blockchain.add_transaction(tx))
    if "move_number" in tx:
        for i in range(10):
            tx["move_number"] += 1
            tx["signature"] = pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey)
            print (blockchain.add_transaction(tx))
    print ("tx: " + str(tx))
    blockchain.pushtx(tx, quick_mine.peers_list)
Example #6
0
def mine():
    last_blk = blockchain.last_block
    last_proof = last_blk.proof
    proof = blockchain.proof_of_work(last_proof)

    blockchain.add_transaction(constant.MINER_KEY, node_address,
                               constant.MINER_REWARD, "")

    prev_hash = last_blk.hash
    blk = blockchain.add_block(proof, prev_hash)

    response = {
        'message': 'New block mined!',
        'index': blk['index'],
        'transactions': blk['transactions'],
        'proof': blk['proof'],
        'previous_hash': blk['prev_hash']
    }

    return jsonify(response), 201
Example #7
0
File: gui.py Project: Aeium/go-coin
def easy_add_transaction(tx_orig, sign_over, privkey):
    state=state_library.current_state()
    pubkey=pt.privtopub(privkey)
    if pubkey not in state or 'count' not in state[pubkey]:
        my_count=1
    else:
        my_count=state[pubkey]['count']
    txs=blockchain.load_transactions()
    my_txs=filter(lambda x: x['id']==pubkey, txs)
    tx=copy.deepcopy(tx_orig)
    tx['count']=len(my_txs)+my_count
    tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey)
    print(blockchain.add_transaction(tx))
    if 'move_number' in tx:
        for i in range(10):
            tx['move_number']+=1
            tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey)
            print(blockchain.add_transaction(tx))
    print('tx: ' +str(tx))
    blockchain.pushtx(tx, config.peers_list)
Example #8
0
def mine_block():
    previous_block = blockchain.get_previous_block()
    previous_proof = previous_block['proof']
    proof = blockchain.proof_of_work(previous_proof)
    previous_hash = blockchain.hash(previous_block)
    # Transactions from newly mined block
    blockchain.add_transaction(sender=blockchain.node_url,
                               receiver='Sab',
                               amount=1)
    # Create Block for work done
    block = blockchain.create_block(proof, previous_hash)
    response = {
        'message': 'Block mined!',
        'index': block['index'],
        'timestamp': block['timestamp'],
        'proof': block['proof'],
        'previous_hash': block['previous_hash'],
        'transactions': block['transactions']
    }

    return jsonify(response), 200
Example #9
0
def easy_add_transaction(tx_orig, sign_over, privkey, state):
    tx=copy.deepcopy(tx_orig)
    pubkey=pt.privtopub(privkey)
    if pubkey not in state or 'count' not in state[pubkey]:
        tx['count']=1
    else:
        tx['count']=state[pubkey]['count']
    txs=blockchain.load_transactions()
    my_txs=filter(lambda x: x['id']==pubkey, txs)
    tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey)
    if blockchain.add_transaction(tx):
        blockchain.pushtx(tx, config.peers_list)
        return True
    if 'move_number' in tx:
        for i in range(10):
            tx['move_number']+=1
            tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey)
            if blockchain.add_transaction(tx):
                blockchain.pushtx(tx, config.peers_list)
                return True
    print('SOMETHING IS BROKEN')
    return False
Example #10
0
def easy_add_transaction(tx_orig, sign_over, privkey, state):
    tx = copy.deepcopy(tx_orig)
    pubkey = pt.privtopub(privkey)
    if pubkey not in state or 'count' not in state[pubkey]:
        tx['count'] = 1
    else:
        tx['count'] = state[pubkey]['count']
    txs = blockchain.load_transactions()
    my_txs = filter(lambda x: x['id'] == pubkey, txs)
    tx['signature'] = pt.ecdsa_sign(go.message2signObject(tx, sign_over),
                                    privkey)
    if blockchain.add_transaction(tx):
        blockchain.pushtx(tx, config.peers_list)
        return True
    if 'move_number' in tx:
        for i in range(10):
            tx['move_number'] += 1
            tx['signature'] = pt.ecdsa_sign(
                go.message2signObject(tx, sign_over), privkey)
            if blockchain.add_transaction(tx):
                blockchain.pushtx(tx, config.peers_list)
                return True
    print('SOMETHING IS BROKEN')
    return False
Example #11
0
def new_transaction():
    body = request.get_json()
    required_params = ['sender', 'receiver', 'amount', 'signature']

    try:
        if not all(e in body for e in required_params):
            return 'Missing parameters', 400
    except TypeError:
        return 'Missing parameters', 400

    result = blockchain.add_transaction(body['sender'], body['receiver'],
                                        body['amount'], body['signature'])
    if result < 0:
        response = {'message': 'Invalid transaction'}
        return jsonify(response), 406
    else:
        response = {
            'message': f'Transaction will be appended to block {result}'
        }
        return jsonify(response), 201
Example #12
0
def pushtx(dic):
#    try:
    blockchain.add_transaction(dic['tx'])
Example #13
0
        sha256_hex = codecs.encode(sha256_2_nbpk_digest, 'hex')

        checksum = sha256_hex[:8]

        address_hex = (network_bitcoin_public_key + checksum).decode('utf-8')

        blockchain_address = base58.b58encode(address_hex).decode('utf-8')

        return blockchain_address


if __name__ == '__main__':
    wallet_A = Wallet()
    wallet_B = Wallet()
    value = 100
    print(f'wallet_A\'s blockchain_address : {wallet_A.blockchain_address}')
    print(f'wallet_B\'s blockchain_address : {wallet_B.blockchain_address}')
    print(f'value : {value}')
    t = transaction.Transaction(wallet_A.blockchain_address,
                                wallet_B.blockchain_address, 100,
                                wallet_A.private_key, wallet_A.public_key)
    blockchain = blockchain.BlockChain()
    blockchain.add_transaction(
        sender_blockchain_address=wallet_A.blockchain_address,
        recipient_blockchain_address=wallet_B.blockchain_address,
        value=value,
        sender_public_key=wallet_A.public_key,
        signature=t.generate_signature())
    blockchain.mining()
    print(blockchain.print_chain())
Example #14
0
        message = sha256.digest()
        private_key = SigningKey.from_string(
            bytes().fromhex(self.sender_private_key), curve=NIST256p
        )
        private_key_sign = private_key.sign(message)
        signature = private_key_sign.hex()
        return signature

if __name__ == '__main__':
    wallet_M = Wallet()
    wallet_A = Wallet()
    wallet_B = Wallet()
    t = Transaction(wallet_A.private_key, wallet_A.public_key, wallet_A.blockchain_address,
        wallet_B.blockchain_address, 1.0)
    
    ########### Blockchain Node Side
    import blockchain
    blockchain = blockchain.BlockChain(
        blockchain_address = wallet_M.blockchain_address
    )
    is_added = blockchain.add_transaction(
        wallet_A.blockchain_address, wallet_B.blockchain_address,
        1.0, wallet_A.public_key, t.generate_signature()
    )
    print('Added ?', is_added)
    blockchain.mining()
    utils.pprint(blockchain.chain)

    print('A', blockchain.calculate_total_amount(wallet_A.blockchain_address))
    print('B', blockchain.calculate_total_amount(wallet_B.blockchain_address))