Skip to content
This repository has been archived by the owner on Mar 11, 2019. It is now read-only.

Commit

Permalink
few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthchirp committed Oct 13, 2017
1 parent b60f4e1 commit 170587f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
28 changes: 23 additions & 5 deletions coldwallet/command_line.py
Expand Up @@ -10,11 +10,21 @@

def main():
parser = argparse.ArgumentParser()
parser.add_argument("--disable-randomness", help=argparse.SUPPRESS, action="store_true")
parser.add_argument("--version", help="show version and exit", action="store_true")
parser.add_argument("--addresses", type=int, default=4, help="generate this many bitcoin addresses")
parser.add_argument("--addresses", type=int, default=10, help="generate this many bitcoin addresses")
parser.add_argument("--codes", type=int, default=8, help="the number of code blocks used for the cold wallet")

# advanced parameters:

# Disable all random number generation, making coldwallet deterministic.
# Never use this outside of testing.
parser.add_argument("--disable-randomness", help=argparse.SUPPRESS, action="store_true")

# Use this power of two as the N parameter of the scrypt hashing algorithm.
# The scrypt default is 2^14. This increases the memory requirements for
# de- and encryption of the private keys.
parser.add_argument("--scrypt-N", type=int, default=14, help=argparse.SUPPRESS)

args = parser.parse_args()

if args.disable_randomness:
Expand All @@ -25,7 +35,7 @@ def main():
sys.exit(0)

if args.codes < 2:
print("The minimum number of code blocks is 2. The recommended minimum is 8.")
sys.stderr.write("The minimum number of code blocks is 2. The recommended minimum is 8.\n")
sys.exit(1)

# Step 1. Create a coldwallet key. This key is encoded in human-readable form
Expand All @@ -47,10 +57,18 @@ def main():

# Step 2. Generate Bitcoin addresses
print("Generating Bitcoin addresses:")
bitcoin_addresses = {}
for n in range(args.addresses):
private_key = coldwallet.crypto.generate_random_string(bits=256)
public_address = coldwallet.bitcoin.generate_public_address(private_key)

print(" %s" % public_address)
key_code = coldwallet.crypto.encrypt_secret_key(private_key, coldkey, public_address)
print(" :%s" % key_code)
key_code = coldwallet.crypto.encrypt_secret_key(private_key, coldkey, public_address, scrypt_N=args.scrypt_N)

verify_key = coldwallet.crypto.decrypt_secret_key(key_code, coldkey, public_address, scrypt_N=args.scrypt_N)
assert private_key == verify_key, "key validation error"

bitcoin_addresses[public_address] = key_code

from pprint import pprint
pprint(bitcoin_addresses)
35 changes: 35 additions & 0 deletions tests/test_command_line.py
Expand Up @@ -26,3 +26,38 @@ def test_reject_invalid_parameters(capsys):
assert exc_info.value.code == 2
out, err = capsys.readouterr()
assert 'unrecognized' in err and 'bork' in err

def test_show_version(capsys):
import coldwallet.command_line
sys.argv = [ 'coldwallet', '--version' ]

with pytest.raises(SystemExit) as exc_info:
coldwallet.command_line.main()

assert exc_info.value.code == 0
out, err = capsys.readouterr()
assert err == ''
assert coldwallet.__version__ in out

def test_reject_too_few_codes(capsys):
import coldwallet.command_line
sys.argv = [ 'coldwallet', '--codes', '1' ]

with pytest.raises(SystemExit) as exc_info:
coldwallet.command_line.main()

assert exc_info.value.code == 1
out, err = capsys.readouterr()
assert 'minimum number of code blocks' in err
assert out == ''

def test_create_example_keys(capsys, tmpdir):
import coldwallet.command_line
sys.argv = [ 'coldwallet', '--disable-randomness', '--scrypt-N', '4' ]

coldwallet.command_line.main()

out, err = capsys.readouterr()
err = err.split('\n')
assert len(err) == 4
assert 'random number generation disabled' in err[1]

0 comments on commit 170587f

Please sign in to comment.