Skip to content

Instantly share code, notes, and snippets.

@hal0x2328
Created January 5, 2020 14:20
Show Gist options
  • Save hal0x2328/292e266bc3532d6b44608f70cd96a1da to your computer and use it in GitHub Desktop.
Save hal0x2328/292e266bc3532d6b44608f70cd96a1da to your computer and use it in GitHub Desktop.
Enumerate Neo accounts
#!/usr/bin/env python3
# enumaccounts.py - hal0x2328
# Read neo-cli LevelDB database and dump all accounts to JSON
import plyvel
import io
import binascii
import sys
import struct
#from the neo-python project
from neo.Core.UInt160 import UInt160
from neo.Core.Cryptography.Crypto import Crypto
neo = b'\x9b|\xff\xda\xa6t\xbe\xae\x0f\x93\x0e\xbe`\x85\xaf\x90\x93\xe5\xfeV\xb3J\\"\x0c\xcd\xcfn\xfc3o\xc5'
gas = b'\xe7-(iy\xeel\xb1\xb7\xe6]\xfd\xdf\xb2\xe3\x84\x10\x0b\x8d\x14\x8ewX\xdeB\xe4\x16\x8bqy,`'
pydb = plyvel.DB("/opt/neo-cli/Chain_00746E41", create_if_missing=False)
print("{\"list\":[");
for key, value in pydb.iterator(prefix=b'\x40'):
scripthash = UInt160(data=key[1:])
addr_str = Crypto.ToAddress(scripthash)
vio = io.BytesIO(value)
vio.seek(1)
neobalance = 0
gasbalance = 0
while True:
addressprops = vio.read(23)
if len(addressprops) != 23:
break
asset = vio.read(32)
if len(asset) != 32:
break
balance = vio.read(8)
if len(balance) != 8:
break
balance = struct.unpack('<q', balance)[0]
if asset == neo:
neobalance = balance / 10000000
elif asset == gas:
gasbalance = balance / 10000000
print('{{"address":"{}", "neo":"{}", "gas":"{}"}},'.format(addr_str, neobalance, gasbalance))
print("]}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment