github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/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/onflow/flow-go/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 HexToAddress(hexAddress string, chain flow.Chain) (flow.Address, error) {
    30  	if len(hexAddress) == 0 {
    31  		return flow.EmptyAddress, status.Error(codes.InvalidArgument, "address cannot be empty")
    32  	}
    33  
    34  	address := flow.HexToAddress(hexAddress)
    35  
    36  	if !chain.IsValid(address) {
    37  		return flow.EmptyAddress, status.Errorf(codes.InvalidArgument, "address %s is invalid for chain %s", address, chain)
    38  	}
    39  
    40  	return address, nil
    41  }
    42  
    43  func BlockID(blockID []byte) (flow.Identifier, error) {
    44  	if len(blockID) != flow.IdentifierLen {
    45  		return flow.ZeroID, status.Error(codes.InvalidArgument, "invalid block id")
    46  	}
    47  	return flow.HashToID(blockID), nil
    48  }
    49  
    50  func BlockIDs(blockIDs [][]byte) ([]flow.Identifier, error) {
    51  	if len(blockIDs) == 0 {
    52  		return nil, status.Error(codes.InvalidArgument, "empty block ids")
    53  	}
    54  	flowBlockIDs := make([]flow.Identifier, len(blockIDs))
    55  	for i, b := range blockIDs {
    56  		flowBlockID, err := BlockID(b)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		flowBlockIDs[i] = flowBlockID
    61  	}
    62  	return flowBlockIDs, nil
    63  }
    64  
    65  func CollectionID(collectionID []byte) (flow.Identifier, error) {
    66  	if len(collectionID) == 0 {
    67  		return flow.ZeroID, status.Error(codes.InvalidArgument, "invalid collection id")
    68  	}
    69  	return flow.HashToID(collectionID), nil
    70  }
    71  
    72  func EventType(eventType string) (string, error) {
    73  	if len(strings.TrimSpace(eventType)) == 0 {
    74  		return "", status.Error(codes.InvalidArgument, "invalid event type")
    75  	}
    76  	return eventType, nil
    77  }
    78  
    79  func TransactionID(txID []byte) (flow.Identifier, error) {
    80  	if len(txID) == 0 {
    81  		return flow.ZeroID, status.Error(codes.InvalidArgument, "invalid transaction id")
    82  	}
    83  	return flow.HashToID(txID), nil
    84  }