Code Monkey home page Code Monkey logo

ghost-contract's Introduction

Ghost Contract

                                               ██████
                                           ████▒▒▒▒▒▒████
                                         ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒██
                                       ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██
                                     ██▒▒▒▒▒▒▒▒    ▒▒▒▒▒▒▒▒
                                     ██▒▒▒▒▒▒  ▒▒▓▓▒▒▒▒▒▒  ▓▓▓▓
                                     ██▒▒▒▒▒▒  ▒▒▓▓▒▒▒▒▒▒  ▒▒▓▓
                                   ██▒▒▒▒▒▒▒▒▒▒    ▒▒▒▒▒▒▒▒    ██
                                   ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██
                                   ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██
                                   ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██
                                   ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██
                                   ██▒▒██▒▒▒▒▒▒██▒▒▒▒▒▒▒▒██▒▒▒▒██
                                   ████  ██▒▒██  ██▒▒▒▒██  ██▒▒██
                                   ██      ██      ████      ████

The Ghost Contract implements a minimalistic way to execute a payload while making the codesize of the msg.sender appear to be zero in the context of the targeted contract.

I had originally come across this idea when searching for a way to trustlessly verify if the msg.sender was a smart contract or an EOA. One approach that has been previously attempted was to check the code size of the sender. If the code size is greater than 0, it is a contract, however if the code size is 0, this does not necessarily mean that the msg.sender is an EOA. Contracts that rely on this logic for security are vulnerable to exploits.

As demonstrated in this repo, a contract can use the CREATE opcode and execute an arbitrary payload, which can call another contract, transfer funds or anything else that a normal contract can do. When using this approach to call another contract, because the execution of this logic is within the constructor, the codesize of the msg.sender is 0.

Below is a quick look at the Ghost contract.

contract Ghost {
    /// @notice This function takes a bytecode payload that will execute during the use of the CREATE opcode
    /// @dev Since the payload executes during deployment, if the payload calls an external contract, the msg.sender is rendered as the zero address.
    function sendGhostTransaction(bytes calldata payload) public returns (bool) {

        assembly {
            /// @notice Copy the payload into memory so that it can be passed in as deployment bytecode
            calldatacopy(0x80, payload.offset, payload.length)

            /// @notice Deploy a new account executing the payload on deployment
            let deployedAddress := create(0, 0x80, payload.length)

            /// @notice If the deployment failed, return false
            if iszero(deployedAddress){
                mstore(0x00, false)
                return(0x00, 0x20)
            }

            /// @notice return a bool to indicate the ghost transaction was a success
            mstore(0x00, true)
            return(0x00, 0x20)
        }
    }
}

ghost-contract's People

Contributors

0xkitsune avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.