0

Please how can I add the intervention of a 3rd account that approves the execution of a function?

Example : Bob wants to execute function F, but it won't be executed unless Alice comes in and checks the data sent and approves it on the smart contract.

3 Answers 3

0

You could have something like :

contract Example{
  // Those are the parameters of the function to execute
  struct Params{
    uint a;
    uint b;
  }
  // We use a mapping(uint => ) because it's essentially impossible to delete an 
  // entry from an array in solidity while keeping the elements in-place. 
  mapping(uint => Params) public queued;
  uint nextToQueue;
  // This contains the logic to run
  function funcToRun(uint a, uint b) internal {
    //Logic
  }
  // This is the function that Bob calls, it sends Bob an index that they can submit to Alice for review
  function queueTransaction(uint a, uint b) public returns(uint index) {
    index = nextToQueue++; //NB: this stores the current value of nextToQueue in index and then increments it, as opposed to ++nextToQueue which would do it the other way around (not what we want here)
    Params memory toQueue = new Params(a, b);
    queued[index] = toQueue;
  }
  // This is the function Alice (the owner) calls to actually run the transaction 
  function executeTransaction(uint index) public onlyOwner {
    Params memory toExecute = queued[index];
    require(toExecute.a != 0 || toExecute.b != 0, "invalid index"); // An uninitialized slot in the mapping would return all 0s. Problem, if we need 0 to be a valid parameter a workaround is needed. In a real world case, `Params` would most likely include Bob's address anyways, so you can check for just that
    funcToRun(toExecute.a, toExecute.b);
    delete queued[index];
  }
}

If any of this is unclear to you, feel free to ask questions in comments.

2
  • thank you all for your answers. The problem is that I want the approval to be instantaneous and done automatically with a script and the mnemonic phrase. Jun 21, 2023 at 9:52
  • With @Ethan approchach, send the data to a backend, verify it, generate the signature, send it back to the user so they can call execute. With my approach, have queueTransaction emit an event, catch that event in the backend, verify the data and call executeTransaction (not instantaneous, there is gonna be a few blocks delay between queueTransaction and executeTransaction
    – Foxxxey
    Jun 21, 2023 at 10:00
0

Alice need signature the data, function will execute successfully.

verify signature demo: https://solidity-by-example.org/signature

function excute(bytes calldata data, bytes calldata signature){
    _verify(data, signature);
    _excute(data);
}
1
  • thank you all for your answers. The problem is that I want the approval to be instantaneous and done automatically with a script and the mnemonic phrase. Jun 21, 2023 at 9:51
0

You can implement a modifier in your smart contract that requires the approval of a third account before a function can be executed.

contract MyContract {
  address public owner;
  address public alice;
  address public bob;
  bool public approved;
   constructor(address _alice, address _bob) {
    owner = msg.sender;
    alice = _alice;
    bob = _bob;
  }
   modifier onlyApproved() {
    require(msg.sender == bob && approved, "Function can only be executed by Bob after approval from Alice");
    _;
  }
   function F() public onlyApproved {
    // Function code goes here
  }
   function approve() public {
    require(msg.sender == alice, "Approval can only be given by Alice");
    approved = true;
  }
}
1
  • thank you all for your answers. The problem is that I want the approval to be instantaneous and done automatically with a script and the mnemonic phrase. Jun 21, 2023 at 9:52

Your Answer

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

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