0

Using this link I can understand something about a private key BASE58 (P2PKH):

"such encodings will always yield a 51-character string that starts with '5', or more specifically, either '5H', '5J', or '5K'."

So let's say I have this model of private key:

5J1111111111111111111111111111111111110111111222222

I know that is not a valid private key because I can use a website such as bitaddress to tell me about this.

Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.

Keep in mind I don't have any other information, just a string like that.

2
  • I think the search term you are missing is "wallet import format": en.bitcoin.it/wiki/Wallet_import_format
    – Murch
    Oct 18, 2020 at 3:02
  • I think I should say WIF instead private key, but anyway, those steps are a little bit confused to me because I don't have a compressed public key to verify the WIF, I only have a string. Oct 18, 2020 at 3:33

1 Answer 1

1

Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.

  1. Check if the string has valid Base-58 characters (123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz)
  2. Decode the string to get the byte array using Base-58 encoding while removing and validating its checksum (last 4 bytes). Refer to the wiki link you posted for details.
  3. Take the first byte and check if it is using the expected (version) value (128 for MainNet, 239 for TestNet).
  4. Check the remaining data length (without first byte):
    4.1. If it is 32 bytes, that is the key
    4.2. If it is 33 bytes, remove last byte and check if it is equal to 1, the rest is the key
    4.3. If the length is anything else, it is invalid
  5. Convert the 32 bytes you have from step 4 to a 256-bit integer using big-endian definition
  6. The value of step 5 has to be >= 1 and <= 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140

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