Guest User

Untitled

a guest
Feb 8th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import base64
  2. import crc16
  3. import warnings
  4. warnings.filterwarnings("ignore", category=DeprecationWarning)
  5.  
  6. def hex_addr_to_base64(fullAddr, bounceable=True, testnet=False):
  7.     print("bounceable: " + ("True" if bounceable else "no-bounceable"))
  8.     buff = fullAddr.split(':')
  9.     workchain = int(buff[0])
  10.     addr_hex = buff[1]
  11.     if len(addr_hex) != 64:
  12.         raise Exception("hex_addr_to_base64 error: Invalid length of hexadecimal address")
  13.  
  14.     b = bytearray(36)
  15.     b[0] = 0x51 - bounceable * 0x40 + testnet * 0x80
  16.     b[1] = workchain % 256
  17.     b[2:34] = bytearray.fromhex(addr_hex)
  18.     buff = bytes(b[:34])
  19.     crc = crc16.crc16xmodem(buff)
  20.     # print(crc)
  21.     b[34] = crc >> 8
  22.     b[35] = crc & 0xff
  23.     result = base64.b64encode(b)
  24.     result = result.decode()
  25.     result = result.replace('+', '-')
  26.     result = result.replace('/', '_')
  27.     # print([i for i in b])
  28.     # print(b[-1])
  29.     return result
  30.  
  31.  
  32. if __name__ == "__main__":
  33.     addr = "-1:85578413C4B97EEC2B7713A32A3AD32DF911973318B6EE0E08EB8E65C3A3135A"
  34.     testnet = False
  35.     print("addr raw: " + addr)
  36.     print("network: " + ("testnet" if testnet else "mainnet"))
  37.     b64addr = hex_addr_to_base64(addr, testnet=testnet)
  38.     print("b64 addr: " + b64addr)
Add Comment
Please, Sign In to add comment