tdon't use bitcoinrpc anymore - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 9f4eb222ac6a645c730b477c897f993290679138 DIR parent 928498d695745bb6a527f33d06dbbdbb362248f0 HTML Author: ThomasV <thomasv@gitorious> Date: Fri, 23 Dec 2011 13:14:37 +0300 don't use bitcoinrpc anymore Diffstat: M server/README | 10 +++++----- D server/patches/bitcoinrpc_connecti… | 41 ------------------------------- M server/server.py | 38 +++++++++++++++++++------------ 3 files changed, 28 insertions(+), 61 deletions(-) --- DIR diff --git a/server/README b/server/README t@@ -7,9 +7,10 @@ Language: Python Features: * The server uses a patched version of the Bitcoin daemon that can -forward transactions, and bitcoin-abe * The server code is open -source. Anyone can run a server, removing single points of failure -concerns. +forward transactions, and bitcoin-abe + +* The server code is open source. Anyone can run a server, removing +single points of failure concerns. * The server knows which set of Bitcoin addresses belong to the same wallet, which might raise concerns about anonymity. However, it should t@@ -22,8 +23,7 @@ INSTALL 2. install bitcoin-abe : https://github.com/jtobey/bitcoin-abe -3. download bitcoin-python: https://github.com/laanwj/bitcoin-python - patch it with bitcoinrpc_connection.py.diff +3. install jsonrpclib: code.google.com/p/jsonrpclib/ 4. launch the server: nohup python -u server.py > /var/log/electrum.log & DIR diff --git a/server/patches/bitcoinrpc_connection.py.diff b/server/patches/bitcoinrpc_connection.py.diff t@@ -1,41 +0,0 @@ -diff --git a/src/bitcoinrpc/connection.py b/src/bitcoinrpc/connection.py -index 960cf5d..79e04bf 100644 ---- a/src/bitcoinrpc/connection.py -+++ b/src/bitcoinrpc/connection.py -@@ -67,7 +67,35 @@ class BitcoinConnection(object): - return self.proxy.getblockcount() - except JSONRPCException,e: - raise _wrap_exception(e.error) -- -+ -+ def getmemorypool(self): -+ """sign""" -+ try: -+ return self.proxy.getmemorypool() -+ except JSONRPCException,e: -+ raise _wrap_exception(e.error) -+ -+ def importtransaction(self, tx): -+ """sign""" -+ try: -+ return self.proxy.importtransaction(tx) -+ except JSONRPCException,e: -+ raise _wrap_exception(e.error) -+ -+ def signmessage(self, address,message): -+ """sign""" -+ try: -+ return self.proxy.signmessage(address, message) -+ except JSONRPCException,e: -+ raise _wrap_exception(e.error) -+ -+ def verifymessage(self, address, sig, message): -+ """verif""" -+ try: -+ return self.proxy.verifymessage(address, sig, message ) -+ except JSONRPCException,e: -+ raise _wrap_exception(e.error) -+ - def getblocknumber(self): - """ - Returns the block number of the latest block in the longest block chain. DIR diff --git a/server/server.py b/server/server.py t@@ -27,13 +27,14 @@ Todo: import time, socket, operator, thread, ast, sys,re import psycopg2, binascii -import bitcoinrpc from Abe.abe import hash_to_address, decode_check_address from Abe.DataStore import DataStore as Datastore_class from Abe import DataStore, readconf, BCDataStream, deserialize, util, base58 import ConfigParser +from json import dumps, loads +import urllib config = ConfigParser.ConfigParser() # set some defaults, which will be overwritten by the config file t@@ -57,6 +58,7 @@ except: print "Could not read electrum.conf. I will use the default values." password = config.get('server','password') +bitcoind_url = 'http://%s:%s@%s:%s/' % ( config.get('bitcoind','user'), config.get('bitcoind','password'), config.get('bitcoind','host'), config.get('bitcoind','port')) stopping = False block_number = -1 t@@ -317,15 +319,18 @@ class MyStore(Datastore_class): + def send_tx(tx): - import bitcoinrpc - conn = bitcoinrpc.connect_to_local() + postdata = dumps({"method": 'importtransaction', 'params': [tx], 'id':'jsonrpc'}) + respdata = urllib.urlopen(bitcoind_url, postdata).read() try: - v = conn.importtransaction(tx) + v = loads(respdata)['result'] except: v = "error: transaction rejected by memorypool" return v + + def random_string(N): import random, string return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N)) t@@ -364,7 +369,7 @@ def get_cache(pw,addr): def cmd_poll(session_id): session = sessions.get(session_id) if session is None: - print time.asctime(), "session not found", session_id, ipaddr + print time.asctime(), "session not found", session_id out = repr( (-1, {})) else: t1 = time.time() t@@ -412,7 +417,7 @@ def new_session(addresses, version, ipaddr): sessions[session_id]['last_time'] = time.time() return out -def update_session(session_id,addresses): +def update_session(session_id,addresses,ipaddr): print time.strftime("[%d/%m/%Y-%H:%M:%S]"), "update session", ipaddr, addresses[0] if addresses else addresses, len(addresses) sessions[session_id]['addresses'] = {} for a in addresses: t@@ -488,7 +493,7 @@ def do_command(cmd, data, ipaddr): except: print "error" return None - out = update_session(session_id,addresses) + out = update_session(session_id,addresses,ipaddr) elif cmd == 'bccapi_login': t@@ -570,12 +575,12 @@ def do_command(cmd, data, ipaddr): def memorypool_update(store): ds = BCDataStream.BCDataStream() store.mempool_keys = [] - conn = bitcoinrpc.connect_to_local() - try: - v = conn.getmemorypool() - except: - print "cannot contact bitcoin daemon" - return + + postdata = dumps({"method": 'getmemorypool', 'params': [], 'id':'jsonrpc'}) + respdata = urllib.urlopen(bitcoind_url, postdata).read() + v = loads(respdata)['result'] + + v = v['transactions'] for hextx in v: ds.clear() t@@ -651,13 +656,14 @@ def jsonrpc_thread(store): # see http://code.google.com/p/jsonrpclib/ from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer server = SimpleJSONRPCServer(('localhost', 8080)) - server.register_function(store.get_history, 'history') server.register_function(lambda : peer_list.values(), 'peers') server.register_function(cmd_stop, 'stop') server.register_function(cmd_load, 'load') server.register_function(lambda : block_number, 'blocks') server.register_function(clear_cache, 'clear_cache') server.register_function(get_cache, 'get_cache') + server.register_function(send_tx, 'blockchain.transaction.broadcast') + server.register_function(store.get_history, 'blockchain.address.get_history') server.serve_forever() t@@ -681,7 +687,9 @@ if __name__ == '__main__': elif cmd == 'get_cache': out = server.get_cache(password,sys.argv[2]) elif cmd == 'h': - out = server.history(sys.argv[2]) + out = server.blockchain.address.get_history(sys.argv[2]) + elif cmd == 'tx': + out = server.blockchain.transaction.broadcast(sys.argv[2]) elif cmd == 'b': out = server.blocks() print out