2014-07-26 14:27:28 +00:00
|
|
|
'''BridgeTransport implements transport TREZOR Bridge (aka trezord).'''
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import protobuf_json
|
|
|
|
import json
|
|
|
|
import mapping
|
|
|
|
from transport import Transport
|
|
|
|
import messages_pb2 as proto
|
|
|
|
|
2015-03-05 10:00:18 +00:00
|
|
|
TREZORD_HOST = 'https://localback.net:21324'
|
2014-07-26 14:27:28 +00:00
|
|
|
CONFIG_URL = 'https://mytrezor.com/data/plugin/config_signed.bin'
|
|
|
|
|
2014-07-26 15:15:12 +00:00
|
|
|
def get_error(resp):
|
|
|
|
return ' (error=%d str=%s)' % (resp.status_code, resp.json()['error'])
|
|
|
|
|
2014-07-26 14:27:28 +00:00
|
|
|
class BridgeTransport(Transport):
|
|
|
|
def __init__(self, device, *args, **kwargs):
|
|
|
|
|
|
|
|
r = requests.get(CONFIG_URL)
|
|
|
|
if r.status_code != 200:
|
|
|
|
raise Exception('Could not fetch config from %s' % CONFIG_URL)
|
|
|
|
|
2014-07-27 14:22:24 +00:00
|
|
|
config = r.text
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2015-03-05 10:00:18 +00:00
|
|
|
self.conn = requests.Session();
|
|
|
|
|
2015-03-05 10:15:53 +00:00
|
|
|
r = self.conn.post(TREZORD_HOST + '/configure', data=config)
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2014-07-26 15:15:12 +00:00
|
|
|
raise Exception('trezord: Could not configure' + get_error(r))
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2015-03-05 10:15:53 +00:00
|
|
|
r = self.conn.get(TREZORD_HOST + '/enumerate')
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2014-07-26 15:15:12 +00:00
|
|
|
raise Exception('trezord: Could not enumerate devices' + get_error(r))
|
2014-07-26 14:27:28 +00:00
|
|
|
enum = r.json()
|
|
|
|
|
|
|
|
if len(enum) < 1:
|
|
|
|
raise Exception('trezord: No devices found')
|
|
|
|
|
|
|
|
self.path = enum[0]['path']
|
|
|
|
self.session = None
|
|
|
|
self.response = None
|
|
|
|
|
|
|
|
super(BridgeTransport, self).__init__(device, *args, **kwargs)
|
|
|
|
|
|
|
|
def _open(self):
|
2015-03-05 10:15:53 +00:00
|
|
|
r = self.conn.post(TREZORD_HOST + '/acquire/%s' % self.path)
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2014-07-26 15:15:12 +00:00
|
|
|
raise Exception('trezord: Could not acquire session' + get_error(r))
|
2014-07-26 14:27:28 +00:00
|
|
|
resp = r.json()
|
|
|
|
self.session = resp['session']
|
|
|
|
|
|
|
|
def _close(self):
|
2015-03-05 10:15:53 +00:00
|
|
|
r = self.conn.post(TREZORD_HOST + '/release/%s' % self.session)
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2014-07-26 15:15:12 +00:00
|
|
|
raise Exception('trezord: Could not release session' + get_error(r))
|
2014-07-26 14:27:28 +00:00
|
|
|
else:
|
|
|
|
self.session = None
|
|
|
|
|
|
|
|
def ready_to_read(self):
|
|
|
|
return self.response != None
|
|
|
|
|
|
|
|
def _write(self, msg, protobuf_msg):
|
|
|
|
cls = protobuf_msg.__class__.__name__
|
|
|
|
msg = protobuf_json.pb2json(protobuf_msg)
|
2014-07-27 14:22:24 +00:00
|
|
|
payload = '{"type": "%s", "message": %s}' % (cls, json.dumps(msg))
|
2015-03-05 10:15:53 +00:00
|
|
|
r = self.conn.post(TREZORD_HOST + '/call/%s' % self.session, data=payload)
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2014-07-26 15:15:12 +00:00
|
|
|
raise Exception('trezord: Could not write message' + get_error(r))
|
2014-07-26 14:27:28 +00:00
|
|
|
else:
|
|
|
|
self.response = r.json()
|
|
|
|
|
|
|
|
def _read(self):
|
|
|
|
if self.response == None:
|
|
|
|
raise Exception('No response stored')
|
|
|
|
cls = getattr(proto, self.response['type'])
|
|
|
|
inst = cls()
|
|
|
|
pb = protobuf_json.json2pb(inst, self.response['message'])
|
|
|
|
return ('protobuf', pb)
|