0

I understand that smart contracts:

  • Don't have private keys
  • They can hold, receive and spend ether

How does the latter work ? Let's say I want to send 1 eth to a smart contract and leave it locked up in there until person B calls some function on the same contract. How does the smart contract hold my 1 eth and how does it get freed up again upon receiving the signed transaction from person B ?

1

This is going to be simplified, to try and illustrate.

Any account can call any function of any smart contract by sending a transaction that calls a function. When you send such a "function call" transaction, the code in that function "comes to life" and executes itself.

Typically, the first thing that the function does is to check if the account calling it is authorized to call it. And if it isn't, the function fails / rolls back the transaction (by calling "revert").

In your example, imagine the following flow:

  1. You (or anyone) creates the contract by loading it onto the chain (in a "create" transaction). You add Address B to the contract as an authorized withdrawer.
  2. You send 1 ETH to the contract. The contract now has a balance of 1 ETH.
  3. Account B calls your custom "withdraw" function on the contract. The withdraw function first checks to make sure Account B is authorized to withdraw, then uses the built-in solidity "send" function to send ether to Address B.

Solidity abstracts away the transaction's signature, so you can always trust the value of msg.sender.

Like I said this is pretty oversimplified so there are a couple of important small technical things glossed over, but it gives you the basic gist.

3
  • so basically, you don't need a private key to spend the ETH in the smart contract account, right ? any code executing inside the smart contract can spend as much of its own account as it wants, and it's up to the developer to make sure his/her own code only spends funds when it is really desired (e.g. the msg.sender has the correct public key) – Chris Nov 23 '20 at 14:44
  • 1
    @Chris Correct. The code has authority to spend as much eth as it wants, the code itself decides when it's authorized to. – Maximillian Laumeister Nov 23 '20 at 21:52
  • Awesome, thanks ! – Chris Nov 24 '20 at 17:22

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.