0

Is there anyway or method to extract the address of a linked library from the contract bytecode (not a verified contract code) or deployment transaction?

my question is about step 3:

step 1. someone deploying the SafeMath.sol and getting the address as 0x1234........

step 2. someone else linking and deploying the Test.sol which use that library, the command will look something like this :

solc --abi --bin Test.sol --libraries SafeMath:0x1234........ -o bin

step 3. a third one go to etherscan and look at the bytecode of that deployed contract or at the deployment transaction

1

From solidity docs:

The EVM does not provide a direct way for a contract to detect whether it was called using CALL or not, but a contract can use the ADDRESS opcode to find out “where” it is currently running. [...]

More specifically, the runtime code of a library always starts with a push instruction, which is a zero of 20 bytes at compilation time. When the deploy code runs, this constant is replaced in memory by the current address and this modified code is stored in the contract. [...]


Library:

pragma solidity ^0.5.8;

library lib {

    function isZero (address _address) public pure returns (bool) {
        return _address == address(0x0);
    }
}

Contract:

pragma solidity ^0.5.8;

import "./lib.sol";

contract Importer {

    function doSom (address _address) public pure returns (bool) {
        require(lib.isZero(_address));
        return true;
    }   
}

You can see in the bytecode the PUSH20 instruction of a zero of 20 bytes:

...
JUMPDEST
PUSH1 0x0
PUSH20 0x0
...

But in Remix (Debug):

143 PUSH20 bbf289d846208c16edc8474705c748aff07732db

enter image description here

What you can do is look for a DELEGATECALL opcode in the bytecode and try to search the identifier of the function (in this case: isZero(address): 0x673C8BE). Only when you have knowledge about the method_id (signature) of the function, something easy with the SafeMath.sol library.

DELEGATECALL:

...
JUMPDEST
POP
GAS
DELEGATECALL
ISZERO
...

In Remix (Debug):

enter image description here

Function identifier:

...
PUSH20 0x0
PUSH4 0x673C8BE
DUP4
...

In Remix (Debug):

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.