コード例 #1
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)
コード例 #2
0
def private_from_xprivate_key(xprivate_key, wif=True, hex=False):
    """
    Get private key from extended private key

    :param bytes xprivate_key: extended public in base58, HEX or bytes string format.
    :param boolean wif: (optional) return result as WIF format, by default True.
    :param boolean hex: (optional) return result as HEX encoded string, by default False.
                        In case True WIF flag value will be ignored.
    :return: private key  in HEX or bytes string format.
    """
    if isinstance(xprivate_key, str):
        if len(xprivate_key) == 156:
            xprivate_key = bytes.fromhex(xprivate_key)
        else:
            xprivate_key = decode_base58_with_checksum(xprivate_key)
    if not isinstance(xprivate_key, bytes):
        raise TypeError("xprivate_key should be HEX, Base58 or bytes string")
    if xprivate_key[:4] not in [
            MAINNET_XPRIVATE_KEY_PREFIX, TESTNET_XPRIVATE_KEY_PREFIX
    ]:
        raise ValueError("invalid extended private key")

    if hex:
        return xprivate_key[46:].hex()
    elif wif:
        if xprivate_key[:4] == MAINNET_XPRIVATE_KEY_PREFIX:
            testnet = False
        else:
            testnet = True
        return private_key_to_wif(xprivate_key[46:], testnet=testnet)
    return xprivate_key[46:].hex() if hex else xprivate_key[46:]
コード例 #3
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
コード例 #4
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")
コード例 #5
0
def private_key_to_wif(h, compressed=True, testnet=False):
    """
    Encode private key in HEX or RAW bytes format to WIF format.

    :param h: private key 32 byte string or HEX encoded string.
    :param compressed: (optional) flag of public key compressed format, by default set to True.
    :param testnet: (optional) flag for testnet network, by default is False.
    :return: Private key in WIF format.
    """
    # uncompressed: 0x80 + [32-byte secret] + [4 bytes of Hash() of previous 33 bytes], base58 encoded.
    # compressed: 0x80 + [32-byte secret] + 0x01 + [4 bytes of Hash() previous 34 bytes], base58 encoded.
    return __parent__.private_key_to_wif(h, compressed=compressed, testnet=testnet)
コード例 #6
0
def test_private_key_to_wif():
    assert private_key_to_wif("ceda1ae4286015d45ec5147fe3f63e9377ccd6d4e98bcf0847df9937da1944a4") == \
           "L49obCXV7fGz2YRzLCSJgeZBYmGeBbKPT7xiehUeYX2S4URkPFZX"
    assert private_key_to_wif(bytes_from_hex("ceda1ae4286015d45ec5147fe3f63e9377ccd6d4e98bcf0847df9937da1944a4")) == \
           "L49obCXV7fGz2YRzLCSJgeZBYmGeBbKPT7xiehUeYX2S4URkPFZX"
    with pytest.raises(TypeError):
        private_key_to_wif(
            "ceda1ae4286015d45ec5147fe3f63e9377ccd6d4e98bcf0847df9937da1944")
    assert private_key_to_wif("ceda1ae4286015d45ec5147fe3f63e9377ccd6d4e98bcf0847df9937da1944a4",
                              testnet=False,
                              compressed=True) == \
           "L49obCXV7fGz2YRzLCSJgeZBYmGeBbKPT7xiehUeYX2S4URkPFZX"
    assert private_key_to_wif("ceda1ae4286015d45ec5147fe3f63e9377ccd6d4e98bcf0847df9937da1944a4",
                              testnet=False,
                              compressed=False) == \
           "5KPPLXhtga99qqMceRo4Z6LXV3Kx6a9hRx3ez2U7EwP5KZfy2Wf"
    assert private_key_to_wif("ceda1ae4286015d45ec5147fe3f63e9377ccd6d4e98bcf0847df9937da1944a4",
                              testnet=True,
                              compressed=True) == \
           "cUWo47XLYiyFByuFicFS3y4FAza3r3R5XA7Bm7wA3dgSKDYox7h6"
    assert private_key_to_wif("ceda1ae4286015d45ec5147fe3f63e9377ccd6d4e98bcf0847df9937da1944a4",
                              testnet=True,
                              compressed=False) == \
           "93A1vGXSGoDHotruGmgyRgtV8hgfFjgtmtuc4epcag886W9d44L"