def connect(): if current_user.wallet_created is False: data = { 'result': 'fail', 'message': 'Wallet not yet created' } return jsonify(data) if current_user.wallet_connected is False: wallet = docker.start_wallet(current_user.id) port = docker.get_port(wallet) current_user.wallet_connected = docker.container_exists(wallet) current_user.wallet_port = port current_user.wallet_container = wallet current_user.wallet_start = datetime.utcnow() db.session.commit() capture_event(current_user.id, 'start_wallet') data = { 'result': 'success', 'message': 'Wallet has been connected' } else: data = { 'result': 'fail', 'message': 'Wallet is already connected' } return jsonify(data)
def login(): form = Login() if current_user.is_authenticated: flash('Already registered and authenticated.') return redirect(url_for('wallet.dashboard')) if form.validate_on_submit(): # Check if user doesn't exist user = User.query.filter_by(email=form.email.data).first() if not user: flash('Invalid username or password.') return redirect(url_for('auth.login')) # Check if password is correct password_matches = bcrypt.check_password_hash(user.password, form.password.data) if not password_matches: flash('Invalid username or password.') return redirect(url_for('auth.login')) # Capture event, login user, and redirect to wallet page capture_event(user.id, 'login') login_user(user) return redirect(url_for('wallet.dashboard')) return render_template("auth/login.html", form=form)
def register(): form = Register() if current_user.is_authenticated: flash('Already registered and authenticated.') return redirect(url_for('wallet.dashboard')) if form.validate_on_submit(): # Check if email already exists user = User.query.filter_by(email=form.email.data).first() if user: flash('This email is already registered.') return redirect(url_for('auth.login')) # Save new user user = User( email=form.email.data, password=bcrypt.generate_password_hash( form.password.data).decode('utf8'), ) db.session.add(user) db.session.commit() # Capture event, login user and redirect to wallet page capture_event(user.id, 'register') login_user(user) return redirect(url_for('wallet.setup')) return render_template("auth/register.html", form=form)
def logout(): if current_user.is_authenticated: docker.stop_container(current_user.wallet_container) capture_event(current_user.id, 'stop_container') current_user.clear_wallet_data() capture_event(current_user.id, 'logout') logout_user() return redirect(url_for('meta.index'))
def send(): send_form = Send() redirect_url = url_for('wallet.dashboard') + '#send' wallet = Wallet( proto='http', host='127.0.0.1', port=current_user.wallet_port, username=current_user.id, password=current_user.wallet_password ) if send_form.validate_on_submit(): address = str(send_form.address.data) user = User.query.get(current_user.id) # Check if Wownero wallet is available if wallet.connected is False: flash('Wallet RPC interface is unavailable at this time. Try again later.') capture_event(user.id, 'tx_fail_rpc_unavailable') return redirect(redirect_url) # Quick n dirty check to see if address is WOW if len(address) not in [97, 108]: flash('Invalid Wownero address provided.') capture_event(user.id, 'tx_fail_address_invalid') return redirect(redirect_url) # Check if we're sweeping or not if send_form.amount.data == 'all': tx = wallet.transfer(address, None, 'sweep_all') else: # Make sure the amount provided is a number try: amount = to_atomic(Decimal(send_form.amount.data)) except: flash('Invalid Wownero amount specified.') capture_event(user.id, 'tx_fail_amount_invalid') return redirect(redirect_url) # Send transfer tx = wallet.transfer(address, amount) # Inform user of result and redirect if 'message' in tx: msg = tx['message'].capitalize() msg_lower = tx['message'].replace(' ', '_').lower() flash(f'There was a problem sending the transaction: {msg}') capture_event(user.id, f'tx_fail_{msg_lower}') else: flash('Successfully sent transfer.') capture_event(user.id, 'tx_success') return redirect(redirect_url) else: for field, errors in send_form.errors.items(): flash(f'{send_form[field].label}: {", ".join(errors)}') return redirect(redirect_url)
def create(): if current_user.wallet_created is False: c = docker.create_wallet(current_user.id) cache.store_data(f'init_wallet_{current_user.id}', 30, c) capture_event(current_user.id, 'create_wallet') current_user.wallet_created = True db.session.commit() return redirect(url_for('wallet.loading')) else: return redirect(url_for('wallet.dashboard'))
def delete(): form = Delete() if form.validate_on_submit(): docker.stop_container(current_user.wallet_container) capture_event(current_user.id, 'stop_container') sleep(1) docker.delete_wallet_data(current_user.id) capture_event(current_user.id, 'delete_wallet') current_user.clear_wallet_data(reset_password=True, reset_wallet=True) flash('Successfully deleted wallet data') return redirect(url_for('wallet.setup')) else: flash('Please confirm deletion of the account') return redirect(url_for('wallet.dashboard'))
def dashboard(): send_form = Send() delete_form = Delete() _address_qr = BytesIO() all_transfers = list() wallet = Wallet( proto='http', host='127.0.0.1', port=current_user.wallet_port, username=current_user.id, password=current_user.wallet_password ) if not docker.container_exists(current_user.wallet_container): current_user.clear_wallet_data() return redirect(url_for('wallet.loading')) if not wallet.connected: sleep(1.5) return redirect(url_for('wallet.loading')) address = wallet.get_address() transfers = wallet.get_transfers() for type in transfers: for tx in transfers[type]: all_transfers.append(tx) balances = wallet.get_balances() qr_uri = f'wownero:{address}?tx_description={current_user.email}' address_qr = qrcode_make(qr_uri).save(_address_qr) qrcode = b64encode(_address_qr.getvalue()).decode() seed = wallet.seed() spend_key = wallet.spend_key() view_key = wallet.view_key() capture_event(current_user.id, 'load_dashboard') return render_template( 'wallet/dashboard.html', transfers=all_transfers, sorted_txes=get_sorted_txes(transfers), balances=balances, address=address, qrcode=qrcode, send_form=send_form, delete_form=delete_form, user=current_user, seed=seed, spend_key=spend_key, view_key=view_key, )
def setup(): if current_user.wallet_created: return redirect(url_for('wallet.dashboard')) else: restore_form = Restore() if restore_form.validate_on_submit(): c = docker.create_wallet(current_user.id, restore_form.seed.data) cache.store_data(f'init_wallet_{current_user.id}', 30, c) capture_event(current_user.id, 'restore_wallet') current_user.wallet_created = True db.session.commit() return redirect(url_for('wallet.loading')) else: return render_template( 'wallet/setup.html', restore_form=restore_form )