From bf755f8ac09df4337e67ebbddc5c410bad2895fd Mon Sep 17 00:00:00 2001 From: ThomasV Date: Sun, 7 Jun 2015 19:11:54 +0200 Subject: [PATCH] jsonrpc interface --- gui/jsonrpc.py | 120 ++++++++++++++++++++++++++++++++++++++++++++ lib/commands.py | 2 +- lib/payrequest.html | 82 ++++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 gui/jsonrpc.py create mode 100644 lib/payrequest.html diff --git a/gui/jsonrpc.py b/gui/jsonrpc.py new file mode 100644 index 000000000..0920fb3de --- /dev/null +++ b/gui/jsonrpc.py @@ -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 . + + +""" +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 + + + +""" diff --git a/lib/commands.py b/lib/commands.py index 4a4e67b49..afd9130d0 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -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.add_argument("url", nargs='?', default=None, help="bitcoin URI (or bip70 file)") 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("-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") diff --git a/lib/payrequest.html b/lib/payrequest.html new file mode 100644 index 000000000..da388b3ea --- /dev/null +++ b/lib/payrequest.html @@ -0,0 +1,82 @@ + + + + Payment request + + + + + + + + + + + + + +
+

+

+

+
+ Pay with Bitcoin +
+
+
+

+
+
+ + + +