Skip to content

Commit

Permalink
disasm: Replace zeroPad
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Aug 21, 2023
1 parent 703b366 commit 3c3ac88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
18 changes: 13 additions & 5 deletions src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import { hexToBytes, bytesToHex, keccak256 } from "../utils";

describe('Utils', () => {
test.each([
[new Uint8Array([0,1,2,3]), "0x00010203"],
[new Uint8Array([42,69,255]), "0x2a45ff"],
[new Uint8Array([255]), "0xff"],
[new Uint8Array([255,255]), "0xffff"],
[new Uint8Array([0,255,0,255]), "0x00ff00ff"],
new Uint8Array([0,1,2,3]),
new Uint8Array([42,69,255]),
new Uint8Array([255]),
new Uint8Array([255,255]),
new Uint8Array([0,255,0,255]),
])("bytesToHex %s", (bytes) => {
expect(bytesToHex(bytes)).toStrictEqual(ethers.utils.hexlify(bytes));
});

test("bytesToHex padding", () => {
expect(bytesToHex(new Uint8Array([0]), 20)).toStrictEqual("0x0000000000000000000000000000000000000000");
expect(bytesToHex(new Uint8Array([255,255,255]), 20)).toStrictEqual("0x0000000000000000000000000000000000ffffff");
});


test.each([
"0x00010203",
"0x0000102030",
Expand All @@ -30,6 +36,8 @@ describe('Utils', () => {
"0x00010203",
"0xffff",
"0xffff0000111122223333444455556666777788889999aaaabbbbccccddddeeee",
new Uint8Array([0,1,2,3]),
new Uint8Array([255,0,255,0,255,0]),
])("keccak256 %s", (hex) => {
expect(keccak256(hex)).toStrictEqual(ethers.utils.keccak256(hex));
});
Expand Down
27 changes: 10 additions & 17 deletions src/disasm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { arrayify, hexlify, zeroPad } from "@ethersproject/bytes";
import { hexToBytes, bytesToHex } from "./utils";

import { ABI, ABIFunction, ABIEvent, StateMutability } from "./abi";

Expand All @@ -9,7 +9,7 @@ import { slotResolvers, ProxyResolver, SequenceWalletProxyResolver, FixedProxyRe

function valueToOffset(value: Uint8Array): number {
// FIXME: Should be a cleaner way to do this...
return parseInt(hexlify(value), 16);
return parseInt(bytesToHex(value), 16);
}

// BytecodeIter takes EVM bytecode and handles iterating over it with correct
Expand Down Expand Up @@ -38,7 +38,7 @@ export class BytecodeIter {
this.posBufferSize = Math.max(config.bufferSize || 1, 1);
this.posBuffer = [];

this.bytecode = arrayify(bytecode, { allowMissingPrefix: true });
this.bytecode = hexToBytes(bytecode);
}

hasMore(): boolean {
Expand Down Expand Up @@ -246,7 +246,7 @@ export function disasm(bytecode: string): Program {
// This is probably not bullet proof but seems like a good starting point
if (inst === opcodes.PUSH32) {
const v = code.value();
const resolver = slotResolvers[hexlify(v)];
const resolver = slotResolvers[bytesToHex(v)];
if (resolver !== undefined) {
// While we're looking at PUSH32, let's find proxy slots
p.proxies.push(resolver);
Expand All @@ -255,7 +255,7 @@ export function disasm(bytecode: string): Program {
}
continue
} else if (isLog(inst) && lastPush32.length > 0) {
p.eventCandidates.push(hexlify(lastPush32));
p.eventCandidates.push(bytesToHex(lastPush32));
continue
}

Expand All @@ -266,7 +266,7 @@ export function disasm(bytecode: string): Program {
if (isPush(code.at(-3))) {
// Hardcoded delegate address
// TODO: We can probably do more here to determine which kind? Do we care?
const addr = hexlify(zeroPad(code.valueAt(-3), 20));
const addr = bytesToHex(code.valueAt(-3), 20);
p.proxies.push(new FixedProxyResolver("HardcodedDelegateProxy", addr));

} else if (
Expand Down Expand Up @@ -401,12 +401,8 @@ export function disasm(bytecode: string): Program {
) {
// Found a function selector sequence, save it to check against JUMPDEST table later
let value = code.valueAt(-4)
if (value.length < 4) {
// 0-prefixed comparisons get optimized to a smaller width than PUSH4
// FIXME: Could just use ethers.utils.hexzeropad
value = zeroPad(value, 4);
}
const selector: string = hexlify(value);
// 0-prefixed comparisons get optimized to a smaller width than PUSH4
const selector: string = bytesToHex(value, 4);
p.selectors[selector] = offsetDest;
selectorDests.add(offsetDest);

Expand All @@ -422,11 +418,8 @@ export function disasm(bytecode: string): Program {
) {
// Found a function selector sequence, save it to check against JUMPDEST table later
let value = code.valueAt(-5)
if (value.length < 4) {
// 0-prefixed comparisons get optimized to a smaller width than PUSH4
value = zeroPad(value, 4);
}
const selector: string = hexlify(value);
// 0-prefixed comparisons get optimized to a smaller width than PUSH4
const selector: string = bytesToHex(value, 4);
p.selectors[selector] = offsetDest;
selectorDests.add(offsetDest);

Expand Down
8 changes: 6 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ export function hexToBytes(hex: string): Uint8Array {
return r;
}

export function bytesToHex(bytes: Uint8Array): string {
return "0x" + Array.prototype.map.call(bytes, function(n: number) {
export function bytesToHex(bytes: Uint8Array, padToBytes?: number): string {
const hex = Array.prototype.map.call(bytes, function(n: number) {
return n.toString(16).padStart(2, "0");
}).join("");
if (padToBytes !== undefined && hex.length < padToBytes * 2) {
return "0x" + hex.padStart(padToBytes * 2, "0");
}
return "0x" + hex;
}


Expand Down

0 comments on commit 3c3ac88

Please sign in to comment.