Example #1
0
    def _do_test_bip32(self, seed: str, sequence):
        node = BIP32Node.from_rootseed(bfh(seed), xtype='standard')
        xprv, xpub = node.to_xprv(), node.to_xpub()
        self.assertEqual("m/", sequence[0:2])
        sequence = sequence[2:]
        for n in sequence.split('/'):
            if n[-1] != "'":
                xpub2 = BIP32Node.from_xkey(xpub).subkey_at_public_derivation(n).to_xpub()
            node = BIP32Node.from_xkey(xprv).subkey_at_private_derivation(n)
            xprv, xpub = node.to_xprv(), node.to_xpub()
            if n[-1] != "'":
                self.assertEqual(xpub, xpub2)

        return xpub, xprv
Example #2
0
 def update(self, window: 'ElectrumWindow'):
     wallet = window.wallet
     if type(wallet) != Multisig_Wallet:
         return
     assert isinstance(wallet,
                       Multisig_Wallet)  # only here for type-hints in IDE
     if self.listener is None:
         self.logger.info("starting listener")
         self.listener = Listener(self)
         self.listener.start()
     elif self.listener:
         self.logger.info("shutting down listener")
         self.listener.stop()
         self.listener = None
     self.keys = []
     self.cosigner_list = []
     for key, keystore in wallet.keystores.items():
         xpub = keystore.get_master_public_key()  # type: str
         pubkey = BIP32Node.from_xkey(xpub).eckey.get_public_key_bytes(
             compressed=True)
         _hash = bh2u(crypto.sha256d(pubkey))
         if not keystore.is_watching_only():
             self.keys.append((key, _hash, window))
         else:
             self.cosigner_list.append((window, xpub, pubkey, _hash))
     if self.listener:
         self.listener.set_keyhashes([t[1] for t in self.keys])
Example #3
0
 def request_root_fingerprint_from_device(self) -> str:
     # digitalbitbox (at least) does not reveal xpubs corresponding to unhardened paths
     # so ask for a direct child, and read out fingerprint from that:
     child_of_root_xpub = self.get_xpub("m/0'", xtype='standard')
     root_fingerprint = BIP32Node.from_xkey(
         child_of_root_xpub).fingerprint.hex().lower()
     return root_fingerprint
Example #4
0
 def get_xpub(self, bip32_path, xtype):
     self.checkDevice()
     # bip32_path is of the form 44'/5'/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")
     bip32_path = bip32.normalize_bip32_derivation(bip32_path)
     bip32_intpath = bip32.convert_bip32_path_to_list_of_uint32(bip32_path)
     bip32_path = bip32_path[2:]  # cut off "m/"
     if len(bip32_intpath) >= 1:
         prevPath = bip32.convert_bip32_intpath_to_strpath(
             bip32_intpath[:-1])[2:]
         nodeData = self.dongleObject.getWalletPublicKey(prevPath)
         publicKey = compress_public_key(nodeData['publicKey'])
         fingerprint_bytes = hash_160(publicKey)[0:4]
         childnum_bytes = bip32_intpath[-1].to_bytes(length=4,
                                                     byteorder="big")
     else:
         fingerprint_bytes = bytes(4)
         childnum_bytes = bytes(4)
     nodeData = self.dongleObject.getWalletPublicKey(bip32_path)
     publicKey = compress_public_key(nodeData['publicKey'])
     depth = len(bip32_intpath)
     return BIP32Node(xtype=xtype,
                      eckey=ecc.ECPubkey(bytes(publicKey)),
                      chaincode=nodeData['chainCode'],
                      depth=depth,
                      fingerprint=fingerprint_bytes,
                      child_number=childnum_bytes).to_xpub()
Example #5
0
 def _make_node_path(self, xpub, address_n):
     bip32node = BIP32Node.from_xkey(xpub)
     node = self.types.HDNodeType(
         depth=bip32node.depth,
         fingerprint=int.from_bytes(bip32node.fingerprint, 'big'),
         child_num=int.from_bytes(bip32node.child_number, 'big'),
         chain_code=bip32node.chaincode,
         public_key=bip32node.eckey.get_public_key_bytes(compressed=True),
     )
     return self.types.HDNodePathType(node=node, address_n=address_n)
