4
4
"bytes"
5
5
"crypto"
6
6
"encoding/binary"
7
+ "errors"
8
+ "fmt"
7
9
8
10
// adds sha256 capability to crypto.SHA256
9
11
_ "crypto/sha256"
@@ -12,8 +14,6 @@ import (
12
14
13
15
// adds ripemd160 capability to crypto.RIPEMD160
14
16
_ "golang.org/x/crypto/ripemd160"
15
-
16
- "github.com/pkg/errors"
17
17
)
18
18
19
19
// Apply will calculate the leaf hash given the key and value being proven
@@ -26,11 +26,11 @@ func (op *LeafOp) Apply(key []byte, value []byte) ([]byte, error) {
26
26
}
27
27
pkey , err := prepareLeafData (op .PrehashKey , op .Length , key )
28
28
if err != nil {
29
- return nil , errors . Wrap ( err , "prehash key" )
29
+ return nil , fmt . Errorf ( "prehash key, %w" , err )
30
30
}
31
31
pvalue , err := prepareLeafData (op .PrehashValue , op .Length , value )
32
32
if err != nil {
33
- return nil , errors . Wrap ( err , "prehash value" )
33
+ return nil , fmt . Errorf ( "prehash value, %w" , err )
34
34
}
35
35
data := append (op .Prefix , pkey ... )
36
36
data = append (data , pvalue ... )
@@ -42,27 +42,27 @@ func (op *LeafOp) CheckAgainstSpec(spec *ProofSpec) error {
42
42
lspec := spec .LeafSpec
43
43
44
44
if op .Hash != lspec .Hash {
45
- return errors .Errorf ("Unexpected HashOp: %d" , op .Hash )
45
+ return fmt .Errorf ("Unexpected HashOp: %d" , op .Hash )
46
46
}
47
47
if op .PrehashKey != lspec .PrehashKey {
48
- return errors .Errorf ("Unexpected PrehashKey: %d" , op .PrehashKey )
48
+ return fmt .Errorf ("Unexpected PrehashKey: %d" , op .PrehashKey )
49
49
}
50
50
if op .PrehashValue != lspec .PrehashValue {
51
- return errors .Errorf ("Unexpected PrehashValue: %d" , op .PrehashValue )
51
+ return fmt .Errorf ("Unexpected PrehashValue: %d" , op .PrehashValue )
52
52
}
53
53
if op .Length != lspec .Length {
54
- return errors .Errorf ("Unexpected LengthOp: %d" , op .Length )
54
+ return fmt .Errorf ("Unexpected LengthOp: %d" , op .Length )
55
55
}
56
56
if ! bytes .HasPrefix (op .Prefix , lspec .Prefix ) {
57
- return errors .Errorf ("Leaf Prefix doesn't start with %X" , lspec .Prefix )
57
+ return fmt .Errorf ("Leaf Prefix doesn't start with %X" , lspec .Prefix )
58
58
}
59
59
return nil
60
60
}
61
61
62
62
// Apply will calculate the hash of the next step, given the hash of the previous step
63
63
func (op * InnerOp ) Apply (child []byte ) ([]byte , error ) {
64
64
if len (child ) == 0 {
65
- return nil , errors .Errorf ("Inner op needs child value" )
65
+ return nil , errors .New ("Inner op needs child value" )
66
66
}
67
67
preimage := append (op .Prefix , child ... )
68
68
preimage = append (preimage , op .Suffix ... )
@@ -72,19 +72,19 @@ func (op *InnerOp) Apply(child []byte) ([]byte, error) {
72
72
// CheckAgainstSpec will verify the InnerOp is in the format defined in spec
73
73
func (op * InnerOp ) CheckAgainstSpec (spec * ProofSpec ) error {
74
74
if op .Hash != spec .InnerSpec .Hash {
75
- return errors .Errorf ("Unexpected HashOp: %d" , op .Hash )
75
+ return fmt .Errorf ("Unexpected HashOp: %d" , op .Hash )
76
76
}
77
77
78
78
leafPrefix := spec .LeafSpec .Prefix
79
79
if bytes .HasPrefix (op .Prefix , leafPrefix ) {
80
- return errors .Errorf ("Inner Prefix starts with %X" , leafPrefix )
80
+ return fmt .Errorf ("Inner Prefix starts with %X" , leafPrefix )
81
81
}
82
82
if len (op .Prefix ) < int (spec .InnerSpec .MinPrefixLength ) {
83
- return errors .Errorf ("InnerOp prefix too short (%d)" , len (op .Prefix ))
83
+ return fmt .Errorf ("InnerOp prefix too short (%d)" , len (op .Prefix ))
84
84
}
85
85
maxLeftChildBytes := (len (spec .InnerSpec .ChildOrder ) - 1 ) * int (spec .InnerSpec .ChildSize )
86
86
if len (op .Prefix ) > int (spec .InnerSpec .MaxPrefixLength )+ maxLeftChildBytes {
87
- return errors .Errorf ("InnerOp prefix too long (%d)" , len (op .Prefix ))
87
+ return fmt .Errorf ("InnerOp prefix too long (%d)" , len (op .Prefix ))
88
88
}
89
89
return nil
90
90
}
@@ -137,11 +137,12 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
137
137
hash .Write (preimage )
138
138
return hash .Sum (nil ), nil
139
139
}
140
- return nil , errors .Errorf ("Unsupported hashop: %d" , hashOp )
140
+ return nil , fmt .Errorf ("Unsupported hashop: %d" , hashOp )
141
141
}
142
142
143
143
// doLengthOp will calculate the proper prefix and return it prepended
144
- // doLengthOp(op, data) -> length(data) || data
144
+ //
145
+ // doLengthOp(op, data) -> length(data) || data
145
146
func doLengthOp (lengthOp LengthOp , data []byte ) ([]byte , error ) {
146
147
switch lengthOp {
147
148
case LengthOp_NO_PREFIX :
@@ -151,12 +152,12 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) {
151
152
return res , nil
152
153
case LengthOp_REQUIRE_32_BYTES :
153
154
if len (data ) != 32 {
154
- return nil , errors .Errorf ("Data was %d bytes, not 32" , len (data ))
155
+ return nil , fmt .Errorf ("Data was %d bytes, not 32" , len (data ))
155
156
}
156
157
return data , nil
157
158
case LengthOp_REQUIRE_64_BYTES :
158
159
if len (data ) != 64 {
159
- return nil , errors .Errorf ("Data was %d bytes, not 64" , len (data ))
160
+ return nil , fmt .Errorf ("Data was %d bytes, not 64" , len (data ))
160
161
}
161
162
return data , nil
162
163
case LengthOp_FIXED32_LITTLE :
@@ -170,7 +171,7 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) {
170
171
// case LengthOp_FIXED64_BIG:
171
172
// case LengthOp_FIXED64_LITTLE:
172
173
}
173
- return nil , errors .Errorf ("Unsupported lengthop: %d" , lengthOp )
174
+ return nil , fmt .Errorf ("Unsupported lengthop: %d" , lengthOp )
174
175
}
175
176
176
177
func encodeVarintProto (l int ) []byte {
0 commit comments