11

library ECVerify is deployed on 0x3bbb367afe5075e0461f535d6ed2a640822edb1c on the Ropsten test net.

How do I call function ecverify(bytes32 hash, bytes sig, address signer) returns (bool) {} in that library contract from a contract foo.

1 Answer 1

9

A library is similar to a contract and you can use an abstract contract to declare its interface and then invoke (like Calling function from deployed contract).

contract ECVerify {
    function ecverify(bytes32 hash, bytes sig, address signer) returns (bool);
}

contract Foo {
    ECVerify ecv = ECVerify(0x3bbb367afe5075e0461f535d6ed2a640822edb1c);

    function callEcv(bytes32 hash, bytes sig, address signer) {
        bool b = ecv.ecverify(hash, sig, signer);
        // handle b
    }
}

The main feature of a library is that its code is executed in the context of the calling contract. library ECVerify does not access storage so the simple code above works, but it treats and executes just like any non-library contract.

The other case is a library that accesses storage, then your Foo contract should have the storage defined correctly: the library will be writing to your contract's storage.

Solidity docs have an example of a library Set, which needs a mapping(uint => bool). This means that your contract should have this mapping. Whenever the library writes to the mapping, it writes to the mapping in your contract.

Here's:

library Set {
  // We define a new struct datatype that will be used to
  // hold its data in the calling contract.


  struct Data { mapping(uint => bool) flags; }
  ...

and an example of defining the mapping in Foo:

contract Foo {
    Set.Data knownValues;

Another example of linking and calling a library is given in What are the steps to compile and deploy a library in Solidity?

2
  • From your example above: how does the compiler know that ecv.ecverify(hash, sig, signer) is a library call and has to be executed as delegatecall?
    – ivicaa
    Oct 9, 2018 at 19:29
  • @ivicaa You're right delegatecall isn't used for ECVerify: delegatecall is not needed because ECVerify doesn't access storage. (As mentioned in this answer, the last link and the Set example, provide more details on how to link and call a library that accesses storage.)
    – eth
    Oct 22, 2018 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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