Skip to content

Commit f242ae1

Browse files
committed
Support linking via hash.
1 parent 5b7cdba commit f242ae1

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

linker.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
var keccak = require('keccak');
2+
3+
function keccak256 (input) {
4+
return keccak('keccak256').update(input).digest();
5+
}
6+
17
var linkBytecode = function (bytecode, libraries) {
28
// NOTE: for backwards compatibility support old compiler which didn't use file names
39
var librariesComplete = {};
@@ -19,11 +25,6 @@ var linkBytecode = function (bytecode, libraries) {
1925
}
2026

2127
for (libraryName in librariesComplete) {
22-
// truncate to 37 characters
23-
var internalName = libraryName.slice(0, 36);
24-
// prefix and suffix with __
25-
var libLabel = '__' + internalName + Array(37 - internalName.length).join('_') + '__';
26-
2728
var hexAddress = librariesComplete[libraryName];
2829
if (hexAddress.slice(0, 2) !== '0x' || hexAddress.length > 42) {
2930
throw new Error('Invalid address specified for ' + libraryName);
@@ -32,9 +33,19 @@ var linkBytecode = function (bytecode, libraries) {
3233
hexAddress = hexAddress.slice(2);
3334
hexAddress = Array(40 - hexAddress.length + 1).join('0') + hexAddress;
3435

35-
while (bytecode.indexOf(libLabel) >= 0) {
36-
bytecode = bytecode.replace(libLabel, hexAddress);
37-
}
36+
// Support old (library name) and new (hash of library name)
37+
// placeholders.
38+
var replace = function (name) {
39+
// truncate to 37 characters
40+
var truncatedName = name.slice(0, 36);
41+
var libLabel = '__' + truncatedName + Array(37 - truncatedName.length).join('_') + '__';
42+
while (bytecode.indexOf(libLabel) >= 0) {
43+
bytecode = bytecode.replace(libLabel, hexAddress);
44+
}
45+
};
46+
47+
replace(libraryName);
48+
replace(keccak256(libraryName).toString('hex'));
3849
}
3950

4051
return bytecode;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"homepage": "https://github.com/ethereum/solc-js#readme",
4141
"dependencies": {
4242
"fs-extra": "^0.30.0",
43+
"keccak": "^1.0.2",
4344
"memorystream": "^0.3.1",
4445
"require-from-string": "^2.0.0",
4546
"semver": "^5.5.0",

test/linker.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,19 @@ tape('Linking', function (t) {
140140
st.end();
141141
});
142142
});
143+
144+
tape('Linker via hash', function (t) {
145+
t.test('Non-hashed placeholder', function (st) {
146+
var bytecode = '6060604052341561000__lib2.sol:L____________________________66606060606060';
147+
bytecode = linker.linkBytecode(bytecode, { 'lib2.sol:L': '0x123456' });
148+
st.equal(bytecode, '6060604052341561000000000000000000000000000000000000012345666606060606060');
149+
st.end();
150+
});
151+
152+
t.test('Hashed placeholder', function (st) {
153+
var bytecode = '6060604052341561000__cb901161e812ceb78cfe30ca65050c433771__66606060606060';
154+
bytecode = linker.linkBytecode(bytecode, { 'lib2.sol:L': '0x123456' });
155+
st.equal(bytecode, '6060604052341561000000000000000000000000000000000000012345666606060606060');
156+
st.end();
157+
});
158+
});

0 commit comments

Comments
 (0)