3

I was following the tutorial here : https://hackernoon.com/how-to-generate-bitcoin-addresses-technical-address-generation-explanation-rus3z9e

i imported base58 to my python project :

import base58

I tried to convert the private key at the tutorial into the base58 version

privatekey = "a4f228d49910e8ecb53ba6f23f33fbfd2bad442e902ea20b8cf89c473237bf9f"

# expected : C6t2iJ7AXA2X1KQVnqw3r7NKtKaERdLnaGmbDZNqxXjk
privatekey_base58 = base58.b58encode(privatekey)

But it didn't give me the expected value like in the tutorial , instead it gives me this enter image description here

How do i properly encode a string to base58 and decode base58 to original string ?

2 Answers 2

2

Use b58encode_check instead of non-check version and convert your hex to binary before passing it to the library.

4
  • And how do i decode base58 string ? Should i turn the string to binary too before decoding ? Oct 10, 2021 at 12:04
  • No, but to decode convert the decode result to hex because it'll be in binary
    – MCCCS
    Oct 10, 2021 at 12:23
  • help it doesnt work , i tried base58.b58encode_check(bin(int("0x000c2c910a661178ef63e276dd0e239883b862f58cc3c0439f", 16))) but it doesnt gives 127NVqnjf8gB9BFAW2dnQeM6wqmy1gbGtv like in the tutorial Oct 11, 2021 at 2:34
  • Does this give you any inspiration stackoverflow.com/a/23565829 ?
    – MCCCS
    Oct 11, 2021 at 6:23
2

You need to convert string hex to bytes hex format before feeding it to b58encode to produce C6t2iJ7AXA2X1KQVnqw3r7NKtKaERdLnaGmbDZNqxXjk.

import base58
    
privatekey_base58 = base58.b58encode(bytes.fromhex(
    "a4f228d49910e8ecb53ba6f23f33fbfd2bad442e902ea20b8cf89c473237bf9f"
))

The tutorial you linked to is not accessible anymore, so I am not sure whether they talked about it there, but there are actually several steps between generation private key and conversion to Wallet Import Format (WIF). You can see more details about it at https://en.bitcoin.it/wiki/Wallet_import_format. According to the wiki page, you need to extend private key with 0x80 (for mainnet addresses). Extended private key is then passed through double sha256 to extract a checksum (first 4 bytes). Finally, extended private key and checksum are concatenated and Base58 check encoding is applied to produce WIF private key. Surprisingly enough, their tutorial does not use Base58 check encoding, but Base58 encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.