def select_t_valid_share(self, round):
        """
        Select a set of threshold+1 valid shares for a given message.
        The returned dictionnary has the following shape:

        .. code-block:: python

            {id_user1: share1, id_user2: share2}

        Args:
            file_id (int): the id of the message for which we want to select the shares.

        Returns:
            dict: set of :math:`threshold+1` valid shares.
        """
        db = get_db()
        db_shares = db.execute("SELECT * FROM share WHERE round=?", (round,)).fetchall()
        threshold = self.contract.caller().t()
        if len(db_shares) < threshold + 1:
            raise ValueError('Not enough shares available')

        shares={}
        for i in range(threshold+1):
            point = (int(db_shares[i]['a']),int(db_shares[i]['b']))
            shares[int(db_shares[i]['ui'])] = cru.point_from_eth(point)

        return shares
Beispiel #2
0
def login():
    """Login a user in the webapp.

    Returns:
        The page index if login succeed and the login page else
    """
    if request.method == 'POST':
        username = request.form['username']
        db = get_db()
        error = None
        user = db.execute(
            'SELECT * FROM user WHERE username = ?', (username,)
        ).fetchone()

        if user is None:
            error = 'Incorrect username.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('toad.index'))

        flash(error)

    return render_template('auth/login.html')
Beispiel #3
0
def decrypt(round):
    """
    Decrypt a given message calling :meth:`webapp.Client.Client.decrypt_message` if it
    is possible.
    Args:
        file_id: the id of the message to decrypt.
    """
    result = None
    try:
        result = g.client.decrypt_file(round)
        isdecrypt = True

    except ValueError:
        error = 'Not enough shares available or corrupted ciphered file'
        isdecrypt = False
        flash(error)

    if isdecrypt:
        with open('download/result', 'wb') as result_file:
            result_file.write(result)
        db = get_db()
        file_hash = db.execute("select hash from encrypted_file where round=?",
                               (round, )).fetchone()['hash']
        return send_file('../download/result',
                         as_attachment=True,
                         attachment_filename=str(file_hash)[2:-1])

    return redirect(url_for('toad.index'))
Beispiel #4
0
def getTempsForCook(cookId, dateSince=None):
    db = wadb.get_db()

    if dateSince == None:
        rtn = db.execute(
            'SELECT TempLogId, EventDate, Temp1, Temp2, Temp3, CookId '
            'FROM TempLog WHERE CookId = ? ', (cookId, )).fetchall()
    else:
        rtn = db.execute(
            'SELECT TempLogId, EventDate, Temp1, Temp2, Temp3, CookId '
            'FROM TempLog WHERE CookId = ? '
            '  AND EventDate > ?', (cookId, dateSince)).fetchall()

    temps = []

    for x in rtn:
        temp = TempLog()
        temp.TempLogId = x[0]
        temp.EventDate = dh.convertTime(x[1])
        temp.Temp1 = float('{0:.2f}'.format(x[2]))
        temp.Temp2 = float('{0:.2f}'.format(x[3]))
        temp.Temp3 = float('{0:.2f}'.format(x[4]))
        temp.CookId = x[5]

        temps.append(temp)

    return temps
