/
dsowner.go
286 lines (266 loc) · 7.23 KB
/
dsowner.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package objs
import (
"bytes"
"github.com/MadBase/MadNet/errorz"
"github.com/MadBase/MadNet/constants"
"github.com/MadBase/MadNet/crypto"
"github.com/MadBase/MadNet/utils"
)
// DataStoreOwner is the struct which specifies the owner of the DataStore
type DataStoreOwner struct {
SVA SVA
CurveSpec constants.CurveSpec
Account []byte
}
// New makes a new DataStoreOwner
func (dso *DataStoreOwner) New(acct []byte, curveSpec constants.CurveSpec) {
dso.SVA = DataStoreSVA
dso.CurveSpec = curveSpec
dso.Account = utils.CopySlice(acct)
}
// NewFromOwner takes an Owner object and creates the corresponding
// DataStoreOwner
func (dso *DataStoreOwner) NewFromOwner(o *Owner) error {
if dso == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
if err := o.Validate(); err != nil {
return err
}
dso.New(o.Account, o.CurveSpec)
if err := dso.Validate(); err != nil {
dso.SVA = 0
dso.CurveSpec = 0
dso.Account = nil
return err
}
return nil
}
// MarshalBinary takes the DataStoreOwner object and returns the canonical
// byte slice
func (dso *DataStoreOwner) MarshalBinary() ([]byte, error) {
if err := dso.Validate(); err != nil {
return nil, err
}
owner := []byte{}
owner = append(owner, []byte{uint8(dso.SVA)}...)
owner = append(owner, []byte{uint8(dso.CurveSpec)}...)
owner = append(owner, utils.CopySlice(dso.Account)...)
return owner, nil
}
// Validate validates the DataStoreOwner
func (dso *DataStoreOwner) Validate() error {
if dso == nil {
return errorz.ErrInvalid{}.New("object is nil")
}
if err := dso.validateSVA(); err != nil {
return err
}
if err := dso.validateCurveSpec(); err != nil {
return err
}
if err := dso.validateAccount(); err != nil {
return err
}
return nil
}
// UnmarshalBinary takes a byte slice and returns the corresponding
// DataStoreOwner object
func (dso *DataStoreOwner) UnmarshalBinary(o []byte) error {
if dso == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
owner := utils.CopySlice(o)
sva, owner, err := extractSVA(owner)
if err != nil {
return err
}
curveSpec, owner, err := extractCurveSpec(owner)
if err != nil {
return err
}
account, owner, err := extractAccount(owner)
if err != nil {
return err
}
if err := extractZero(owner); err != nil {
return err
}
dso.SVA = sva
dso.CurveSpec = curveSpec
dso.Account = account
if err := dso.Validate(); err != nil {
return err
}
return nil
}
// ValidateSignature validates the DataStoreSignature
func (dso *DataStoreOwner) ValidateSignature(msg []byte, sig *DataStoreSignature, isExpired bool) error {
if err := dso.Validate(); err != nil {
return errorz.ErrInvalid{}.New("invalid DataStoreOwner")
}
if err := sig.Validate(); err != nil {
return errorz.ErrInvalid{}.New("invalid DataStoreSignature")
}
if !isExpired && sig.CurveSpec != dso.CurveSpec {
return errorz.ErrInvalid{}.New("unmatched curve spec")
}
switch sig.CurveSpec {
case constants.CurveSecp256k1:
val := crypto.Secp256k1Validator{}
pk, err := val.Validate(msg, sig.Signature)
if err != nil {
return err
}
if !isExpired {
account := crypto.GetAccount(pk)
if !bytes.Equal(account, dso.Account) {
return errorz.ErrInvalid{}.New("invalid sig for account")
}
}
return nil
case constants.CurveBN256Eth:
val := crypto.BNValidator{}
pk, err := val.Validate(msg, sig.Signature)
if err != nil {
return err
}
if !isExpired {
account := crypto.GetAccount(pk)
if !bytes.Equal(account, dso.Account) {
return errorz.ErrInvalid{}.New("invalid sig for account")
}
}
return nil
default:
return errorz.ErrInvalid{}.New("invalid curve spec")
}
}
func (dso *DataStoreOwner) validateCurveSpec() error {
if dso == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
if !(dso.CurveSpec == constants.CurveSecp256k1) && !(dso.CurveSpec == constants.CurveBN256Eth) {
return errorz.ErrInvalid{}.New("invalid curve spec for DataStoreOwner")
}
return nil
}
func (dso *DataStoreOwner) validateSVA() error {
if dso == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
if dso.SVA != DataStoreSVA {
return errorz.ErrInvalid{}.New("signature verification algorithm invalid for DataStoreOwner")
}
return nil
}
func (dso *DataStoreOwner) validateAccount() error {
if dso == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
if len(dso.Account) != constants.OwnerLen {
return errorz.ErrInvalid{}.New("account length wrong")
}
return nil
}
// Sign allows has the DataStoreOwner sign the message msg with signer s
func (dso *DataStoreOwner) Sign(msg []byte, s Signer) (*DataStoreSignature, error) {
sig := &DataStoreSignature{
SVA: DataStoreSVA,
}
switch s.(type) {
case *crypto.Secp256k1Signer:
sig.CurveSpec = constants.CurveSecp256k1
signature, err := s.Sign(msg)
if err != nil {
return nil, err
}
sig.Signature = signature
return sig, nil
case *crypto.BNSigner:
sig.CurveSpec = constants.CurveBN256Eth
signature, err := s.Sign(msg)
if err != nil {
return nil, err
}
sig.Signature = signature
return sig, nil
default:
return nil, errorz.ErrInvalid{}.New("invalid signer type in DataStoreOwner.Sign")
}
}
// DataStoreSignature ...
type DataStoreSignature struct {
SVA SVA
CurveSpec constants.CurveSpec
Signature []byte
}
// UnmarshalBinary takes a byte slice and returns the corresponding
// DataStoreSignature object
func (dss *DataStoreSignature) UnmarshalBinary(signature []byte) error {
if dss == nil {
return errorz.ErrInvalid{}.New("object is nil")
}
sva, signature, err := extractSVA(signature)
if err != nil {
return err
}
dss.SVA = sva
curveSpec, signature, err := extractCurveSpec(signature)
if err != nil {
return err
}
dss.CurveSpec = curveSpec
signature, null, err := extractSignature(signature, curveSpec)
if err != nil {
return err
}
dss.Signature = signature
if err := extractZero(null); err != nil {
return err
}
return dss.Validate()
}
// MarshalBinary takes the DataStoreSignature object and returns the canonical
// byte slice
func (dss *DataStoreSignature) MarshalBinary() ([]byte, error) {
if err := dss.Validate(); err != nil {
return nil, err
}
signature := []byte{}
signature = append(signature, []byte{uint8(dss.SVA)}...)
signature = append(signature, []byte{uint8(dss.CurveSpec)}...)
signature = append(signature, utils.CopySlice(dss.Signature)...)
return signature, nil
}
// Validate validates the DataStoreSignature object
func (dss *DataStoreSignature) Validate() error {
if dss == nil {
return errorz.ErrInvalid{}.New("object is nil")
}
if err := dss.validateSVA(); err != nil {
return err
}
if err := dss.validateCurveSpec(); err != nil {
return err
}
return validateSignatureLen(dss.Signature, dss.CurveSpec)
}
func (dss *DataStoreSignature) validateSVA() error {
if dss == nil {
return errorz.ErrInvalid{}.New("object is nil")
}
if dss.SVA != DataStoreSVA {
return errorz.ErrInvalid{}.New("signature verification algorithm invalid for DataStoreSignature")
}
return nil
}
func (dss *DataStoreSignature) validateCurveSpec() error {
if dss == nil {
return errorz.ErrInvalid{}.New("object is nil")
}
if !(dss.CurveSpec == constants.CurveSecp256k1) && !(dss.CurveSpec == constants.CurveBN256Eth) {
return errorz.ErrInvalid{}.New("invalid curve spec for DataStoreSignature")
}
return nil
}