I’m writing a script for decoding a hex of a TON wallet.
Documentation says:
[address verification – 2 bytes] — In user-friendly addresses, address verification is composed of a CRC16-CCITT signature from the previous 34 bytes. (Example) In fact, the idea pertaining to verification for user-friendly addresses is quite similar to the Luhn algorithm, which is used on all credit cards to prevent users from entering non-existing card numbers by mistake.
Here’s the code:
import binascii
import base64
import crcmod.predefined
def unhexify_wallet(hex_string):
binary_data = binascii.unhexlify(hex_string)
test = b'x51x00' + binary_data
crc = crcmod.mkCrcFun(poly=0x11021, initCrc=0xFFFF, xorOut=0x0000, rev=True)(test)
crc_bytes = crc.to_bytes(2, byteorder='big')
test_with_crc = test + crc_bytes
result = base64.urlsafe_b64encode(test_with_crc).decode('utf-8').rstrip("=")
return result
if __name__ == "__main__":
hex_string = "867ac2b47d1955de6c8e23f57994fad507ea3bcfe2a7d76ff38f29ec46729627"
print(unhexify_wallet(hex_string))
So crc_bytes is my address verification. However, instead of getting ‘UQCGesK0fRlV3myOI_V5lPrVB-o7z-Kn12_zjynsRnKWJ_3M’ as a result, I get ‘UQCGesK0fRlV3myOI_V5lPrVB-o7z-Kn12_zjynsRnKWJ_Or’. Making rev=False gave the wrong result also. What might be the problem?
illusionZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.