@@ -7,67 +7,71 @@ import (
7
7
_ "crypto/sha256"
8
8
// adds sha512 capability to crypto.SHA512
9
9
_ "crypto/sha512"
10
- fmt "fmt"
11
10
12
11
// adds ripemd160 capability to crypto.RIPEMD160
13
12
_ "golang.org/x/crypto/ripemd160"
13
+
14
+ "github.com/pkg/errors"
14
15
)
15
16
17
+ // Apply will calculate the leaf hash given the key and value being proven
16
18
func (op * LeafOp ) Apply (key []byte , value []byte ) ([]byte , error ) {
17
19
if len (key ) == 0 {
18
- return nil , fmt . Errorf ("Leaf op needs key" )
20
+ return nil , errors . New ("Leaf op needs key" )
19
21
}
20
22
if len (value ) == 0 {
21
- return nil , fmt . Errorf ("Leaf op needs value" )
23
+ return nil , errors . New ("Leaf op needs value" )
22
24
}
23
25
pkey , err := prepareLeafData (op .PrehashKey , op .Length , key )
24
26
if err != nil {
25
- return nil , err
27
+ return nil , errors . Wrap ( err , "prehash key" )
26
28
}
27
29
pvalue , err := prepareLeafData (op .PrehashValue , op .Length , value )
28
30
if err != nil {
29
- return nil , err
31
+ return nil , errors . Wrap ( err , "prehash value" )
30
32
}
31
33
data := append (op .Prefix , pkey ... )
32
34
data = append (data , pvalue ... )
33
35
return doHash (op .Hash , data )
34
36
}
35
37
38
+ // CheckAgainstSpec will verify the LeafOp is in the format defined in spec
36
39
func (op * LeafOp )CheckAgainstSpec (spec * ProofSpec ) error {
37
40
lspec := spec .LeafSpec
38
41
39
42
if op .Hash != lspec .Hash {
40
- return fmt .Errorf ("Unexpected HashOp: %d" , op .Hash )
43
+ return errors .Errorf ("Unexpected HashOp: %d" , op .Hash )
41
44
}
42
45
if op .PrehashKey != lspec .PrehashKey {
43
- return fmt .Errorf ("Unexpected PrehashKey: %d" , op .PrehashKey )
46
+ return errors .Errorf ("Unexpected PrehashKey: %d" , op .PrehashKey )
44
47
}
45
48
if op .PrehashValue != lspec .PrehashValue {
46
- return fmt .Errorf ("Unexpected PrehashValue: %d" , op .PrehashValue )
49
+ return errors .Errorf ("Unexpected PrehashValue: %d" , op .PrehashValue )
47
50
}
48
51
if op .Length != lspec .Length {
49
- return fmt .Errorf ("Unexpected LengthOp: %d" , op .Length )
52
+ return errors .Errorf ("Unexpected LengthOp: %d" , op .Length )
50
53
}
51
54
if ! bytes .HasPrefix (op .Prefix , lspec .Prefix ) {
52
- return fmt .Errorf ("Leaf Prefix doesn't start with %X" , lspec .Prefix )
55
+ return errors .Errorf ("Leaf Prefix doesn't start with %X" , lspec .Prefix )
53
56
}
54
57
return nil
55
58
}
56
59
57
-
60
+ // Apply will calculate the hash of the next step, given the hash of the previous step
58
61
func (op * InnerOp ) Apply (child []byte ) ([]byte , error ) {
59
62
if len (child ) == 0 {
60
- return nil , fmt .Errorf ("Inner op needs child value" )
63
+ return nil , errors .Errorf ("Inner op needs child value" )
61
64
}
62
65
preimage := append (op .Prefix , child ... )
63
66
preimage = append (preimage , op .Suffix ... )
64
67
return doHash (op .Hash , preimage )
65
68
}
66
69
67
- func (inner * InnerOp )CheckAgainstSpec (spec * ProofSpec ) error {
70
+ // CheckAgainstSpec will verify the InnerOp is in the format defined in spec
71
+ func (op * InnerOp )CheckAgainstSpec (spec * ProofSpec ) error {
68
72
leafPrefix := spec .LeafSpec .Prefix
69
- if bytes .HasPrefix (inner .Prefix , leafPrefix ) {
70
- return fmt .Errorf ("Inner Prefix starts with %X" , leafPrefix )
73
+ if bytes .HasPrefix (op .Prefix , leafPrefix ) {
74
+ return errors .Errorf ("Inner Prefix starts with %X" , leafPrefix )
71
75
}
72
76
return nil
73
77
}
@@ -117,7 +121,7 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
117
121
hash .Write (tmp )
118
122
return hash .Sum (nil ), nil
119
123
}
120
- return nil , fmt .Errorf ("Unsupported hashop: %d" , hashOp )
124
+ return nil , errors .Errorf ("Unsupported hashop: %d" , hashOp )
121
125
}
122
126
123
127
// doLengthOp will calculate the proper prefix and return it prepended
@@ -131,12 +135,12 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) {
131
135
return res , nil
132
136
case LengthOp_REQUIRE_32_BYTES :
133
137
if len (data ) != 32 {
134
- return nil , fmt .Errorf ("Data was %d bytes, not 32" , len (data ))
138
+ return nil , errors .Errorf ("Data was %d bytes, not 32" , len (data ))
135
139
}
136
140
return data , nil
137
141
case LengthOp_REQUIRE_64_BYTES :
138
142
if len (data ) != 64 {
139
- return nil , fmt .Errorf ("Data was %d bytes, not 64" , len (data ))
143
+ return nil , errors .Errorf ("Data was %d bytes, not 64" , len (data ))
140
144
}
141
145
return data , nil
142
146
// TODO
@@ -146,7 +150,7 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) {
146
150
// case LengthOp_FIXED32_LITTLE:
147
151
// case LengthOp_FIXED64_LITTLE:
148
152
}
149
- return nil , fmt .Errorf ("Unsupported lengthop: %d" , lengthOp )
153
+ return nil , errors .Errorf ("Unsupported lengthop: %d" , lengthOp )
150
154
}
151
155
152
156
func encodeVarintProto (l int ) []byte {
0 commit comments