Skip to content

Commit

Permalink
Merge pull request #130 from numary/feature/property-checks
Browse files Browse the repository at this point in the history
Add checks on source/destination/asset transaction properties.
  • Loading branch information
gfyrag authored Jan 20, 2022
2 parents 3eb1c27 + 941b508 commit a2a7a40
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/core/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package core

import "regexp"

func AssetIsValid(v string) bool {
re := regexp.MustCompile("[A-Z]{1,8}")
var assetRegexp = regexp.MustCompile("[A-Z]{1,16}(\\/\\d{1,6})?")

return re.Match([]byte(v))
func AssetIsValid(v string) bool {
return assetRegexp.Match([]byte(v))
}
8 changes: 8 additions & 0 deletions pkg/core/posting.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package core

import "regexp"

type Posting struct {
Source string `json:"source"`
Destination string `json:"destination"`
Expand All @@ -21,3 +23,9 @@ func (ps Postings) Reverse() {
ps[opp].Source, ps[opp].Destination = ps[opp].Destination, ps[opp].Source
}
}

var addressRegexp = regexp.MustCompile("^[a-zA-Z_0-9]+(:[a-zA-Z_0-9]+){0,}$")

func ValidateAddress(addr string) bool {
return addressRegexp.Match([]byte(addr))
}
9 changes: 9 additions & 0 deletions pkg/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ func (l *Ledger) Commit(ctx context.Context, ts []core.Transaction) ([]core.Tran
if p.Amount < 0 {
return ts, NewValidationError("negative amount")
}
if !core.ValidateAddress(p.Source) {
return nil, NewValidationError("invalid source address")
}
if !core.ValidateAddress(p.Destination) {
return nil, NewValidationError("invalid destination address")
}
if !core.AssetIsValid(p.Asset) {
return nil, NewValidationError("invalid asset")
}
if _, ok := rf[p.Source]; !ok {
rf[p.Source] = map[string]int64{}
}
Expand Down

0 comments on commit a2a7a40

Please sign in to comment.