@@ -27,14 +27,24 @@ function libraryHashPlaceholder (fullyQualifiedLibraryName) {
27
27
*
28
28
* @param address Address to replace placeholders with. Must be the right length.
29
29
* It will **not** be padded with zeros if too short.
30
+ *
31
+ * @param linkReferences A optional mapping of libraries to lists of placeholder positions in the binary.
30
32
*/
31
- function replacePlaceholder ( bytecode , label , address ) {
33
+ function replacePlaceholder ( bytecode : string , label : string , address : string , linkReferences ?: LinkReferences ) : string {
34
+ // Try to find link references if `linkReferences` is not provided
35
+ if ( ! linkReferences ) {
36
+ linkReferences = findLinkReferences ( bytecode ) ;
37
+ }
38
+
32
39
// truncate to 36 characters
33
40
const truncatedName = label . slice ( 0 , 36 ) ;
34
- const libLabel = `__${ truncatedName . padEnd ( 36 , '_' ) } __` ;
35
41
36
- while ( bytecode . indexOf ( libLabel ) >= 0 ) {
37
- bytecode = bytecode . replace ( libLabel , address ) ;
42
+ if ( linkReferences && linkReferences [ truncatedName ] ) {
43
+ linkReferences [ truncatedName ] . forEach ( function ( reference ) {
44
+ const start = reference . start * 2 ;
45
+ const end = ( reference . start + reference . length ) * 2 ;
46
+ bytecode = bytecode . replace ( bytecode . substring ( start , end ) , address ) ;
47
+ } ) ;
38
48
}
39
49
40
50
return bytecode ;
@@ -55,10 +65,12 @@ function replacePlaceholder (bytecode, label, address) {
55
65
* @param libraries Mapping between fully qualified library names and the hex-encoded
56
66
* addresses they should be replaced with. Addresses shorter than 40 characters are automatically padded with zeros.
57
67
*
68
+ * @param linkReferences A optional mapping of libraries to lists of placeholder positions in the binary.
69
+ *
58
70
* @returns bytecode Hex-encoded bytecode string with placeholders replaced with addresses.
59
71
* Note that some placeholders may remain in the bytecode if `libraries` does not provide addresses for all of them.
60
72
*/
61
- function linkBytecode ( bytecode : string , libraries : LibraryAddresses ) : string {
73
+ function linkBytecode ( bytecode : string , libraries : LibraryAddresses , linkReferences ?: LinkReferences ) : string {
62
74
assert ( typeof bytecode === 'string' ) ;
63
75
assert ( typeof libraries === 'object' ) ;
64
76
@@ -103,8 +115,8 @@ function linkBytecode (bytecode: string, libraries: LibraryAddresses): string {
103
115
// remove 0x prefix
104
116
hexAddress = hexAddress . slice ( 2 ) . padStart ( 40 , '0' ) ;
105
117
106
- bytecode = replacePlaceholder ( bytecode , libraryName , hexAddress ) ;
107
- bytecode = replacePlaceholder ( bytecode , libraryHashPlaceholder ( libraryName ) , hexAddress ) ;
118
+ bytecode = replacePlaceholder ( bytecode , libraryName , hexAddress , linkReferences ) ;
119
+ bytecode = replacePlaceholder ( bytecode , libraryHashPlaceholder ( libraryName ) , hexAddress , linkReferences ) ;
108
120
}
109
121
110
122
return bytecode ;
0 commit comments