SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 6c35610da6fd5d294b77fb119012b1d5d357bf91
parent 658f291febba9796bcacaa510bcd475ebc850fc8
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Tue, 22 Dec 2020 01:25:56 +1100

New flask socketio interface with javascript

Diffstat:
Ademo.py | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atemplates/index.html | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 148 insertions(+), 0 deletions(-)

diff --git a/demo.py b/demo.py @@ -0,0 +1,87 @@ +from flask import Flask, render_template, session +from flask_socketio import SocketIO, emit, disconnect +from markupsafe import escape +import time + +import main +import config +import invoice +from pay import bitcoind + + +async_mode = None +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret!' +socket_ = SocketIO(app, async_mode=async_mode) +# thread = None +# thread_lock = Lock() + + +@app.route('/') +def index(): + return render_template('index.html', async_mode=socket_.async_mode) + +@socket_.on('initialise', namespace='/pay') +def test_message(message): + emit('my_response', {'time_left': config.payment_timeout, 'response': message['data']}) + +@socket_.on('payment', namespace='/pay') +def make_payment(message): + print("Requesting payment for {}".format(message['amount'])) + + # Check the amount is a float + amount = message['amount'] + try: + amount = float(amount) + except: + emit('my_response', {'status' : '', 'address' : '', 'amount' : '', 'time_left': 0, 'response': "Invalid payment mount.".format(unconf_paid)}) + amount = None + return + + # Validate amount is a positive float + if not (isinstance(amount, float) and amount >= 0): + emit('my_response', {'status' : '', 'address' : '', 'amount' : '', 'time_left': 0, 'response': "Invalid payment mount.".format(unconf_paid)}) + amount = None + return + + # Initialise this payment + payment = create_invoice(amount, "USD", "wee") + emit('my_response', {'status' : 'Awaiting payment.', 'address' : payment.address, 'amount' : payment.value, 'time_left': config.payment_timeout, 'response' : 'Awaiting payment.'}) + + # Track start_time for payment timeouts + start_time = time.time() + while (time_left := config.payment_timeout - (time.time() - start_time)) > 0: + conf_paid, unconf_paid = payment.check_payment() + print(conf_paid, unconf_paid) + + if conf_paid > payment.value: + print("Invoice {} paid! {} BTC.".format(payment.label, conf_paid)) + payment.paid = True + break + + elif unconf_paid > 0: + emit('my_response', {'status' : 'Discovered {} BTC payment... Waiting for {} confirmations.'.format(config.required_confirmations), 'address' : payment.address, 'amount' : payment.value, 'time_left': time_left, 'response': "Discovered {} BTC payment, waiting for {} confirmations.".format(unconf_paid, config.required_confirmations)}) + socket_.sleep(15) + else: + emit('my_response', {'status' : 'Awaiting payment.', 'address' : payment.address, 'amount' : payment.value, 'time_left': time_left, 'response': 'Awaiting {} BTC payment...'.format(payment.value)}) + socket_.sleep(15) + else: + emit('my_response', {'status' : 'EXPIRED', 'address' : payment.address, 'amount' : payment.value, 'time_left': 0, 'response':'INVOICE EXPIRED'}) + print("Invoice {} expired.".format(payment.label)) + payment.paid = False + + if payment.paid: + print("PAID") + emit('my_response', {'status' : 'Paid!', 'address' : payment.address, 'amount' : payment.value, 'time_left': time_left, 'response': 'Payment finalised.'}) + +def create_invoice(amount, currency, label): + inv = invoice.invoice(amount, currency, label) + + payment = bitcoind.btcd() + payment.load_invoice(inv) + payment.get_address() + return payment + + +if __name__ == '__main__': + socket_.run(app, debug=True) diff --git a/templates/index.html b/templates/index.html @@ -0,0 +1,61 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>BTCPyServer</title> + <script src="//code.jquery.com/jquery-1.12.4.min.js"></script> + <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js"></script> + <script type="text/javascript" charset="utf-8"> + $(document).ready(function() { + namespace = '/pay'; + var socket = io(namespace); + + socket.on('connect', function() { + socket.emit('initialise', {data: 'initialising payment...'}); + }); + + socket.on('my_response', function(msg, cb) { + console.log(msg.response); + $('#status').text(msg.status).html(); + $('#address').text(msg.address).html(); + $('#amount').text(msg.amount).html(); + $('#log').append('<br>' + $('<div/>').text(msg.response).html()); + $('#timer').text(msg.time_left).html(); + if (cb) + cb(); + }); + + $('form#pay').submit(function(event) { + socket.emit('payment', {amount: $('#pay_data').val()}); + return false; + }); + }); + </script> + + <script type="text/javascript" charset="utf-8"> + intervalTimer = setInterval(function () { + var currentTime = document.getElementById('timer').innerHTML; + if (currentTime <= 0) { + currentTime = 1; + } + document.getElementById('timer').innerHTML = Math.round(currentTime - 1); + }, 1000) + </script> + +</head> +<body style="background-color:white;"> + + <h1 style="background-color:white;">BTCPyServer</h1> + + <form id="pay" method="POST" action='#'> + <input type="float" name="pay_data" id="pay_data" placeholder="Amount"> + <input type="submit" value="$ (USD)"> + </form> + + <h2>Payment</h2> + <p><span id="status"></span></p> + <p>Address: <span id="address"></span></p> + <p>Amount: <span id="amount"></span> BTC</p> + <p><span id="timer"></span> seconds left.<p> + <div id="log" ></div> +</body> +</html>