How to use the bitcoinlib.keys.HDKey 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 / tests / test_wallets.py View on Github external
def test_wallet_import_private_for_known_public_p2sh_segwit(self):
        pk1 = HDKey('YXscyqNJ5YK411nwB3VjLYgjht3dqfKxyLdGSqNMGKhYdcK4Gh1CRSJyxS2So8KXSQrxtysS1jAmHtLnxRKa47xEiAx6hP'
                    'vrj8tuEzyeR8TQNu5e')
        pk2 = HDKey('YXscyqNJ5YK411nwB4Jo3JCQ1GZNetf4BrLJjZiqdWKVzoXwPtyJ5xyNdZjuEWtqCeSZGtmg7SuQerERwniHLYL3aVcnyS'
                    'ciEAxk7gLgDkoZC5Lq')
        w = HDWallet.create('segwit-p2sh-p2wsh-import',
                            [pk1, pk2.public_master(witness_type='p2sh-segwit', multisig=True)],
                            witness_type='p2sh-segwit', network='bitcoinlib_test', db_uri=self.DATABASE_URI)
        w.get_key()
        w.utxos_update()
        t = w.sweep('23CvEnQKsTVGgqCZzW6ewXPSJH9msFPsBt3')
        self.assertEqual(len(t.inputs[0].signatures), 1)
        self.assertFalse(t.verify())

        w.import_key(pk2)
        wc0 = w.cosigner[0]
        self.assertEqual(len(wc0.keys(is_private=False)), 0)
        t2 = w.send_to('23CvEnQKsTVGgqCZzW6ewXPSJH9msFPsBt3', 1000000)
github 1200wd / bitcoinlib / tests / test_wallets.py View on Github external
def test_wallet_single_key_main_key(self):
        w = HDWallet.create('multisig_with_single_key', [HDKey().public_master_multisig(), HDKey(key_type='single')],
                            sigs_required=2, db_uri=self.DATABASE_URI)
        w.new_key()
        self.assertEqual(len(w.keys_addresses()), 1)
