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/>.
|
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
from __future__ import absolute_import
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2016-05-26 15:20:44 +00:00
|
|
|
import requests
|
2017-07-10 13:36:44 +00:00
|
|
|
from google.protobuf import json_format
|
2017-08-24 12:29:27 +00:00
|
|
|
|
2017-12-21 19:13:01 +00:00
|
|
|
from . import messages
|
2017-09-05 15:15:00 +00:00
|
|
|
from .transport import Transport, TransportException
|
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
|
|
|
|
2017-06-23 19:31:42 +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'])
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
class BridgeTransport(Transport):
|
|
|
|
'''
|
|
|
|
BridgeTransport implements transport through TREZOR Bridge (aka trezord).
|
|
|
|
'''
|
2017-04-20 11:16:15 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
configured = False
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def __init__(self, device):
|
|
|
|
super(BridgeTransport, self).__init__()
|
2015-12-21 17:18:06 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
self.device = device
|
|
|
|
self.conn = requests.Session()
|
2015-12-21 17:18:06 +00:00
|
|
|
self.session = None
|
|
|
|
self.response = None
|
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.device['path']
|
2015-12-21 17:18:06 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def configure():
|
2017-08-24 12:29:27 +00:00
|
|
|
if BridgeTransport.configured:
|
2017-06-23 19:31:42 +00:00
|
|
|
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:
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException(
|
|
|
|
'Could not fetch config from %s' % CONFIG_URL)
|
2017-08-24 12:29:27 +00:00
|
|
|
r = requests.post(TREZORD_HOST + '/configure', data=r.text)
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException('trezord: Could not configure' +
|
|
|
|
get_error(r))
|
2017-08-24 12:29:27 +00:00
|
|
|
BridgeTransport.configured = True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def enumerate():
|
|
|
|
BridgeTransport.configure()
|
2015-12-21 17:18:06 +00:00
|
|
|
r = requests.get(TREZORD_HOST + '/enumerate')
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException('trezord: Could not enumerate devices' +
|
|
|
|
get_error(r))
|
2017-08-24 12:29:27 +00:00
|
|
|
return [BridgeTransport(dev) for dev in r.json()]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def find_by_path(path):
|
|
|
|
for transport in BridgeTransport.enumerate():
|
|
|
|
if path is None or transport.device['path'] == path:
|
|
|
|
return transport
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException('Bridge device not found')
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
def open(self):
|
|
|
|
r = self.conn.post(TREZORD_HOST + '/acquire/%s' % self.device['path'])
|
2014-07-26 14:27:28 +00:00
|
|
|
if r.status_code != 200:
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException('trezord: Could not acquire session' +
|
|
|
|
get_error(r))
|
2017-08-24 12:29:27 +00:00
|
|
|
self.session = r.json()['session']
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def close(self):
|
|
|
|
if not self.session:
|
|
|
|
return
|
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:
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException('trezord: Could not release session' +
|
|
|
|
get_error(r))
|
2017-08-24 12:29:27 +00:00
|
|
|
self.session = None
|
|
|
|
|
|
|
|
def write(self, msg):
|
|
|
|
msgname = msg.__class__.__name__
|
|
|
|
msgjson = json_format.MessageToJson(
|
|
|
|
msg, preserving_proto_field_name=True)
|
|
|
|
payload = '{"type": "%s", "message": %s}' % (msgname, msgjson)
|
|
|
|
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:
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException('trezord: Could not write message' +
|
|
|
|
get_error(r))
|
2017-08-24 12:29:27 +00:00
|
|
|
self.response = r.json()
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def read(self):
|
2016-05-26 15:20:44 +00:00
|
|
|
if self.response is None:
|
2017-09-05 15:15:00 +00:00
|
|
|
raise TransportException('No response stored')
|
2017-12-21 19:13:01 +00:00
|
|
|
msgtype = getattr(messages, self.response['type'])
|
2017-08-24 12:29:27 +00:00
|
|
|
msg = msgtype()
|
|
|
|
msg = json_format.ParseDict(self.response['message'], msg)
|
|
|
|
self.response = None
|
|
|
|
return msg
|