mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-05 05:15:12 +00:00
jsonrpc interface
This commit is contained in:
parent
48e53498db
commit
bf755f8ac0
3 changed files with 203 additions and 1 deletions
120
gui/jsonrpc.py
Normal file
120
gui/jsonrpc.py
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Electrum - lightweight Bitcoin client
|
||||||
|
# Copyright (C) 2015 Thomas Voegtlin
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
jsonrpc interface for webservers
|
||||||
|
"""
|
||||||
|
|
||||||
|
import socket, os
|
||||||
|
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer, SimpleJSONRPCRequestHandler
|
||||||
|
|
||||||
|
from electrum.wallet import WalletStorage, Wallet
|
||||||
|
from electrum.commands import known_commands, Commands
|
||||||
|
|
||||||
|
|
||||||
|
class RequestHandler(SimpleJSONRPCRequestHandler):
|
||||||
|
|
||||||
|
def do_OPTIONS(self):
|
||||||
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
def end_headers(self):
|
||||||
|
self.send_header("Access-Control-Allow-Headers",
|
||||||
|
"Origin, X-Requested-With, Content-Type, Accept")
|
||||||
|
self.send_header("Access-Control-Allow-Origin", "*")
|
||||||
|
SimpleJSONRPCRequestHandler.end_headers(self)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class ElectrumGui:
|
||||||
|
|
||||||
|
def __init__(self, config, network):
|
||||||
|
self.network = network
|
||||||
|
self.config = config
|
||||||
|
host = config.get('rpchost', 'localhost')
|
||||||
|
port = config.get('rpcport', 7777)
|
||||||
|
self.server = SimpleJSONRPCServer((host, port), requestHandler=RequestHandler)
|
||||||
|
self.server.socket.settimeout(1)
|
||||||
|
self.server.register_function(self.do_getrequest, 'getrequest')
|
||||||
|
|
||||||
|
def do_getrequest(self, key):
|
||||||
|
# fixme: we load and sync the wallet on each request
|
||||||
|
# the wallet should be synchronized in the daemon instead
|
||||||
|
storage = WalletStorage(self.config.get_wallet_path())
|
||||||
|
if not storage.file_exists:
|
||||||
|
raise BaseException("Wallet not found")
|
||||||
|
wallet = Wallet(storage)
|
||||||
|
wallet.start_threads(self.network)
|
||||||
|
cmd_runner = Commands(self.config, wallet, self.network)
|
||||||
|
result = cmd_runner.getrequest(key)
|
||||||
|
wallet.stop_threads()
|
||||||
|
return result
|
||||||
|
|
||||||
|
def main(self, url):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
self.server.handle_request()
|
||||||
|
except socket.timeout:
|
||||||
|
continue
|
||||||
|
except:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
* replace merchant script:
|
||||||
|
* client process that connects to the daemon, receives notifications and pushes callbacks
|
||||||
|
* it requires a new gui type
|
||||||
|
|
||||||
|
electrum -g jsonrpc &
|
||||||
|
or: electrum daemon loadwallet
|
||||||
|
|
||||||
|
|
||||||
|
use the daemon:
|
||||||
|
pros: single process instead of 2
|
||||||
|
jsonrpc is not really a gui
|
||||||
|
|
||||||
|
the wallet sould be synced in the daemon (so that we can add requests from the gui, list them, etc)
|
||||||
|
|
||||||
|
short-term solution:
|
||||||
|
* serve jsonrpc requests with the daemon
|
||||||
|
* load wallet on each command
|
||||||
|
* open some rpc commands to public
|
||||||
|
|
||||||
|
* other solution:
|
||||||
|
* use a database
|
||||||
|
* 'addrequest' writes request to database
|
||||||
|
|
||||||
|
the daemon does not need to load and sync the wallet
|
||||||
|
|
||||||
|
* daemon loadwallet
|
||||||
|
*
|
||||||
|
|
||||||
|
|
||||||
|
Private methods:
|
||||||
|
wallet commands
|
||||||
|
|
||||||
|
Public methods:
|
||||||
|
- getrequest(key)
|
||||||
|
- paymentack():
|
||||||
|
is sent as the body of the POST
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
|
@ -708,7 +708,7 @@ def get_parser(run_gui, run_daemon, run_cmdline):
|
||||||
parser_gui = subparsers.add_parser('gui', parents=[parent_parser], description="Run Electrum's Graphical User Interface.", help="Run GUI (default)")
|
parser_gui = subparsers.add_parser('gui', parents=[parent_parser], description="Run Electrum's Graphical User Interface.", help="Run GUI (default)")
|
||||||
parser_gui.add_argument("url", nargs='?', default=None, help="bitcoin URI (or bip70 file)")
|
parser_gui.add_argument("url", nargs='?', default=None, help="bitcoin URI (or bip70 file)")
|
||||||
parser_gui.set_defaults(func=run_gui)
|
parser_gui.set_defaults(func=run_gui)
|
||||||
parser_gui.add_argument("-g", "--gui", dest="gui", help="select graphical user interface", choices=['qt', 'lite', 'gtk', 'text', 'stdio'])
|
parser_gui.add_argument("-g", "--gui", dest="gui", help="select graphical user interface", choices=['qt', 'lite', 'gtk', 'text', 'stdio', 'jsonrpc'])
|
||||||
parser_gui.add_argument("-m", action="store_true", dest="hide_gui", default=False, help="hide GUI on startup")
|
parser_gui.add_argument("-m", action="store_true", dest="hide_gui", default=False, help="hide GUI on startup")
|
||||||
parser_gui.add_argument("-L", "--lang", dest="language", default=None, help="default language used in GUI")
|
parser_gui.add_argument("-L", "--lang", dest="language", default=None, help="default language used in GUI")
|
||||||
parser_gui.add_argument("-o", "--offline", action="store_true", dest="offline", default=False, help="Run the GUI offline")
|
parser_gui.add_argument("-o", "--offline", action="store_true", dest="offline", default=False, help="Run the GUI offline")
|
||||||
|
|
82
lib/payrequest.html
Normal file
82
lib/payrequest.html
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Payment request</title>
|
||||||
|
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
|
||||||
|
<script type="text/javascript" src="https://raw.github.com/datagraph/jquery-jsonrpc/master/jquery.jsonrpc.js"></script>
|
||||||
|
<script type="text/javascript" src="https://raw.githubusercontent.com/davidshimjs/qrcodejs/master/qrcode.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
|
||||||
|
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
|
||||||
|
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(document).ready(function() {
|
||||||
|
$.jsonRPC.setup({
|
||||||
|
endPoint : "http://localhost:7777",
|
||||||
|
namespace : ""
|
||||||
|
});
|
||||||
|
if (window.location.hash) {
|
||||||
|
$.jsonRPC.request('getrequest', {
|
||||||
|
params : [window.location.hash.substr(1)],
|
||||||
|
success : function(data) {
|
||||||
|
new QRCode(document.getElementById("qrcode"), data.result.URI);
|
||||||
|
$("<p />").text(data.result.reason).appendTo($("p#reason"));
|
||||||
|
$("<p />").text(data.result.amount + "BTC").appendTo($("p#amount"));
|
||||||
|
$("a").attr("href", data.result.URI);
|
||||||
|
$("<p />").text("Powered by Electrum").appendTo($("p#powered"));
|
||||||
|
|
||||||
|
$(function () {
|
||||||
|
var current;
|
||||||
|
var max = 100;
|
||||||
|
var initial = data.result.timestamp;
|
||||||
|
var duration = data.result.expiration;
|
||||||
|
var current = 100 * (Math.floor(Date.now()/1000) - initial)/duration;
|
||||||
|
$("#progressbar").progressbar({
|
||||||
|
value: current,
|
||||||
|
max: max
|
||||||
|
});
|
||||||
|
function update() {
|
||||||
|
current = 100 * (Math.floor(Date.now()/1000) - initial)/duration;
|
||||||
|
$("#progressbar").progressbar({
|
||||||
|
value: current
|
||||||
|
});
|
||||||
|
if (duration && current >= max) {
|
||||||
|
$("#container").html("expired:", duration);
|
||||||
|
}
|
||||||
|
if(!duration) clearInterval(interval);
|
||||||
|
};
|
||||||
|
var interval = setInterval(update, 1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
},
|
||||||
|
error : function(data) {
|
||||||
|
$("<p />").text("error").appendTo($("p#error"));
|
||||||
|
$("<p />").text(data.error.message).appendTo($("p#error"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="container" style="width:20em; text-align:center; margin:auto; font-family:arial, serif;">
|
||||||
|
<p id="error"></p>
|
||||||
|
<p id="reason"></p>
|
||||||
|
<p id="amount"></p>
|
||||||
|
<div style="background-color:#7777aa; border-radius: 5px; padding:10px;">
|
||||||
|
<a style="color:#ffffff; text-decoration:none;" id="paylink">Pay with Bitcoin</a>
|
||||||
|
</div>
|
||||||
|
<br/>
|
||||||
|
<div id="qrcode" align="center"></div>
|
||||||
|
<p id="powered" style="font-size:80%;"></p>
|
||||||
|
<div id="progressbar"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
Loading…
Add table
Reference in a new issue