Skip to content

Commit

Permalink
Add commandline wallet unittest to send and empty wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Jan 21, 2024
1 parent aaa51fc commit b1c80a2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
6 changes: 2 additions & 4 deletions bitcoinlib/tools/clw.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def create_wallet(wallet_name, args, db_uri, output_to):
else:
passphrase = args.passphrase
if passphrase is None:
passphrase = get_passphrase(args.passphrase_strength, args.yes)
passphrase = get_passphrase(args.passphrase_strength, args.yes, args.quiet)
if len(passphrase.split(' ')) < 3:
raise WalletError("Please specify passphrase with 3 words or more. However less than 12 words is insecure!")
hdkey = HDKey.from_passphrase(passphrase, network=args.network)
Expand Down Expand Up @@ -322,7 +322,7 @@ def main():
print("Failed to import key", file=output_to)

elif args.wallet_empty:
wallet_empty(args.wallet_name)
wallet_empty(args.wallet_name, args.database)
print("Removed transactions and emptied wallet. Use --update-wallet option to update again.",
file=output_to)
elif args.update_utxos:
Expand Down Expand Up @@ -439,8 +439,6 @@ def main():
print("Wallet info for %s" % wlt.name, file=output_to)
if not args.quiet:
wlt.info()
else:
pprint(wlt.as_dict())


if __name__ == '__main__':
Expand Down
74 changes: 64 additions & 10 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# Unit Tests for Bitcoinlib Tools
# © 2018 May - 1200 Web Development <http://1200wd.com/>
# © 2018 - 2024 January - 1200 Web Development <http://1200wd.com/>
#

import os
Expand Down Expand Up @@ -56,22 +56,24 @@ def init_mysql(_):
con.close()


db_uris = (('sqlite:///' + SQLITE_DATABASE_FILE, init_sqlite),)
if UNITTESTS_FULL_DATABASE_TEST:
db_uris += (
# ('mysql://root@localhost:3306/' + DATABASE_NAME, init_mysql),
('postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME, init_postgresql),
)
# db_uris = (('sqlite:///' + SQLITE_DATABASE_FILE, init_sqlite),)
# if UNITTESTS_FULL_DATABASE_TEST:
# db_uris += (
# ('mysql://root@localhost:3306/' + DATABASE_NAME, init_mysql),
# ('postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME, init_postgresql),
# )


@parameterized_class(('DATABASE_URI', 'init_fn'), db_uris)
# @parameterized_class(('DATABASE_URI', 'init_fn'), db_uris)
class TestToolsCommandLineWallet(unittest.TestCase):

def setUp(self):
self.init_fn()
# self.init_fn()
init_sqlite(self)
self.python_executable = sys.executable
self.clw_executable = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),
'../bitcoinlib/tools/clw.py'))
self.DATABASE_URI = SQLITE_DATABASE_FILE

def test_tools_clw_create_wallet(self):
cmd_wlt_create = '%s %s new -w test --passphrase "emotion camp sponsor curious bacon squeeze bean world ' \
Expand Down Expand Up @@ -143,7 +145,7 @@ def test_tools_clw_transaction_with_script(self):
(self.python_executable, self.clw_executable, self.DATABASE_URI)
cmd_wlt_update = "%s %s -w test2 -x -d %s" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI)
cmd_wlt_transaction = "%s %s -w test2 -d %s -s 21HVXMEdxdgjNzgfERhPwX4okXZ8WijHkvu 50000000 -f 100000 -p" % \
cmd_wlt_transaction = "%s %s -w test2 -d %s -s 21HVXMEdxdgjNzgfERhPwX4okXZ8WijHkvu 0.5 -f 100000 -p" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI)
cmd_wlt_delete = "%s %s -w test2 --wallet-remove -d %s" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI)
Expand Down Expand Up @@ -205,6 +207,58 @@ def test_tools_clw_create_multisig_wallet_p2sh_segwit(self):
poutput = process.communicate(input=b'testms-p2sh-segwit')
self.assertIn(output_wlt_delete, normalize_string(poutput[0]))

def test_tools_generate_key_quiet(self):
cmd_generate_passphrase = "%s %s -gq --passphrase-strength 256" % \
(self.python_executable, self.clw_executable)
process = Popen(cmd_generate_passphrase, stdin=PIPE, stdout=PIPE, shell=True)
poutput = process.communicate()[0]
self.assertEqual(len(poutput.split(b' ')), 24)

def test_tools_wallet_create_from_key(self):
phrase = ("hover rescue clock ocean strategy post melt banner anxiety phone pink paper enhance more "
"copy gate bag brass raise logic stone duck muffin conduct")
cmd_wlt_create = "%s %s new -w wlt_from_key -c \"%s\" -d %s -y" % \
(self.python_executable, self.clw_executable, phrase, self.DATABASE_URI)
output_wlt_create = "bc1qpylcrcyqa5wkwe2stzc6h7q0mhs5skxuas44w2"

poutput = Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate()
self.assertIn(output_wlt_create, normalize_string(poutput[0]))

def test_tools_wallet_send_to_multi(self):
send_str = ("-s blt1qzt90vqqjsqspuaegu9fh4e2htaxrgt0l76d9gz 0.1 "
"-s blt1qu825hm0a6ajg66j79x4tzkn56qmljjms97c5tp 1")
cmd_wlt_create = "%s %s new -w wallet_send_to_multi -d %s -n bitcoinlib_test -yq" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI)
cmd_wlt_update = "%s %s -w wallet_send_to_multi -d %s -x" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI)
cmd_wlt_send = "%s %s -w wallet_send_to_multi -d %s %s" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI, send_str)

Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate()
Popen(cmd_wlt_update, stdin=PIPE, stdout=PIPE, shell=True).communicate()
process = Popen(cmd_wlt_send, stdin=PIPE, stdout=PIPE, shell=True)
self.assertIn(b"Transaction created", process.communicate()[0])

def test_tools_wallet_empty(self):
pk = ("zprvAWgYBBk7JR8GiejuVoZaVXtWf5zNawFbTH88uKao9qnZxBypJQNvh1tGHZghpfjUfSUiS7G7MmNw3cyakkNcNis3MjD4ic54n"
"FY5LQxMszQ")
cmd_wlt_create = "%s %s new -w wlt_create_and_empty -c %s -d %s -y" % \
(self.python_executable, self.clw_executable, pk, self.DATABASE_URI)
output_wlt_create = "bc1qqnqkjpnmr5zsxar76wxqcntp28ltly0fz6crdg"
poutput = Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate()
self.assertIn(output_wlt_create, normalize_string(poutput[0]))

cmd_wlt_empty = "%s %s -w wlt_create_and_empty -d %s --wallet-empty" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI)
poutput = Popen(cmd_wlt_empty, stdin=PIPE, stdout=PIPE, shell=True).communicate()
self.assertIn("Removed transactions and emptied wallet", normalize_string(poutput[0]))

cmd_wlt_info = "%s %s -w wlt_create_and_empty -d %s -i" % \
(self.python_executable, self.clw_executable, self.DATABASE_URI)
poutput = Popen(cmd_wlt_info, stdin=PIPE, stdout=PIPE, shell=True).communicate()
self.assertIn("- - Transactions Account 0 (0)", normalize_string(poutput[0]))
self.assertNotIn(output_wlt_create, normalize_string(poutput[0]))


if __name__ == '__main__':
unittest.main()

0 comments on commit b1c80a2

Please sign in to comment.