SatSale

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

commit 604c41da5c1ce7bb2536216894c964e27f4ba999
parent d4fc10ae73b7ab809eb697e750fbfc8aca308483
Author: nickfarrow <nicholas.w.farrow@gmail.com>
Date:   Fri, 14 May 2021 16:00:31 +1000

Added support for specifying wallet in config

Diffstat:
M.gitignore | 5+++++
Mconfig.py | 10++++++++--
Mpay/lnd.py | 19++++++++++---------
Mserver.py | 15+++++----------
Dstatic/jquery.min.js | 24------------------------
Mstatic/server_connection.js | 12++++++++++++
6 files changed, 40 insertions(+), 45 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,8 @@ *.macaroon *.cert config.py +__pycache__/ +gateways/__pycache__/ +invoice/__pycache__/ +pay/__pycache__/ +BTCPyment_API_key diff --git a/config.py b/config.py @@ -12,6 +12,9 @@ password = "RPAPASSWORD" # Wallet ("" if single-wallet node, OR wallet name/path as shown in `biitcoin-cli listwallets`) wallet = "" +# File in which API key will be stored +api_key_path = "BTCPyment_API_key" + # SSH tunnel to node (raspberry pi!) # Make sure this command works `ssh HOST@IP -q -N -L 8332:localhost:8332` # This forwards the ports required to talk to the node via RPC (or gRPC in the case of lightning) @@ -30,15 +33,18 @@ required_confirmations = 2 # Global connection attempts connection_attempts = 3 -# Redirect, not currently working.. -redirect = None +# Generic redirect url after payment +redirect = "https://github.com/nickfarrow/btcpyment" # Payment method pay_method = "bitcoind" # Switch payment_method to lnd if you want to use lightning payments instead. And uncomment lnd_dir. #pay_method = "lnd" +# lnd_dir is only needed if you want to copy macaroon and TLS cert locally #lnd_dir = "~/.lnd/" #lnd_rpcport = "10009" +#lnd_macaroon = "invoice.macaroon" +#lnd_cert = "tls.cert" # DO NOT CHANGE THIS TO TRUE UNLESS YOU WANT ALL PAYMENTS TO AUTOMATICALLY # BE CONSIDERED AS PAID. diff --git a/pay/lnd.py b/pay/lnd.py @@ -17,7 +17,7 @@ class lnd(invoice): from lndgrpc import LNDClient - # Copy admin macaroon and tls cert to local machine + # Copy invoice macaroon and tls cert to local machine self.copy_certs() # Conect to lightning node @@ -40,8 +40,7 @@ class lnd(invoice): if test: print("Getting lnd info...") - info = self.lnd.get_info() - print(info) + self.lnd.list_invoices() print("Successfully contacted lnd.") break @@ -64,20 +63,22 @@ class lnd(invoice): # Copy tls and macaroon certs from remote machine. def copy_certs(self): - self.certs = {'tls' : 'tls.cert', 'macaroon' : 'admin.macaroon'} + self.certs = {'tls' : config.lnd_cert, 'macaroon' : config.lnd_macaroon} - if (not os.path.isfile("tls.cert")) or (not os.path.isfile("admin.macaroon")): + if (not os.path.isfile(config.lnd_cert)) or (not os.path.isfile(config.lnd_macaroon)): try: tls_file = os.path.join(config.lnd_dir, "tls.cert") macaroon_file = os.path.join( - config.lnd_dir, "data/chain/bitcoin/mainnet/admin.macaroon" + config.lnd_dir, "data/chain/bitcoin/mainnet/invoice.macaroon" ) # SSH copy if config.tunnel_host is not None: print( - "Could not find tls.cert or admin.macaroon in BTCPyment folder. \ - Attempting to download from remote lnd directory." + "Could not find {} or {} \ + Attempting to download from remote lnd directory.".format( + config.lnd_cert, config.lnd_macaroon + ) ) subprocess.run( @@ -100,7 +101,7 @@ class lnd(invoice): print(e) print("Failed to copy tls and macaroon files to local machine.") else: - print("Found tls.cert and admin.macaroon.") + print("Found tls.cert and invoice.macaroon.") return # Create lightning invoice diff --git a/server.py b/server.py @@ -16,10 +16,10 @@ app = Flask(__name__) # Load an API key or create a new one if os.path.exists("BTCPyment_API_key"): - with open("BTCPyment_API_key", "r") as f: + with open(config.api_key_path, "r") as f: app.config["SECRET_KEY"] = f.read().strip() else: - with open("BTCPyment_API_key", "w") as f: + with open(config.api_key_path, "w") as f: app.config["SECRET_KEY"] = os.urandom(64).hex() f.write(app.config["SECRET_KEY"]) @@ -53,6 +53,7 @@ def index(): # /pay is the main payment method for initiating a payment websocket. @app.route("/pay") def payment_page(): + # Arguments passed to HTML and also server_connection.js params = dict(request.args) params['lnd_enabled'] = (config.pay_method == "lnd") # Render payment page with the request arguments (?amount= etc.) @@ -111,14 +112,6 @@ def make_payment(payload): print("Successfully confirmed payment via webhook.") update_status(payment, "Order confirmed.") - # Redirect after payment - # TODO: add a delay here. Test. - if config.redirect is not None: - print("Redirecting to {}".format(config.redirect)) - return redirect(config.redirect) - else: - print("No redirect, closing.") - return @@ -140,6 +133,8 @@ def update_status(payment, status, console_status=True): "time_left": payment.time_left, "uuid": payment.uuid, "response": payment.response, + "paid": payment.paid, + "redirect": config.redirect # Can later expand to invoice specific redirects. }, ) return diff --git a/static/jquery.min.js b/static/jquery.min.js @@ -1,24 +0,0 @@ -$(document).ready(function() { - namespace = '/pay'; - var socket = io(namespace); - - socket.on('connect', function() { - socket.emit('initialise', {'data': 'initialising payment...'}); - }); - - socket.on('payresponse', 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(), 'label' : null}); - return false; - }); -}); diff --git a/static/server_connection.js b/static/server_connection.js @@ -27,6 +27,18 @@ function initiate(payment_data) { // on the contents and status of the payment. conditionalPageLogic(msg) + console.log(msg); + + // Actions if paid + if (msg.paid == true) { + + // Redirect if paid + if (msg.redirect != null) { + setTimeout(() => { window.location.replace(msg.redirect); }, 5000); + } + } + + // If close? I forget.. if (cb) cb();