Skip to content

Instantly share code, notes, and snippets.

@smlu
Last active January 23, 2020 13:16
Show Gist options
  • Save smlu/93f7450ae32133fe7cf9725a95950856 to your computer and use it in GitHub Desktop.
Save smlu/93f7450ae32133fe7cf9725a95950856 to your computer and use it in GitHub Desktop.
Python EOS account name converter from/to string
eos_charmap = ".12345abcdefghijklmnopqrstuvwxyz"
def eos_account_to_string(num):
mask = 0xF800000000000000
out = ""
for i in range(0, 13):
if num == 0: return out
indx = (num & mask) >> (60 if i == 12 else 59)
out += eos_charmap[indx]
num <<= 5
return out
def string_to_eos_account(account):
if len(account) > 13:
raise ValueError("Account name too long")
num = 0
n = min(len(account), 12)
for i in range(0, n):
num <<= 5
num |= eos_charmap.index(account[i]);
num <<= 4 + 5*(12 - n)
if len(account) == 13:
v = eos_charmap.index(account[12])
if v > 0x0F:
raise ValueError("thirteenth character in name cannot be a letter that comes after j")
num |= v
return num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment