0

Let us look at the following contract

contract LibraryA {
    function getSenderAddress() public returns (address); 
    }

contract LibraryB {
    function getSenderAddress() public returns (address); 
    }

contract UseCase {
    function checkToken(LibraryA a) public returns (address)
        {return a.getSenderAddress();}}

How to make sure that invocation of function checkToken only uses the address of LibraryA? Is hardcoding the address a good idea? like function checkToken(0xab3242443c23424523 a) ? What is the design rationale for this design in solidity? Are there any legitimate use cases? How to ensure the use of the library the developer intended to use?

0

If there's a single contract possible for a to an existing LibraryA instance I'd use a constant in the contract.

If you have two fixed addresses that the user can choose from. I'd hardcode the addresses and use an enum for the user to be easy to choose from.

contract UseCase {

    LibraryA libA = LibraryA(0x1234123412341234123412341234123412341234);
    LibraryB libB = LibraryB(0x0012341234123412341234123412341234123412);

    enum Type { Magic, MoreMagic }

    function checkToken(Type type) public returns (address) {
        if (type == Type.Magic) {
            return libA.getSenderAddress();
        } else {
            return libB.getSenderAddress();
        }
    }
}

If addresses are dynamic, so that you cannot hardcode them one possibility is to use a whitelist so the user cannot call an arbitrary address. But it really depends on the particular task you want to solve.

    mapping (address => bool) enabled;

    function checkToken(address a) public returns (address) {
        require(enabled[a], "Addresss not enabled");

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.