Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import base64
- import crc16
- import warnings
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- def hex_addr_to_base64(fullAddr, bounceable=True, testnet=False):
- print("bounceable: " + ("True" if bounceable else "no-bounceable"))
- buff = fullAddr.split(':')
- workchain = int(buff[0])
- addr_hex = buff[1]
- if len(addr_hex) != 64:
- raise Exception("hex_addr_to_base64 error: Invalid length of hexadecimal address")
- b = bytearray(36)
- b[0] = 0x51 - bounceable * 0x40 + testnet * 0x80
- b[1] = workchain % 256
- b[2:34] = bytearray.fromhex(addr_hex)
- buff = bytes(b[:34])
- crc = crc16.crc16xmodem(buff)
- # print(crc)
- b[34] = crc >> 8
- b[35] = crc & 0xff
- result = base64.b64encode(b)
- result = result.decode()
- result = result.replace('+', '-')
- result = result.replace('/', '_')
- # print([i for i in b])
- # print(b[-1])
- return result
- if __name__ == "__main__":
- addr = "-1:85578413C4B97EEC2B7713A32A3AD32DF911973318B6EE0E08EB8E65C3A3135A"
- testnet = False
- print("addr raw: " + addr)
- print("network: " + ("testnet" if testnet else "mainnet"))
- b64addr = hex_addr_to_base64(addr, testnet=testnet)
- print("b64 addr: " + b64addr)
Add Comment
Please, Sign In to add comment