github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/precompiles/selector.go (about)

     1  package precompiles
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	gethCrypto "github.com/onflow/go-ethereum/crypto"
     8  )
     9  
    10  const FunctionSelectorLength = 4
    11  
    12  // This is derived as the first 4 bytes of the Keccak hash of the ASCII form of the signature of the method
    13  type FunctionSelector [FunctionSelectorLength]byte
    14  
    15  func (fs FunctionSelector) Bytes() []byte {
    16  	return fs[:]
    17  }
    18  
    19  // ComputeFunctionSelector computes the function selector
    20  // given the canonical name of function and args.
    21  // for example the canonical format for int is int256
    22  func ComputeFunctionSelector(name string, args []string) FunctionSelector {
    23  	var sig FunctionSelector
    24  	input := fmt.Sprintf("%v(%v)", name, strings.Join(args, ","))
    25  	copy(sig[0:FunctionSelectorLength], gethCrypto.Keccak256([]byte(input))[:FunctionSelectorLength])
    26  	return sig
    27  }
    28  
    29  // SplitFunctionSelector splits the function signature from input data and
    30  // returns the rest of the data
    31  func SplitFunctionSelector(input []byte) (FunctionSelector, []byte) {
    32  	var funcSig FunctionSelector
    33  	copy(funcSig[:], input[0:FunctionSelectorLength])
    34  	return funcSig, input[FunctionSelectorLength:]
    35  }