Skip to content

Commit 99eafc2

Browse files
authored
Merge pull request #604 from stephensli/types/linker
Provide type support to linker.ts
2 parents f54dabf + 6409860 commit 99eafc2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

common/types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* A mapping between libraries and the addresses to which they were deployed.
3+
*
4+
* Containing support for two level configuration, These two level
5+
* configurations can be seen below.
6+
*
7+
* {
8+
* "lib.sol:L1": "0x...",
9+
* "lib.sol:L2": "0x...",
10+
* "lib.sol": {"L3": "0x..."}
11+
* }
12+
*/
13+
export interface LibraryAddresses {
14+
[qualifiedNameOrSourceUnit: string]: string | { [unqualifiedLibraryName: string]: string };
15+
}
16+
17+
/**
18+
* A mapping between libraries and lists of placeholder instances present in their hex-encoded bytecode.
19+
* For each placeholder its length and the position of the first character is stored.
20+
*
21+
* Each start and length entry will always directly refer to the position in
22+
* binary and not hex-encoded bytecode.
23+
*/
24+
export interface LinkReferences {
25+
[libraryLabel: string]: Array<{ start: number, length: number }>;
26+
}

linker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'assert';
22
import { keccak256 } from 'js-sha3';
33
import { isNil, isObject } from './common/helpers';
4+
import { LibraryAddresses, LinkReferences } from './common/types';
45

56
/**
67
* Generates a new-style library placeholder from a fully-qualified library name.
@@ -57,12 +58,12 @@ function replacePlaceholder (bytecode, label, address) {
5758
* @returns bytecode Hex-encoded bytecode string with placeholders replaced with addresses.
5859
* Note that some placeholders may remain in the bytecode if `libraries` does not provide addresses for all of them.
5960
*/
60-
function linkBytecode (bytecode, libraries) {
61+
function linkBytecode (bytecode: string, libraries: LibraryAddresses): string {
6162
assert(typeof bytecode === 'string');
6263
assert(typeof libraries === 'object');
6364

6465
// NOTE: for backwards compatibility support old compiler which didn't use file names
65-
const librariesComplete = {};
66+
const librariesComplete: { [fullyQualifiedLibraryName: string]: string } = {};
6667

6768
for (const [fullyQualifiedLibraryName, libraryObjectOrAddress] of Object.entries(libraries)) {
6869
if (isNil(libraryObjectOrAddress)) {
@@ -136,12 +137,12 @@ function linkBytecode (bytecode, libraries) {
136137
* offsets and lengths refer to the *binary* (not hex-encoded) bytecode, just
137138
* like in `evm.bytecode.linkReferences`.
138139
*/
139-
function findLinkReferences (bytecode) {
140+
function findLinkReferences (bytecode: string): LinkReferences {
140141
assert(typeof bytecode === 'string');
141142

142143
// find 40 bytes in the pattern of __...<36 digits>...__
143144
// e.g. __Lib.sol:L_____________________________
144-
const linkReferences = {};
145+
const linkReferences: LinkReferences = {};
145146

146147
let offset = 0;
147148

0 commit comments

Comments
 (0)