2016-11-25 21:53:55 +00:00
|
|
|
# This file is part of the TREZOR project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
|
|
|
|
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
# Copyright (C) 2016 Jochen Hoenicke <hoenicke@gmail.com>
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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 Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-07-26 14:27:28 +00:00
|
|
|
'''BridgeTransport implements transport TREZOR Bridge (aka trezord).'''
|
|
|
|
|
|
|
|
import json
|
2016-05-26 15:20:44 +00:00
|
|
|
import requests
|
2016-05-05 01:16:17 +00:00
|
|
|
from . import protobuf_json
|
|
|
|
from . import messages_pb2 as proto
|
2016-06-27 16:21:40 +00:00
|
|
|
from .transport import TransportV1
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2015-03-05 10:00:18 +00:00
|
|
|
TREZORD_HOST = 'https://localback.net:21324'
|
2016-06-27 15:11:43 +00:00
|
|
|
CONFIG_URL = 'https://wallet.trezor.io/data/config_signed.bin'
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2014-07-26 15:15:12 +00:00
|
|
|
def get_error(resp):
|
|
|
|
return ' (error=%d str=%s)' % (resp.status_code, resp.json()['error'])
|
|
|
|
|
2016-06-27 16:21:40 +00:00
|
|
|
class BridgeTransport(TransportV1):
|
2017-04-20 11:16:15 +00:00
|
|
|
CONFIGURED = False
|
|
|
|
|
2014-07-26 14:27:28 +00:00
|
|
|
def __init__(self, device, *args, **kwargs):
|
2015-12-21 17:18:06 +00:00
|
|
|
self.configure()
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2016-06-30 14:47:17 +00:00
|
|
|
self.path = device['path']
|
2015-12-21 17:18:06 +00:00
|
|
|
|
|
|
|
self.session = None
|
|
|
|
self.response = None
|
2016-05-26 15:20:44 +00:00
|
|
|
self.conn = requests.Session()
|
2015-12-21 17:18:06 +00:00
|
|
|
|
|
|
|
super(BridgeTransport, self).__init__(device, *args, **kwargs)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def configure():
|
2017-04-20 11:16:15 +00:00
|
|
|
if BridgeTransport.CONFIGURED: return
|
2016-06-27 15:11:43 +00:00
|
|
|
r = requests.get(CONFIG_URL, verify=False)
|
2014-07-26 14:27:28 +00:00
|
|
|
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-12-21 17:18:06 +00:00
|
|
|
r = requests.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))
|
2017-04-20 11:16:15 +00:00
|
|
|
BridgeTransport.CONFIGURED = True
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2015-12-21 17:18:06 +00:00
|
|
|
@classmethod
|
|
|
|
def enumerate(cls):
|
|
|
|
"""
|
|
|
|
Return a list of available TREZOR devices.
|
|
|
|
"""
|
|
|
|
cls.configure()
|
|
|
|
r = requests.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))
|
2015-12-21 17:18:06 +00:00
|
|
|
|
2014-07-26 14:27:28 +00:00
|
|
|
enum = r.json()
|
|
|
|
|
2016-02-10 15:46:58 +00:00
|
|
|
return enum
|
2014-07-26 14:27:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-06-27 16:21:40 +00:00
|
|
|
def _ready_to_read(self):
|
2014-07-26 14:27:28 +00:00
|
|
|
return self.response != None
|
|
|
|
|
2016-06-27 16:21:40 +00:00
|
|
|
def write(self, protobuf_msg):
|
|
|
|
# Override main 'write' method, HTTP transport cannot be
|
|
|
|
# splitted to chunks
|
2014-07-26 14:27:28 +00:00
|
|
|
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):
|
2016-05-26 15:20:44 +00:00
|
|
|
if self.response is None:
|
2014-07-26 14:27:28 +00:00
|
|
|
raise Exception('No response stored')
|
|
|
|
cls = getattr(proto, self.response['type'])
|
|
|
|
inst = cls()
|
|
|
|
pb = protobuf_json.json2pb(inst, self.response['message'])
|
2016-06-27 16:21:40 +00:00
|
|
|
return (0, 'protobuf', pb)
|