Skip to content

Commit

Permalink
Merge pull request #604 from stephensli/types/linker
Browse files Browse the repository at this point in the history
Provide type support to linker.ts
  • Loading branch information
cameel committed Mar 23, 2022
2 parents f54dabf + 6409860 commit 99eafc2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 26 additions & 0 deletions common/types.ts
Original file line number Diff line number Diff line change
@@ -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 }>;
}
9 changes: 5 additions & 4 deletions linker.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 99eafc2

Please sign in to comment.