Skip to content

Commit

Permalink
fix: adjust replacement logic such that offset is correctly used
Browse files Browse the repository at this point in the history
  • Loading branch information
cristovaoth committed Sep 9, 2024
1 parent 080731c commit df8a837
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/artifact/internal/getBuildArtifact.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import assert from "assert";
import path from "path";
import { isAddress } from "ethers";
import { readdirSync, readFileSync, statSync } from "fs";

import { BuildArtifact, MastercopyArtifact } from "../../types";
import assert from "assert";

/**
* Retrieves the build artifact for a specified contract.
Expand Down Expand Up @@ -78,26 +79,27 @@ export function resolveLinksInBytecode(
let { address: libraryAddress } =
mastercopies[libraryName][contractVersion];

libraryAddress = libraryAddress.toLowerCase().replace(/^0x/, "");

assert(libraryAddress.length == 40);
assert(isAddress(libraryAddress));

for (const { length, start } of artifact.linkReferences[libraryPath][
libraryName
]) {
for (const { length, start: offset } of artifact.linkReferences[
libraryPath
][libraryName]) {
assert(length == 20);
const left = bytecode.slice(0, start * 2);
const right = bytecode.slice((start + length) * 2);
bytecode = `${left}${libraryAddress}${right}`;

// the offset is in bytes, and does not account for the trailing 0x
const left = 2 + offset * 2;
const right = left + length * 2;

bytecode = `${bytecode.slice(0, left)}${libraryAddress.slice(2).toLowerCase()}${bytecode.slice(right)}`;

console.log(
`Replaced library reference at ${start} with address ${libraryAddress}`
`Replaced library reference at ${offset} with address ${libraryAddress}`
);
}
}
}

return bytecode.replace("__", "");
return bytecode;
}

/**
Expand Down

0 comments on commit df8a837

Please sign in to comment.