2020-09-21 08:03:04 +01:00
|
|
|
from time import sleep
|
2020-08-31 05:10:28 +01:00
|
|
|
from io import BytesIO
|
|
|
|
from base64 import b64encode
|
|
|
|
from qrcode import make as qrcode_make
|
2020-09-11 21:59:48 +01:00
|
|
|
from decimal import Decimal
|
2020-09-20 09:53:30 +01:00
|
|
|
from flask import request, render_template, session, jsonify
|
|
|
|
from flask import redirect, url_for, current_app, flash
|
2020-08-27 06:50:08 +01:00
|
|
|
from flask_login import login_required, current_user
|
2020-09-20 09:53:30 +01:00
|
|
|
from socket import socket
|
|
|
|
from datetime import datetime
|
2020-08-27 06:50:08 +01:00
|
|
|
from wowstash.blueprints.wallet import wallet_bp
|
2020-09-21 08:03:04 +01:00
|
|
|
from wowstash.library.docker import docker
|
2020-09-20 09:53:30 +01:00
|
|
|
from wowstash.library.jsonrpc import Wallet, to_atomic
|
|
|
|
from wowstash.library.cache import cache
|
2020-09-11 21:59:48 +01:00
|
|
|
from wowstash.forms import Send
|
2020-09-20 09:53:30 +01:00
|
|
|
from wowstash.factory import db
|
|
|
|
from wowstash.models import User
|
|
|
|
from wowstash import config
|
2020-08-27 06:50:08 +01:00
|
|
|
|
|
|
|
|
2020-09-20 09:53:30 +01:00
|
|
|
@wallet_bp.route('/wallet/loading')
|
|
|
|
@login_required
|
|
|
|
def loading():
|
|
|
|
if current_user.wallet_connected and current_user.wallet_created:
|
2020-09-21 08:03:04 +01:00
|
|
|
sleep(1)
|
2020-09-20 09:53:30 +01:00
|
|
|
return redirect(url_for('wallet.dashboard'))
|
|
|
|
return render_template('wallet/loading.html')
|
|
|
|
|
|
|
|
@wallet_bp.route('/wallet/dashboard')
|
2020-08-27 06:50:08 +01:00
|
|
|
@login_required
|
|
|
|
def dashboard():
|
2020-09-11 21:59:48 +01:00
|
|
|
send_form = Send()
|
2020-08-31 05:10:28 +01:00
|
|
|
_address_qr = BytesIO()
|
2020-09-20 09:53:30 +01:00
|
|
|
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 wallet.connected:
|
|
|
|
return redirect(url_for('wallet.loading'))
|
|
|
|
|
|
|
|
address = wallet.get_address()
|
|
|
|
transfers = wallet.get_transfers()
|
2020-08-30 20:27:35 +01:00
|
|
|
for type in transfers:
|
|
|
|
for tx in transfers[type]:
|
|
|
|
all_transfers.append(tx)
|
2020-09-20 09:53:30 +01:00
|
|
|
balances = wallet.get_balances()
|
2020-09-21 08:03:04 +01:00
|
|
|
qr_uri = f'wownero:{address}?tx_description={current_user.email}'
|
2020-08-31 05:10:28 +01:00
|
|
|
address_qr = qrcode_make(qr_uri).save(_address_qr)
|
|
|
|
qrcode = b64encode(_address_qr.getvalue()).decode()
|
2020-08-27 06:50:08 +01:00
|
|
|
return render_template(
|
2020-09-20 09:53:30 +01:00
|
|
|
'wallet/dashboard.html',
|
|
|
|
transfers=all_transfers,
|
2020-08-30 20:27:35 +01:00
|
|
|
balances=balances,
|
2020-09-20 09:53:30 +01:00
|
|
|
address=address,
|
2020-09-11 21:59:48 +01:00
|
|
|
qrcode=qrcode,
|
|
|
|
send_form=send_form,
|
2020-09-20 09:53:30 +01:00
|
|
|
user=current_user
|
2020-08-27 06:50:08 +01:00
|
|
|
)
|
2020-09-11 21:59:48 +01:00
|
|
|
|
2020-09-20 09:53:30 +01:00
|
|
|
@wallet_bp.route('/wallet/connect')
|
|
|
|
@login_required
|
|
|
|
def connect():
|
|
|
|
if current_user.wallet_connected is False:
|
2020-09-21 08:03:04 +01:00
|
|
|
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
|
|
|
|
db.session.commit()
|
2020-09-20 09:53:30 +01:00
|
|
|
|
2020-09-21 08:03:04 +01:00
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
@wallet_bp.route('/wallet/create')
|
|
|
|
@login_required
|
|
|
|
def create():
|
|
|
|
if current_user.wallet_created is False:
|
|
|
|
docker.create_wallet(current_user.id)
|
|
|
|
current_user.wallet_created = True
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return 'ok'
|
2020-09-20 09:53:30 +01:00
|
|
|
|
|
|
|
@wallet_bp.route('/wallet/status')
|
|
|
|
@login_required
|
|
|
|
def status():
|
|
|
|
data = {
|
2020-09-21 08:03:04 +01:00
|
|
|
'created': current_user.wallet_created,
|
|
|
|
'connected': current_user.wallet_connected,
|
|
|
|
'port': current_user.wallet_port,
|
|
|
|
'container': current_user.wallet_container
|
2020-09-20 09:53:30 +01:00
|
|
|
}
|
|
|
|
return jsonify(data)
|
|
|
|
|
|
|
|
@wallet_bp.route('/wallet/send', methods=['GET', 'POST'])
|
2020-09-11 21:59:48 +01:00
|
|
|
@login_required
|
|
|
|
def send():
|
|
|
|
send_form = Send()
|
2020-09-20 09:53:30 +01:00
|
|
|
redirect_url = url_for('wallet.dashboard') + '#send'
|
2020-09-11 21:59:48 +01:00
|
|
|
if send_form.validate_on_submit():
|
|
|
|
address = str(send_form.address.data)
|
2020-09-20 09:53:30 +01:00
|
|
|
user = User.query.get(current_user.id)
|
2020-09-11 21:59:48 +01:00
|
|
|
|
|
|
|
# Check if Wownero wallet is available
|
|
|
|
if wallet.connected is False:
|
|
|
|
flash('Wallet RPC interface is unavailable at this time. Try again later.')
|
|
|
|
return redirect(redirect_url)
|
|
|
|
|
|
|
|
# Check if user funds flag is locked
|
|
|
|
if current_user.funds_locked:
|
|
|
|
flash('You currently have transactions pending and transfers are locked. Try again later.')
|
|
|
|
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.')
|
|
|
|
return redirect(redirect_url)
|
|
|
|
|
|
|
|
# Make sure the amount provided is a number
|
|
|
|
try:
|
2020-09-20 09:53:30 +01:00
|
|
|
amount = to_atomic(Decimal(send_form.amount.data))
|
2020-09-11 21:59:48 +01:00
|
|
|
except:
|
|
|
|
flash('Invalid Wownero amount specified.')
|
|
|
|
return redirect(redirect_url)
|
|
|
|
|
2020-09-20 09:53:30 +01:00
|
|
|
# Make sure the amount does not exceed the balance
|
|
|
|
if amount >= user.balance:
|
|
|
|
flash('Not enough funds to transfer that much.')
|
|
|
|
return redirect(redirect_url)
|
|
|
|
|
2020-09-11 21:59:48 +01:00
|
|
|
# Lock user funds
|
|
|
|
user.funds_locked = True
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# Queue the transaction
|
2020-09-20 09:53:30 +01:00
|
|
|
tx = TransferQueue(
|
|
|
|
user=user.id,
|
2020-09-11 21:59:48 +01:00
|
|
|
address=address,
|
2020-09-20 09:53:30 +01:00
|
|
|
amount=amount
|
2020-09-11 21:59:48 +01:00
|
|
|
)
|
|
|
|
db.session.add(tx)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# Redirect back
|
|
|
|
flash('Successfully queued transfer.')
|
|
|
|
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)
|