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

     1  package trigger
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  //go:generate stringer -type=Type -output=trigger_type_string.go
     9  
    10  // Type represents a trigger type used in C# reference node: https://github.com/neo-project/neo/blob/c64748ecbac3baeb8045b16af0d518398a6ced24/neo/SmartContract/TriggerType.cs#L3
    11  type Type byte
    12  
    13  // Viable list of supported trigger type constants.
    14  const (
    15  	// OnPersist is a trigger type that indicates that the script is being invoked
    16  	// internally by the system during block persistence (before transaction
    17  	// processing).
    18  	OnPersist Type = 0x01
    19  
    20  	// PostPersist is a trigger type that indicates that the script is being invoked
    21  	// by the system after block persistence (transcation processing) has
    22  	// finished.
    23  	PostPersist Type = 0x02
    24  
    25  	// The verification trigger indicates that the contract is being invoked as a verification function.
    26  	// The verification function can accept multiple parameters and should return a boolean value that indicates the validity of the transaction or block.
    27  	// The entry point of the contract will be invoked if the contract is triggered by Verification:
    28  	//     main(...);
    29  	// The entry point of the contract must be able to handle this type of invocation.
    30  	Verification Type = 0x20
    31  
    32  	// The application trigger indicates that the contract is being invoked as an application function.
    33  	// The application function can accept multiple parameters, change the states of the blockchain, and return any type of value.
    34  	// The contract can have any form of entry point, but we recommend that all contracts should have the following entry point:
    35  	//     public byte[] main(string operation, params object[] args)
    36  	// The functions can be invoked by creating an InvocationTransaction.
    37  	Application Type = 0x40
    38  
    39  	// All represents any trigger type.
    40  	All Type = OnPersist | PostPersist | Verification | Application
    41  )
    42  
    43  // FromString converts a string to the trigger Type.
    44  func FromString(str string) (Type, error) {
    45  	triggers := []Type{OnPersist, PostPersist, Verification, Application, All}
    46  	str = strings.ToLower(str)
    47  	for _, t := range triggers {
    48  		if strings.ToLower(t.String()) == str {
    49  			return t, nil
    50  		}
    51  	}
    52  	return 0, fmt.Errorf("unknown trigger type: %s", str)
    53  }