Beispiel #5
0
def delete(cookId):
    db = wadb.get_db()

    rtn = db.execute('DELETE FROM TempLog where CookId = ?', (cookId, ))
    rtn = db.execute('DELETE FROM Cooks where CookId = ?', (cookId, ))

    db.commit()
    def decrypt_file(self, round):
        """
        Decrypt a given message if there are at least threshold+1 shares available.
        Args:
            file_id (int): the id of the message

        Returns:
            (bytes) the decrypted message
        """
        shares = self.select_t_valid_share(round)
        c1_power_s = cru.point_from_eth(cru.point_to_eth(cru.Cipher.recover_c1(shares)))

        db = get_db()
        file_info = db.execute('SELECT * FROM encrypted_file WHERE round=?', (round,)).fetchone()

        c2 = (int(file_info['c2x']), int(file_info['c2y']))
        c2 = cru.point_from_eth(c2)

        cid_ipfs = str(file_info['hash'])[2:-1]

        ipfs_api = ipfsApi.Client('127.0.0.1', 8080)
        ipfs_api.get(cid_ipfs)
        with open(cid_ipfs, 'rb') as f:
            cipher_file = f.read()
        os.remove(cid_ipfs)

        encrypt_file = {
            'cipher_file': cipher_file,
            'c1': c1_power_s,
            'c2': c2
        }
        return cru.Cipher.decrypt(encrypt_file)
    def send_share(self, file_info):
        """
        Send a share for a specific message along with its proof of correctness.
        Warning:
            Call the function share on CipherETHDKG, and so send a transaction.
        Args:
            row (dict): a row of the database containing the message
            message_id (int): the id of the message
        """
        db = get_db()
        gsk_sql = db.execute("SELECT * FROM gsk WHERE user_pk=? AND round=?", (self.private_key, file_info['round'])).fetchone()
        gsk = int(gsk_sql['gsk'])
        gpk = multiply(cru.H1, gsk)
        ui = int(gsk_sql['ui'])
        db.close()

        c1 = (int(file_info['c1x']), int(file_info['c1y']))
        c1 = cru.point_from_eth(c1)
        share_for_decryption = multiply(c1, gsk)
        proof = cru.dleq(c1, share_for_decryption, cru.H1, gpk, gsk)
        transaction = self.contract.functions.share_for_dec(
            ui,
            file_info['round'],
            cru.point_to_eth(share_for_decryption),
            proof
        ).buildTransaction(
            {
                'chainId': 1,
                'gas': 200000,
                'nonce': self.w3.eth.getTransactionCount(self.account)
            }
        )
        signed_tx = self.w3.eth.account.signTransaction(transaction, self.private_key)
        txn_hash = self.w3.eth.sendRawTransaction(signed_tx.rawTransaction)
        return txn_hash
 def get_public_keys(self, selected_accounts):
     db = get_db()
     placeholders = ', '.join('?' for account in selected_accounts)
     public_keys = db.execute(
         "SELECT * from eth_public_key WHERE account_address IN (%s)"%placeholders,
         selected_accounts
     ).fetchall()
     return [(int(row['pk_x'],0), int(row['pk_y'],0)) for row in public_keys]
Beispiel #9
0
def insertSubscription(cookId, email):
    db = wadb.get_db()

    db.execute('INSERT INTO Subscriptions (CookId, Email) VALUES(?,?)', (
        cookId,
        email,
    ))

    db.commit()
Beispiel #10
0
def delete(tiltBrewId):
    db = wadb.get_db()

    rtn = db.execute('DELETE FROM TiltData where TiltBrewId = ?',
                     (tiltBrewId, ))
    rtn = db.execute('DELETE FROM TiltBrews where TiltBrewId = ?',
                     (tiltBrewId, ))

    db.commit()
Beispiel #11
0
def getCurrentTiltBrewId():
    db = wadb.get_db()

    rtn = db.execute(
        'SELECT TiltBrewId FROM TiltBrews WHERE EndDate is null').fetchone()

    if rtn is not None:
        return rtn[0]
    else:
        return 0
Beispiel #12
0
def shares_list():
    """
    Show a list of all valid shares received.
    """
    db = get_db()
    shares = db.execute("""
    SELECT share.round, share.ui
    FROM share
    """).fetchall()
    return render_template("toad/shares.html", shares=shares)
Beispiel #13
0
def getCurrentCookId():
    db = wadb.get_db()

    rtn = db.execute(
        'SELECT CookId FROM Cooks WHERE CookEnd is null').fetchone()

    if rtn is not None:
        return rtn[0]
    else:
        return 0
Beispiel #14
0
def getCook(cookId):
    db = wadb.get_db()

    rtn = db.execute(
        'SELECT CookId, Title, CookStart, CookEnd, SmokerTarget, Target, ApproachAlert, ArrivedAlert, OverAlert FROM Cooks WHERE CookId = ?',
        (str(cookId), )).fetchone()
    if rtn:
        cook = objectifyCook(rtn)
    else:
        cook = Cook()
    return cook
Beispiel #15
0
def index():
    """
    Show info about user and received messages.
    """
    db = get_db()
    messages = db.execute("""
    select encrypted_file.*, count(share.round) as "nb_shares"
    from encrypted_file
    left join share on share.round=encrypted_file.round
    group by encrypted_file.round
    """).fetchall()
    return render_template('toad/index.html', messages=messages)
Beispiel #16
0
def startTiltBrew(title, tempTarget, gravityTarget):
    db = wadb.get_db()

    db.execute(
        'INSERT INTO TiltBrews (Title, StartDate, TempTarget, GravityTarget) VALUES(?,?,?,?)',
        (
            title,
            datetime.now(),
            tempTarget,
            gravityTarget,
        ))
    db.commit()
