github.com/koko1123/flow-go-1@v0.29.6/engine/common/rpc/convert/validate.go (about)

     1  package convert
     2  
     3  import (
     4  	"strings"
     5  
     6  	"google.golang.org/grpc/codes"
     7  	"google.golang.org/grpc/status"
     8  
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  )
    11  
    12  // Common validation and conversion for incoming Access API requests
    13  
    14  // Address validates the input address and returns a Flow address if valid and error otherwise
    15  func Address(rawAddress []byte, chain flow.Chain) (flow.Address, error) {
    16  	if len(rawAddress) == 0 {
    17  		return flow.EmptyAddress, status.Error(codes.InvalidArgument, "address cannot be empty")
    18  	}
    19  
    20  	address := flow.BytesToAddress(rawAddress)
    21  
    22  	if !chain.IsValid(address) {
    23  		return flow.EmptyAddress, status.Errorf(codes.InvalidArgument, "address %s is invalid for chain %s", address, chain)
    24  	}
    25  
    26  	return address, nil
    27  }
    28  
    29  func BlockID(blockID []byte) (flow.Identifier, error) {
    30  	if len(blockID) != flow.IdentifierLen {
    31  		return flow.ZeroID, status.Error(codes.InvalidArgument, "invalid block id")
    32  	}
    33  	return flow.HashToID(blockID), nil
    34  }
    35  
    36  func BlockIDs(blockIDs [][]byte) ([]flow.Identifier, error) {
    37  	if len(blockIDs) == 0 {
    38  		return nil, status.Error(codes.InvalidArgument, "empty block ids")
    39  	}
    40  	flowBlockIDs := make([]flow.Identifier, len(blockIDs))
    41  	for i, b := range blockIDs {
    42  		flowBlockID, err := BlockID(b)
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  		flowBlockIDs[i] = flowBlockID
    47  	}
    48  	return flowBlockIDs, nil
    49  }
    50  
    51  func CollectionID(collectionID []byte) (flow.Identifier, error) {
    52  	if len(collectionID) == 0 {
    53  		return flow.ZeroID, status.Error(codes.InvalidArgument, "invalid collection id")
    54  	}
    55  	return flow.HashToID(collectionID), nil
    56  }
    57  
    58  func EventType(eventType string) (string, error) {
    59  	if len(strings.TrimSpace(eventType)) == 0 {
    60  		return "", status.Error(codes.InvalidArgument, "invalid event type")
    61  	}
    62  	return eventType, nil
    63  }
    64  
    65  func TransactionID(txID []byte) (flow.Identifier, error) {
    66  	if len(txID) == 0 {
    67  		return flow.ZeroID, status.Error(codes.InvalidArgument, "invalid transaction id")
    68  	}
    69  	return flow.HashToID(txID), nil
    70  }