github 1200wd / bitcoinlib / tests / test_wallets.py View on Github external
def test_wallet_import_master_key(self):
        k = HDKey()
        w = HDWallet.create('test_wallet_import_master_key', keys=k.public_master(),
                            db_uri=self.DATABASE_URI)
        self.assertFalse(w.main_key.is_private)
        self.assertRaisesRegexp(WalletError, "Please supply a valid private BIP32 master key with key depth 0",
                                w.import_master_key, k.public())
        self.assertRaisesRegexp(WalletError, "Network of Wallet class, main account key and the imported private "
                                             "key must use the same network",
                                w.import_master_key, HDKey(network='litecoin'))
        self.assertRaisesRegexp(WalletError, "This key does not correspond to current public master key",
                                w.import_master_key, HDKey())
        w.import_master_key(k.wif_private())
        self.assertTrue(w.main_key.is_private)

        k2 = HDKey()
        w2 = HDWallet.create('test_wallet_import_master_key2', keys=k2.subkey_for_path("m/32'"), scheme='single',
                             db_uri=self.DATABASE_URI)
        self.assertRaisesRegexp(WalletError, "Main key is already a private key, cannot import key",
                                w2.import_master_key, k2)
        w2.main_key = None
        self.assertRaisesRegexp(WalletError, "Main wallet key is not an HDWalletKey instance",
                                w2.import_master_key, k2)

        k3 = HDKey()
        w3 = HDWallet.create('test_wallet_import_master_key3', keys=k3.subkey_for_path("m/32'").public(),
github 1200wd / bitcoinlib / tests / test_wallets.py View on Github external
def test_wallet_key_import_and_sign_multisig(self):
        network = 'bitcoinlib_test'
        words = 'square innocent drama'
        seed = Mnemonic().to_seed(words, 'password')
        hdkey = HDKey.from_seed(seed, network=network)
        hdkey.key_type = 'single'

        key_list = [
            HDKey(network=network, multisig=True).public_master(),
            HDKey(network=network),
            hdkey.public()
        ]
        with HDWallet.create('Multisig-2-of-3-example', key_list, sigs_required=2,
                             db_uri=self.DATABASE_URI) as wlt:
            wlt.new_key()
            wlt.utxos_update()
            wt = wlt.send_to('21A6yyUPRL9hZZo1Rw4qP5G6h9idVVLUncE', 10000000)
            wt.sign(hdkey)
            wt.send()
            self.assertIsNone(wt.error)
github 1200wd / bitcoinlib / tests / test_wallets.py View on Github external
def test_wallet_transaction_sign_with_wif(self):
        wif = 'YXscyqNJ5YK411nwB4eU6PmyGTJkBUHjgXEf53z4TTjHCDXPPXKJD2PyfXonwtT7VwSdqcZJS2oeDbvg531tEsx3yq4425Mfrb9aS' \
              'PyNQ5bUGFwu'
        wif2 = 'YXscyqNJ5YK411nwB4UK8ScMahPWewyKrTBjgM5BZKRkPg8B2HmKT3r8yc2GFg9GqgFXaWmxkTRhNkRGVxbzUREMH8L5HxoKGCY8' \
               'WDdf1GcW2k8q'
        w = wallet_create_or_open('test_wallet_transaction_sign_with_wif',
                                  keys=[wif, HDKey(wif2).public_master_multisig(witness_type='segwit')],
                                  witness_type='segwit', network='bitcoinlib_test',
                                  db_uri=self.DATABASE_URI)
        w.get_key()
        w.utxos_update()
        t = w.send_to('blt1q285vnphcs4r0t5dw06tmxl7aryj3jnx88duehv4p7eldsshrmygsmlq84z', 2000, fee=1000)
        t.sign(wif2)
        self.assertIsNone(t.send())
        self.assertTrue(t.pushed)
github 1200wd / bitcoinlib / tests / test_wallets.py View on Github external
def test_wallet_segwit_multiple_account_paths(self):
        pk1 = HDKey(
            "ZprvAhadJRUYsNge9JCXTr7xphZaR6sW3HEeSQL7wgtEXceG5hoUViB9KQ4EX6hAdgziW7MorQAjyasWYirrCQrb3ySHaPBa8EiLTx"
            "t4LmqTyzp")
        pk2 = HDKey(
            "ZprvAhadJRUYsNgeBbjftwKvAhDEV1hrYBGY19wATHqnEt5jfWXxXChYP8Qfnw3w2zJZskNercma5S1fWYH7e7XwbTVPgbabvs1CfU"
            "zY2KQD2cB")
        w = HDWallet.create("account-test", keys=[pk1, pk2.public_master(multisig=True)], witness_type='segwit',
                            db_uri=self.DATABASE_URI)
        w.new_account()
        w.new_account()
        w.new_account(account_id=100)
        self.assertRaisesRegexp(WalletError, "Account with ID 100 already exists for this wallet",
                                w.new_account, 'test', 100)
        paths = ["m/48'/0'/0'/2'", "m/48'/0'/0'/2'/0/0", "m/48'/0'/0'/2'/1/0", "m/48'/0'/1'/2'", "m/48'/0'/1'/2'/0/0",
                 "m/48'/0'/1'/2'/1/0", "m/48'/0'/100'/2'", "m/48'/0'/100'/2'/0/0", "m/48'/0'/100'/2'/1/0"]
        self.assertListEqual(sorted(paths), sorted([k.path for k in w.keys()]))
        self.assertListEqual(w.accounts(), [0, 1, 100])
github 1200wd / bitcoinlib / tests / test_wallets.py View on Github external
def test_wallet_multisig_litecoin_transaction_send_offline(self):
        self.db_remove()
        NETWORK = 'litecoin_legacy'
        pk2 = HDKey('e2cbed99ad03c500f2110f1a3c90e0562a3da4ba0cff0e74028b532c3d69d29d', network=NETWORK)
        key_list = [
            HDKey('e9e5095d3e26643cc4d996efc6cb9a8d8eb55119fdec9fa28a684ba297528067', network=NETWORK),
            pk2.public_master(multisig=True),
            HDKey('86b77aee5cfc3a55eb0b1099752479d82cb6ebaa8f1c4e9ef46ca0d1dc3847e6',
                  network=NETWORK).public_master(multisig=True),
        ]
        wl = HDWallet.create('multisig_test_bitcoin_send', key_list, sigs_required=2, network=NETWORK,
                             db_uri=self.DATABASE_URI)
        wl.get_key(number_of_keys=2)
        wl.utxo_add(wl.get_key().address, 200000, '46fcfdbdc3573756916a0ced8bbc5418063abccd2c272f17bf266f77549b62d5', 0)
        t = wl.transaction_create([('3DrP2R8XmHswUyeK9GeYgHJxvyxTfMNkid', 100000)])
        t.sign(pk2.subkey_for_path("m/45'/2/0/0"))
        t.send(offline=True)
        self.assertTrue(t.verify())
        self.assertIsNone(t.error)
github 1200wd / bitcoinlib / tests / test_wallets.py View on Github external
def test_wallet_multisig_litecoin_transaction_send_offline(self):
        self.db_remove()
        NETWORK = 'litecoin_legacy'
        pk2 = HDKey('e2cbed99ad03c500f2110f1a3c90e0562a3da4ba0cff0e74028b532c3d69d29d', network=NETWORK)
        key_list = [
            HDKey('e9e5095d3e26643cc4d996efc6cb9a8d8eb55119fdec9fa28a684ba297528067', network=NETWORK),
            pk2.public_master(multisig=True),
            HDKey('86b77aee5cfc3a55eb0b1099752479d82cb6ebaa8f1c4e9ef46ca0d1dc3847e6',
                  network=NETWORK).public_master(multisig=True),
        ]
        wl = HDWallet.create('multisig_test_bitcoin_send', key_list, sigs_required=2, network=NETWORK,
                             db_uri=self.DATABASE_URI)
        wl.get_key(number_of_keys=2)
        wl.utxo_add(wl.get_key().address, 200000, '46fcfdbdc3573756916a0ced8bbc5418063abccd2c272f17bf266f77549b62d5', 0)
        t = wl.transaction_create([('3DrP2R8XmHswUyeK9GeYgHJxvyxTfMNkid', 100000)])
        t.sign(pk2.subkey_for_path("m/45'/2/0/0"))
        t.send(offline=True)
        self.assertTrue(t.verify())
        self.assertIsNone(t.error)
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