Beispiel #17
0
def getValues():
    db = wadb.get_db()

    rtn = db.execute('SELECT CurrentDataId, Name, Value, LastUpdated '
                        'FROM CurrentData').fetchall()

    values = []

    for x in rtn:
        values.append(objectify(x))
        
    return values
Beispiel #18
0
def startCook(title, smokerTarget, target):
    db = wadb.get_db()

    db.execute(
        'INSERT INTO Cooks (Title, CookStart, SmokerTarget, Target) VALUES(?,?,?,?)',
        (
            title,
            datetime.now(),
            smokerTarget,
            target,
        ))
    db.commit()
Beispiel #19
0
def getTiltBrew(tiltBrewId):
    db = wadb.get_db()

    tiltBrewRtn = db.execute(
        'SELECT TiltBrewId, Title, StartDate, EndDate, TempTarget, GravityTarget FROM TiltBrews WHERE TiltBrewId = ?',
        (str(tiltBrewId), )).fetchone()

    if tiltBrewRtn:
        tiltBrew = objectifyTiltBrew(tiltBrewRtn)
    else:
        tiltBrew = TiltBrew()

    return tiltBrew
Beispiel #20
0
def getCooks():
    db = wadb.get_db()

    rtn = db.execute(
        'SELECT CookId, Title, CookStart, CookEnd, SmokerTarget, Target, ApproachAlert, ArrivedAlert, OverAlert FROM Cooks'
    ).fetchall()
    cooks = []

    if rtn is not None:
        for x in rtn:
            cook = objectifyCook(x)
            cooks.append(cook)
    return cooks
Beispiel #21
0
def update(tiltBrew):
    db = wadb.get_db()
    db.execute(
        'UPDATE TiltBrews '
        'SET Title = ?, '
        'TempTarget = ?, '
        'GravityTarget = ?, '
        'WHERE TiltBrewId = ?', (
            tiltBrew.Title,
            tiltBrew.TempTarget,
            tiltBrew.GravityTarget,
        ))
    db.commit()
Beispiel #22
0
def getTiltBrews():
    db = wadb.get_db()

    rtn = db.execute(
        'SELECT TiltBrewId, Title, StartDate, EndDate, TempTarget, GravityTarget FROM TiltBrews'
    ).fetchall()
    cooks = []

    if rtn is not None:
        for x in rtn:
            cook = objectifyTiltBrew(x)
            cooks.append(cook)
    return cooks
Beispiel #23
0
def register():
    """Register the private key and account address of
    an ethereum account in the database.

    Returns:
        The template of register.html if the form is empty
        and redirect to login page else.
    """
    if request.method == 'POST':
        username = request.form['username']
        private_key = request.form['private_key']
        account_address = request.form['account_address']
        db = get_db()
        error = None

        if not username:
            error = 'Username is required.'
        elif not private_key:
            error = 'Private key is required.'
        elif not account_address:
            error = 'Account address is required'
        elif db.execute(
            'SELECT id FROM user WHERE username = ?', (username,)
        ).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)
        else:
            try:
                pk = keys.PrivateKey(bytearray.fromhex(private_key[2:]))
                if pk.public_key.to_checksum_address() != account_address:
                    error = 'The couple account address and private key is wrong'
            except (ValidationError,ValueError):
                error = "Invalid private key"


        if error is None:
            db.execute(
                'INSERT INTO user (username, private_key, account_address) VALUES (?, ?, ?)',
                (username, private_key, account_address)
            )
            pk_x, pk_y = compute_public_key(private_key)
            db.execute(
                "INSERT INTO eth_public_key (account_address, pk_x, pk_y) VALUES (?,?,?)",
                [account_address, hex(pk_x), hex(pk_y)]
            )
            db.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
Beispiel #24
0
def logTemps(temps, cookId):
    # if DEBUG:
    #    print("value: {0} {1} {2} {3}".format(temps[0], temps[1], temps[2], cookId))

    db = wadb.get_db()
    rtn = db.execute(
        'INSERT INTO TempLog (EventDate, Temp1, Temp2, Temp3, CookId) VALUES(?, ?, ?, ?, ?)',
        (
            datetime.now(),
            temps[0],
            temps[1],
            temps[2],
            cookId,
        ))
    db.commit()
