Exemplo n.º 1
0
def load_or_create():
    """Load an existing wallet, or create a
    new one.
    """
    global public
    global private

    if os.path.exists(WALLET_FILE):
        # Load existing.
        with open(WALLET_FILE, 'r') as f:
            wallet_contents = f.read()
        wallet_obj = json.loads(wallet_contents)
        public = wallet_obj['public']
        private = wallet_obj['private']

        print "Loaded existing wallet from", WALLET_FILE
    else:
        # Create a new address and dump it.
        private, public = generate_keys()

        wallet_contents = json.dumps({'public': public, 'private': private})

        with open(WALLET_FILE, 'w') as f:
            f.write(wallet_contents)
        print "Created new wallet in", WALLET_FILE

    print "Your wallet address is:", public
Exemplo n.º 2
0
 def register_patient(self, patient_name, patient_id):
     """
     Function to register the patient.
     :param patient_name: Patient name
     :param patient_id: Patient id
     :return: card
     """
     uid = patient_name + patient_id
     hash_uid = hash(uid)
     # Check if hashed uid resides on public blockchain.
     print(">>> Sending request to bc to check if %s exists in blockchain" %(hash_uid))
     response = self.send_message_to_bc(bc_msg.contains_hash_uid_msg(hash_uid))
     if response == ERROR:
         return None
     if response:
         print("ERROR: Patient " + patient_name + " is affiliated with a hospital already")
         return None
     # Generate keys.
     priv_key, pub_key = crypto.generate_keys()
     # Update public blockchain.
     self.add_to_blockchain(hash_uid, pub_key);
     card = Card(patient_name, patient_id, uid, priv_key, self.name)
     # Insert into hospital k,v store.
     self.insert(uid, pub_key, MedicalRecord(self.name, card))
     return card
Exemplo n.º 3
0
def create_new_wallet(filename):
    """ Create a brand new wallet
    """
    # Create a new address and dump it.
    private, public = generate_keys()

    wallet_contents = json.dumps({'public': public, 'private': private})

    with open(filename, 'w') as f:
        f.write(wallet_contents)
    print "Created new wallet in", filename
Exemplo n.º 4
0
    return json.dumps(response)


@app.route('/ballot', methods=['POST'])
def ballot():
    data = request.data.decode('utf-8')
    with open('ballots.csv', 'a') as file:
        file.write(data + '\n')
    return '', 200


@app.route('/tally', methods=['POST'])
def tally():
    ballots = []
    with open('ballots.csv', 'r') as file:
        for line in file:
            _, a, b = line.strip().split(',')
            ballots.append((int(a), int(b)))

    a, b = crypto.add(ballots)
    total = crypto.decrypt(sk, a, b)
    return str(total)


if __name__ == '__main__':
    pk, sk = crypto.generate_keys()
    if os.path.isfile('ballots.csv'):
        os.remove('ballots.csv')

    app.run()
Exemplo n.º 5
0
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

# connect to BoB
conn, addr = s.accept()

# get prime from Bob
prime = receive(conn)

# generate encryption key such that gcd(encryptionkey,prime) == 1
# generate decryption key such that (encyptionkey)(decyptionkey) = 1 mod(prime)
en_key, de_key = generate_keys(prime)

# encrypt the cards
encrypted_card_numbers = []
for cardnumber in cards.keys():
    encrypted_card_numbers.append(encrypt(cardnumber, en_key, prime))

# send encrytped cards to Bob
send(conn, encrypted_card_numbers)

# receive my crads
mycards = receive(conn)

# receive bob cards
bobcards = receive(conn)
Exemplo n.º 6
0
              previous_hash=previous.hash_block())
    miner.mine_till_found(b)
    return b


def paySomeone(public, private, target, amount):
    txn = Transaction(id=gen_uuid(),
                      owner=public,
                      receiver=target,
                      coins=amount,
                      signature=None)
    txn.signature = sign(txn.comp(), private)
    return txn


wallets = (generate_keys(), generate_keys())
json.dump({
    "public": wallets[0][1],
    "private": wallets[0][0]
}, open("walletA.json", "w"))
json.dump({
    "public": wallets[1][1],
    "private": wallets[1][0]
}, open("walletB.json", "w"))
print(repr(wallets))

blocksToSubmit = []

#mine initial block
reward = Transaction(id=gen_uuid(),
                     owner="mined",