neo-decompiler 0.11.0

Neo N3 NEF decompiler: parse, disassemble, lift bytecode to high-level pseudocode and C# skeletons, with a CLI, JSON reports, and optional WebAssembly bindings.
Documentation
import { hexOffset } from "./util.js";

export function finalizeCallGraph(methods, edges) {
  const methodByOffset = new Map(methods.map((method) => [method.offset, method]));
  for (const edge of edges) {
    if (edge.target.kind === "Internal") {
      const method = edge.target.method;
      methodByOffset.set(method.offset, method);
    }
  }

  const finalMethods = [...methodByOffset.values()].sort((left, right) => left.offset - right.offset);
  const sortedOffsets = finalMethods.map((method) => method.offset);
  for (const edge of edges) {
    edge.caller = methodForOffset(finalMethods, sortedOffsets, edge.callOffset);
  }
  return { methods: finalMethods, edges };
}

export function resolveMethodTarget(methodByOffset, targetOffset) {
  return (
    methodByOffset.get(targetOffset) ?? {
      offset: targetOffset,
      name: `sub_0x${hexOffset(targetOffset)}`,
    }
  );
}

function methodForOffset(methods, sortedOffsets, offset) {
  let lo = 0;
  let hi = sortedOffsets.length;
  while (lo < hi) {
    const mid = (lo + hi) >> 1;
    if (sortedOffsets[mid] <= offset) {
      lo = mid + 1;
    } else {
      hi = mid;
    }
  }
  return methods[Math.max(0, lo - 1)] ?? { offset: 0, name: "script_entry" };
}