0

I'm using a contract as a template, of sorts, such that it is not deployed immediately with the rest of my contracts, but may be instantiated (multiple times) by a "contract factory," call it the Manager. So, for example, some contract or user calls a function Manager.createNewContract(data), which then creates a new Contract and saves its address in an array for further use....like this:

contract C { ... }

contract Manager {
    address[] contracts;

    constructor() { ... }

    function createNewContract(...) {
        C c = new C(...);
        contracts.push(address(c));
    }
}

Now, I'd like to impose the restriction that only the Manager may create a new C, and that C should throw if any other address attempts to instantiate, like the onlyOwner modifier from the Ownable contract (open-zeppelin). Is there any way to do this? I'm thinking something like a permissions contract that is inherited by C, but then each C gets its own copy so there's no central governing system. If anything needs to be clarified, let me know.

0

Just to elaborate on @smarx's answer, there's nothing you can do to prevent someone from copying code and deploying it, so you have to focus on the interactions.

you can consider only the contracts created by createNewContract to be "valid."

In practical terms, you might do something like this:

contract Manager is Ownable {

    address[] public contracts;
    mapping(address => bool) public isTrusted;

    constructor() { ... }

    modifier onlyTrustedContracts {
        require(isTrusted[msg.sender]);
        _;
    }

    function createNewContract(...) onlyOwner {
        C c = new C(...);
        contracts.push(address(c));
        isTrusted[c] = true;
    }

    // Now you can guard access.

    function sensitive() onlyTrustedContracts ... {}
}

Obviously, too many loose ends to actually compile, but I hope it's illustrative.

Hope it helps.

  • Having a concrete example definitely helped clear it up in my head. Thank you! – confusedpigeon Jul 7 '18 at 1:08
  • Gotta spread the love. And reputation. – confusedpigeon Jul 7 '18 at 1:17
0

You can certainly put permissions on createNewContract, but you can't put any meaningful permissions in C itself. Anyone could just copy/paste the code and remove that check. But there's no reason to care; you can consider only the contracts created by createNewContract to be "valid."

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.