github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/smartcontract/nef/method_token.go (about)

     1  package nef
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/io"
     8  	"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
     9  	"github.com/nspcc-dev/neo-go/pkg/util"
    10  )
    11  
    12  // maxMethodLength is the maximum length of method.
    13  const maxMethodLength = 32
    14  
    15  var (
    16  	errInvalidMethodName = errors.New("method name should't start with '_'")
    17  	errInvalidCallFlag   = errors.New("invalid call flag")
    18  )
    19  
    20  // MethodToken is contract method description.
    21  type MethodToken struct {
    22  	// Hash is contract hash.
    23  	Hash util.Uint160 `json:"hash"`
    24  	// Method is method name.
    25  	Method string `json:"method"`
    26  	// ParamCount is method parameter count.
    27  	ParamCount uint16 `json:"paramcount"`
    28  	// HasReturn is true if method returns value.
    29  	HasReturn bool `json:"hasreturnvalue"`
    30  	// CallFlag is a set of call flags the method will be called with.
    31  	CallFlag callflag.CallFlag `json:"callflags"`
    32  }
    33  
    34  // EncodeBinary implements io.Serializable.
    35  func (t *MethodToken) EncodeBinary(w *io.BinWriter) {
    36  	w.WriteBytes(t.Hash[:])
    37  	w.WriteString(t.Method)
    38  	w.WriteU16LE(t.ParamCount)
    39  	w.WriteBool(t.HasReturn)
    40  	w.WriteB(byte(t.CallFlag))
    41  }
    42  
    43  // DecodeBinary implements io.Serializable.
    44  func (t *MethodToken) DecodeBinary(r *io.BinReader) {
    45  	r.ReadBytes(t.Hash[:])
    46  	t.Method = r.ReadString(maxMethodLength)
    47  	if r.Err == nil && strings.HasPrefix(t.Method, "_") {
    48  		r.Err = errInvalidMethodName
    49  		return
    50  	}
    51  	t.ParamCount = r.ReadU16LE()
    52  	t.HasReturn = r.ReadBool()
    53  	t.CallFlag = callflag.CallFlag(r.ReadB())
    54  	if r.Err == nil && t.CallFlag&^callflag.All != 0 {
    55  		r.Err = errInvalidCallFlag
    56  	}
    57  }