/
validation.go
79 lines (69 loc) · 2.35 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package types
import (
"fmt"
"net/url"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/docker/distribution/reference"
)
// MaxSaltSize is the longest salt that can be used when instantiating a contract
const MaxSaltSize = 64
var (
// MaxLabelSize is the longest label that can be used when instantiating a contract
MaxLabelSize = 128 // extension point for chains to customize via compile flag.
// MaxWasmSize is the largest a compiled contract code can be when storing code on chain
MaxWasmSize = 800 * 1024 // extension point for chains to customize via compile flag.
// MaxProposalWasmSize is the largest a gov proposal compiled contract code can be when storing code on chain
MaxProposalWasmSize = 3 * 1024 * 1024 // extension point for chains to customize via compile flag.
)
func validateWasmCode(s []byte, maxSize int) error {
if len(s) == 0 {
return sdkerrors.Wrap(ErrEmpty, "is required")
}
if len(s) > maxSize {
return sdkerrors.Wrapf(ErrLimit, "cannot be longer than %d bytes", maxSize)
}
return nil
}
// ValidateLabel ensure label constraints
func ValidateLabel(label string) error {
if label == "" {
return sdkerrors.Wrap(ErrEmpty, "is required")
}
if len(label) > MaxLabelSize {
return ErrLimit.Wrapf("cannot be longer than %d characters", MaxLabelSize)
}
return nil
}
// ValidateSalt ensure salt constraints
func ValidateSalt(salt []byte) error {
switch n := len(salt); {
case n == 0:
return sdkerrors.Wrap(ErrEmpty, "is required")
case n > MaxSaltSize:
return ErrLimit.Wrapf("cannot be longer than %d characters", MaxSaltSize)
}
return nil
}
// ValidateVerificationInfo ensure source, builder and checksum constraints
func ValidateVerificationInfo(source, builder string, codeHash []byte) error {
// if any set require others to be set
if len(source) != 0 || len(builder) != 0 || codeHash != nil {
if source == "" {
return fmt.Errorf("source is required")
}
if _, err := url.ParseRequestURI(source); err != nil {
return fmt.Errorf("source: %s", err)
}
if builder == "" {
return fmt.Errorf("builder is required")
}
if _, err := reference.ParseDockerRef(builder); err != nil {
return fmt.Errorf("builder: %s", err)
}
if codeHash == nil {
return fmt.Errorf("code hash is required")
}
// code hash checksum match validation is done in the keeper, ungzipping consumes gas
}
return nil
}