How to use the bitcoinlib.wallets.HDWalletKey function in bitcoinlib

To help you get started, we’ve selected a few bitcoinlib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
else:
                path = "multisig-%d-of-" % self.multisig_n_required + '/'.join(public_key_ids)
            if not name:
                name = "Multisig Key " + '/'.join(public_key_ids)
            multisig_key = DbKey(
                name=name, wallet_id=self.wallet_id, purpose=self.purpose, account_id=account_id,
                depth=depth, change=change, address_index=0, parent_id=0, is_private=False, path=path,
                public=to_hexstring(redeemscript), wif='multisig-%s' % address, address=address,
                key_type='multisig', network_name=network)
            self._session.add(multisig_key)
            self._session.commit()
            for child_id in public_key_ids:
                self._session.add(DbKeyMultisigChildren(key_order=public_key_ids.index(child_id),
                                                        parent_id=multisig_key.id, child_id=child_id))
            self._session.commit()
            return HDWalletKey(multisig_key.id, session=self._session)
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
:type key_type: str

        :return HDWalletKey: HDWalletKey object
        """

        if isinstance(key, HDKey):
            k = key
        else:
            if network is None:
                network = DEFAULT_NETWORK
            k = HDKey(import_key=key, network=network)

        keyexists = session.query(DbKey).filter(DbKey.wallet_id == wallet_id, DbKey.wif == k.wif()).first()
        if keyexists:
            _logger.warning("Key %s already exists" % (key or k.wif()))
            return HDWalletKey(keyexists.id, session, k)

        if key_type != 'single' and k.depth != len(path.split('/'))-1:
            if path == 'm' and k.depth == 3:
                # Create path when importing new account-key
                nw = Network(network)
                networkcode = nw.bip44_cointype
                path = "m/%d'/%s'/%d'" % (purpose, networkcode, account_id)
            else:
                raise WalletError("Key depth of %d does not match path length of %d for path %s" %
                                  (k.depth, len(path.split('/')) - 1, path))

        wk = session.query(DbKey).filter(DbKey.wallet_id == wallet_id,
                                         or_(DbKey.public == k.public_hex,
                                             DbKey.wif == k.wif())).first()
        if wk:
            return HDWalletKey(wk.id, session, k)
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
def accounts(self, network=None):
        """
        Get list of accounts for this wallet
        
        :param network: Network name filter
        :type network: str
                
        :return list: List of accounts as HDWalletKey objects
        """

        accounts = []
        if self.scheme == 'multisig':
            for wlt in self.cosigner:
                if wlt.main_key.is_private:
                    accounts.append(HDWalletKey(wlt.main_key.key_id, self._session))
        else:
            wks = self.keys_accounts(network=network)

            for wk in wks:
                accounts.append(HDWalletKey(wk.id, self._session))
        return accounts
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
self.network = Network(db_wlt.network_name)
            self.purpose = db_wlt.purpose
            self.scheme = db_wlt.scheme
            self._balance = None
            self._balances = []
            self.main_key_id = db_wlt.main_key_id
            self.main_key = None
            self.default_account_id = 0
            self.multisig_n_required = db_wlt.multisig_n_required
            self.multisig_compressed = None
            co_sign_wallets = self._session.query(DbWallet).\
                filter(DbWallet.parent_id == self.wallet_id).order_by(DbWallet.name).all()
            self.cosigner = [HDWallet(w.id, databasefile=databasefile) for w in co_sign_wallets]
            self.sort_keys = db_wlt.sort_keys
            if main_key_object:
                self.main_key = HDWalletKey(self.main_key_id, session=self._session, hdkey_object=main_key_object)
            elif db_wlt.main_key_id:
                self.main_key = HDWalletKey(self.main_key_id, session=self._session)
            if self.main_key:
                self.default_account_id = self.main_key.account_id
            _logger.info("Opening wallet '%s'" % self.name)
            self._key_objects = {
                self.main_key_id: self.main_key
            }
        else:
            raise WalletError("Wallet '%s' not found, please specify correct wallet ID or name." % wallet)
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
last_used_qr = self._session.query(DbKey).\
            filter_by(wallet_id=self.wallet_id, account_id=account_id, network_name=network,
                      used=True, change=change, depth=keys_depth).\
            order_by(DbKey.id.desc()).first()
        last_used_key_id = 0
        if last_used_qr:
            last_used_key_id = last_used_qr.id
        dbkey = self._session.query(DbKey).\
            filter_by(wallet_id=self.wallet_id, account_id=account_id, network_name=network,
                      used=False, change=change, depth=keys_depth).filter(DbKey.id > last_used_key_id).\
            order_by(DbKey.id).all()
        key_list = []
        for i in range(number_of_keys):
            if dbkey:
                dk = dbkey.pop()
                nk = HDWalletKey(dk.id, session=self._session)
            else:
                nk = self.new_key(account_id=account_id, network=network, change=change, max_depth=depth_of_keys)
            key_list.append(nk)
        if len(key_list) == 1:
            return key_list[0]
        else:
            return key_list
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
:param network: Network name filter
        :type network: str
                
        :return list: List of accounts as HDWalletKey objects
        """

        accounts = []
        if self.scheme == 'multisig':
            for wlt in self.cosigner:
                if wlt.main_key.is_private:
                    accounts.append(HDWalletKey(wlt.main_key.key_id, self._session))
        else:
            wks = self.keys_accounts(network=network)

            for wk in wks:
                accounts.append(HDWalletKey(wk.id, self._session))
        return accounts
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
def import_master_key(self, hdkey, name='Masterkey (imported)'):
        """
        Import (another) masterkey in this wallet

        :param hdkey: Private key
        :type hdkey: HDKey, str
        :param name: Key name of masterkey
        :type name: str

        :return HDKey: Main key as HDKey object
        """
        network, account_id, acckey = self._get_account_defaults()

        if not isinstance(hdkey, HDKey):
            hdkey = HDKey(hdkey)
        if not isinstance(self.main_key, HDWalletKey):
            raise WalletError("Main wallet key is not an HDWalletKey instance. Type %s" % type(self.main_key))
        if not hdkey.isprivate or hdkey.depth != 0:
            raise WalletError("Please supply a valid private BIP32 master key with key depth 0")
        if self.main_key.depth != 3 or self.main_key.is_private or self.main_key.key_type != 'bip32':
            raise WalletError("Current main key is not a valid BIP32 public account key")
        if self.main_key.wif != hdkey.account_key().wif_public():
            raise WalletError("This key does not correspond to current main account key")
        if not (self.network.network_name == self.main_key.network.network_name == hdkey.network.network_name):
            raise WalletError("Network of Wallet class, main account key and the imported private key must use "
                              "the same network")

        self.main_key = HDWalletKey.from_key(
            key=hdkey.wif(), name=name, session=self._session, wallet_id=self.wallet_id, network=network,
            account_id=account_id, purpose=self.purpose, key_type='bip32')
        self.main_key_id = self.main_key.key_id
        network_code = self.network.bip44_cointype
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
I.e: Use account(0).key().wif_public() to get wallet's public account key

        :param account_id: ID of account. Default is 0
        :type account_id: int

        :return HDWalletKey:

        """
        qr = self._session.query(DbKey).\
            filter_by(wallet_id=self.wallet_id, purpose=self.purpose, network_name=self.network.network_name,
                      account_id=account_id, depth=3).scalar()
        if not qr:
            raise WalletError("Account with ID %d not found in this wallet" % account_id)
        key_id = qr.id
        return HDWalletKey(key_id, session=self._session)
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
dbkey = None
        qr = self._session.query(DbKey).filter_by(wallet_id=self.wallet_id, purpose=self.purpose)
        if isinstance(term, numbers.Number):
            dbkey = qr.filter_by(id=term).scalar()
        if not dbkey:
            dbkey = qr.filter_by(address=term).first()
        if not dbkey:
            dbkey = qr.filter_by(wif=term).first()
        if not dbkey:
            dbkey = qr.filter_by(name=term).first()
        if dbkey:
            if dbkey.id in self._key_objects.keys():
                return self._key_objects[dbkey.id]
            else:
                return HDWalletKey(key_id=dbkey.id, session=self._session)
        else:
            raise KeyError("Key '%s' not found" % term)