Title: wif to integer - problem checksum Post by: ecdsa123 on January 01, 2023, 07:25:34 PM I got problem - and I do not know where is problem. I got problem with checksum
any help? Code: import hashlib Code: WIF="KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" Title: Re: wif to integer - problem checksum Post by: BlackHatCoiner on January 01, 2023, 07:38:25 PM Printing is the friend of debugging. What does it print for checksum? It should be 0x4671fc3f for compressed private key = 1. Also, what's the error it shows to you? You can't expect everyone who sees this, to run it on their local machine. Explain better.
and I do not know where is problem. I got problem with checksum You don't know where the problem is, but you know that it has to do with checksum? Again, what's the error?Title: Re: wif to integer - problem checksum Post by: ecdsa123 on January 01, 2023, 08:18:39 PM error is : Invalid WIF checksum
Title: Re: wif to integer - problem checksum Post by: brainless on January 01, 2023, 08:19:02 PM https://github.com/matja/bitcoin-tool
use these tools, for all side converters Title: Re: wif to integer - problem checksum Post by: witcher_sense on January 01, 2023, 09:09:51 PM Code: import base58 Title: Re: wif to integer - problem checksum Post by: pooya87 on January 02, 2023, 04:08:15 AM # Decode the WIF string to a bytes object You want to "decode" then you call "encode" method?! Doesn't that return a string instead of bytes? If so, the rest of your code is also wrong since it is not using the decoded base58 but a converted string.wif_bytes = wif.encode() Title: Re: wif to integer - problem checksum Post by: odolvlobo on January 02, 2023, 04:36:53 AM Code: # The last four bytes of the WIF string represent the checksum The last four bytes of the decoded WIF is the checksum, and the the last four characters in the string. https://en.bitcoin.it/wiki/Wallet_import_format Title: Re: wif to integer - problem checksum Post by: witcher_sense on January 02, 2023, 05:02:39 AM Have you consulted https://en.bitcoin.it/wiki/Wallet_import_format page before writing your code?
For example (as pooya87 has already noted): Code: # Decode the WIF string to a bytes object Wrong. You should be using b58decode function from base58 module or something similar to convert encoded string to bytes. Therefore, the following two lines are also wrong because you are dealing with improperly decoded string. And yes, when you calculate a checksum you don't disregard a first byte (you add it instead). Code: # The last four bytes of the WIF string represent the checksum Checksum is a double hash of a byte string, so your calculated checksum is wrong by definition: Code: calculated_checksum = hashlib.sha256(private_key_bytes).digest()[:4] Lines like Code: if wif_bytes[0] == 0x80: Code: wif_bytes[0] == 0xef: Something like the following should work: Code: def wif_to_int_private_key(wif: str) -> int: Title: Re: wif to integer - problem checksum Post by: mash23 on January 02, 2023, 07:14:08 AM if wif_bytes[0] == 0x80:
will never work because you didn't specify encoding (bytes[0].hex() == '80' or b.hex() == f'{0x80:x}' if you like) Title: Re: wif to integer - problem checksum Post by: witcher_sense on January 02, 2023, 07:54:44 AM if wif_bytes[0] == 0x80: You can also convert a hex representation of bytes to integer with base 16 and compare it with a given hexadecimal value.will never work because you didn't specify encoding (bytes[0].hex() == '80' or b.hex() == f'{0x80:x}' if you like) You can't compare them directly because bytes can represent different things (not only integers). You can also use this method to compare bytes as integers: If you want to interpret these sequences explicitly as integer literals, then use ast.literal_eval() to interpret decoded text values; always normalise first before comparison: Code: >>> import ast Here is a complete script to convert wif to an integer: Code: import base58 |