Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for converting swarm hash to STOP and toggling disasse… #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pyevmasm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def main():
group_action.add_argument('-t', '--print-opcode-table', action='store_true', help='List supported EVM opcodes')
parser.add_argument('-bi', '--binary-input', action='store_true', help='Binary input mode (-d only)')
parser.add_argument('-bo', '--binary-output', action='store_true', help='Binary output mode (-a only)')
parser.add_argument('-N', '--no-offsets', action='store_true', help='Do not print the file offset of the current instruction (-d only)')
parser.add_argument('-S', '--no-swarmhash', action='store_true', help='Convert swarm hash to STOP opcodes')
parser.add_argument('-i', '--input', nargs='?', default=sys.stdin, type=argparse.FileType('r'),
help='Input file, default=stdin')
parser.add_argument('-o', '--output', nargs='?', default=sys.stdout, type=argparse.FileType('w'),
Expand Down Expand Up @@ -65,10 +67,24 @@ def main():
if buf_set <= hex_set: # subset
buf = binascii.unhexlify(buf)

# replace all swarm hashes with \x00
if args.no_swarmhash:
while True: #detect multiple swarm hashes, about 700 contracts do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify it by using re.sub (regex replace). Example:

In [9]: re.sub(b'\x12..', b'\x00'*4, b'\x1234 abc \x1233 \x1234')
Out[9]: b'\x00\x00\x00\x00 abc \x00\x00\x00\x00 \x00\x00\x00\x00'

So here it should probably be (pls check this):

buf = re.sub(b'\xa1ebzzr0X .{34}', b'\x00'*43, buf)

if '\xa1ebzzr0X ' in buf:
# replace the swarm hash with stops
offset = buf.find('\xa1ebzzr0X ')
buf = buf[:offset] + b'\x00' * 43 + buf[offset+43:]
else:
break

insns = list(disassemble_all(buf))
for i in insns:
args.output.write("%08x: %s\n" % (i.pc, str(i)))
if args.no_offsets:
args.output.write("%s\n" % (str(i),))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the (,) is redundant so it could be % str(i) but I understand it might be better to leave it s it is.
We could also use "{}\n".format(i)

else:
args.output.write("%08x: %s\n" % (i.pc, str(i)))


print("done")
if __name__ == "__main__":
main()