Example #1
0
 def callback_PassphraseRequest(self, msg):
     passphrase = self.ask_for_pass_fun(msg)
     if passphrase is None:
         raise HardwareWalletCancelException('Cancelled')
     else:
         passphrase = unicodedata.normalize('NFKD', passphrase)
     return keepkey_proto.PassphraseAck(passphrase=passphrase)
 def callback_PassphraseRequest(self, msg):
     passphrase = self.ask_for_pass_fun(msg)
     if passphrase is None:
         raise HardwareWalletCancelException('Cancelled')
     else:
         if self.passphrase_encoding in ('NFKD', 'NFC'):
             passphrase = unicodedata.normalize(self.passphrase_encoding, passphrase)
         else:
             raise Exception('Invalid passphrase encoding value: ' + self.passphrase_encoding)
     return keepkey_proto.PassphraseAck(passphrase=passphrase)
Example #3
0
    def callback_WordRequest(self, msg):
        if msg.type in (trezor_proto.WordRequestType.Matrix9,
                        trezor_proto.WordRequestType.Matrix6):
            return self.callback_RecoveryMatrix(msg)

        msg = "Enter one word of mnemonic: "
        word = ask_for_word_callback(msg, self.__mnemonic.wordlist)
        if not word:
            raise HardwareWalletCancelException('Cancelled')
        return trezor_proto.WordAck(word=word)
def sign_message(hw_session: HwSessionInfo, bip32path, message):
    client = hw_session.hw_client
    address_n = client.expand_path(clean_bip32_path(bip32path))
    try:
        return client.sign_message(hw_session.app_config.hw_coin_name,
                                   address_n, message)
    except CallException as e:
        if e.args and len(
                e.args) >= 2 and e.args[1].lower().find('cancelled') >= 0:
            raise HardwareWalletCancelException('Cancelled')
        else:
            raise
 def callback_PinMatrixRequest(self, msg):
     if msg.type == 1:
         desc = 'Enter current PIN'
     elif msg.type == 2:
         desc = 'Enter new PIN'
     elif msg.type == 3:
         desc = 'Enter new PIN again'
     else:
         desc = 'Enter PIN'
     pin = self.ask_for_pin_fun(desc)
     if not pin:
         raise HardwareWalletCancelException('Cancelled')
     return keepkey_proto.PinMatrixAck(pin=pin)
Example #6
0
def get_entropy(hw_device_id, len_bytes):
    client = None
    try:
        client = connect_keepkey(hw_device_id)

        if client:
            client.get_entropy(len_bytes)
            client.close()
        else:
            raise Exception('Couldn\'t connect to Trezor device.')
    except CallException as e:
        if not (len(e.args) >= 0 and str(e.args[1]) == 'Action cancelled by user'):
            raise
        else:
            if client:
                client.close()
            raise HardwareWalletCancelException('Cancelled')

    except HardwareWalletCancelException:
        if client:
            client.close()
        raise
 def callback_WordRequest(self, msg):
     msg = "Enter one word of mnemonic: "
     word = ask_for_word_callback(msg, self.__mnemonic.wordlist)
     if not word:
         raise HardwareWalletCancelException('Cancelled')
     return keepkey_proto.WordAck(word=word)
Example #8
0
def sign_message(main_ui, bip32_path, message):

    client = main_ui.hw_client
    # Ledger doesn't accept characters other that ascii printable:
    # https://ledgerhq.github.io/btchip-doc/bitcoin-technical.html#_sign_message
    message = message.encode('ascii', 'ignore')
    bip32_path = clean_bip32_path(bip32_path)

    ok = False
    for i in range(1, 4):
        info = client.signMessagePrepare(bip32_path, message)
        if info['confirmationNeeded'] and info['confirmationType'] == 34:
            if i == 1 or \
                WndUtils.queryDlg('Another application (such as Ledger Wallet Bitcoin app) has probably taken over '
                     'the communication with the Ledger device.'
                     '\n\nTo continue, close that application and click the <b>Retry</b> button.'
                     '\nTo cancel, click the <b>Abort</b> button',
                 buttons=QMessageBox.Retry | QMessageBox.Abort,
                 default_button=QMessageBox.Retry, icon=QMessageBox.Warning) == QMessageBox.Retry:

                # we need to reconnect the device; first, we'll try to reconnect to HW without closing the intefering
                # application; it it doesn't help we'll display a message requesting the user to close the app
                main_ui.disconnectHardwareWallet()
                if main_ui.connectHardwareWallet():
                    client = main_ui.hw_client
                else:
                    raise Exception('Hardware wallet reconnect error.')
            else:
                break
        else:
            ok = True
            break

    if not ok:
        raise HardwareWalletCancelException('Cancelled')

    try:
        signature = client.signMessageSign()
    except Exception as e:
        logging.exception('Exception while signing message with Ledger Nano S')
        raise Exception(
            'Exception while signing message with Ledger Nano S. Details: ' +
            str(e))

    try:
        pubkey = client.getWalletPublicKey(bip32_path)
    except Exception as e:
        logging.exception(
            'Could not get public key for BIP32 path from Ledger Nano S')
        raise Exception(
            'Could not get public key for BIP32 path from Ledger Nano S. Details: '
            + str(e))

    if len(signature) > 4:
        r_length = signature[3]
        r = signature[4:4 + r_length]
        if len(signature) > 4 + r_length + 1:
            s_length = signature[4 + r_length + 1]
            if len(signature) > 4 + r_length + 2:
                s = signature[4 + r_length + 2:]
                if r_length == 33:
                    r = r[1:]
                if s_length == 33:
                    s = s[1:]
            else:
                logging.error(
                    'client.signMessageSign() returned invalid response (code 3): '
                    + signature.hex())
                raise Exception('Invalid signature returned (code 3).')
        else:
            logging.error(
                'client.signMessageSign() returned invalid response (code 2): '
                + signature.hex())
            raise Exception('Invalid signature returned (code 2).')
    else:
        logging.error(
            'client.signMessageSign() returned invalid response (code 1): ' +
            signature.hex())
        raise Exception('Invalid signature returned (code 1).')

    return MessageSignature(
        pubkey.get('address').decode('ascii'),
        bytes(chr(27 + 4 + (signature[0] & 0x01)), "utf-8") + r + s)
 def callback_PassphraseRequest(self, msg):
     passphrase = self.ask_for_pass_fun(msg)
     if not passphrase:
         raise HardwareWalletCancelException('Cancelled')
     return keepkey_proto.PassphraseAck(passphrase=passphrase)