From 00ad9ce6b086bc69331c2693946f5255d897c066 Mon Sep 17 00:00:00 2001 From: juliopavila Date: Fri, 6 Sep 2024 14:43:15 -0300 Subject: [PATCH] feat: improve resolveLinksInBytecode method --- src/artifact/internal/getBuildArtifact.ts | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/artifact/internal/getBuildArtifact.ts b/src/artifact/internal/getBuildArtifact.ts index 0ad1f3b..8ccd5ea 100644 --- a/src/artifact/internal/getBuildArtifact.ts +++ b/src/artifact/internal/getBuildArtifact.ts @@ -2,6 +2,7 @@ import path from "path"; import { readdirSync, readFileSync, statSync } from "fs"; import { BuildArtifact, MastercopyArtifact } from "../../types"; +import assert from "assert"; /** * Retrieves the build artifact for a specified contract. @@ -79,27 +80,16 @@ export function resolveLinksInBytecode( libraryAddress = libraryAddress.toLowerCase().replace(/^0x/, ""); - if (libraryAddress.length !== 40) { - throw new Error(`Invalid library address: ${libraryAddress}`); - } + assert(libraryAddress.length == 40); for (const { length, start } of artifact.linkReferences[libraryPath][ libraryName ]) { - if (length !== 20) { - throw new Error( - `Library reference length mismatch: expected 20, got ${length}` - ); - } - - const bytecodeArray = bytecode.split(""); - const addressArray = libraryAddress.split(""); - - for (let i = 0; i < addressArray.length; i++) { - bytecodeArray[start * 2 + i] = addressArray[i]; - } + assert(length == 20); + const left = bytecode.slice(0, start * 2); + const right = bytecode.slice((start + length) * 2); + bytecode = `${left}${libraryAddress}${right}`; - bytecode = bytecodeArray.join(""); console.log( `Replaced library reference at ${start} with address ${libraryAddress}` );