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/>.
|
|
|
|
|
|
|
|
|
2012-03-31 02:47:16 -07:00
|
|
|
import random, socket, ast, re
|
|
|
|
import threading, traceback, sys, time, json, Queue
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-05-02 05:21:58 -07:00
|
|
|
from version import ELECTRUM_VERSION
|
|
|
|
|
2012-03-14 10:00:24 -07:00
|
|
|
DEFAULT_TIMEOUT = 5
|
2012-04-01 08:50:12 -07:00
|
|
|
DEFAULT_SERVERS = [ 'ecdsa.org:50001:t', 'electrum.novit.ro:50001:t', 'electrum.bitcoins.sk:50001:t'] # 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-03-31 02:47:16 -07:00
|
|
|
class Interface(threading.Thread):
|
2012-03-23 09:46:48 -07:00
|
|
|
def __init__(self, host, port):
|
2012-03-31 02:47:16 -07:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.daemon = True
|
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-30 05:15:05 -07:00
|
|
|
self.poll_interval = 1
|
2012-03-17 15:32:36 -07:00
|
|
|
|
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-05-04 00:28:44 -07:00
|
|
|
self.unanswered_requests = {}
|
2012-03-23 06:37:02 -07:00
|
|
|
|
2012-03-30 05:15:05 -07:00
|
|
|
def poke(self):
|
|
|
|
# push a fake response so that the getting thread exits its loop
|
|
|
|
self.responses.put(None)
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-23 06:37:02 -07:00
|
|
|
def queue_json_response(self, c):
|
2012-04-29 09:33:01 -07:00
|
|
|
|
|
|
|
#print "<--",c
|
2012-03-17 15:32:36 -07:00
|
|
|
msg_id = c.get('id')
|
|
|
|
error = c.get('error')
|
2012-03-30 01:48:32 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
if error:
|
2012-04-29 09:33:01 -07:00
|
|
|
print "received error:", c
|
|
|
|
return
|
|
|
|
|
|
|
|
if msg_id is not None:
|
2012-05-04 00:28:44 -07:00
|
|
|
method, params = self.unanswered_requests.pop(msg_id)
|
2012-04-29 09:33:01 -07:00
|
|
|
result = c.get('result')
|
2012-03-17 15:32:36 -07:00
|
|
|
else:
|
2012-04-29 09:33:01 -07:00
|
|
|
# notification
|
|
|
|
method = c.get('method')
|
|
|
|
params = c.get('params')
|
|
|
|
|
|
|
|
if method == 'blockchain.numblocks.subscribe':
|
|
|
|
result = params[0]
|
|
|
|
params = []
|
|
|
|
|
|
|
|
elif method == 'blockchain.address.subscribe':
|
|
|
|
addr = params[0]
|
|
|
|
result = params[1]
|
|
|
|
params = [addr]
|
|
|
|
|
|
|
|
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-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.send(messages)
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
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-03-31 02:47:16 -07:00
|
|
|
def __init__(self, host, port):
|
|
|
|
Interface.__init__(self, host, port)
|
|
|
|
self.session_id = None
|
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):
|
2012-03-31 02:47:16 -07:00
|
|
|
pass
|
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-31 02:47:16 -07:00
|
|
|
def run(self):
|
|
|
|
self.is_connected = True
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
2012-02-08 04:37:14 -08:00
|
|
|
try:
|
2012-03-31 02:47:16 -07:00
|
|
|
if self.session_id:
|
|
|
|
self.poll()
|
2012-03-30 05:15:05 -07:00
|
|
|
time.sleep(self.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-31 02:47:16 -07:00
|
|
|
self.poke()
|
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
|
|
|
|
2012-03-31 07:35:54 -07:00
|
|
|
class HttpStratumInterface(PollingInterface):
|
2012-03-14 08:31:31 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
def poll(self):
|
2012-03-31 02:47:16 -07:00
|
|
|
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 } )
|
2012-05-04 00:28:44 -07:00
|
|
|
self.unanswered_requests[self.message_id] = method, params
|
2012-03-17 15:32:36 -07:00
|
|
|
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-30 05:15:05 -07:00
|
|
|
if response:
|
|
|
|
self.poll_interval = 1
|
|
|
|
else:
|
|
|
|
if self.poll_interval < 15:
|
|
|
|
self.poll_interval += 1
|
|
|
|
#print self.poll_interval, response
|
|
|
|
|
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-31 07:35:54 -07:00
|
|
|
class TcpStratumInterface(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-31 02:47:16 -07:00
|
|
|
def __init__(self, host, port):
|
|
|
|
Interface.__init__(self, host, port)
|
|
|
|
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
2012-05-02 07:04:00 -07:00
|
|
|
self.s.settimeout(5*60)
|
2012-03-31 02:47:16 -07:00
|
|
|
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
2012-03-13 08:43:56 -07:00
|
|
|
try:
|
2012-03-31 02:47:16 -07:00
|
|
|
self.s.connect(( self.host, self.port))
|
2012-03-13 08:43:56 -07:00
|
|
|
self.is_connected = True
|
2012-05-02 05:21:58 -07:00
|
|
|
self.send([('server.version', [ELECTRUM_VERSION])])
|
2012-05-12 00:38:42 -07:00
|
|
|
print "Connected to %s:%d"%(self.host,self.port)
|
2012-03-31 02:47:16 -07:00
|
|
|
except:
|
|
|
|
self.is_connected = False
|
2012-05-12 00:38:42 -07:00
|
|
|
print "Not connected"
|
2012-03-31 02:47:16 -07:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
2012-03-13 08:43:56 -07:00
|
|
|
out = ''
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
|
|
|
try: msg = self.s.recv(1024)
|
2012-05-02 07:04:00 -07:00
|
|
|
except socket.timeout:
|
|
|
|
# ping the server with server.version, as a real ping does not exist yet
|
|
|
|
self.send([('server.version', [ELECTRUM_VERSION])])
|
2012-03-31 02:47:16 -07:00
|
|
|
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-04-07 06:13:18 -07:00
|
|
|
print "poking"
|
2012-03-30 05:15:05 -07:00
|
|
|
self.poke()
|
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 } )
|
2012-05-04 00:28:44 -07:00
|
|
|
self.unanswered_requests[self.message_id] = method, params
|
2012-04-29 09:33:01 -07:00
|
|
|
#print "-->",request
|
2012-03-17 08:02:28 -07:00
|
|
|
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-16 14:18:58 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-31 02:47:16 -07:00
|
|
|
class WalletSynchronizer(threading.Thread):
|
|
|
|
|
|
|
|
def __init__(self, wallet, loop=False):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.wallet = wallet
|
|
|
|
self.loop = loop
|
|
|
|
self.start_interface()
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
|
2012-03-31 02:47:16 -07:00
|
|
|
def handle_response(self, r):
|
|
|
|
if r is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
method = r['method']
|
|
|
|
params = r['params']
|
|
|
|
result = r['result']
|
|
|
|
|
|
|
|
if method == 'server.banner':
|
|
|
|
self.wallet.banner = result
|
|
|
|
self.wallet.was_updated = True
|
|
|
|
|
|
|
|
elif method == 'server.peers.subscribe':
|
|
|
|
servers = []
|
|
|
|
for item in result:
|
|
|
|
s = []
|
|
|
|
host = item[1]
|
2012-03-31 05:03:30 -07:00
|
|
|
ports = []
|
2012-03-31 02:47:16 -07:00
|
|
|
if len(item)>2:
|
|
|
|
for v in item[2]:
|
2012-04-29 09:33:01 -07:00
|
|
|
if re.match("[th]\d+",v):
|
2012-03-31 05:03:30 -07:00
|
|
|
ports.append((v[0],v[1:]))
|
|
|
|
if ports:
|
|
|
|
servers.append( (host, ports) )
|
2012-03-31 02:47:16 -07:00
|
|
|
self.interface.servers = servers
|
|
|
|
|
|
|
|
elif method == 'blockchain.address.subscribe':
|
2012-04-29 09:33:01 -07:00
|
|
|
addr = params[0]
|
2012-03-31 02:47:16 -07:00
|
|
|
self.wallet.receive_status_callback(addr, result)
|
|
|
|
|
|
|
|
elif method == 'blockchain.address.get_history':
|
|
|
|
addr = params[0]
|
|
|
|
self.wallet.receive_history_callback(addr, result)
|
|
|
|
self.wallet.was_updated = True
|
|
|
|
|
|
|
|
elif method == 'blockchain.transaction.broadcast':
|
|
|
|
self.wallet.tx_result = result
|
|
|
|
self.wallet.tx_event.set()
|
|
|
|
|
|
|
|
elif method == 'blockchain.numblocks.subscribe':
|
|
|
|
self.wallet.blocks = result
|
|
|
|
|
|
|
|
elif method == 'server.version':
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
print "unknown message:", method, params, result
|
|
|
|
|
|
|
|
|
|
|
|
def start_interface(self):
|
2012-03-14 06:55:12 -07:00
|
|
|
try:
|
2012-03-31 02:47:16 -07:00
|
|
|
host, port, protocol = self.wallet.server.split(':')
|
|
|
|
port = int(port)
|
2012-03-14 07:35:39 -07:00
|
|
|
except:
|
2012-03-31 02:47:16 -07:00
|
|
|
self.wallet.pick_random_server()
|
|
|
|
host, port, protocol = self.wallet.server.split(':')
|
|
|
|
port = int(port)
|
|
|
|
|
|
|
|
#print protocol, host, port
|
2012-04-29 09:33:01 -07:00
|
|
|
if protocol == 't':
|
2012-03-31 07:35:54 -07:00
|
|
|
InterfaceClass = TcpStratumInterface
|
2012-03-31 02:47:16 -07:00
|
|
|
elif protocol == 'h':
|
2012-03-31 07:35:54 -07:00
|
|
|
InterfaceClass = HttpStratumInterface
|
2012-03-31 02:47:16 -07:00
|
|
|
else:
|
|
|
|
print "unknown protocol"
|
2012-04-29 09:33:01 -07:00
|
|
|
InterfaceClass = TcpStratumInterface
|
2012-03-31 02:47:16 -07:00
|
|
|
|
|
|
|
self.interface = InterfaceClass(host, port)
|
2012-05-02 05:21:58 -07:00
|
|
|
self.interface.start()
|
2012-03-31 02:47:16 -07:00
|
|
|
self.wallet.interface = self.interface
|
|
|
|
|
2012-05-02 05:21:58 -07:00
|
|
|
if self.interface.is_connected:
|
|
|
|
self.wallet.start_session(self.interface)
|
2012-03-31 02:47:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
import socket, time
|
|
|
|
while True:
|
2012-04-07 06:13:18 -07:00
|
|
|
while self.interface.is_connected:
|
|
|
|
new_addresses = self.wallet.synchronize()
|
|
|
|
if new_addresses:
|
|
|
|
self.interface.subscribe(new_addresses)
|
|
|
|
|
|
|
|
if self.wallet.is_up_to_date():
|
|
|
|
if not self.wallet.up_to_date:
|
2012-03-31 02:47:16 -07:00
|
|
|
self.wallet.up_to_date = True
|
2012-04-07 06:13:18 -07:00
|
|
|
self.wallet.was_updated = True
|
2012-03-31 02:47:16 -07:00
|
|
|
self.wallet.up_to_date_event.set()
|
2012-04-07 06:13:18 -07:00
|
|
|
else:
|
|
|
|
if self.wallet.up_to_date:
|
2012-03-31 02:47:16 -07:00
|
|
|
self.wallet.up_to_date = False
|
2012-04-07 06:13:18 -07:00
|
|
|
self.wallet.was_updated = True
|
2012-03-31 02:47:16 -07:00
|
|
|
|
2012-04-07 06:13:18 -07:00
|
|
|
if self.wallet.was_updated:
|
|
|
|
self.wallet.gui_callback()
|
|
|
|
self.wallet.was_updated = False
|
|
|
|
|
|
|
|
response = self.interface.responses.get()
|
|
|
|
self.handle_response(response)
|
2012-03-31 02:47:16 -07:00
|
|
|
|
2012-04-07 06:13:18 -07:00
|
|
|
self.wallet.gui_callback()
|
2012-03-31 02:47:16 -07:00
|
|
|
if self.loop:
|
|
|
|
time.sleep(5)
|
|
|
|
self.start_interface()
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2012-03-14 08:08:23 -07:00
|
|
|
|