The road to blockchain system exploration: Private key compression and WIF format details

In the previous chapters, we introduced the compression of the public key in detail. In the Bitcoin network, a private key can correspond to two addresses. One address is the address generated by the uncompressed public key, and the other is the address generated by the compressed public key. The created address, the conversion algorithm from the public key to the blockchain address, we will give a detailed description and code implementation here. In this section, we will look at the compression of the private key and the related WIF data format.

The funny thing is that after the private key is "compressed", its length is increased by one byte compared to before compression. The "compression" method is also quite simple, which is to add a byte 01 at the end of the private key. For example, if the data of the private key is: 1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD, then the
corresponding "compression" format is:
1E99423A4ED27608A15A2616A2 B0E9E52CED330AC530EDCC32C8FFC6A526AEDD01
we can see by comparison, "compression "The private key after " is the byte 01 added at the end. Why "compress" the private key. We mentioned earlier that there are two storage methods for public keys, compressed format and uncompressed format. Whether the private key is "compressed" corresponds to which public key storage mode is created. If the private key is "compressed", it means that it is used to create the public key in compressed format, or if "compressed" is not available, use it to create the public key in uncompressed format.

The compression of the public key is for transmission on the network. Usually, the private key does not need to be frequently transmitted on the network, because exposing the private key too much to the network will increase the chance of leakage. Once the private key is leaked, all your assets or Private information will be lost. However, in some cases, the private key also needs to be transmitted. For example, when the private key is sent from one blockchain client to another client for import, the private key needs to be transmitted over the network, so we have The need to encode it, so the encoding corresponding to the private key is called WIF for short.

Let's look at the basic steps of the WIF encoding format:
1. If the private key corresponds to the Bitcoin main network, then add a byte 0x80 at the beginning of the private key, and add a byte 0xef.
2 if it corresponds to the test network. Big-endian format storage
3, if the private key is to be used to create a public key in compressed format, then add 1 byte 0x01 at the end of step 2
4, perform sha256 hash on step 3, and then remove the first 4 characters of the result Section
5. Connect the results of steps 3 and 4 end to end, and then do base58 encoding.
Let's use the code to realize it:

privKey = 0x038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b14776
pubKey = privKey * G

class PrivateKey:
    def __init__(self, secret):
        self.secret = secret

    def wif(self, compressed=True, testnet=False):
        #先将私钥进行大端转换
        secret_bytes = self.secret.to_bytes(32, 'big')
        if testnet:
            #如果是测试网络的私钥则在开头增加字节0xef
            prefix = b'\xef'
        else:
            #如果是主网络则在开头增加字节0x80
            prefix = b'\0x80'
        if compressed:
            #如果要创建压缩格式的公钥,在末尾增加自己0x1
            suffix = b'\0x01'
        else:
            suffix = ''

        return encode_base58_checksum(prefix + secret_bytes + suffix)

private_key = PrivateKey(privKey)
wif_private_key = private_key.wif()
print(f"the wif for give private key is: {wif_private_key}")

The result after running the above code is:

the wif for give private key is: 19re3h9z4eEC6WYaziGHvAY8nS8hNddiPcxe4B9a6vA2SbEaSjtqDLYC3SYk

For more content, please search coding Disney at station b

Guess you like

Origin blog.csdn.net/tyler_download/article/details/132255743