2012-12-03 15:36:03 +00:00
#!/usr/bin/python
import binascii
import argparse
import json
from bitkeylib . client import BitkeyClient
2013-01-05 14:43:21 +00:00
from bitkeylib . debuglink import DebugLink
2013-09-12 22:17:06 +00:00
from bitkeylib . protobuf_json import pb2json
2012-12-03 15:36:03 +00:00
def parse_args ( commands ) :
parser = argparse . ArgumentParser ( description = ' Commandline tool for Bitkey devices. ' )
2013-08-30 21:32:02 +00:00
parser . add_argument ( ' -t ' , ' --transport ' , dest = ' transport ' , choices = [ ' usb ' , ' serial ' , ' pipe ' , ' socket ' ] , default = ' usb ' , help = " Transport used for talking with the device " )
parser . add_argument ( ' -p ' , ' --path ' , dest = ' path ' , default = ' ' , help = " Path used by the transport (usually serial port) " )
2012-12-03 15:36:03 +00:00
parser . add_argument ( ' -dt ' , ' --debuglink-transport ' , dest = ' debuglink_transport ' , choices = [ ' usb ' , ' serial ' , ' pipe ' , ' socket ' ] , default = ' socket ' , help = " Debuglink transport " )
2013-08-30 21:32:02 +00:00
parser . add_argument ( ' -dp ' , ' --debuglink-path ' , dest = ' debuglink_path ' , default = ' 127.0.0.1:2000 ' , help = " Path used by the transport (usually serial port) " )
2012-12-03 15:36:03 +00:00
parser . add_argument ( ' -j ' , ' --json ' , dest = ' json ' , action = ' store_true ' , help = " Prints result as json object " )
2012-12-05 19:31:26 +00:00
parser . add_argument ( ' -d ' , ' --debug ' , dest = ' debug ' , action = ' store_true ' , help = ' Enable low-level debugging ' )
2012-12-03 15:36:03 +00:00
cmdparser = parser . add_subparsers ( title = ' Available commands ' )
for cmd in commands . _list_commands ( ) :
func = object . __getattribute__ ( commands , cmd )
try :
help = func . help
except AttributeError :
help = ' '
try :
arguments = func . arguments
except AttributeError :
arguments = ( ( ( ' params ' , ) , { ' nargs ' : ' * ' } ) , )
item = cmdparser . add_parser ( cmd , help = func . help )
for arg in arguments :
item . add_argument ( * arg [ 0 ] , * * arg [ 1 ] )
item . set_defaults ( func = func )
2013-04-01 14:59:42 +00:00
item . set_defaults ( cmd = cmd )
2012-12-03 15:36:03 +00:00
return parser . parse_args ( )
def get_transport ( transport_string , path ) :
if transport_string == ' usb ' :
2013-04-01 14:59:42 +00:00
from bitkeylib . transport_hid import HidTransport
2013-08-30 21:32:02 +00:00
if path == ' ' :
2013-08-31 21:45:53 +00:00
try :
path = list_usb ( ) [ 0 ]
except IndexError :
raise Exception ( " No Trezor found on USB " )
2013-08-30 21:32:02 +00:00
2013-04-01 14:59:42 +00:00
return HidTransport ( path )
2012-12-03 15:36:03 +00:00
if transport_string == ' serial ' :
from bitkeylib . transport_serial import SerialTransport
return SerialTransport ( path )
if transport_string == ' pipe ' :
from bitkeylib . transport_pipe import PipeTransport
return PipeTransport ( path , is_device = False )
if transport_string == ' socket ' :
2012-12-05 19:31:26 +00:00
from bitkeylib . transport_socket import SocketTransportClient
return SocketTransportClient ( path )
2012-12-03 15:36:03 +00:00
if transport_string == ' fake ' :
from bitkeylib . transport_fake import FakeTransport
return FakeTransport ( path )
raise NotImplemented ( " Unknown transport " )
class Commands ( object ) :
def __init__ ( self , client ) :
self . client = client
@classmethod
def _list_commands ( cls ) :
return [ x for x in dir ( cls ) if not x . startswith ( ' _ ' ) ]
2013-04-01 14:59:42 +00:00
def list ( self , args ) :
# Fake method for advertising 'list' command
pass
2013-01-05 14:43:21 +00:00
def get_address ( self , args ) :
return self . client . get_address ( args . n )
2013-09-09 13:38:15 +00:00
2012-12-03 15:36:03 +00:00
def get_entropy ( self , args ) :
return binascii . hexlify ( self . client . get_entropy ( args . size ) )
2013-01-05 14:43:21 +00:00
2013-09-12 22:17:06 +00:00
def get_features ( self , args ) :
return pb2json ( self . client . features )
def ping ( self , args ) :
return self . client . ping ( args . msg )
2013-01-05 14:43:21 +00:00
def get_master_public_key ( self , args ) :
return binascii . hexlify ( self . client . get_master_public_key ( ) )
2012-12-03 15:36:03 +00:00
2013-09-12 22:17:06 +00:00
def get_serial_number ( self , args ) :
return binascii . hexlify ( self . client . get_serial_number ( ) )
def set_label ( self , args ) :
return self . client . apply_settings ( label = args . label )
def set_coin ( self , args ) :
return self . client . apply_settings ( coin_shortcut = args . coin_shortcut )
2012-12-05 19:31:26 +00:00
def load_device ( self , args ) :
seed = ' ' . join ( args . seed )
2013-09-09 13:38:15 +00:00
return self . client . load_device ( seed , args . pin )
2012-12-05 19:31:26 +00:00
2013-04-01 14:59:42 +00:00
list . help = ' List connected Trezor USB devices '
2013-09-12 22:17:06 +00:00
ping . help = ' Send ping message '
2013-01-05 14:43:21 +00:00
get_address . help = ' Get bitcoin address in base58 encoding '
2012-12-03 15:36:03 +00:00
get_entropy . help = ' Get example entropy '
2013-09-12 22:17:06 +00:00
get_features . help = ' Retrieve device features and settings '
get_serial_number . help = ' Get device \' s unique identifier '
2012-12-03 15:36:03 +00:00
get_master_public_key . help = ' Get master public key '
2013-09-12 22:17:06 +00:00
set_label . help = ' Set new wallet label '
set_coin . help = ' Switch device to another crypto currency '
2012-12-05 19:31:26 +00:00
load_device . help = ' Load custom configuration to the device '
2012-12-03 15:36:03 +00:00
2013-01-05 14:43:21 +00:00
get_address . arguments = (
( ( ' n ' , ) , { ' metavar ' : ' N ' , ' type ' : int , ' nargs ' : ' + ' } ) ,
)
2012-12-03 15:36:03 +00:00
get_entropy . arguments = (
( ( ' size ' , ) , { ' type ' : int } ) ,
)
2013-09-12 22:17:06 +00:00
get_features . arguments = ( )
ping . arguments = (
( ( ' msg ' , ) , { ' type ' : str } ) ,
)
2012-12-03 15:36:03 +00:00
2013-09-12 22:17:06 +00:00
set_label . arguments = (
( ( ' label ' , ) , { ' type ' : str } ) ,
)
set_coin . arguments = (
( ( ' coin_shortcut ' , ) , { ' type ' : str } ) ,
)
2012-12-05 19:31:26 +00:00
load_device . arguments = (
( ( ' -s ' , ' --seed ' ) , { ' type ' : str , ' nargs ' : ' + ' } ) ,
( ( ' -n ' , ' --pin ' ) , { ' type ' : str , ' default ' : ' ' } ) ,
)
2013-08-30 21:32:02 +00:00
def list_usb ( ) :
from bitkeylib . transport_hid import HidTransport
devices = HidTransport . enumerate ( )
return devices
2012-12-03 15:36:03 +00:00
def main ( ) :
args = parse_args ( Commands )
2013-04-01 14:59:42 +00:00
if args . cmd == ' list ' :
2013-08-30 21:32:02 +00:00
devices = list_usb ( )
2013-04-01 14:59:42 +00:00
if args . json :
print json . dumps ( devices )
else :
for dev in devices :
print dev
return
2012-12-03 15:36:03 +00:00
transport = get_transport ( args . transport , args . path )
2012-12-05 19:31:26 +00:00
if args . debug :
debuglink_transport = get_transport ( args . debuglink_transport , args . debuglink_path )
2013-01-05 14:43:21 +00:00
debuglink = DebugLink ( debuglink_transport )
2012-12-05 19:31:26 +00:00
else :
2013-01-05 14:43:21 +00:00
debuglink = None
2013-04-05 15:12:18 +00:00
client = BitkeyClient ( transport , debuglink = debuglink )
2013-09-09 13:38:15 +00:00
client . setup_debuglink ( button = True , pin_correct = True )
2012-12-03 15:36:03 +00:00
cmds = Commands ( client )
res = args . func ( cmds , args )
if args . json :
2013-09-12 22:17:06 +00:00
print json . dumps ( res , sort_keys = True , indent = 4 )
2012-12-03 15:36:03 +00:00
else :
print res
if __name__ == ' __main__ ' :
2013-04-01 14:59:42 +00:00
main ( )