-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinker.js
94 lines (80 loc) · 2.84 KB
/
linker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var keccak = require('keccak');
function keccak256 (input) {
return keccak('keccak256').update(input).digest();
}
function libraryHashPlaceholder (input) {
return '$' + keccak256(input).toString('hex').slice(0, 34) + '$';
}
var linkBytecode = function (bytecode, libraries) {
// NOTE: for backwards compatibility support old compiler which didn't use file names
var librariesComplete = {};
for (var libraryName in libraries) {
if (typeof libraries[libraryName] === 'object') {
// API compatible with the standard JSON i/o
for (var lib in libraries[libraryName]) {
librariesComplete[lib] = libraries[libraryName][lib];
librariesComplete[libraryName + ':' + lib] = libraries[libraryName][lib];
}
} else {
// backwards compatible API for early solc-js versions
var parsed = libraryName.match(/^([^:]+):(.+)$/);
if (parsed) {
librariesComplete[parsed[2]] = libraries[libraryName];
}
librariesComplete[libraryName] = libraries[libraryName];
}
}
for (libraryName in librariesComplete) {
var hexAddress = librariesComplete[libraryName];
if (hexAddress.slice(0, 2) !== '0x' || hexAddress.length > 42) {
throw new Error('Invalid address specified for ' + libraryName);
}
// remove 0x prefix
hexAddress = hexAddress.slice(2);
hexAddress = Array(40 - hexAddress.length + 1).join('0') + hexAddress;
// Support old (library name) and new (hash of library name)
// placeholders.
var replace = function (name) {
// truncate to 37 characters
var truncatedName = name.slice(0, 36);
var libLabel = '__' + truncatedName + Array(37 - truncatedName.length).join('_') + '__';
while (bytecode.indexOf(libLabel) >= 0) {
bytecode = bytecode.replace(libLabel, hexAddress);
}
};
replace(libraryName);
replace(libraryHashPlaceholder(libraryName));
}
return bytecode;
};
var findLinkReferences = function (bytecode) {
// find 40 bytes in the pattern of __...<36 digits>...__
// e.g. __Lib.sol:L_____________________________
var linkReferences = {};
var offset = 0;
while (true) {
var found = bytecode.match(/__(.{36})__/);
if (!found) {
break;
}
var start = found.index;
// trim trailing underscores
// NOTE: this has no way of knowing if the trailing underscore was part of the name
var libraryName = found[1].replace(/_+$/gm, '');
if (!linkReferences[libraryName]) {
linkReferences[libraryName] = [];
}
linkReferences[libraryName].push({
// offsets are in bytes in binary representation (and not hex)
start: (offset + start) / 2,
length: 20
});
offset += start + 20;
bytecode = bytecode.slice(start + 20);
}
return linkReferences;
};
module.exports = {
linkBytecode: linkBytecode,
findLinkReferences: findLinkReferences
};