1
+ var keccak = require ( 'keccak' ) ;
2
+
3
+ function keccak256 ( input ) {
4
+ return keccak ( 'keccak256' ) . update ( input ) . digest ( ) ;
5
+ }
6
+
1
7
var linkBytecode = function ( bytecode , libraries ) {
2
8
// NOTE: for backwards compatibility support old compiler which didn't use file names
3
9
var librariesComplete = { } ;
@@ -19,11 +25,6 @@ var linkBytecode = function (bytecode, libraries) {
19
25
}
20
26
21
27
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
28
var hexAddress = librariesComplete [ libraryName ] ;
28
29
if ( hexAddress . slice ( 0 , 2 ) !== '0x' || hexAddress . length > 42 ) {
29
30
throw new Error ( 'Invalid address specified for ' + libraryName ) ;
@@ -32,9 +33,19 @@ var linkBytecode = function (bytecode, libraries) {
32
33
hexAddress = hexAddress . slice ( 2 ) ;
33
34
hexAddress = Array ( 40 - hexAddress . length + 1 ) . join ( '0' ) + hexAddress ;
34
35
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' ) ) ;
38
49
}
39
50
40
51
return bytecode ;
0 commit comments