Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICS 24 Implementation #5229

Merged
merged 4 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions x/ibc/23-commitment/types/merkle.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package types

import (
"fmt"
"strings"

"github.com/tendermint/tendermint/crypto/merkle"

"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported"
validation "github.com/cosmos/cosmos-sdk/x/ibc/24-validation"
)

// ICS 023 Merkle Types Implementation
Expand Down Expand Up @@ -104,6 +106,9 @@ func (p Path) String() string {
// CONTRACT: provided path string MUST be a well formated path. See ICS24 for
// reference.
func ApplyPrefix(prefix exported.PrefixI, path string) Path {
if !validation.DefaultPathValidator(path) {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
panic(fmt.Sprintf("Path: %s is malformed", path))
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}
// Split paths by the separator
pathSlice := strings.Split(path, "/")
commitmentPath := NewPath(pathSlice)
Expand Down
61 changes: 61 additions & 0 deletions x/ibc/24-validation/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package validation
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

import (
"regexp"
"strings"
)

// regular expression to check string is lowercase alphabetic characters only
var isAlphaLower = regexp.MustCompile(`^[a-z]+$`).MatchString

// regular expression to check string is alphanumeric
var isAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString

// Validator function type to validate path and identifier bytestrings
type Validator func(string) bool
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

// Default validator function for Client, Connection, and Channel
// identifiers
// Valid Identifier must be between 10-20 characters and only
// contain lowercase alphabetic characters
func DefaultIdentifierValidator(id string) bool {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
// valid id must be between 10 and 20 characters
if len(id) < 10 || len(id) > 20 {
return false
}
// valid id must contain only lower alphabetic characters
if !isAlphaLower(string(id)) {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
return false
}
return true
}

// NewPathValidator takes in a Identifier Validator function and returns
// a Path Validator function which requires path only has valid identifiers
// alphanumeric character strings, and "/" separators
func NewPathValidator(idValidator Validator) Validator {
return func(path string) bool {
pathArr := strings.Split(path, "/")
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
for _, p := range pathArr {
// Each path element must either be valid identifier or alphanumeric
if !idValidator(p) && !isAlphaNumeric(p) {
return false
}
}
return true
}
}

// Default Path Validator takes in path string and validates
// with default identifier rules. This is optimized by simply
// checking that all path elements are alphanumeric
func DefaultPathValidator(path string) bool {
pathArr := strings.Split(path, "/")
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
for _, p := range pathArr {
// Each path element must either be alphanumeric
if !isAlphaNumeric(p) {
return false
}
}
return true
}