Skip to content

Commit

Permalink
Validate tx arg datatypes in StartupCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
bandreghetti committed Jun 9, 2021
1 parent 73b8487 commit f8c713b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 8 additions & 3 deletions assets/dataType.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func CustomDataTypes(m map[string]DataType) error {
return errors.NewCCError(fmt.Sprintf("invalid custom data type '%s': nil Parse function", k), 500)
}

dataTypeMap[k] = v
dataTypeMap[k] = &v
}
return nil
}
Expand All @@ -41,13 +41,18 @@ func CustomDataTypes(m map[string]DataType) error {
func DataTypeMap() map[string]DataType {
ret := map[string]DataType{}
for k, v := range dataTypeMap {
ret[k] = v
ret[k] = *v
}
return ret
}

// FetchDataType returns a pointer to the DataType object or nil if asset type is not found.
func FetchDataType(dataTypeTag string) *DataType {
return dataTypeMap[dataTypeTag]
}

// dataTypeMap contains the "standard" primitive data types
var dataTypeMap = map[string]DataType{
var dataTypeMap = map[string]*DataType{
"string": {
AcceptedFormats: []string{"string"},
Parse: func(data interface{}) (string, interface{}, errors.ICCError) {
Expand Down
22 changes: 22 additions & 0 deletions transactions/startupCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package transactions
import (
"fmt"
"regexp"
"strings"

"github.com/goledgerdev/cc-tools/assets"
"github.com/goledgerdev/cc-tools/errors"
)

Expand All @@ -23,6 +25,26 @@ func StartupCheck() errors.ICCError {
}
}
}

for _, arg := range tx.Args {
dtype := strings.TrimPrefix(arg.DataType, "[]")
if dtype != "@asset" &&
dtype != "@key" &&
dtype != "@update" &&
dtype != "@query" &&
dtype != "@object" {
if strings.HasPrefix(dtype, "->") {
dtype = strings.TrimPrefix(dtype, "->")
if assets.FetchAssetType(dtype) == nil {
return errors.NewCCError(fmt.Sprintf("invalid arg type %s", arg.DataType), 500)
}
} else {
if assets.FetchDataType(dtype) == nil {
return errors.NewCCError(fmt.Sprintf("invalid arg type %s", arg.DataType), 500)
}
}
}
}
}
return nil
}

0 comments on commit f8c713b

Please sign in to comment.