Skip to content

Commit

Permalink
Fix richardkiss#83. Key.is_private -> Key.is_private() where appropri…
Browse files Browse the repository at this point in the history
…ate.

[gitreformat yapf-ify (github/ghtdak) on Fri Jul 15 12:51:43 2016]
[from commit: 8044714]
  • Loading branch information
posita committed Feb 25, 2015
1 parent 6656a72 commit e451618
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pycoin/key/Key.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,6 @@ def _use_uncompressed(self, use_uncompressed=None):

def __repr__(self):
r = self.public_copy().as_text()
if self.is_private:
if self.is_private():
return "private_for <%s>" % r
return "<%s>" % r
35 changes: 18 additions & 17 deletions pycoin/scripts/genwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,20 @@ def main():
else:
child_index = "%d" % wallet.child_index()
if args.json:
d = dict(wallet_key=wallet.wallet_key(as_private=wallet.is_private),
public_pair_x=wallet.public_pair[0],
public_pair_y=wallet.public_pair[1],
tree_depth=wallet.depth,
fingerprint=b2h(wallet.fingerprint()),
parent_fingerprint=b2h(wallet.parent_fingerprint),
child_index=child_index,
chain_code=b2h(wallet.chain_code),
bitcoin_addr=wallet.bitcoin_address(),
bitcoin_addr_uncompressed=wallet.bitcoin_address(
compressed=False),
network="test" if wallet.is_test else "main",)
if wallet.is_private:
d = dict(
wallet_key=wallet.wallet_key(as_private=wallet.is_private()),
public_pair_x=wallet.public_pair[0],
public_pair_y=wallet.public_pair[1],
tree_depth=wallet.depth,
fingerprint=b2h(wallet.fingerprint()),
parent_fingerprint=b2h(wallet.parent_fingerprint),
child_index=child_index,
chain_code=b2h(wallet.chain_code),
bitcoin_addr=wallet.bitcoin_address(),
bitcoin_addr_uncompressed=wallet.bitcoin_address(
compressed=False),
network="test" if wallet.is_test else "main",)
if wallet.is_private():
d.update(dict(key="private",
secret_exponent=wallet.secret_exponent,
WIF=wallet.wif(),
Expand All @@ -128,9 +129,9 @@ def main():
d.update(dict(key="public"))
print(json.dumps(d, indent=3))
elif args.info:
print(wallet.wallet_key(as_private=wallet.is_private))
print(wallet.wallet_key(as_private=wallet.is_private()))
print(full_network_name_for_netcode(wallet.netcode))
if wallet.is_private:
if wallet.is_private():
print("private key")
print("secret exponent: %d" % wallet.secret_exponent)
else:
Expand All @@ -142,7 +143,7 @@ def main():
print("parent f'print: %s" % b2h(wallet.parent_fingerprint))
print("child index: %s" % child_index)
print("chain code: %s" % b2h(wallet.chain_code))
if wallet.is_private:
if wallet.is_private():
print("WIF: %s" % wallet.wif())
print(" uncompressed: %s" % wallet.wif(compressed=False))
print("Bitcoin address: %s" % wallet.bitcoin_address())
Expand All @@ -153,7 +154,7 @@ def main():
elif args.wif:
print(wallet.wif(compressed=not args.uncompressed))
else:
print(wallet.wallet_key(as_private=wallet.is_private))
print(wallet.wallet_key(as_private=wallet.is_private()))
except PublicPrivateMismatchError as ex:
print(ex.args[0])

Expand Down
17 changes: 17 additions & 0 deletions tests/bip32_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ def test_public_subkey(self):
self.assertEqual(None,
uag.subkey(i=0, as_private=False).secret_exponent())

def test_repr(self):
from pycoin.key import Key
netcode = 'XTN'
key = Key(secret_exponent=273, netcode=netcode)
wallet = BIP32Node.from_master_secret(
bytes(key.wif().encode('ascii')), netcode)

address = wallet.address()
pub_k = wallet.from_text(address)
self.assertEqual(repr(pub_k), '<myb5gZNXePNf2E2ksrjnHRFCwyuvt7oEay>')

wif = wallet.wif()
priv_k = wallet.from_text(wif)
self.assertEqual(
repr(priv_k),
'private_for <03ad094b1dc9fdce5d3648ca359b4e210a89d049532fdd39d9ccdd8ca393ac82f4>')


if __name__ == '__main__':
unittest.main()
20 changes: 20 additions & 0 deletions tests/key_validate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import unittest

# This is necessary for the import Wallet statement below to work?
import tests.bip32_test

from pycoin.block import Block
from pycoin.encoding import hash160_sec_to_bitcoin_address
from pycoin.key import Key
Expand Down Expand Up @@ -123,3 +126,20 @@ def test_is_public_private_bip32_valid(self):
self.assertEqual(
is_public_bip32_valid(a, allowable_netcodes=NETWORK_NAMES),
None)

def test_repr(self):
key = Key(secret_exponent=273, netcode='XTN')

address = key.address()
pub_k = Key.from_text(address)
self.assertEqual(repr(pub_k), '<mhDVBkZBWLtJkpbszdjZRkH1o5RZxMwxca>')

wif = key.wif()
priv_k = Key.from_text(wif)
self.assertEqual(
repr(priv_k),
'private_for <0264e1b1969f9102977691a40431b0b672055dcf31163897d996434420e6c95dc9>')


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

0 comments on commit e451618

Please sign in to comment.