Skip to content

Commit

Permalink
Add sha3
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Oct 28, 2023
1 parent 68d7485 commit 649cc37
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/opcodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import revert from "./revert";
import selfdestruct from "./selfdestruct";
import sload from "./sload";
import sstore from "./sstore";
import sha3 from "./sha3";
import staticcall from "./staticcall";

export function parse(
Expand Down Expand Up @@ -56,6 +57,8 @@ export function parse(
return sload.parse(step);
case "SSTORE":
return sstore.parse(step);
case "SHA3":
return sha3.parse(step);
case "REVERT":
return revert.parse(step);
default:
Expand Down Expand Up @@ -100,6 +103,8 @@ export async function format(
return sload.format(item);
case "SSTORE":
return sstore.format(item);
case "SHA3":
return sha3.format(item);
case "REVERT":
return revert.format(item, dependencies);
case "SELFDESTRUCT":
Expand Down
49 changes: 49 additions & 0 deletions src/opcodes/sha3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { InterpreterStep } from "@nomicfoundation/ethereumjs-evm";

import { AwaitedItem, Item } from "../types";
import {
colorKey,
colorLabel,
colorValue,
parseBytes32,
parseNumber,
} from "../utils";

export interface SHA3 {
offset: number;
size: number;
data: string;
hash: string;
}

function parse(step: InterpreterStep): AwaitedItem<SHA3> {
const offset = parseNumber(step.stack[step.stack.length - 1].toString(16));
const size = parseNumber(step.stack[step.stack.length - 2].toString(16));
const data = step.memory.slice(offset, offset + size).toString("hex");

const next = 1; // get stack just after this opcode
return {
isAwaitedItem: true,
next,
parse: (stepNext: InterpreterStep) => ({
opcode: "SHA3",
params: {
offset,
size,
data,
hash: parseBytes32(stepNext.stack[step.stack.length - 1].toString(16)),
},
format(): string {
return format(this);
},
}),
};
}

function format(item: Item<SHA3>): string {
return `${colorLabel("[SHA3]")} ${colorKey(item.params.data)}${colorValue(
item.params.hash
)}`;
}

export default { parse, format };

0 comments on commit 649cc37

Please sign in to comment.