diff --git a/common/types.ts b/common/types.ts new file mode 100644 index 00000000..254484a1 --- /dev/null +++ b/common/types.ts @@ -0,0 +1,26 @@ +/** + * A mapping between libraries and the addresses to which they were deployed. + * + * Containing support for two level configuration, These two level + * configurations can be seen below. + * + * { + * "lib.sol:L1": "0x...", + * "lib.sol:L2": "0x...", + * "lib.sol": {"L3": "0x..."} + * } + */ +export interface LibraryAddresses { + [qualifiedNameOrSourceUnit: string]: string | { [unqualifiedLibraryName: string]: string }; +} + +/** + * A mapping between libraries and lists of placeholder instances present in their hex-encoded bytecode. + * For each placeholder its length and the position of the first character is stored. + * + * Each start and length entry will always directly refer to the position in + * binary and not hex-encoded bytecode. + */ +export interface LinkReferences { + [libraryLabel: string]: Array<{ start: number, length: number }>; +} diff --git a/linker.ts b/linker.ts index 2955b66f..619cae57 100644 --- a/linker.ts +++ b/linker.ts @@ -1,6 +1,7 @@ import assert from 'assert'; import { keccak256 } from 'js-sha3'; import { isNil, isObject } from './common/helpers'; +import { LibraryAddresses, LinkReferences } from './common/types'; /** * Generates a new-style library placeholder from a fully-qualified library name. @@ -57,12 +58,12 @@ function replacePlaceholder (bytecode, label, address) { * @returns bytecode Hex-encoded bytecode string with placeholders replaced with addresses. * Note that some placeholders may remain in the bytecode if `libraries` does not provide addresses for all of them. */ -function linkBytecode (bytecode, libraries) { +function linkBytecode (bytecode: string, libraries: LibraryAddresses): string { assert(typeof bytecode === 'string'); assert(typeof libraries === 'object'); // NOTE: for backwards compatibility support old compiler which didn't use file names - const librariesComplete = {}; + const librariesComplete: { [fullyQualifiedLibraryName: string]: string } = {}; for (const [fullyQualifiedLibraryName, libraryObjectOrAddress] of Object.entries(libraries)) { if (isNil(libraryObjectOrAddress)) { @@ -136,12 +137,12 @@ function linkBytecode (bytecode, libraries) { * offsets and lengths refer to the *binary* (not hex-encoded) bytecode, just * like in `evm.bytecode.linkReferences`. */ -function findLinkReferences (bytecode) { +function findLinkReferences (bytecode: string): LinkReferences { assert(typeof bytecode === 'string'); // find 40 bytes in the pattern of __...<36 digits>...__ // e.g. __Lib.sol:L_____________________________ - const linkReferences = {}; + const linkReferences: LinkReferences = {}; let offset = 0;