Ejemplo n.º 1
0
def test_create_private_key():
    wk = create_private_key()
    k = wif_to_private_key(wk)
    assert private_key_to_wif(k) == wk
    wk = create_private_key(hex=True)
    assert len(wk) == 64
    wk = create_private_key(hex=False, wif=False)
    assert len(wk) == 32
Ejemplo n.º 2
0
def test_wif_to_private_key():
    wk = create_private_key(testnet=True)
    k = wif_to_private_key(wk)
    assert private_key_to_wif(k, testnet=True) == wk

    wk = create_private_key(compressed=False)
    k = wif_to_private_key(wk)
    assert private_key_to_wif(k, compressed=False) == wk

    wk = create_private_key(compressed=False, testnet=False)
    k = wif_to_private_key(wk)
    assert private_key_to_wif(k, compressed=False, testnet=False) == wk

    wk = create_private_key(compressed=False, testnet=True)
    k = wif_to_private_key(wk)
    assert private_key_to_wif(k, compressed=False, testnet=True) == wk

    with pytest.raises(TypeError):
        wif_to_private_key(
            "L49obCXV7fGz2YRzLCSJgeZBYmGeBbKPT7xiehUeYX2S4URkPFqX")
Ejemplo n.º 3
0
def create_private_key(compressed=True, testnet=False, wif=True, hex=False):
    """
    Create private key

    :param compressed: (optional) Type of public key, by default set to compressed.
                                 Using uncompressed public keys is deprecated in new SEGWIT addresses,
                                 use this option only for backward compatibility.
    :param testnet: (optional) flag for testnet network, by default is False.
    :param wif:  (optional) If set to True return key in WIF format, by default is True.
    :param hex:  (optional) If set to True return key in HEX format, by default is False.
    :return: Private key in wif format (default), hex encoded byte string in case of hex flag or
             raw bytes string in case wif and hex flags set to False.

    """
    return __parent__.create_private_key(compressed=compressed, testnet=testnet, wif=wif, hex=hex)
Ejemplo n.º 4
0
    def __init__(self, key=None, compressed=True, testnet=False):

        if key is None:

            #: flag for compressed type of corresponding public key (boolean)
            self.compressed = compressed
            #: flag for testnet network private key  (boolean)
            self.testnet = testnet

            #: private key in  bytes (bytes)
            self.key = create_private_key(wif=False)
            #: private key in HEX (string)
            self.hex = self.key.hex()
            #: private key in WIF format (string)
            self.wif = private_key_to_wif(self.key, compressed, testnet)

        else:
            if isinstance(key, str):
                try:
                    key = bytes_from_hex(key)
                except:
                    pass
            if isinstance(key, bytes):
                if len(key) != 32:
                    raise TypeError("private key invalid length")
                self.key = key
                self.compressed = compressed
                self.testnet = testnet
                self.hex = self.key.hex()
                self.wif = private_key_to_wif(self.key, compressed, testnet)
                return
            if not isinstance(key, str) or not is_wif_valid(key):
                raise TypeError("private key invalid")

            self.key = wif_to_private_key(key, hex=False)
            self.hex = self.key.hex()
            if key[0] in (MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX,
                          TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX):
                self.compressed = False
            else:
                self.compressed = True
            if key[0] in (TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX,
                          TESTNET_PRIVATE_KEY_COMPRESSED_PREFIX):
                self.testnet = True
            else:
                self.testnet = False
            self.wif = private_key_to_wif(self.key, self.compressed,
                                          self.testnet)