27

Could I spend the money a contract owns if I had the private key that goes to the public address that a contract resides at? (I realize that I'd have a better chance of being struck by lightning several times in a row for years.) But if you forced it in code somehow as a test, would it actually allow you to spend the money a contract owns?

Or is the Ethereum core strict enough that it treats these as two separate things and it could never happen?

2
  • 1
    How can you generate the private key?
    – lixq
    Commented May 22, 2018 at 12:31
  • 1
    @lixq Wouldn't you like to know. Commented Jun 3, 2021 at 19:49

1 Answer 1

33

From my reading of the Yellow Paper (YP), Yes you would be able to spend the funds of the contract. But as you note, it is with extremely low probability of being able to find a contract's private key.

Spending money in Ethereum is done via a CALL opcode, passing it parameters such as the sender, recipient, value (being spent). Since you have the private key, you can create a valid signature for the transaction, which the network will accept. There is no special check on the sender apart from the validity of the signature. Below are the checks in the YP, and the others are easy to satisfy (for example, an account's transaction nonce is easily looked up).

  1. The transaction is well-formed RLP, with no ad- ditional trailing bytes.
  2. The transaction signature is valid.
  3. The transaction nonce is valid (equivalent to the sender account’s current nonce).
  4. The gas limit is no smaller than the intrinsic gas, g0, used by the transaction.
  5. The sender account balance contains at least the cost, v0, required in up-front payment.

EDIT June 2021: EIP-3607 has been drafted so that it would not be possible to spend a contract's funds using a private key: such transactions would be rejected as invalid.

3
  • 1
    according to your answer here there are no keys associated with a contract account address. Then how can a private key end up generating the same address as a contract's one that is created via "The sender and nonce are RLP encoded and then hashed with Keccak-256"?
    – ehsan0x
    Commented Apr 22, 2019 at 16:14
  • 3
    Usually contracts do not have keys, but it's technically possible to find that key by brute-forcing. Contracts and wallets (externally owned accounts) share the exact same address space, in that they produce addresses in the same range of numbers, so it's possible, but astronomically unlikely, to find the key for a contract by iterating over private keys until the wallet address matches the contract address. You could also do the opposite, iterating over the owner of a contract until the resulting contract would have an address the same as an existing wallet.
    – ihatecsv
    Commented Aug 1, 2019 at 16:23
  • 3
    @the0roamer The cost of brute-forcing is "1 year, of Bitcoin mining. This is $10 billion in June 2021."
    – eth
    Commented Jun 26, 2021 at 6:11

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.