wallet.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:bitcoin_tools 作者: sr-gi 项目源码 文件源码
def sk_to_wif(sk, compressed=True, mode='image', v='test'):
    """ Generates a Wallet Import Format (WIF) representation of a provided elliptic curve private key.

    :param sk: elliptic curve private key.
    :type sk: hex str
    :param compressed: Whether the WIF will be used with a compressed or uncompressed public key
    :type compressed: bool
    :param mode: defines the type of return.
    :type mode: str
    :param v: version (prefix) used to calculate the WIF, it depends on the type of network.
    :type v: str
    :return: The WIF representation of the private key.

        - main network WIF if v is 'main'.
        - testnet WIF otherwise.
    :rtype:

        - qrcode is mode is 'image'.
        - str otherwise.
    """

    # Choose the proper version depending on the provided 'v'.
    if v in ['mainnet', 'main']:
        v = WIF
    elif v in ['testnet', 'test']:
        v = TESTNET_WIF
    else:
        raise Exception("Invalid version, use either 'main' or 'test'.")

    # Add the network version leading the private key (in hex).
    e_pkey = chr(v) + unhexlify(sk)
    # Flag compressed pk when needed
    if compressed:
        e_pkey += unhexlify('01')
    # Double sha256.
    h = sha256(sha256(e_pkey).digest()).digest()
    # Add the two first bytes of the result as a checksum tailing the encoded key.
    wif = e_pkey + h[0:4]
    # Enconde the result in base58.
    wif = b58encode(wif)

    # Choose the proper return mode depending on 'mode'.
    if mode is 'image':
        response = qr_make(wif)
    elif mode is 'text':
        response = wif
    else:
        raise Exception("Invalid mode, used either 'image' or 'text'.")

    return response
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号