Skip to content

Commit

Permalink
Replace library reference using linkReferences
Browse files Browse the repository at this point in the history
  • Loading branch information
arthcp authored and axic committed Jul 4, 2019
1 parent 9b1f06a commit 43d59bb
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ function libraryHashPlaceholder (input) {
return '$' + keccak256(input).slice(0, 34) + '$';
}

var linkBytecode = function (bytecode, libraries) {
var linkBytecode = function (bytecode, libraries, linkReferences) {
if (typeof linkReferences === typeof '' || linkReferences === null || linkReferences === undefined) {
linkReferences = findLinkReferences(bytecode);
}

// NOTE: for backwards compatibility support old compiler which didn't use file names
var librariesComplete = {};
for (var libraryName in libraries) {
Expand Down Expand Up @@ -35,7 +39,7 @@ var linkBytecode = function (bytecode, libraries) {

// Support old (library name) and new (hash of library name)
// placeholders.
var replace = function (name) {
var findAndReplace = function (name) {
// truncate to 37 characters
var truncatedName = name.slice(0, 36);
var libLabel = '__' + truncatedName + Array(37 - truncatedName.length).join('_') + '__';
Expand All @@ -44,6 +48,21 @@ var linkBytecode = function (bytecode, libraries) {
}
};

var replace = function (name) {
// truncate to 37 characters
var truncatedName = name.slice(0, 36);
if (linkReferences && linkReferences[truncatedName]) {
linkReferences[truncatedName].forEach(function (reference) {
var start = reference.start * 2;
var end = (reference.start + reference.length) * 2;
bytecode = bytecode.slice(0, start) + hexAddress + bytecode.slice(end);
});
} else {
// manually find and replace if link reference is not present
findAndReplace(name);
}
};

replace(libraryName);
replace(libraryHashPlaceholder(libraryName));
}
Expand Down

0 comments on commit 43d59bb

Please sign in to comment.