1
+ var keccak = require ( 'keccak' ) ;
2
+
3
+ function keccak256 ( input ) {
4
+ return keccak ( 'keccak256' ) . update ( input ) . digest ( ) ;
5
+ }
6
+
7
+ function libraryHashPlaceholder ( input ) {
8
+ return '$' + keccak256 ( input ) . toString ( 'hex' ) . slice ( 0 , 34 ) + '$' ;
9
+ }
10
+
1
11
var linkBytecode = function ( bytecode , libraries ) {
2
12
// NOTE: for backwards compatibility support old compiler which didn't use file names
3
13
var librariesComplete = { } ;
@@ -19,11 +29,6 @@ var linkBytecode = function (bytecode, libraries) {
19
29
}
20
30
21
31
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
-
27
32
var hexAddress = librariesComplete [ libraryName ] ;
28
33
if ( hexAddress . slice ( 0 , 2 ) !== '0x' || hexAddress . length > 42 ) {
29
34
throw new Error ( 'Invalid address specified for ' + libraryName ) ;
@@ -32,9 +37,19 @@ var linkBytecode = function (bytecode, libraries) {
32
37
hexAddress = hexAddress . slice ( 2 ) ;
33
38
hexAddress = Array ( 40 - hexAddress . length + 1 ) . join ( '0' ) + hexAddress ;
34
39
35
- while ( bytecode . indexOf ( libLabel ) >= 0 ) {
36
- bytecode = bytecode . replace ( libLabel , hexAddress ) ;
37
- }
40
+ // Support old (library name) and new (hash of library name)
41
+ // placeholders.
42
+ var replace = function ( name ) {
43
+ // truncate to 37 characters
44
+ var truncatedName = name . slice ( 0 , 36 ) ;
45
+ var libLabel = '__' + truncatedName + Array ( 37 - truncatedName . length ) . join ( '_' ) + '__' ;
46
+ while ( bytecode . indexOf ( libLabel ) >= 0 ) {
47
+ bytecode = bytecode . replace ( libLabel , hexAddress ) ;
48
+ }
49
+ } ;
50
+
51
+ replace ( libraryName ) ;
52
+ replace ( libraryHashPlaceholder ( libraryName ) ) ;
38
53
}
39
54
40
55
return bytecode ;
0 commit comments