Skip to content

Instantly share code, notes, and snippets.

@hal0x2328
Last active May 20, 2019 12:01
Show Gist options
  • Save hal0x2328/d858cc9b794f7b5463f2ddddd10de8bc to your computer and use it in GitHub Desktop.
Save hal0x2328/d858cc9b794f7b5463f2ddddd10de8bc to your computer and use it in GitHub Desktop.
Neo invocation script parser
from neocore.Cryptography.Crypto import Crypto
from neocore.UInt160 import UInt160
from neo.VM.OpCode import *
import binascii
import json
import sys
import io
tokens = {}
with open('tokens.json') as t:
tokens = json.load(t)
def bytesToAddress(bytes):
return Crypto.ToAddress(UInt160(data=bytes))
def contractName(scripthash):
scripthash = "0x{}".format(scripthash)
for token in tokens['results']:
if token['token']['script_hash'] == scripthash:
return token['token']['name']
return "Unknown contract"
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} [script hex]")
sys.exit(-1)
script = ""
try:
script = binascii.unhexlify(sys.argv[1])
except:
print("Malformed script (should be even number of hex digits)")
sys.exit(-1)
f = io.BufferedReader(io.BytesIO(script))
print(f"Parsing {sys.argv[1]}")
while True:
opcode = f.read(1)
if not opcode: break
sarg = ""
if opcode == APPCALL:
sc = bytearray(f.read(20))
sc.reverse()
hexhash = binascii.hexlify(sc).decode()
sarg = "{} ({})".format(hexhash, contractName(hexhash))
elif opcode > b'\x00' and opcode < b'\x4c':
sc = bytearray(f.read(ord(opcode)))
if (opcode == PUSHBYTES20):
sarg = bytesToAddress(sc)
elif f.peek(1)[0] == ord(APPCALL):
sarg = sc.decode()
else:
sarg = binascii.hexlify(sc).decode()
print(f"{ToName(opcode)} {sarg}")
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment