From 770656dc746abd13f4526ffd300c218faf96261c Mon Sep 17 00:00:00 2001 From: csyhuang Date: Mon, 5 Oct 2020 13:23:40 -0500 Subject: [PATCH 1/3] add generate function in PrivateKey --- btcpy/structs/crypto.py | 12 ++++++++++++ tests/unit.py | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/btcpy/structs/crypto.py b/btcpy/structs/crypto.py index a7aa5b6..c277bb5 100644 --- a/btcpy/structs/crypto.py +++ b/btcpy/structs/crypto.py @@ -9,6 +9,7 @@ # propagated, or distributed except according to the terms contained in the # LICENSE.md file. +import secrets from binascii import unhexlify from ..lib.base58 import b58decode_check, b58encode_check from hashlib import sha256 @@ -61,6 +62,17 @@ def from_wif(wif, strict=None): def unhexlify(hexa): return PrivateKey(bytearray(unhexlify(hexa))) + @staticmethod + def generate(nbytes=32): + """ + Generate a random text string, in hexadecimal with nbytes random bytes. Its length is thus 2 * nbytes. + Args: + nbytes(int): number of random bytes in the random hexadecimal string generated. + Returns: + A random hexadecimal string of length 2 * nbytes. + """ + return secrets.token_hex(nbytes) + def __init__(self, priv, public_compressed=True): self.key = priv self.public_compressed = public_compressed diff --git a/tests/unit.py b/tests/unit.py index 89f5331..7f95f92 100644 --- a/tests/unit.py +++ b/tests/unit.py @@ -418,6 +418,10 @@ def test_to_wif(self): priv.public_compressed = False self.assertEqual(priv.to_wif(w['mainnet']), w['wif']) + def test_private_key_generation(self): + key = PrivateKey.generate(32) + self.assertEqual(len(key), 64) + class TestPubkey(unittest.TestCase): From 39950efdb3d7cb32ea5e5d495fdcdfed56335f6d Mon Sep 17 00:00:00 2001 From: csyhuang Date: Wed, 7 Oct 2020 13:31:51 -0500 Subject: [PATCH 2/3] Return a PrivateKey object in the class method --- btcpy/structs/crypto.py | 4 ++-- tests/unit.py | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/btcpy/structs/crypto.py b/btcpy/structs/crypto.py index c277bb5..3478da3 100644 --- a/btcpy/structs/crypto.py +++ b/btcpy/structs/crypto.py @@ -63,7 +63,7 @@ def unhexlify(hexa): return PrivateKey(bytearray(unhexlify(hexa))) @staticmethod - def generate(nbytes=32): + def generate(): """ Generate a random text string, in hexadecimal with nbytes random bytes. Its length is thus 2 * nbytes. Args: @@ -71,7 +71,7 @@ def generate(nbytes=32): Returns: A random hexadecimal string of length 2 * nbytes. """ - return secrets.token_hex(nbytes) + return PrivateKey(bytearray(secrets.token_hex(nbytes=32), encoding='ascii')) def __init__(self, priv, public_compressed=True): self.key = priv diff --git a/tests/unit.py b/tests/unit.py index 7f95f92..07c888d 100644 --- a/tests/unit.py +++ b/tests/unit.py @@ -419,8 +419,10 @@ def test_to_wif(self): self.assertEqual(priv.to_wif(w['mainnet']), w['wif']) def test_private_key_generation(self): - key = PrivateKey.generate(32) - self.assertEqual(len(key), 64) + with patch('secrets.token_hex', return_value='1697da9ed39e670abb6981dcfe9ea96db2f06e7d6d18db2474ffb2ffadd1e3bb'): + priv = PrivateKey.generate() + self.assertEqual(len(priv.key), 64) + self.assertEqual(priv.key, bytearray(b'1697da9ed39e670abb6981dcfe9ea96db2f06e7d6d18db2474ffb2ffadd1e3bb')) class TestPubkey(unittest.TestCase): From 09f8abe71a604c2f4fd3b0eed322e08850840f0c Mon Sep 17 00:00:00 2001 From: csyhuang Date: Wed, 7 Oct 2020 13:33:58 -0500 Subject: [PATCH 3/3] correct docstring --- btcpy/structs/crypto.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/btcpy/structs/crypto.py b/btcpy/structs/crypto.py index 3478da3..62be3d1 100644 --- a/btcpy/structs/crypto.py +++ b/btcpy/structs/crypto.py @@ -65,11 +65,7 @@ def unhexlify(hexa): @staticmethod def generate(): """ - Generate a random text string, in hexadecimal with nbytes random bytes. Its length is thus 2 * nbytes. - Args: - nbytes(int): number of random bytes in the random hexadecimal string generated. - Returns: - A random hexadecimal string of length 2 * nbytes. + Generate a random text string, in hexadecimal with 32 random bytes. """ return PrivateKey(bytearray(secrets.token_hex(nbytes=32), encoding='ascii'))