def test_export_private_key_deterministic(self, mock_save_db): wallet = restore_wallet_from_text( 'hint shock chair puzzle shock traffic drastic note dinosaur mention suggest sweet', gap_limit=2, path='if_this_exists_mocking_failed_648151893', config=self.config)['wallet'] cmds = Commands(config=self.config) # single address tests with self.assertRaises(Exception): cmds._run('getprivatekeys', ("asdasd", ), wallet=wallet ) # invalid addr, though might raise "not in wallet" with self.assertRaises(Exception): cmds._run('getprivatekeys', ("XdDHzW6aTeuQsraNXeEsPy5gAv1nUz7Y7Q", ), wallet=wallet) # not in wallet self.assertEqual( "p2pkh:XE5VEmWKQRK5N7kQMfw6KqoRp3ExKWgaeCKsxsmDFBxJJBgdQdTH", cmds._run('getprivatekeys', ("XvmHzyQe8QWbvv17wc1PPMyJgaomknSp7W", ), wallet=wallet)) # list of addresses tests with self.assertRaises(Exception): cmds._run('getprivatekeys', (['XvmHzyQe8QWbvv17wc1PPMyJgaomknSp7W', 'asd'], ), wallet=wallet) self.assertEqual([ 'p2pkh:XE5VEmWKQRK5N7kQMfw6KqoRp3ExKWgaeCKsxsmDFBxJJBgdQdTH', 'p2pkh:XGtpLmVGmaRnfvRvd4qxSeE7PqJoi9FUfkgPKD24PeoJsZCh1EXg' ], cmds._run('getprivatekeys', ([ 'XvmHzyQe8QWbvv17wc1PPMyJgaomknSp7W', 'XoEUKPPiPETff1S4oQmo4HGR1rYrRAX6uT' ], ), wallet=wallet))
def test_export_private_key_imported(self, mock_save_db): wallet = restore_wallet_from_text( 'p2pkh:XGx8LpkmLRv9RiMvpYx965BCaQKQbeMVVqgAh7B5SQVdosQiKJ4i p2pkh:XEn9o6oayjsRmoEQwDbvkrWVvjRNqPj3xNskJJPAKraJTrWuutwd', path='if_this_exists_mocking_failed_648151893', config=self.config)['wallet'] cmds = Commands(config=self.config) # single address tests with self.assertRaises(Exception): cmds._run('getprivatekeys', ("asdasd", ), wallet=wallet ) # invalid addr, though might raise "not in wallet" with self.assertRaises(Exception): cmds._run('getprivatekeys', ("XdDHzW6aTeuQsraNXeEsPy5gAv1nUz7Y7Q", ), wallet=wallet) # not in wallet self.assertEqual( "p2pkh:XEn9o6oayjsRmoEQwDbvkrWVvjRNqPj3xNskJJPAKraJTrWuutwd", cmds._run('getprivatekeys', ("Xci5KnMVkHrqBQk9cU4jwmzJfgaTPopHbz", ), wallet=wallet)) # list of addresses tests with self.assertRaises(Exception): cmds._run('getprivatekeys', (['XmQ3Tn67Fgs7bwNXthtiEnBFh7ZeDG3aw2', 'asd'], ), wallet=wallet) self.assertEqual([ 'p2pkh:XGx8LpkmLRv9RiMvpYx965BCaQKQbeMVVqgAh7B5SQVdosQiKJ4i', 'p2pkh:XEn9o6oayjsRmoEQwDbvkrWVvjRNqPj3xNskJJPAKraJTrWuutwd' ], cmds._run('getprivatekeys', ([ 'XmQ3Tn67Fgs7bwNXthtiEnBFh7ZeDG3aw2', 'Xci5KnMVkHrqBQk9cU4jwmzJfgaTPopHbz' ], ), wallet=wallet))
def test_restore_wallet_from_text_addresses(self): text = 'XdjwdihsyoLpoCHFUpd8x3iH1rsMsS2q5P Xdt8NqE5wSX9ytfP958t4tKdXoZDo6Bm6T' d = restore_wallet_from_text(text, path=self.wallet_path, config=self.config) wallet = d['wallet'] # type: Imported_Wallet self.assertEqual('XdjwdihsyoLpoCHFUpd8x3iH1rsMsS2q5P', wallet.get_receiving_addresses()[0]) self.assertEqual(2, len(wallet.get_receiving_addresses())) # also test addr deletion wallet.delete_address('Xdt8NqE5wSX9ytfP958t4tKdXoZDo6Bm6T') self.assertEqual(1, len(wallet.get_receiving_addresses()))
def test_restore_wallet_from_text_xpub(self): text = 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ' d = restore_wallet_from_text(text, path=self.wallet_path, gap_limit=1, config=self.config) wallet = d['wallet'] # type: Standard_Wallet self.assertEqual(text, wallet.keystore.get_master_public_key()) self.assertEqual('XmQ3Tn67Fgs7bwNXthtiEnBFh7ZeDG3aw2', wallet.get_receiving_addresses()[0])
def test_restore_wallet_from_text_xprv(self): text = 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs' d = restore_wallet_from_text(text, path=self.wallet_path, gap_limit=1, config=self.config) wallet = d['wallet'] # type: Standard_Wallet self.assertEqual(text, wallet.keystore.get_master_private_key(password=None)) self.assertEqual('XmQ3Tn67Fgs7bwNXthtiEnBFh7ZeDG3aw2', wallet.get_receiving_addresses()[0])
def test_encrypt_decrypt(self, mock_save_db): wallet = restore_wallet_from_text( 'p2pkh:XJvTzLoBy3jPMZFSTzK6KqTiNR3n5xbreSScEy7u9C8fEf1GZG3X', path='if_this_exists_mocking_failed_648151893', config=self.config)['wallet'] cmds = Commands(config=self.config) cleartext = "asdasd this is the message" pubkey = "021f110909ded653828a254515b58498a6bafc96799fb0851554463ed44ca7d9da" ciphertext = cmds._run('encrypt', (pubkey, cleartext)) self.assertEqual( cleartext, cmds._run('decrypt', (pubkey, ciphertext), wallet=wallet))
def test_restore_wallet_from_text_privkeys(self): text = 'p2pkh:XGx8LpkmLRv9RiMvpYx965BCaQKQbeMVVqgAh7B5SQVdosQiKJ4i p2pkh:XEn9o6oayjsRmoEQwDbvkrWVvjRNqPj3xNskJJPAKraJTrWuutwd' d = restore_wallet_from_text(text, path=self.wallet_path, config=self.config) wallet = d['wallet'] # type: Imported_Wallet addr0 = wallet.get_receiving_addresses()[0] self.assertEqual('Xci5KnMVkHrqBQk9cU4jwmzJfgaTPopHbz', addr0) self.assertEqual('p2pkh:XEn9o6oayjsRmoEQwDbvkrWVvjRNqPj3xNskJJPAKraJTrWuutwd', wallet.export_private_key(addr0, password=None)) self.assertEqual(2, len(wallet.get_receiving_addresses())) # also test addr deletion wallet.delete_address('XmQ3Tn67Fgs7bwNXthtiEnBFh7ZeDG3aw2') self.assertEqual(1, len(wallet.get_receiving_addresses()))
def test_restore_wallet_from_text_xkey_that_is_also_a_valid_electrum_seed_by_chance( self): text = 'xprv9s21ZrQH143K39BUkM2iuppjFpkJ37KUqQAaDvvTzeCSDCtpqM1TsN47vmuUqyJqEVJUmFBDo55dsKwhzYD6sPTecP2JeNbYoia7hzf6Jzt' d = restore_wallet_from_text(text, path=self.wallet_path, gap_limit=1, config=self.config) wallet = d['wallet'] # type: Standard_Wallet self.assertEqual(text, wallet.keystore.get_master_private_key(password=None)) self.assertEqual('Xj1vN7ntKrNp87tBjXCSGFXPtAVgJA525U', wallet.get_receiving_addresses()[0])
def test_getprivatekeyforpath(self, mock_save_db): wallet = restore_wallet_from_text( 'hint shock chair puzzle shock traffic drastic note dinosaur mention suggest sweet', gap_limit=2, path='if_this_exists_mocking_failed_648151893', config=self.config)['wallet'] cmds = Commands(config=self.config) self.assertEqual( "p2pkh:cRVRdGfHrP9zb3cNTT1HGoG9JPcZfvjBMqUa2vTDMGDnKG1dNu24", cmds._run('getprivatekeyforpath', ([0, 10000], ), wallet=wallet)) self.assertEqual( "p2pkh:cRVRdGfHrP9zb3cNTT1HGoG9JPcZfvjBMqUa2vTDMGDnKG1dNu24", cmds._run('getprivatekeyforpath', ("m/0/10000", ), wallet=wallet)) self.assertEqual( "p2pkh:cS2exaULytoQ9CR89QHJDMg82NWKZ6f8rFboU7LGbHhdUMXxpPcd", cmds._run('getprivatekeyforpath', ("m/5h/100000/88h/7", ), wallet=wallet))
def test_restore_wallet_from_text_mnemonic(self): text = 'hint shock chair puzzle shock traffic drastic note dinosaur mention suggest sweet' passphrase = 'mypassphrase' password = '******' encrypt_file = True d = restore_wallet_from_text(text, path=self.wallet_path, passphrase=passphrase, password=password, encrypt_file=encrypt_file, gap_limit=1, config=self.config) wallet = d['wallet'] # type: Standard_Wallet self.assertEqual(passphrase, wallet.keystore.get_passphrase(password)) self.assertEqual(text, wallet.keystore.get_seed(password)) self.assertEqual(encrypt_file, wallet.storage.is_encrypted()) self.assertEqual('XdDHzW6aTeuQsraNXeEsPy5gAv1nUz7Y7Q', wallet.get_receiving_addresses()[0])