1

Is there any way I can automatically test who can withdraw ether from a smart contract? I need to perform this test automatically. So I cannot manually inspect the instructions.

Can I design a smart contract in such a way that it becomes easy to test? I want to ensure that only the creator of the smart contract can withdraw ether through one specific function. I can easily test that this function exists, but how do I automatically test that no other function exists through which the creator (or any other person) of the smart contract can withdraw ether?

Update:

I am aware how to design a smart contract such that only the creator can withdraw ether. However, I need to automatically test who can withdraw ether from a smart contract that is given to me.

1

A smart contract can have lots of functions, so to achieve what you want you would have to test each of them independently. You would have to think about all possible inputs that can be given to the functions.

Basic TypeScript example in a mocha test suite:

describe("my exhaustive test suite", function() {
  it("checks that foo doesn't change the contract balance", async function() {
    const currentBalance: BigNumber = ...
    await myContract.foo();
    const balanceAfterContractCall: BigNumber = ... 
    expect(currentBalance).to.equal(balanceAfterContractCall);
  });
});

Note that in the example above, the test doesn't check specifically that foo doesn't allow withdrawals - it only checks equalities of balances. The contract caller could be allowed to withdraw, but immediately send an equivalent amount back. To thoroughly check this you would have to eavesdrop on internal transactions.

In any case, what you're probably looking for is formal verification and symbolic execution, not automated ways to check that a function does or does not do something. This is advanced security tech that can mathematically prove that a program does what it says it does. Refer to awesome-ethereum-security for more details.

| improve this answer | |
0

Create a private address variable owner. In your constructor assign your address as owner. Create a modifier called isOwner. Use this modifier on the withdraw function, this will check automatically if the person withdrawing the amount is owner or not.

This should be easy to test in truffle.

how do I automatically test that no other function exists through which the creator (or any other person) of the smart contract can withdraw ether?

Just don't make any such function. If you have then use the modifier.

Hope it helps.

| improve this answer | |
  • Thanks. To clarify: I am not always the creator of the contract. So I must test if such other withdraw methods exist. Simply not creating such a function does not solve the problem. Hence, your answer unfortunately does not answer my question. – NoMorePen Dec 30 '17 at 18:32

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.