Example #6
0
 def get_xpub(self, bip32_path, xtype, creating=False):
     address_n = parse_path(bip32_path)
     with self.run_flow(creating_wallet=creating):
         node = trezorlib.btc.get_public_node(self.client, address_n).node
     return BIP32Node(xtype=xtype,
                      eckey=ecc.ECPubkey(node.public_key),
                      chaincode=node.chain_code,
                      depth=node.depth,
                      fingerprint=self.i4b(node.fingerprint),
                      child_number=self.i4b(node.child_num)).to_xpub()
Example #7
0
 def get_xpub(self, bip32_path, xtype):
     address_n = self.expand_path(bip32_path)
     creating = False
     node = self.get_public_node(address_n, creating).node
     return BIP32Node(xtype=xtype,
                      eckey=ecc.ECPubkey(node.public_key),
                      chaincode=node.chain_code,
                      depth=node.depth,
                      fingerprint=self.i4b(node.fingerprint),
                      child_number=self.i4b(node.child_num)).to_xpub()
Example #8
0
    def check_from_x(self, x, prv):
        node = BIP32Node.from_xkey(x)

        self.assertEqual(self.xtype, node.xtype)
        self.assertEqual(1, node.depth)
        self.assertEqual(self.master_fpr, bh2u(node.fingerprint))
        self.assertEqual(self.child_num, bh2u(node.child_number))
        self.assertEqual(self.chain_code, bh2u(node.chaincode))
        self.assertEqual(self.pub_key, node.eckey.get_public_key_hex())
        if prv:
            self.assertEqual(self.sec_key, bh2u(node.eckey.get_secret_bytes()))
Example #9
0
    def on_receive(self, keyhash, message):
        self.logger.info(f"signal arrived for {keyhash}")
        for key, _hash, window in self.keys:
            if _hash == keyhash:
                break
        else:
            self.logger.info("keyhash not found")
            return

        wallet = window.wallet
        if isinstance(wallet.keystore, keystore.Hardware_KeyStore):
            window.show_warning(
                _('An encrypted transaction was retrieved from cosigning pool.'
                  ) + '\n' +
                _('However, hardware wallets do not support message decryption, '
                  'which makes them not compatible with the current design of cosigner pool.'
                  ))
            return
        elif wallet.has_keystore_encryption():
            password = window.password_dialog(
                _('An encrypted transaction was retrieved from cosigning pool.'
                  ) + '\n' + _('Please enter your password to decrypt it.'))
            if not password:
                return
        else:
            password = None
            if not window.question(
                    _("An encrypted transaction was retrieved from cosigning pool."
                      ) + '\n' + _("Do you want to open it now?")):
                return

        xprv = wallet.keystore.get_master_private_key(password)
        if not xprv:
            return
        try:
            privkey = BIP32Node.from_xkey(xprv).eckey
            message = privkey.decrypt_message(message)
        except Exception as e:
            self.logger.exception('')
            window.show_error(_('Error decrypting message') + ':\n' + repr(e))
            return

        self.listener.clear(keyhash)
        try:
            tx = tx_from_any(message)
        except SerializationError as e:
            window.show_error(
                _("Dash Electrum was unable to deserialize the transaction:") +
                "\n" + str(e))
            return
        show_transaction(tx, parent=window, prompt_if_unsaved=True)
Example #10
0
 def get_xpub(self, bip32_path, xtype):
     assert xtype in self.plugin.SUPPORTED_XTYPES
     reply = self._get_xpub(bip32_path)
     if reply:
         xpub = reply['xpub']
         # Change type of xpub to the requested type. The firmware
         # only ever returns the mainnet standard type, but it is agnostic
         # to the type when signing.
         if xtype != 'standard' or constants.net.TESTNET:
             node = BIP32Node.from_xkey(xpub, net=constants.BitcoinMainnet)
             xpub = node._replace(xtype=xtype).to_xpub()
         return xpub
     else:
         raise Exception('no reply')
Example #11
0
 def get_account_xpub(account_path):
     root_seed = bip39_to_seed(mnemonic, passphrase)
     root_node = BIP32Node.from_rootseed(root_seed, xtype="standard")
     account_node = root_node.subkey_at_private_derivation(account_path)
     account_xpub = account_node.to_xpub()
     return account_xpub