Skip to content

Commit

Permalink
Add zero padding to derived privkeys.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhtitor committed Jan 10, 2019
1 parent 9f53940 commit 4db3d15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions graphenebase/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ def derive_from_seed(self, offset):
order = ecdsa.SECP256k1.order
secexp = (seed + z) % order
secret = "%0x" % secexp
if len(secret) < 64: # left-pad with zeroes
secret = ("0" * (64-len(secret))) + secret
return PrivateKey(secret, prefix=self.pubkey.prefix)

def __format__(self, _format):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,16 @@ def test_derive_private_key(self):
repr(p2), "2dc7cb99933132e25b37710f9ea806228b04a583da11a137ef97fd42c0007390"
)

def test_derive_child(self):
# NOTE: this key + offset pair is particularly nasty, as
# the resulting derived value is less then 64 bytes long.
# Thus, this test also tests for proper padding.
p = PrivateKey("5K6hMUtQB2xwjuz3SRR6uM5HNERWgBqcK7gPPZ31XtAyBNoATZd")
p2 = p.child(b'\xaf\x8f: \xf6T?V\x0bM\xd8\x16 \xfd\xde\xe9\xb9\xac\x03\r\xba\xb2\x8d\x868-\xc2\x90\x80\xe8\x1b\xce')
self.assertEqual(
repr(p2), "0c5fae344a513a4cfab312b24c08df2b2d6afa25c0ead0d3d1d0d3e76794109b"
)

def test_init_wrong_format(self):
with self.assertRaises(NotImplementedError):
PrivateKey("KJWcdkhL3w4RkVPcZMdJsjos22yB5cSkPExerktvKnRNZR5gx1S")
Expand Down

0 comments on commit 4db3d15

Please sign in to comment.