2014-08-24 10:44:26 -07:00
from PyQt4 . Qt import QApplication , QMessageBox , QDialog , QVBoxLayout , QLabel , QThread , SIGNAL
import PyQt4 . QtCore as QtCore
from binascii import unhexlify
from binascii import hexlify
from struct import pack , unpack
from sys import stderr
from time import sleep
from base64 import b64encode , b64decode
2014-08-31 06:33:20 -07:00
import electrum
2014-08-24 10:44:26 -07:00
from electrum_gui . qt . password_dialog import make_password_dialog , run_password_dialog
from electrum . account import BIP32_Account
2014-08-28 15:38:16 -07:00
from electrum . bitcoin import EncodeBase58Check , DecodeBase58Check , public_key_to_bc_address , bc_address_to_hash_160
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
2014-08-24 10:44:26 -07:00
from electrum . transaction import deserialize
2015-05-25 15:09:26 -07:00
from electrum . wallet import BIP32_HD_Wallet , BIP32_Wallet
2014-08-24 10:44:26 -07:00
2014-08-25 08:25:02 -07:00
from electrum . util import format_satoshis
2014-08-24 10:44:26 -07:00
import hashlib
try :
2015-03-27 10:38:40 -07:00
from usb . core import USBError
2014-08-24 10:44:26 -07:00
from btchip . btchipComm import getDongle , DongleWait
from btchip . btchip import btchip
from btchip . btchipUtils import compress_public_key , format_transaction , get_regular_input_script
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
2014-08-31 09:55:31 -07:00
BTCHIP_DEBUG = False
2014-08-24 10:44:26 -07:00
except ImportError :
BTCHIP = False
class Plugin ( BasePlugin ) :
def __init__ ( self , gui , name ) :
BasePlugin . __init__ ( self , gui , name )
self . _is_available = self . _init ( )
2015-02-01 01:44:29 -08:00
self . wallet = None
2015-05-23 01:38:19 -07:00
2015-05-24 11:37:05 -07:00
def constructor ( self , s ) :
return BTChipWallet ( s )
2014-08-24 10:44:26 -07:00
def _init ( self ) :
return BTCHIP
2015-01-31 17:29:21 -08:00
def is_available ( self ) :
if not self . _is_available :
return False
if not self . wallet :
return False
if self . wallet . storage . get ( ' wallet_type ' ) != ' btchip ' :
return False
return True
2014-08-24 10:44:26 -07:00
def set_enabled ( self , enabled ) :
self . wallet . storage . put ( ' use_ ' + self . name , enabled )
def is_enabled ( self ) :
if not self . is_available ( ) :
return False
2015-01-31 17:29:21 -08:00
if self . wallet . has_seed ( ) :
return False
return True
2014-08-24 10:44:26 -07:00
2015-01-31 17:29:21 -08:00
def btchip_is_connected ( self ) :
try :
self . wallet . get_client ( ) . getFirmwareVersion ( )
except :
return False
return True
2014-08-31 02:42:40 -07:00
@hook
2015-06-09 00:58:40 -07:00
def load_wallet ( self , wallet , window ) :
2015-06-10 17:13:12 -07:00
self . wallet = wallet
self . window = window
2015-01-31 17:29:21 -08:00
if self . btchip_is_connected ( ) :
if not self . wallet . check_proper_device ( ) :
QMessageBox . information ( self . window , _ ( ' Error ' ) , _ ( " This wallet does not match your BTChip device " ) , _ ( ' OK ' ) )
self . wallet . force_watching_only = True
else :
QMessageBox . information ( self . window , _ ( ' Error ' ) , _ ( " BTChip device not detected. \n Continuing in watching-only mode. " ) , _ ( ' OK ' ) )
self . wallet . force_watching_only = True
2014-08-31 02:42:40 -07:00
@hook
2014-08-24 10:44:26 -07:00
def installwizard_restore ( self , wizard , storage ) :
2014-08-31 03:40:57 -07:00
if storage . get ( ' wallet_type ' ) != ' btchip ' :
return
2014-08-24 10:44:26 -07:00
wallet = BTChipWallet ( storage )
try :
wallet . create_main_account ( None )
except BaseException as e :
QMessageBox . information ( None , _ ( ' Error ' ) , str ( e ) , _ ( ' OK ' ) )
return
return wallet
2014-08-31 02:42:40 -07:00
@hook
2014-08-24 10:44:26 -07:00
def send_tx ( self , tx ) :
2014-10-23 22:49:20 -07:00
tx . error = None
2014-08-24 10:44:26 -07:00
try :
2014-10-30 13:10:12 -07:00
self . wallet . sign_transaction ( tx , None )
2014-08-24 10:44:26 -07:00
except Exception as e :
tx . error = str ( e )
2015-01-31 17:29:21 -08:00
class BTChipWallet ( BIP32_HD_Wallet ) :
2014-08-24 10:44:26 -07:00
wallet_type = ' btchip '
2015-01-31 17:29:21 -08:00
root_derivation = " m/44 ' /0 ' "
2014-08-24 10:44:26 -07:00
def __init__ ( self , storage ) :
2015-01-31 17:29:21 -08:00
BIP32_HD_Wallet . __init__ ( self , storage )
2014-08-24 10:44:26 -07:00
self . transport = None
self . client = None
self . mpk = None
self . device_checked = False
2014-09-20 05:27:13 -07:00
self . signing = False
2015-01-31 17:29:21 -08:00
self . force_watching_only = False
2014-08-24 10:44:26 -07:00
2014-09-19 04:39:12 -07:00
def give_error ( self , message , clear_client = False ) :
if not self . signing :
QMessageBox . warning ( QDialog ( ) , _ ( ' Warning ' ) , _ ( message ) , _ ( ' OK ' ) )
else :
self . signing = False
2014-09-20 15:49:36 -07:00
if clear_client and self . client is not None :
2014-09-19 04:39:12 -07:00
self . client . bad = True
2014-09-20 15:49:36 -07:00
self . device_checked = False
2014-09-19 04:39:12 -07:00
raise Exception ( message )
2014-08-24 10:44:26 -07:00
def get_action ( self ) :
if not self . accounts :
return ' create_accounts '
def can_create_accounts ( self ) :
2015-05-25 15:06:28 -07:00
return False
def synchronize ( self ) :
# synchronize existing accounts
BIP32_Wallet . synchronize ( self )
# no further accounts for the moment
2014-08-24 10:44:26 -07:00
def can_change_password ( self ) :
return False
def is_watching_only ( self ) :
2015-01-31 17:29:21 -08:00
return self . force_watching_only
2014-08-24 10:44:26 -07:00
def get_client ( self , noPin = False ) :
if not BTCHIP :
2014-09-19 04:39:12 -07:00
self . give_error ( ' please install github.com/btchip/btchip-python ' )
2014-08-24 10:44:26 -07:00
aborted = False
if not self . client or self . client . bad :
try :
2014-08-31 09:55:31 -07:00
d = getDongle ( BTCHIP_DEBUG )
2014-08-24 10:44:26 -07:00
d . setWaitImpl ( DongleWaitQT ( d ) )
self . client = btchip ( d )
firmware = self . client . getFirmwareVersion ( ) [ ' version ' ] . split ( " . " )
2015-02-23 07:30:33 -08:00
if not checkFirmware ( firmware ) :
2014-09-19 06:51:10 -07:00
d . close ( )
try :
updateFirmware ( )
except Exception , e :
aborted = True
raise e
d = getDongle ( BTCHIP_DEBUG )
d . setWaitImpl ( DongleWaitQT ( d ) )
self . client = btchip ( d )
2014-08-27 14:19:14 -07:00
try :
self . client . getOperationMode ( )
2014-08-28 15:38:16 -07:00
except BTChipException , e :
2014-08-27 14:19:14 -07:00
if ( e . sw == 0x6985 ) :
d . close ( )
dialog = StartBTChipPersoDialog ( )
dialog . exec_ ( )
# Then fetch the reference again as it was invalidated
2014-08-31 09:55:31 -07:00
d = getDongle ( BTCHIP_DEBUG )
2014-08-27 14:19:14 -07:00
d . setWaitImpl ( DongleWaitQT ( d ) )
self . client = btchip ( d )
else :
raise e
2014-08-28 15:38:16 -07:00
if not noPin :
2014-08-24 10:44:26 -07:00
# Immediately prompts for the PIN
2014-08-28 15:38:16 -07:00
remaining_attempts = self . client . getVerifyPinRemainingAttempts ( )
if remaining_attempts < > 1 :
msg = " Enter your BTChip PIN - remaining attempts : " + str ( remaining_attempts )
else :
msg = " Enter your BTChip PIN - WARNING : LAST ATTEMPT. If the PIN is not correct, the dongle will be wiped. "
confirmed , p , pin = self . password_dialog ( msg )
2014-08-24 10:44:26 -07:00
if not confirmed :
aborted = True
2014-08-28 15:38:16 -07:00
raise Exception ( ' Aborted by user - please unplug the dongle and plug it again before retrying ' )
pin = pin . encode ( )
2014-08-24 10:44:26 -07:00
self . client . verifyPin ( pin )
2014-08-28 15:38:16 -07:00
except BTChipException , e :
try :
self . client . dongle . close ( )
except :
pass
self . client = None
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
2014-08-24 10:44:26 -07:00
except Exception , e :
2014-08-28 15:38:16 -07:00
try :
self . client . dongle . close ( )
except :
pass
self . client = None
2014-08-24 10:44:26 -07:00
if not aborted :
2014-09-19 06:51:10 -07:00
raise Exception ( " Could not connect to your BTChip dongle. Please verify access permissions, PIN, or unplug the dongle and plug it again " )
2014-08-24 10:44:26 -07:00
else :
raise e
self . client . bad = False
self . device_checked = False
self . proper_device = False
return self . client
def address_id ( self , address ) :
account_id , ( change , address_index ) = self . get_address_index ( address )
return " 44 ' /0 ' / %s ' / %d / %d " % ( account_id , change , address_index )
def create_main_account ( self , password ) :
self . create_account ( ' Main account ' , None ) #name, empty password
def derive_xkeys ( self , root , derivation , password ) :
derivation = derivation . replace ( self . root_name , " 44 ' /0 ' / " )
xpub = self . get_public_key ( derivation )
return xpub , None
2014-09-20 05:46:26 -07:00
def get_private_key ( self , address , password ) :
return [ ]
2014-08-24 10:44:26 -07:00
def get_public_key ( self , bip32_path ) :
# 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
waitDialog . start ( " Computing master public key " )
try :
splitPath = bip32_path . split ( ' / ' )
fingerprint = 0
if len ( splitPath ) > 1 :
prevPath = " / " . join ( splitPath [ 0 : len ( splitPath ) - 1 ] )
nodeData = self . get_client ( ) . 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 . get_client ( ) . 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 :
2014-09-19 04:39:12 -07:00
self . give_error ( e , True )
2014-08-24 10:44:26 -07:00
finally :
waitDialog . emit ( SIGNAL ( ' dongle_done ' ) )
return EncodeBase58Check ( xpub )
def get_master_public_key ( self ) :
2014-09-20 15:49:36 -07:00
try :
if not self . mpk :
self . mpk = self . get_public_key ( " 44 ' /0 ' " )
return self . mpk
except Exception , e :
self . give_error ( e , True )
2014-08-24 10:44:26 -07:00
def i4b ( self , x ) :
return pack ( ' >I ' , x )
def add_keypairs ( self , tx , keypairs , password ) :
#do nothing - no priv keys available
pass
def decrypt_message ( self , pubkey , message , password ) :
2014-09-19 04:39:12 -07:00
self . give_error ( " Not supported " )
2014-08-24 10:44:26 -07:00
def sign_message ( self , address , message , password ) :
2014-08-28 15:38:16 -07:00
use2FA = False
2015-01-31 17:29:21 -08:00
self . signing = True
2014-08-26 04:04:38 -07:00
self . get_client ( ) # prompt for the PIN before displaying the dialog if necessary
if not self . check_proper_device ( ) :
2014-09-19 04:39:12 -07:00
self . give_error ( ' Wrong device or password ' )
2014-08-24 10:44:26 -07:00
address_path = self . address_id ( address )
waitDialog . start ( " Signing Message ... " )
try :
info = self . get_client ( ) . signMessagePrepare ( address_path , message )
pin = " "
if info [ ' confirmationNeeded ' ] :
# TODO : handle different confirmation types. For the time being only supports keyboard 2FA
2014-08-28 15:38:16 -07:00
use2FA = True
2014-08-24 10:44:26 -07:00
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 ( )
self . client . bad = True
2014-09-20 15:49:36 -07:00
self . device_checked = False
2014-08-24 10:44:26 -07:00
self . get_client ( True )
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 :
self . give_error ( " Unfortunately, this message cannot be signed by BTChip. Only alphanumerical messages shorter than 140 characters are supported. Please remove any extra characters (tab, carriage return) and retry. " )
else :
2015-02-23 07:30:33 -08:00
self . give_error ( e , True )
except Exception , e :
self . give_error ( e , True )
2014-08-24 10:44:26 -07:00
finally :
if waitDialog . waiting :
waitDialog . emit ( SIGNAL ( ' dongle_done ' ) )
2014-08-28 15:38:16 -07:00
self . client . bad = use2FA
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
2014-08-28 15:38:16 -07:00
return b64encode ( 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
2014-10-24 01:53:09 -07:00
if tx . error :
raise BaseException ( tx . error )
2014-09-19 04:39:12 -07:00
self . signing = True
2014-08-24 10:44:26 -07:00
inputs = [ ]
inputsPaths = [ ]
pubKeys = [ ]
trustedInputs = [ ]
redeemScripts = [ ]
signatures = [ ]
preparedTrustedInputs = [ ]
2014-08-24 15:18:38 -07:00
changePath = " "
2014-08-24 10:44:26 -07:00
changeAmount = None
output = None
outputAmount = None
use2FA = False
2014-08-27 14:19:14 -07:00
pin = " "
2014-08-24 10:44:26 -07:00
# Fetch inputs of the transaction to sign
for txinput in tx . inputs :
if ( ' is_coinbase ' in txinput and txinput [ ' is_coinbase ' ] ) :
2014-09-19 04:39:12 -07:00
self . give_error ( " Coinbase not supported " ) # should never happen
2014-08-24 10:44:26 -07:00
inputs . append ( [ self . transactions [ txinput [ ' prevout_hash ' ] ] . raw ,
txinput [ ' prevout_n ' ] ] )
address = txinput [ ' address ' ]
inputsPaths . append ( self . address_id ( address ) )
pubKeys . append ( self . get_public_keys ( address ) )
# Recognize outputs - only one output and one change is authorized
if len ( tx . outputs ) > 2 : # should never happen
2014-09-19 04:39:12 -07:00
self . give_error ( " Transaction with more than 2 outputs not supported " )
2014-08-24 10:44:26 -07:00
for type , address , amount in tx . outputs :
assert type == ' address '
if self . is_change ( address ) :
changePath = self . address_id ( address )
changeAmount = amount
else :
if output < > None : # should never happen
2014-09-19 04:39:12 -07:00
self . give_error ( " Multiple outputs with no change not supported " )
2014-08-24 10:44:26 -07:00
output = address
outputAmount = amount
self . get_client ( ) # prompt for the PIN before displaying the dialog if necessary
if not self . check_proper_device ( ) :
2014-09-19 04:39:12 -07:00
self . give_error ( ' Wrong device or password ' )
2014-08-24 10:44:26 -07:00
waitDialog . start ( " Signing Transaction ... " )
try :
# Get trusted inputs from the original transactions
for utxo in inputs :
txtmp = bitcoinTransaction ( bytearray ( utxo [ 0 ] . decode ( ' hex ' ) ) )
trustedInputs . append ( self . get_client ( ) . getTrustedInput ( txtmp , utxo [ 1 ] ) )
# TODO : Support P2SH later
redeemScripts . append ( txtmp . outputs [ utxo [ 1 ] ] . script )
# Sign all inputs
firstTransaction = True
inputIndex = 0
while inputIndex < len ( inputs ) :
self . get_client ( ) . startUntrustedTransaction ( firstTransaction , inputIndex ,
trustedInputs , redeemScripts [ inputIndex ] )
outputData = self . get_client ( ) . finalizeInput ( output , format_satoshis ( outputAmount ) ,
2014-09-18 13:11:24 -07:00
format_satoshis ( self . get_tx_fee ( tx ) ) , changePath )
2014-08-24 10:44:26 -07:00
if firstTransaction :
transactionOutput = outputData [ ' outputData ' ]
if outputData [ ' confirmationNeeded ' ] :
use2FA = True
# TODO : handle different confirmation types. For the time being only supports keyboard 2FA
waitDialog . emit ( SIGNAL ( ' dongle_done ' ) )
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 ( )
self . client . bad = True
2014-09-20 15:49:36 -07:00
self . device_checked = False
2014-08-24 10:44:26 -07:00
self . get_client ( True )
waitDialog . start ( " Signing ... " )
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
except Exception , e :
2014-09-19 04:39:12 -07:00
self . give_error ( e , True )
2014-08-24 10:44:26 -07:00
finally :
if waitDialog . waiting :
waitDialog . emit ( SIGNAL ( ' dongle_done ' ) )
# Reformat transaction
inputIndex = 0
while inputIndex < len ( inputs ) :
# TODO : Support P2SH later
inputScript = get_regular_input_script ( signatures [ inputIndex ] , pubKeys [ inputIndex ] [ 0 ] . decode ( ' hex ' ) )
preparedTrustedInputs . append ( [ trustedInputs [ inputIndex ] [ ' value ' ] , inputScript ] )
inputIndex = inputIndex + 1
updatedTransaction = format_transaction ( transactionOutput , preparedTrustedInputs )
updatedTransaction = hexlify ( updatedTransaction )
tx . update ( updatedTransaction )
self . client . bad = use2FA
2014-09-19 04:39:12 -07:00
self . signing = False
2014-08-24 10:44:26 -07:00
def check_proper_device ( self ) :
pubKey = DecodeBase58Check ( self . master_public_keys [ " x/0 ' " ] ) [ 45 : ]
if not self . device_checked :
waitDialog . start ( " Checking device " )
try :
nodeData = self . get_client ( ) . getWalletPublicKey ( " 44 ' /0 ' /0 ' " )
except Exception , e :
2014-09-19 04:39:12 -07:00
self . give_error ( e , True )
2014-08-24 10:44:26 -07:00
finally :
waitDialog . emit ( SIGNAL ( ' dongle_done ' ) )
pubKeyDevice = compress_public_key ( nodeData [ ' publicKey ' ] )
self . device_checked = True
if pubKey != pubKeyDevice :
self . proper_device = False
else :
self . proper_device = True
return self . proper_device
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 " \
" Your BTChip wants to talk to you and tell you a unique second factor code. \r \n " \
" 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 BTChip and plug it back in. \r \n " \
2014-09-19 11:23:28 -07:00
" It should show itself to your computer as a keyboard and output the second factor along with a summary of the transaction it is signing 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) " )
2014-08-24 10:44:26 -07:00
d = QDialog ( )
d . setModal ( 1 )
d . setLayout ( make_password_dialog ( d , None , msg , False ) )
return run_password_dialog ( d , None , None )
class DongleWaitingDialog ( QThread ) :
def __init__ ( self ) :
QThread . __init__ ( self )
self . waiting = False
def start ( self , message ) :
self . d = QDialog ( )
self . d . setModal ( 1 )
self . d . setWindowTitle ( ' Please Wait ' )
self . d . setWindowFlags ( self . d . windowFlags ( ) | QtCore . Qt . WindowStaysOnTopHint )
l = QLabel ( message )
vbox = QVBoxLayout ( self . d )
vbox . addWidget ( l )
self . d . show ( )
if not self . waiting :
self . waiting = True
self . d . connect ( waitDialog , SIGNAL ( ' dongle_done ' ) , self . stop )
def stop ( self ) :
self . d . hide ( )
self . waiting = False
if BTCHIP :
waitDialog = DongleWaitingDialog ( )
2014-09-02 00:00:20 -07:00
# Tickle the UI a bit while waiting
class DongleWaitQT ( DongleWait ) :
def __init__ ( self , dongle ) :
self . dongle = dongle
def waitFirstResponse ( self , timeout ) :
customTimeout = 0
while customTimeout < timeout :
try :
response = self . dongle . waitFirstResponse ( 200 )
return response
except USBError , e :
if e . backend_error_code == - 7 :
QApplication . processEvents ( )
customTimeout = customTimeout + 100
pass
else :
raise e
raise e