2012-01-19 08:11:36 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2011 thomasv@gitorious
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
|
|
|
|
import random, socket, ast
|
2012-03-23 06:37:02 -07:00
|
|
|
import thread, threading, traceback, sys, time, json, Queue
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-03-14 10:00:24 -07:00
|
|
|
DEFAULT_TIMEOUT = 5
|
2012-03-19 07:57:47 -07:00
|
|
|
DEFAULT_SERVERS = ['electrum.bitcoins.sk','ecdsa.org','electrum.novit.ro'] # list of default servers
|
2012-03-14 10:00:24 -07:00
|
|
|
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-03-30 01:48:32 -07:00
|
|
|
def old_to_new(s):
|
|
|
|
s = s.replace("'blk_hash'", "'block_hash'")
|
|
|
|
s = s.replace("'pos'", "'index'")
|
|
|
|
s = s.replace("'nTime'", "'timestamp'")
|
|
|
|
s = s.replace("'is_in'", "'is_input'")
|
|
|
|
s = s.replace("'raw_scriptPubKey'","'raw_output_script'")
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2012-01-19 08:11:36 -08:00
|
|
|
class Interface:
|
2012-03-23 09:46:48 -07:00
|
|
|
def __init__(self, host, port):
|
2012-03-14 10:21:27 -07:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
|
2012-03-19 07:57:47 -07:00
|
|
|
self.servers = [] # actual list from IRC
|
2012-01-19 08:11:36 -08:00
|
|
|
self.rtime = 0
|
|
|
|
|
2012-03-23 09:46:48 -07:00
|
|
|
self.is_connected = True
|
2012-03-14 07:35:39 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
#only asynchrnous
|
|
|
|
self.addresses_waiting_for_status = []
|
|
|
|
self.addresses_waiting_for_history = []
|
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
#json
|
|
|
|
self.message_id = 0
|
2012-03-23 06:37:02 -07:00
|
|
|
self.responses = Queue.Queue()
|
|
|
|
|
2012-03-23 08:34:34 -07:00
|
|
|
|
|
|
|
def is_up_to_date(self):
|
2012-03-23 09:46:48 -07:00
|
|
|
return self.responses.empty() and not ( self.addresses_waiting_for_status or self.addresses_waiting_for_history )
|
2012-03-23 08:34:34 -07:00
|
|
|
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-23 06:37:02 -07:00
|
|
|
def queue_json_response(self, c):
|
2012-03-19 11:20:55 -07:00
|
|
|
#print repr(c)
|
2012-03-17 15:32:36 -07:00
|
|
|
msg_id = c.get('id')
|
|
|
|
result = c.get('result')
|
|
|
|
error = c.get('error')
|
2012-03-27 15:03:05 -07:00
|
|
|
params = c.get('params',[])
|
2012-03-29 14:21:31 -07:00
|
|
|
method = c.get('method',None)
|
|
|
|
if not method:
|
2012-03-17 15:32:36 -07:00
|
|
|
return
|
2012-03-30 01:48:32 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
if error:
|
|
|
|
print "received error:", c, method, params
|
|
|
|
else:
|
2012-03-23 09:46:48 -07:00
|
|
|
self.update_waiting_lists(method, params)
|
2012-03-23 06:37:02 -07:00
|
|
|
self.responses.put({'method':method, 'params':params, 'result':result})
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-23 06:37:02 -07:00
|
|
|
|
2012-03-23 09:46:48 -07:00
|
|
|
def update_waiting_lists(self, method, params):
|
2012-03-28 14:02:33 -07:00
|
|
|
if method == 'blockchain.address.subscribe':
|
2012-03-23 09:46:48 -07:00
|
|
|
addr = params[-1]
|
|
|
|
if addr in self.addresses_waiting_for_status:
|
|
|
|
self.addresses_waiting_for_status.remove(addr)
|
2012-03-28 14:02:33 -07:00
|
|
|
elif method == 'blockchain.address.get_history':
|
2012-03-23 09:46:48 -07:00
|
|
|
addr = params[0]
|
|
|
|
if addr in self.addresses_waiting_for_history:
|
|
|
|
self.addresses_waiting_for_history.remove(addr)
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
def subscribe(self, addresses):
|
|
|
|
messages = []
|
|
|
|
for addr in addresses:
|
2012-03-28 14:02:33 -07:00
|
|
|
messages.append(('blockchain.address.subscribe', [addr]))
|
2012-03-19 05:54:59 -07:00
|
|
|
self.addresses_waiting_for_status.append(addr)
|
|
|
|
self.send(messages)
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-19 07:57:47 -07:00
|
|
|
def get_servers(self, wallet):
|
|
|
|
# loop over default servers
|
|
|
|
# requesting servers could be an independent process
|
|
|
|
addresses = wallet.all_addresses()
|
|
|
|
version = wallet.electrum_version
|
|
|
|
|
|
|
|
for server in DEFAULT_SERVERS:
|
|
|
|
print "connecting to", server
|
|
|
|
try:
|
|
|
|
self.host = server
|
|
|
|
self.start_session(addresses, version)
|
|
|
|
wallet.host = self.host
|
|
|
|
break
|
|
|
|
except socket.timeout:
|
|
|
|
continue
|
|
|
|
except socket.error:
|
|
|
|
continue
|
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
|
|
|
|
|
2012-03-24 23:53:23 -07:00
|
|
|
def start_session(self, addresses, version):
|
|
|
|
#print "Starting new session: %s:%d"%(self.host,self.port)
|
|
|
|
self.start()
|
2012-03-29 10:11:05 -07:00
|
|
|
self.send([('server.version', [version]), ('server.banner',[]), ('blockchain.numblocks.subscribe',[]), ('server.peers.subscribe',[])])
|
2012-03-24 23:53:23 -07:00
|
|
|
self.subscribe(addresses)
|
2012-03-19 07:57:47 -07:00
|
|
|
|
|
|
|
|
2012-03-17 04:03:57 -07:00
|
|
|
class PollingInterface(Interface):
|
2012-03-17 04:07:36 -07:00
|
|
|
""" non-persistent connection. synchronous calls"""
|
2012-01-19 08:11:36 -08:00
|
|
|
|
2012-02-08 04:37:14 -08:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
def get_history(self, address):
|
2012-03-30 01:48:32 -07:00
|
|
|
self.send([('blockchain.address.get_history', [address] )])
|
2012-03-17 03:56:31 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
def poll(self):
|
|
|
|
self.send([('session.poll', [])])
|
|
|
|
|
2012-03-17 03:56:31 -07:00
|
|
|
#if is_new or wallet.remote_url:
|
|
|
|
# self.was_updated = True
|
|
|
|
# is_new = wallet.synchronize()
|
|
|
|
# wallet.update_tx_history()
|
|
|
|
# wallet.save()
|
|
|
|
# return is_new
|
|
|
|
#else:
|
|
|
|
# return False
|
2012-02-08 04:37:14 -08:00
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
def poll_thread(self, poll_interval):
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
2012-02-08 04:37:14 -08:00
|
|
|
try:
|
2012-03-23 08:34:34 -07:00
|
|
|
self.poll()
|
2012-03-19 05:54:59 -07:00
|
|
|
time.sleep(poll_interval)
|
2012-03-14 07:35:39 -07:00
|
|
|
except socket.gaierror:
|
|
|
|
break
|
|
|
|
except socket.error:
|
|
|
|
break
|
2012-02-08 04:37:14 -08:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
break
|
|
|
|
|
|
|
|
self.is_connected = False
|
2012-03-23 06:37:02 -07:00
|
|
|
self.responses.put(None)
|
2012-02-08 04:37:14 -08:00
|
|
|
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 04:03:57 -07:00
|
|
|
class NativeInterface(PollingInterface):
|
|
|
|
|
2012-03-24 23:53:23 -07:00
|
|
|
def start_session(self, addresses, version):
|
|
|
|
self.send([('session.new', [ version, addresses ])] )
|
2012-03-28 14:29:53 -07:00
|
|
|
self.send([('server.peers.subscribe',[])])
|
2012-03-24 23:53:23 -07:00
|
|
|
thread.start_new_thread(self.poll_thread, (5,))
|
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
def send(self, messages):
|
2012-03-17 04:03:57 -07:00
|
|
|
import time
|
|
|
|
cmds = {'session.new':'new_session',
|
2012-03-28 14:29:53 -07:00
|
|
|
'server.peers.subscribe':'peers',
|
2012-03-17 04:03:57 -07:00
|
|
|
'session.poll':'poll',
|
2012-03-28 14:02:33 -07:00
|
|
|
'blockchain.transaction.broadcast':'tx',
|
|
|
|
'blockchain.address.get_history':'h',
|
|
|
|
'blockchain.address.subscribe':'address.subscribe'
|
2012-03-17 04:03:57 -07:00
|
|
|
}
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
cmd = cmds[method]
|
|
|
|
|
2012-03-19 02:19:42 -07:00
|
|
|
if cmd == 'poll':
|
|
|
|
params = self.session_id
|
|
|
|
|
2012-03-20 07:30:36 -07:00
|
|
|
if cmd == 'address.subscribe':
|
|
|
|
params = [ self.session_id] + params
|
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
if cmd in ['h', 'tx']:
|
2012-03-17 15:32:36 -07:00
|
|
|
str_params = params[0]
|
|
|
|
elif type(params) != type(''):
|
|
|
|
str_params = repr( params )
|
|
|
|
else:
|
|
|
|
str_params = params
|
|
|
|
t1 = time.time()
|
|
|
|
request = repr ( (cmd, str_params) ) + "#"
|
|
|
|
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.settimeout(DEFAULT_TIMEOUT)
|
2012-03-19 07:57:47 -07:00
|
|
|
s.connect(( self.host, self.port) )
|
2012-03-17 15:32:36 -07:00
|
|
|
s.send( request )
|
|
|
|
out = ''
|
|
|
|
while 1:
|
|
|
|
msg = s.recv(1024)
|
|
|
|
if msg: out += msg
|
|
|
|
else: break
|
|
|
|
s.close()
|
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
2012-03-19 02:19:42 -07:00
|
|
|
|
2012-03-30 01:48:32 -07:00
|
|
|
if cmd == 'h':
|
|
|
|
out = old_to_new(out)
|
|
|
|
|
2012-03-19 02:19:42 -07:00
|
|
|
if cmd in[ 'peers','h','poll']:
|
2012-03-17 15:32:36 -07:00
|
|
|
out = ast.literal_eval( out )
|
|
|
|
|
|
|
|
if out=='': out=None #fixme
|
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
if cmd == 'new_session':
|
2012-03-25 08:31:19 -07:00
|
|
|
self.session_id, msg = ast.literal_eval( out )
|
|
|
|
self.responses.put({'method':'server.banner', 'params':[], 'result':msg})
|
2012-03-19 05:54:59 -07:00
|
|
|
else:
|
2012-03-23 09:46:48 -07:00
|
|
|
self.update_waiting_lists(method, params)
|
2012-03-23 06:37:02 -07:00
|
|
|
self.responses.put({'method':method, 'params':params, 'result':out})
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 04:03:57 -07:00
|
|
|
|
|
|
|
class HttpInterface(PollingInterface):
|
2012-03-14 08:31:31 -07:00
|
|
|
|
2012-03-24 23:53:23 -07:00
|
|
|
def start(self):
|
2012-03-19 02:19:42 -07:00
|
|
|
self.session_id = None
|
2012-03-19 05:54:59 -07:00
|
|
|
thread.start_new_thread(self.poll_thread, (15,))
|
2012-03-19 02:19:42 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
def poll(self):
|
2012-03-28 05:22:46 -07:00
|
|
|
if self.session_id:
|
|
|
|
self.send( [] )
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
def send(self, messages):
|
2012-03-19 02:19:42 -07:00
|
|
|
import urllib2, json, time, cookielib
|
|
|
|
|
|
|
|
cj = cookielib.CookieJar()
|
|
|
|
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
|
|
|
urllib2.install_opener(opener)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
t1 = time.time()
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
data = []
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
if type(params) != type([]): params = [params]
|
|
|
|
data.append( { 'method':method, 'id':self.message_id, 'params':params } )
|
|
|
|
self.message_id += 1
|
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
if data:
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
else:
|
|
|
|
# poll with GET
|
|
|
|
data_json = None
|
2012-03-19 02:19:42 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
host = 'http://%s:%d'%( self.host, self.port )
|
2012-03-19 02:19:42 -07:00
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
if self.session_id:
|
|
|
|
headers['cookie'] = 'SESSION=%s'%self.session_id
|
|
|
|
|
|
|
|
req = urllib2.Request(host, data_json, headers)
|
2012-03-14 08:31:31 -07:00
|
|
|
response_stream = urllib2.urlopen(req)
|
2012-03-19 02:19:42 -07:00
|
|
|
|
|
|
|
for index, cookie in enumerate(cj):
|
|
|
|
if cookie.name=='SESSION':
|
|
|
|
self.session_id = cookie.value
|
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
response = response_stream.read()
|
|
|
|
if response:
|
|
|
|
response = json.loads( response )
|
|
|
|
if type(response) is not type([]):
|
2012-03-23 06:37:02 -07:00
|
|
|
self.queue_json_response(response)
|
2012-03-19 11:20:55 -07:00
|
|
|
else:
|
|
|
|
for item in response:
|
2012-03-23 06:37:02 -07:00
|
|
|
self.queue_json_response(item)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 05:12:16 -07:00
|
|
|
class AsynchronousInterface(Interface):
|
2012-03-17 04:07:36 -07:00
|
|
|
"""json-rpc over persistent TCP connection, asynchronous"""
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 04:36:27 -07:00
|
|
|
def listen_thread(self):
|
2012-03-13 08:43:56 -07:00
|
|
|
try:
|
|
|
|
self.is_connected = True
|
|
|
|
out = ''
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
|
|
|
try: msg = self.s.recv(1024)
|
|
|
|
except socket.timeout: continue
|
2012-03-13 08:43:56 -07:00
|
|
|
out += msg
|
2012-03-13 09:31:29 -07:00
|
|
|
if msg == '':
|
|
|
|
self.is_connected = False
|
2012-03-14 07:35:39 -07:00
|
|
|
print "disconnected."
|
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
while True:
|
|
|
|
s = out.find('\n')
|
|
|
|
if s==-1: break
|
|
|
|
c = out[0:s]
|
|
|
|
out = out[s+1:]
|
|
|
|
c = json.loads(c)
|
2012-03-23 06:37:02 -07:00
|
|
|
self.queue_json_response(c)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
|
|
|
|
self.is_connected = False
|
2012-03-30 01:48:32 -07:00
|
|
|
# push None so that the getting thread exits its loop
|
2012-03-23 06:37:02 -07:00
|
|
|
self.responses.put(None)
|
2012-03-14 06:55:12 -07:00
|
|
|
|
2012-03-17 08:02:28 -07:00
|
|
|
def send(self, messages):
|
|
|
|
out = ''
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
request = json.dumps( { 'id':self.message_id, 'method':method, 'params':params } )
|
|
|
|
self.message_id += 1
|
|
|
|
out += request + '\n'
|
|
|
|
self.s.send( out )
|
|
|
|
|
2012-03-17 04:36:27 -07:00
|
|
|
def get_history(self, addr):
|
2012-03-30 01:48:32 -07:00
|
|
|
self.send([('blockchain.address.get_history', [addr])])
|
2012-03-17 07:46:24 -07:00
|
|
|
self.addresses_waiting_for_history.append(addr)
|
2012-03-16 14:18:58 -07:00
|
|
|
|
2012-03-24 23:53:23 -07:00
|
|
|
def start(self):
|
2012-03-14 06:55:12 -07:00
|
|
|
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
2012-03-23 06:37:02 -07:00
|
|
|
self.s.settimeout(5)
|
|
|
|
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
2012-03-14 06:55:12 -07:00
|
|
|
self.s.connect(( self.host, self.port))
|
2012-03-17 04:36:27 -07:00
|
|
|
thread.start_new_thread(self.listen_thread, ())
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-23 08:34:34 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
def loop_interfaces_thread(wallet):
|
|
|
|
while True:
|
|
|
|
try:
|
2012-03-23 10:30:06 -07:00
|
|
|
wallet.start_interface()
|
2012-03-23 08:34:34 -07:00
|
|
|
wallet.run()
|
2012-03-14 06:55:12 -07:00
|
|
|
except socket.error:
|
2012-03-14 08:08:23 -07:00
|
|
|
print "socket error"
|
2012-03-29 07:39:40 -07:00
|
|
|
wallet.interface.is_connected = False
|
2012-03-14 06:55:12 -07:00
|
|
|
time.sleep(5)
|
2012-03-14 07:35:39 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-29 07:39:40 -07:00
|
|
|
wallet.interface.is_connected = False
|
2012-03-18 04:38:40 -07:00
|
|
|
time.sleep(5)
|
2012-03-14 07:35:39 -07:00
|
|
|
continue
|
2012-03-14 08:08:23 -07:00
|
|
|
|