2014-08-24 10:44:26 -07:00
from binascii import hexlify
2016-08-28 07:38:30 -07:00
from struct import pack , unpack
2016-01-30 01:00:51 -08:00
import hashlib
2016-01-30 01:33:54 -08:00
import time
2016-09-22 01:25:03 -07:00
import sys
import traceback
2014-08-24 10:44:26 -07:00
2014-08-31 06:33:20 -07:00
import electrum
2016-08-28 07:38:30 -07:00
from electrum . bitcoin import EncodeBase58Check , DecodeBase58Check , TYPE_ADDRESS , int_to_hex , var_int
2014-08-24 10:44:26 -07:00
from electrum . i18n import _
2014-08-31 02:42:40 -07:00
from electrum . plugins import BasePlugin , hook
2016-09-22 01:25:03 -07:00
from electrum . keystore import Hardware_KeyStore , parse_xpubkey
2016-01-30 01:33:54 -08:00
from . . hw_wallet import HW_PluginBase
2016-01-30 01:00:51 -08:00
from electrum . util import format_satoshis_plain , print_error
2016-01-29 22:46:19 -08:00
2014-08-24 10:44:26 -07:00
try :
2016-08-28 07:38:30 -07:00
import hid
from btchip . btchipComm import HIDDongleHIDAPI , DongleWait
2014-08-24 10:44:26 -07:00
from btchip . btchip import btchip
2016-08-28 07:38:30 -07:00
from btchip . btchipUtils import compress_public_key , format_transaction , get_regular_input_script , get_p2sh_input_script
2014-08-24 10:44:26 -07:00
from btchip . bitcoinTransaction import bitcoinTransaction
2014-08-27 14:19:14 -07:00
from btchip . btchipPersoWizard import StartBTChipPersoDialog
2014-09-19 07:02:09 -07:00
from btchip . btchipFirmwareWizard import checkFirmware , updateFirmware
2014-08-27 14:19:14 -07:00
from btchip . btchipException import BTChipException
2014-08-24 10:44:26 -07:00
BTCHIP = True
2015-07-04 00:45:08 -07:00
BTCHIP_DEBUG = False
2014-08-24 10:44:26 -07:00
except ImportError :
BTCHIP = False
2016-08-28 07:38:30 -07:00
class Ledger_Client ( ) :
def __init__ ( self , hidDevice ) :
self . dongleObject = btchip ( hidDevice )
self . preflightDone = False
def is_pairable ( self ) :
return True
def close ( self ) :
self . dongleObject . dongle . close ( )
def timeout ( self , cutoff ) :
pass
def is_initialized ( self ) :
return True
def label ( self ) :
return " "
def i4b ( self , x ) :
return pack ( ' >I ' , x )
def get_xpub ( self , bip32_path ) :
self . checkDevice ( )
# bip32_path is of the form 44'/0'/1'
# S-L-O-W - we don't handle the fingerprint directly, so compute
# it manually from the previous node
# This only happens once so it's bearable
#self.get_client() # prompt for the PIN before displaying the dialog if necessary
#self.handler.show_message("Computing master public key")
try :
splitPath = bip32_path . split ( ' / ' )
if splitPath [ 0 ] == ' m ' :
splitPath = splitPath [ 1 : ]
bip32_path = bip32_path [ 2 : ]
fingerprint = 0
if len ( splitPath ) > 1 :
prevPath = " / " . join ( splitPath [ 0 : len ( splitPath ) - 1 ] )
nodeData = self . dongleObject . getWalletPublicKey ( prevPath )
publicKey = compress_public_key ( nodeData [ ' publicKey ' ] )
h = hashlib . new ( ' ripemd160 ' )
h . update ( hashlib . sha256 ( publicKey ) . digest ( ) )
fingerprint = unpack ( " >I " , h . digest ( ) [ 0 : 4 ] ) [ 0 ]
nodeData = self . dongleObject . getWalletPublicKey ( bip32_path )
publicKey = compress_public_key ( nodeData [ ' publicKey ' ] )
depth = len ( splitPath )
lastChild = splitPath [ len ( splitPath ) - 1 ] . split ( ' \' ' )
if len ( lastChild ) == 1 :
childnum = int ( lastChild [ 0 ] )
else :
childnum = 0x80000000 | int ( lastChild [ 0 ] )
xpub = " 0488B21E " . decode ( ' hex ' ) + chr ( depth ) + self . i4b ( fingerprint ) + self . i4b ( childnum ) + str ( nodeData [ ' chainCode ' ] ) + str ( publicKey )
except Exception , e :
#self.give_error(e, True)
return None
finally :
#self.handler.clear_dialog()
pass
return EncodeBase58Check ( xpub )
def has_detached_pin_support ( self , client ) :
try :
client . getVerifyPinRemainingAttempts ( )
return True
except BTChipException , e :
if e . sw == 0x6d00 :
return False
raise e
def is_pin_validated ( self , client ) :
try :
# Invalid SET OPERATION MODE to verify the PIN status
client . dongle . exchange ( bytearray ( [ 0xe0 , 0x26 , 0x00 , 0x00 , 0x01 , 0xAB ] ) )
except BTChipException , e :
if ( e . sw == 0x6982 ) :
return False
if ( e . sw == 0x6A80 ) :
return True
raise e
def perform_hw1_preflight ( self ) :
try :
firmware = self . dongleObject . getFirmwareVersion ( ) [ ' version ' ] . split ( " . " )
if not checkFirmware ( firmware ) :
2016-10-24 06:45:54 -07:00
self . dongleObject . dongle . close ( )
2016-08-28 07:38:30 -07:00
raise Exception ( " HW1 firmware version too old. Please update at https://www.ledgerwallet.com " )
try :
self . dongleObject . getOperationMode ( )
except BTChipException , e :
if ( e . sw == 0x6985 ) :
2016-10-24 06:45:54 -07:00
self . dongleObject . dongle . close ( )
2016-08-28 07:38:30 -07:00
dialog = StartBTChipPersoDialog ( )
dialog . exec_ ( )
# Acquire the new client on the next run
else :
raise e
if self . has_detached_pin_support ( self . dongleObject ) and not self . is_pin_validated ( self . dongleObject ) and ( self . handler < > None ) :
remaining_attempts = self . dongleObject . getVerifyPinRemainingAttempts ( )
if remaining_attempts < > 1 :
msg = " Enter your Ledger PIN - remaining attempts : " + str ( remaining_attempts )
else :
msg = " Enter your Ledger PIN - WARNING : LAST ATTEMPT. If the PIN is not correct, the dongle will be wiped. "
confirmed , p , pin = self . password_dialog ( msg )
if not confirmed :
raise Exception ( ' Aborted by user - please unplug the dongle and plug it again before retrying ' )
pin = pin . encode ( )
self . dongleObject . verifyPin ( pin )
except BTChipException , e :
if ( e . sw == 0x6faa ) :
raise Exception ( " Dongle is temporarily locked - please unplug it and replug it again " )
if ( ( e . sw & 0xFFF0 ) == 0x63c0 ) :
raise Exception ( " Invalid PIN - please unplug the dongle and plug it again before retrying " )
raise e
def checkDevice ( self ) :
if not self . preflightDone :
2016-10-19 23:38:13 -07:00
try :
self . perform_hw1_preflight ( )
except BTChipException as e :
if ( e . sw == 0x6d00 ) :
raise BaseException ( " Device not in Bitcoin mode " )
raise e
2016-08-28 07:38:30 -07:00
self . preflightDone = True
def password_dialog ( self , msg = None ) :
response = self . handler . get_word ( msg )
if response is None :
return False , None , None
return True , response , response
2014-08-24 10:44:26 -07:00
2016-08-18 02:34:37 -07:00
class Ledger_KeyStore ( Hardware_KeyStore ) :
2016-08-28 07:38:30 -07:00
hw_type = ' ledger '
2016-01-01 01:38:43 -08:00
device = ' Ledger '
2014-08-24 10:44:26 -07:00
2016-08-21 13:44:42 -07:00
def __init__ ( self , d ) :
Hardware_KeyStore . __init__ ( self , d )
2016-01-10 21:38:45 -08:00
# Errors and other user interaction is done through the wallet's
# handler. The handler is per-window and preserved across
# device reconnects
self . force_watching_only = False
2014-09-20 05:27:13 -07:00
self . signing = False
2016-08-18 02:34:37 -07:00
2016-08-28 07:38:30 -07:00
def get_derivation ( self ) :
return self . derivation
2014-08-24 10:44:26 -07:00
2016-08-28 07:38:30 -07:00
def get_client ( self ) :
return self . plugin . get_client ( self )
2014-09-19 04:39:12 -07:00
def give_error ( self , message , clear_client = False ) :
2015-07-05 13:14:53 -07:00
print_error ( message )
2014-09-19 04:39:12 -07:00
if not self . signing :
2016-01-29 22:46:19 -08:00
self . handler . show_error ( message )
2014-09-19 04:39:12 -07:00
else :
self . signing = False
2016-01-10 22:08:12 -08:00
if clear_client :
2016-08-18 02:34:37 -07:00
self . client = None
2015-07-04 00:45:08 -07:00
raise Exception ( message )
2014-09-19 04:39:12 -07:00
2016-08-28 07:38:30 -07:00
def address_id_stripped ( self , address ) :
2016-01-10 02:53:28 -08:00
# Strip the leading "m/"
2016-08-28 07:38:30 -07:00
change , index = self . get_address_index ( address )
derivation = self . derivation
address_path = " %s / %d / %d " % ( derivation , change , index )
return address_path [ 2 : ]
2014-08-24 10:44:26 -07:00
def decrypt_message ( self , pubkey , message , password ) :
2016-08-28 07:38:30 -07:00
raise RuntimeError ( _ ( ' Encryption and decryption are currently not supported for %s ' ) % self . device )
2014-08-24 10:44:26 -07:00
2016-08-28 07:38:30 -07:00
def sign_message ( self , sequence , message , password ) :
2015-01-31 17:29:21 -08:00
self . signing = True
2016-01-10 22:08:12 -08:00
# prompt for the PIN before displaying the dialog if necessary
client = self . get_client ( )
2016-08-28 07:38:30 -07:00
address_path = self . get_derivation ( ) [ 2 : ] + " / %d / %d " % sequence
2016-01-10 21:38:45 -08:00
self . handler . show_message ( " Signing message ... " )
2014-08-24 10:44:26 -07:00
try :
info = self . get_client ( ) . signMessagePrepare ( address_path , message )
pin = " "
2015-07-04 00:45:08 -07:00
if info [ ' confirmationNeeded ' ] :
2014-08-24 10:44:26 -07:00
# TODO : handle different confirmation types. For the time being only supports keyboard 2FA
confirmed , p , pin = self . password_dialog ( )
if not confirmed :
2014-08-28 15:38:16 -07:00
raise Exception ( ' Aborted by user ' )
2014-08-24 10:44:26 -07:00
pin = pin . encode ( )
2016-08-28 07:38:30 -07:00
#self.plugin.get_client(self, True, True)
2014-08-24 10:44:26 -07:00
signature = self . get_client ( ) . signMessageSign ( pin )
2015-02-23 07:30:33 -08:00
except BTChipException , e :
2015-01-31 17:29:21 -08:00
if e . sw == 0x6a80 :
2015-08-14 04:34:22 -07:00
self . give_error ( " Unfortunately, this message cannot be signed by the Ledger wallet. Only alphanumerical messages shorter than 140 characters are supported. Please remove any extra characters (tab, carriage return) and retry. " )
2015-07-04 00:45:08 -07:00
else :
self . give_error ( e , True )
2015-02-23 07:30:33 -08:00
except Exception , e :
self . give_error ( e , True )
2014-08-24 10:44:26 -07:00
finally :
2016-01-29 22:46:19 -08:00
self . handler . clear_dialog ( )
2015-01-31 17:29:21 -08:00
self . signing = False
2014-08-24 10:44:26 -07:00
# Parse the ASN.1 signature
rLength = signature [ 3 ]
r = signature [ 4 : 4 + rLength ]
sLength = signature [ 4 + rLength + 1 ]
s = signature [ 4 + rLength + 2 : ]
if rLength == 33 :
r = r [ 1 : ]
if sLength == 33 :
s = s [ 1 : ]
r = str ( r )
s = str ( s )
# And convert it
2015-07-14 07:37:04 -07:00
return chr ( 27 + 4 + ( signature [ 0 ] & 0x01 ) ) + r + s
2014-08-24 10:44:26 -07:00
2014-10-30 13:10:12 -07:00
def sign_transaction ( self , tx , password ) :
2014-10-24 01:53:09 -07:00
if tx . is_complete ( ) :
2014-09-19 04:39:12 -07:00
return
2016-01-27 11:47:49 -08:00
client = self . get_client ( )
2015-07-04 00:45:08 -07:00
self . signing = True
2014-08-24 10:44:26 -07:00
inputs = [ ]
inputsPaths = [ ]
pubKeys = [ ]
2016-08-28 07:38:30 -07:00
chipInputs = [ ]
2015-07-04 00:45:08 -07:00
redeemScripts = [ ]
2014-08-24 10:44:26 -07:00
signatures = [ ]
preparedTrustedInputs = [ ]
2015-07-04 00:45:08 -07:00
changePath = " "
2014-08-24 10:44:26 -07:00
changeAmount = None
output = None
outputAmount = None
2016-08-28 07:38:30 -07:00
p2shTransaction = False
2016-10-24 06:45:54 -07:00
reorganize = False
2014-08-27 14:19:14 -07:00
pin = " "
2016-08-28 07:38:30 -07:00
self . get_client ( ) # prompt for the PIN before displaying the dialog if necessary
2016-09-30 05:56:53 -07:00
2014-08-24 10:44:26 -07:00
# Fetch inputs of the transaction to sign
2016-09-30 05:56:53 -07:00
derivations = self . get_tx_derivations ( tx )
2016-09-22 01:25:03 -07:00
for txin in tx . inputs ( ) :
if txin . get ( ' is_coinbase ' ) :
2014-09-19 04:39:12 -07:00
self . give_error ( " Coinbase not supported " ) # should never happen
2016-09-22 01:25:03 -07:00
if len ( txin [ ' pubkeys ' ] ) > 1 :
p2shTransaction = True
2016-09-30 05:56:53 -07:00
for i , x_pubkey in enumerate ( txin [ ' x_pubkeys ' ] ) :
if x_pubkey in derivations :
signingPos = i
s = derivations . get ( x_pubkey )
hwAddress = " %s / %d / %d " % ( self . get_derivation ( ) [ 2 : ] , s [ 0 ] , s [ 1 ] )
break
else :
self . give_error ( " No matching x_key for sign_transaction " ) # should never happen
2016-08-28 07:38:30 -07:00
2016-09-30 06:01:22 -07:00
inputs . append ( [ txin [ ' prev_tx ' ] . raw , txin [ ' prevout_n ' ] , txin . get ( ' redeemScript ' ) , txin [ ' prevout_hash ' ] , signingPos ] )
2016-08-28 07:38:30 -07:00
inputsPaths . append ( hwAddress )
2016-09-22 01:25:03 -07:00
pubKeys . append ( txin [ ' pubkeys ' ] )
2016-08-28 07:38:30 -07:00
# Sanity check
if p2shTransaction :
for txinput in tx . inputs ( ) :
if len ( txinput [ ' pubkeys ' ] ) < 2 :
self . give_error ( " P2SH / regular input mixed in same transaction not supported " ) # should never happen
2016-09-22 01:25:03 -07:00
txOutput = var_int ( len ( tx . outputs ( ) ) )
for txout in tx . outputs ( ) :
output_type , addr , amount = txout
txOutput + = int_to_hex ( amount , 8 )
script = tx . pay_script ( output_type , addr )
txOutput + = var_int ( len ( script ) / 2 )
txOutput + = script
txOutput = txOutput . decode ( ' hex ' )
2014-08-24 10:44:26 -07:00
# Recognize outputs - only one output and one change is authorized
2016-08-28 07:38:30 -07:00
if not p2shTransaction :
if len ( tx . outputs ( ) ) > 2 : # should never happen
self . give_error ( " Transaction with more than 2 outputs not supported " )
2016-10-19 23:23:10 -07:00
for _type , address , amount in tx . outputs ( ) :
2016-08-28 07:38:30 -07:00
assert _type == TYPE_ADDRESS
2016-10-19 23:23:10 -07:00
info = tx . output_info . get ( address )
if info is not None :
index , xpubs , m = info
changePath = self . get_derivation ( ) [ 2 : ] + " / %d / %d " % index
2016-08-28 07:38:30 -07:00
changeAmount = amount
else :
output = address
outputAmount = amount
2016-09-22 01:25:03 -07:00
2016-09-22 01:54:32 -07:00
self . handler . show_message ( _ ( " Confirm Transaction on your Ledger device... " ) )
2014-08-24 10:44:26 -07:00
try :
# Get trusted inputs from the original transactions
2016-08-28 07:38:30 -07:00
for utxo in inputs :
if not p2shTransaction :
txtmp = bitcoinTransaction ( bytearray ( utxo [ 0 ] . decode ( ' hex ' ) ) )
chipInputs . append ( self . get_client ( ) . getTrustedInput ( txtmp , utxo [ 1 ] ) )
redeemScripts . append ( txtmp . outputs [ utxo [ 1 ] ] . script )
else :
tmp = utxo [ 3 ] . decode ( ' hex ' ) [ : : - 1 ] . encode ( ' hex ' )
tmp + = int_to_hex ( utxo [ 1 ] , 4 )
2016-09-22 01:54:32 -07:00
chipInputs . append ( { ' value ' : tmp . decode ( ' hex ' ) } )
2016-08-28 07:38:30 -07:00
redeemScripts . append ( bytearray ( utxo [ 2 ] . decode ( ' hex ' ) ) )
2014-08-24 10:44:26 -07:00
# Sign all inputs
firstTransaction = True
inputIndex = 0
2016-10-09 00:20:32 -07:00
rawTx = tx . serialize ( )
2014-08-24 10:44:26 -07:00
while inputIndex < len ( inputs ) :
2015-07-04 00:45:08 -07:00
self . get_client ( ) . startUntrustedTransaction ( firstTransaction , inputIndex ,
2016-09-22 01:25:03 -07:00
chipInputs , redeemScripts [ inputIndex ] )
2016-10-09 00:20:32 -07:00
if not p2shTransaction :
outputData = self . get_client ( ) . finalizeInput ( output , format_satoshis_plain ( outputAmount ) ,
format_satoshis_plain ( tx . get_fee ( ) ) , changePath , bytearray ( rawTx . decode ( ' hex ' ) ) )
2016-10-24 06:45:54 -07:00
reorganize = True
2016-10-09 00:20:32 -07:00
else :
outputData = self . get_client ( ) . finalizeInputFull ( txOutput )
outputData [ ' outputData ' ] = txOutput
2014-08-24 10:44:26 -07:00
if firstTransaction :
transactionOutput = outputData [ ' outputData ' ]
2015-09-03 17:07:18 -07:00
if outputData [ ' confirmationNeeded ' ] :
2014-08-24 10:44:26 -07:00
# TODO : handle different confirmation types. For the time being only supports keyboard 2FA
2016-01-29 22:46:19 -08:00
self . handler . clear_dialog ( )
2015-06-14 02:25:26 -07:00
if ' keycardData ' in outputData :
pin2 = " "
for keycardIndex in range ( len ( outputData [ ' keycardData ' ] ) ) :
msg = " Do not enter your device PIN here ! \r \n \r \n " + \
2015-08-14 04:34:22 -07:00
" Your Ledger Wallet wants to talk to you and tell you a unique second factor code. \r \n " + \
2015-06-14 02:25:26 -07:00
" For this to work, please match the character between stars of the output address using your security card \r \n \r \n " + \
2015-07-04 00:45:08 -07:00
" Output address : "
2015-06-14 02:25:26 -07:00
for index in range ( len ( output ) ) :
if index == outputData [ ' keycardData ' ] [ keycardIndex ] :
msg = msg + " * " + output [ index ] + " * "
else :
msg = msg + output [ index ]
2015-07-04 00:45:08 -07:00
msg = msg + " \r \n "
2015-06-14 02:25:26 -07:00
confirmed , p , pin = self . password_dialog ( msg )
if not confirmed :
2015-07-04 00:45:08 -07:00
raise Exception ( ' Aborted by user ' )
2015-06-14 02:25:26 -07:00
try :
pin2 = pin2 + chr ( int ( pin [ 0 ] , 16 ) )
except :
raise Exception ( ' Invalid PIN character ' )
pin = pin2
else :
confirmed , p , pin = self . password_dialog ( )
if not confirmed :
2015-07-04 00:45:08 -07:00
raise Exception ( ' Aborted by user ' )
2015-06-14 02:25:26 -07:00
pin = pin . encode ( )
2016-08-28 07:38:30 -07:00
#self.plugin.get_client(self, True, True)
2016-01-10 21:38:45 -08:00
self . handler . show_message ( " Signing ... " )
2014-08-24 10:44:26 -07:00
else :
# Sign input with the provided PIN
2014-08-27 14:19:14 -07:00
inputSignature = self . get_client ( ) . untrustedHashSign ( inputsPaths [ inputIndex ] ,
pin )
inputSignature [ 0 ] = 0x30 # force for 1.4.9+
signatures . append ( inputSignature )
2014-08-24 10:44:26 -07:00
inputIndex = inputIndex + 1
firstTransaction = False
2016-09-22 01:25:03 -07:00
except BaseException as e :
traceback . print_exc ( file = sys . stdout )
2014-09-19 04:39:12 -07:00
self . give_error ( e , True )
2014-08-24 10:44:26 -07:00
finally :
2016-01-29 22:46:19 -08:00
self . handler . clear_dialog ( )
2014-08-24 10:44:26 -07:00
# Reformat transaction
inputIndex = 0
while inputIndex < len ( inputs ) :
2016-08-28 07:38:30 -07:00
if p2shTransaction :
signaturesPack = [ signatures [ inputIndex ] ] * len ( pubKeys [ inputIndex ] )
inputScript = get_p2sh_input_script ( redeemScripts [ inputIndex ] , signaturesPack )
preparedTrustedInputs . append ( [ ( " \x00 " * 4 ) + chipInputs [ inputIndex ] [ ' value ' ] , inputScript ] )
else :
inputScript = get_regular_input_script ( signatures [ inputIndex ] , pubKeys [ inputIndex ] [ 0 ] . decode ( ' hex ' ) )
preparedTrustedInputs . append ( [ chipInputs [ inputIndex ] [ ' value ' ] , inputScript ] )
2014-08-24 10:44:26 -07:00
inputIndex = inputIndex + 1
updatedTransaction = format_transaction ( transactionOutput , preparedTrustedInputs )
updatedTransaction = hexlify ( updatedTransaction )
2016-10-24 06:45:54 -07:00
if reorganize :
tx . update ( updatedTransaction )
else :
tx . update_signatures ( updatedTransaction )
2014-09-19 04:39:12 -07:00
self . signing = False
2014-08-24 10:44:26 -07:00
def password_dialog ( self , msg = None ) :
if not msg :
2014-09-19 04:39:12 -07:00
msg = _ ( " Do not enter your device PIN here ! \r \n \r \n " \
2015-08-14 04:34:22 -07:00
" Your Ledger Wallet wants to talk to you and tell you a unique second factor code. \r \n " \
2016-01-26 04:20:11 -08:00
" For this to work, please open a text editor " \
" (on a different computer / device if you believe this computer is compromised) " \
" and put your cursor into it, unplug your Ledger Wallet and plug it back in. \r \n " \
" It should show itself to your computer as a keyboard " \
" and output the second factor along with a summary of " \
" the transaction being signed into the text-editor. \r \n \r \n " \
2014-09-19 04:39:12 -07:00
" Check that summary and then enter the second factor code here. \r \n " \
" Before clicking OK, re-plug the device once more (unplug it and plug it again if you read the second factor code on the same computer) " )
2016-01-29 22:46:19 -08:00
response = self . handler . get_word ( msg )
2015-07-05 13:14:53 -07:00
if response is None :
return False , None , None
return True , response , response
2016-08-21 13:44:42 -07:00
class LedgerPlugin ( HW_PluginBase ) :
libraries_available = BTCHIP
keystore_class = Ledger_KeyStore
client = None
2016-08-28 07:38:30 -07:00
DEVICE_IDS = [
( 0x2581 , 0x1807 ) , # HW.1 legacy btchip
( 0x2581 , 0x2b7c ) , # HW.1 transitional production
( 0x2581 , 0x3b7c ) , # HW.1 ledger production
( 0x2581 , 0x4b7c ) , # HW.1 ledger test
( 0x2c97 , 0x0000 ) , # Blue
( 0x2c97 , 0x0001 ) # Nano-S
]
def __init__ ( self , parent , config , name ) :
HW_PluginBase . __init__ ( self , parent , config , name )
if self . libraries_available :
self . device_manager ( ) . register_devices ( self . DEVICE_IDS )
2016-08-21 13:44:42 -07:00
def btchip_is_connected ( self , keystore ) :
try :
2016-08-28 07:38:30 -07:00
self . get_client ( keystore ) . getFirmwareVersion ( )
2016-08-21 13:44:42 -07:00
except Exception as e :
return False
return True
2016-08-28 07:38:30 -07:00
def get_btchip_device ( self , device ) :
ledger = False
if ( device . product_key [ 0 ] == 0x2581 and device . product_key [ 1 ] == 0x3b7c ) or ( device . product_key [ 0 ] == 0x2581 and device . product_key [ 1 ] == 0x4b7c ) or ( device . product_key [ 0 ] == 0x2c97 ) :
ledger = True
dev = hid . device ( )
dev . open_path ( device . path )
dev . set_nonblocking ( True )
return HIDDongleHIDAPI ( dev , ledger , BTCHIP_DEBUG )
def create_client ( self , device , handler ) :
self . handler = handler
client = self . get_btchip_device ( device )
if client < > None :
client = Ledger_Client ( client )
return client
def setup_device ( self , device_info , wizard ) :
devmgr = self . device_manager ( )
device_id = device_info . device . id_
client = devmgr . client_by_id ( device_id )
#client.handler = wizard
client . handler = self . create_handler ( wizard )
2016-08-28 13:22:58 -07:00
#client.get_xpub('m')
client . get_xpub ( " m/44 ' /0 ' " ) # TODO replace by direct derivation once Nano S > 1.1
2016-08-28 07:38:30 -07:00
def get_xpub ( self , device_id , derivation , wizard ) :
devmgr = self . device_manager ( )
client = devmgr . client_by_id ( device_id )
#client.handler = wizard
client . handler = self . create_handler ( wizard )
client . checkDevice ( )
xpub = client . get_xpub ( derivation )
return xpub
def get_client ( self , keystore , force_pair = True ) :
# All client interaction should not be in the main GUI thread
#assert self.main_thread != threading.current_thread()
devmgr = self . device_manager ( )
handler = keystore . handler
handler = keystore . handler
with devmgr . hid_lock :
client = devmgr . client_for_keystore ( self , handler , keystore , force_pair )
# returns the client for a given keystore. can use xpub
#if client:
# client.used()
if client < > None :
client . checkDevice ( )
client = client . dongleObject
return client