Beispiel #25
0
def group_creation():
    db = get_db()
    accounts = db.execute(
        "SELECT account_address FROM eth_public_key").fetchall()
    accounts = [row['account_address'] for row in accounts]

    if request.method == 'POST':
        selected_accounts = request.form.getlist('accounts')
        try:
            g.client.group_creation(selected_accounts)
        except ValueError:
            flash(
                'The group has already been created, you cannot create a group anymore'
            )
    return render_template('toad/group_creation.html', accounts=accounts)
Beispiel #26
0
def getLastDataForTiltBrew(tiltBrewId):
    db = wadb.get_db()

    rtn = db.execute(
        'SELECT TiltDataId, EventDate, Temp, Gravity, TiltBrewId '
        'FROM TiltData WHERE TiltBrewId = ? '
        'ORDER BY EventDate DESC '
        'LIMIT 1', (tiltBrewId, )).fetchone()

    if rtn:
        tiltData = objectify(rtn)
    else:
        tiltData = TiltData()

    return tiltData
Beispiel #27
0
def getSubscriptionsForCook(cookId):
    db = wadb.get_db()

    rtn = db.execute(
        'SELECT SubscriptionId, CookId, Email '
        'FROM Subscriptions WHERE CookId = ? ', (cookId, )).fetchall()

    subs = []

    for x in rtn:
        sub = Subscription()
        sub.SubscriptionId = x[0]
        sub.CookId = x[1]
        sub.Email = x[2]

        subs.append(sub)

    return subs
Beispiel #28
0
def getDataForTiltBrew(tiltBrewId, dateSince=None):
    db = wadb.get_db()

    if dateSince == None:
        rtn = db.execute(
            'SELECT TiltDataId, EventDate, Temp, Gravity, TiltBrewId '
            'FROM TiltData WHERE TiltBrewId = ? ', (tiltBrewId, )).fetchall()
    else:
        rtn = db.execute(
            'SELECT TiltDataId, EventDate, Temp, Gravity, TiltBrewId '
            'FROM TiltData WHERE TiltBrewId = ? '
            '  AND EventDate > ?', (tiltBrewId, dateSince)).fetchall()

    datas = []

    for row in rtn:
        data = objectify(row)
        datas.append(data)

    return datas
Beispiel #29
0
def send_share(round):
    """
    Send a share for a given message if possible calling the method
    :meth:`webapp.Client.Client.send_share`.
    Args:
        msg_id (int): the id of the message
    """
    db = get_db()
    ui = int(
        db.execute('SELECT * FROM gsk WHERE round=? AND user_pk=?',
                   (round, g.client.private_key)).fetchone()['ui'])
    if db.execute("SELECT * FROM share WHERE round=? AND ui=?",
                  (round, str(ui))).fetchone() is None:
        file_info = db.execute("SELECT * FROM encrypted_file WHERE round=?",
                               (round, )).fetchone()
        g.client.send_share(file_info)
    else:
        flash('error: you have already send your share')

    return redirect(url_for('toad.index'))
Beispiel #30
0
def update(cook):
    db = wadb.get_db()
    db.execute(
        'UPDATE Cooks '
        'SET Title = ?, '
        'SmokerTarget = ?, '
        'Target = ?, '
        'ApproachAlert = ?, '
        'ArrivedAlert = ?, '
        'OverAlert = ? '
        'WHERE CookId = ?', (
            cook.Title,
            cook.SmokerTarget,
            cook.Target,
            cook.ApproachAlert,
            cook.ArrivedAlert,
            cook.OverAlert,
            cook.CookId,
        ))
    db.commit()
Beispiel #31
0
__description__ = 'Simple web app to host and share files'

if os.name == 'nt':
    locale.setlocale(locale.LC_ALL, 'Spanish')  # Windows
else:
    locale.setlocale(locale.LC_ALL, 'es_ES.utf8')  # other (unix)

app = Flask(__name__)
app.config.from_object('config')

if not app.debug:
    # Production runs behind a proxy
    app.wsgi_app = ProxyFix(app.wsgi_app)

# Initialize Database
database = db.get_db()

# Setup Flask-Mail
mailer.init_app(app)

# Loading blueprints
app.register_blueprint(listener)


# Jinja Templates Filters
@app.template_filter()
def floathumanreadable(number: float):
    return locale.format("%.2f", number, True, True)


@app.errorhandler(401)