-
Notifications
You must be signed in to change notification settings - Fork 19
/
data_signer.go
614 lines (517 loc) · 12.9 KB
/
data_signer.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
package common
import (
"encoding/hex"
"fmt"
"strings"
"code.vegaprotocol.io/vega/core/datasource/errors"
"code.vegaprotocol.io/vega/libs/stringer"
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
)
// PubKey.
type PubKey struct {
Key string
}
func (p PubKey) IntoProto() *datapb.PubKey {
return &datapb.PubKey{
Key: p.Key,
}
}
func (p PubKey) String() string {
return fmt.Sprintf(
"pubKey(%s)",
p.Key,
)
}
func (p PubKey) DeepClone() *PubKey {
return &PubKey{
Key: p.Key,
}
}
type SignerPubKey struct {
PubKey *PubKey
}
func (s SignerPubKey) String() string {
return fmt.Sprintf(
"signerPubKey(%s)",
stringer.PtrToString(s.PubKey),
)
}
func (s SignerPubKey) IntoProto() *datapb.Signer_PubKey {
pubKey := &datapb.PubKey{}
if s.PubKey != nil {
pubKey = s.PubKey.IntoProto()
}
return &datapb.Signer_PubKey{
PubKey: pubKey,
}
}
func (s SignerPubKey) oneOfProto() interface{} {
return s.IntoProto()
}
func (s SignerPubKey) GetSignerType() SignerType {
return SignerTypePubKey
}
func (s SignerPubKey) DeepClone() signer {
if s.PubKey == nil {
return &SignerPubKey{}
}
return &SignerPubKey{
PubKey: s.PubKey,
}
}
func (s SignerPubKey) IsEmpty() bool {
if s.PubKey == nil {
return true
}
return s.PubKey.Key == ""
}
func (s SignerPubKey) AsHex(prepend bool) (signer, error) {
if s.PubKey == nil {
return &s, errors.ErrSignerIsEmpty
}
if s.PubKey.Key == "" {
return nil, errors.ErrSignerIsEmpty
}
// Check if the content is already hex encoded
if strings.HasPrefix(s.PubKey.Key, "0x") {
return &s, nil
}
validHex, _ := isHex(s.PubKey.Key)
if validHex {
if prepend {
s.PubKey.Key = fmt.Sprintf("0x%s", s.PubKey.Key)
}
return &s, nil
}
// If the content is not a valid Hex - encode it
s.PubKey.Key = fmt.Sprintf("0x%s", hex.EncodeToString([]byte(s.PubKey.Key)))
return &s, nil
}
func (s SignerPubKey) AsString() (signer, error) {
if s.PubKey == nil {
return nil, errors.ErrSignerIsEmpty
}
// Check if the content is hex encoded
st := strings.TrimPrefix(s.PubKey.Key, "0x")
validHex, _ := isHex(st)
if validHex {
decoded, err := hex.DecodeString(st)
if err != nil {
return &s, fmt.Errorf("error decoding signer: %v", err)
}
s.PubKey.Key = string(decoded)
}
return &s, nil
}
func (s SignerPubKey) Serialize() []byte {
c := strings.TrimPrefix(s.PubKey.Key, "0x")
return append([]byte{byte(SignerPubKeyPrepend)}, []byte(c)...)
}
func DeserializePubKey(data []byte) *SignerPubKey {
return &SignerPubKey{
PubKey: &PubKey{
Key: string(data),
},
}
}
func PubKeyFromProto(s *datapb.Signer_PubKey) *SignerPubKey {
var pubKey *PubKey
if s != nil {
if s.PubKey != nil {
pubKey = &PubKey{
Key: s.PubKey.Key,
}
}
}
return &SignerPubKey{
PubKey: pubKey,
}
}
// ETHAddress.
type ETHAddress struct {
Address string
}
func (e ETHAddress) IntoProto() *datapb.ETHAddress {
return &datapb.ETHAddress{
Address: e.Address,
}
}
func (e ETHAddress) String() string {
return fmt.Sprintf(
"ethAddress(%s)",
e.Address,
)
}
func (e ETHAddress) DeepClone() *ETHAddress {
return ÐAddress{
Address: e.Address,
}
}
type SignerETHAddress struct {
ETHAddress *ETHAddress
}
func (s SignerETHAddress) String() string {
return fmt.Sprintf(
"signerETHAddress(%s)",
stringer.PtrToString(s.ETHAddress),
)
}
func (s SignerETHAddress) IntoProto() *datapb.Signer_EthAddress {
ethAddress := &datapb.ETHAddress{}
if s.ETHAddress != nil {
ethAddress = s.ETHAddress.IntoProto()
}
return &datapb.Signer_EthAddress{
EthAddress: ethAddress,
}
}
func (s SignerETHAddress) oneOfProto() interface{} {
return s.IntoProto()
}
func (s SignerETHAddress) GetSignerType() SignerType {
return SignerTypeEthAddress
}
func (s SignerETHAddress) DeepClone() signer {
if s.ETHAddress == nil {
return &SignerETHAddress{}
}
return &SignerETHAddress{
ETHAddress: s.ETHAddress,
}
}
func (s SignerETHAddress) IsEmpty() bool {
if s.ETHAddress == nil {
return true
}
return s.ETHAddress.Address == ""
}
func (s SignerETHAddress) AsHex(prepend bool) (signer, error) {
if s.ETHAddress == nil {
return nil, errors.ErrSignerIsEmpty
}
if s.ETHAddress.Address == "" {
return nil, errors.ErrSignerIsEmpty
}
// Check if the content is already hex encoded
if strings.HasPrefix(s.ETHAddress.Address, "0x") {
return &s, nil
}
validHex, _ := isHex(s.ETHAddress.Address)
if validHex {
if prepend {
s.ETHAddress.Address = fmt.Sprintf("0x%s", s.ETHAddress.Address)
}
return &s, nil
}
s.ETHAddress.Address = fmt.Sprintf("0x%s", hex.EncodeToString([]byte(s.ETHAddress.Address)))
return &s, nil
}
func (s SignerETHAddress) AsString() (signer, error) {
if s.ETHAddress == nil {
return nil, errors.ErrSignerIsEmpty
}
// Check if the content is hex encoded
st := strings.TrimPrefix(s.ETHAddress.Address, "0x")
validHex, _ := isHex(st)
if validHex {
decoded, err := hex.DecodeString(st)
if err != nil {
return &s, fmt.Errorf("error decoding signer: %v", err)
}
s.ETHAddress.Address = string(decoded)
}
return &s, nil
}
func (s SignerETHAddress) Serialize() []byte {
c := strings.TrimPrefix(s.ETHAddress.Address, "0x")
return append([]byte{byte(ETHAddressPrepend)}, []byte(c)...)
}
func DeserializeETHAddress(data []byte) *SignerETHAddress {
return &SignerETHAddress{
ETHAddress: ÐAddress{
Address: "0x" + string(data),
},
}
}
func ETHAddressFromProto(s *datapb.Signer_EthAddress) *SignerETHAddress {
var ethAddress *ETHAddress
if s != nil {
if s.EthAddress != nil {
ethAddress = ÐAddress{
Address: s.EthAddress.Address,
}
}
}
return &SignerETHAddress{
ETHAddress: ethAddress,
}
}
type SignerType int
const (
SignerTypeUnspecified SignerType = iota
SignerTypePubKey
SignerTypeEthAddress
)
type Signer struct {
Signer signer
}
func (s Signer) oneOfProto() interface{} {
return s.IntoProto()
}
// IntoProto will always return a `datapb.Signer` that needs to be checked
// if it has any content afterwards.
func (s Signer) IntoProto() *datapb.Signer {
signer := &datapb.Signer{}
if s.Signer != nil {
sig := s.Signer.oneOfProto()
switch tp := sig.(type) {
case *datapb.Signer_PubKey:
signer.Signer = tp
case *datapb.Signer_EthAddress:
signer.Signer = tp
}
}
return signer
}
func (s Signer) DeepClone() *Signer {
cpy := s
cpy.Signer = s.Signer.DeepClone()
return &cpy
}
func (s Signer) String() string {
return stringer.ObjToString(s.Signer)
}
func (s Signer) IsEmpty() bool {
if s.Signer != nil {
return s.Signer.IsEmpty()
}
return true
}
func (s Signer) GetSignerPubKey() *PubKey {
if s.Signer != nil {
switch t := s.Signer.(type) {
case *SignerPubKey:
return t.PubKey
}
}
return nil
}
func (s Signer) GetSignerETHAddress() *ETHAddress {
if s.Signer != nil {
switch t := s.Signer.(type) {
case *SignerETHAddress:
return t.ETHAddress
}
}
return nil
}
func (s Signer) GetSignerType() SignerType {
if s.Signer != nil {
switch s.Signer.(type) {
case *SignerPubKey:
return SignerTypePubKey
case *SignerETHAddress:
return SignerTypeEthAddress
}
}
return SignerTypeUnspecified
}
func SignerFromProto(s *datapb.Signer) *Signer {
signer := &Signer{}
if s.Signer != nil {
switch t := s.Signer.(type) {
case *datapb.Signer_PubKey:
signer.Signer = PubKeyFromProto(t)
case *datapb.Signer_EthAddress:
signer.Signer = ETHAddressFromProto(t)
}
}
return signer
}
func CreateSignerFromString(s string, t SignerType) *Signer {
signer := &Signer{}
switch t {
case SignerTypePubKey:
signer.Signer = &SignerPubKey{PubKey: &PubKey{s}}
case SignerTypeEthAddress:
signer.Signer = &SignerETHAddress{ETHAddress: ÐAddress{s}}
}
return signer
}
// SignersIntoProto returns a list of signers after checking the list length.
func SignersIntoProto(s []*Signer) []*datapb.Signer {
protoSigners := []*datapb.Signer{}
if len(s) > 0 {
protoSigners = make([]*datapb.Signer, len(s))
for i, signer := range s {
if signer != nil {
sign := signer.oneOfProto()
protoSigners[i] = sign.(*datapb.Signer)
}
}
}
return protoSigners
}
func SignersToStringList(s []*Signer) []string {
var signers []string
if len(s) > 0 {
for _, signer := range s {
if signer != nil {
signers = append(signers, signer.String())
}
}
return signers
}
return signers
}
// SignersFromProto returns a list of signers.
// The list is allowed to be empty.
func SignersFromProto(s []*datapb.Signer) []*Signer {
signers := []*Signer{}
if len(s) > 0 {
signers = make([]*Signer, len(s))
for i, signer := range s {
if s != nil {
signers[i] = SignerFromProto(signer)
}
}
return signers
}
return signers
}
// Encoding and decoding options
const (
SignerPubKeyPrepend = 0x00
ETHAddressPrepend = 0x01
)
// SignerAsHex represents the signer as a hex encoded string.
// We export this function as a standalone option because there are cases when we are not sure
// what is the signer type we deal with.
func SignerAsHex(signer *Signer) (*Signer, error) {
switch signer.GetSignerType() {
case SignerTypePubKey:
if signer.Signer != nil {
s, err := signer.Signer.(*SignerPubKey).AsHex(false)
return &Signer{s}, err
}
return nil, errors.ErrSignerIsEmpty
case SignerTypeEthAddress:
if signer.Signer != nil {
s, err := signer.Signer.(*SignerETHAddress).AsHex(false)
return &Signer{s}, err
}
return nil, errors.ErrSignerIsEmpty
}
// If the signer type is not among the ones we know, we do not care to
// encode or decode it.
return nil, errors.ErrSignerUnknownType
}
// SignerAsString represents the Signer content as a string.
func SignerAsString(signer *Signer) (*Signer, error) {
switch signer.GetSignerType() {
case SignerTypePubKey:
if signer.Signer != nil {
s, err := signer.Signer.(*SignerPubKey).AsString()
return &Signer{s}, err
}
return nil, errors.ErrSignerIsEmpty
case SignerTypeEthAddress:
if signer.Signer != nil {
s, err := signer.Signer.(*SignerETHAddress).AsString()
return &Signer{s}, err
}
return nil, errors.ErrSignerIsEmpty
}
// If the signer type is not among the ones we know, we do not care to
// encode or decode it.
return nil, errors.ErrSignerUnknownType
}
func isHex(src string) (bool, error) {
dst := make([]byte, hex.DecodedLen(len(src)))
if _, err := hex.Decode(dst, []byte(src)); err != nil {
return false, fmt.Errorf("string is not a valid hex: %v", err)
}
return true, nil
}
// Serialization and deserialization
// SerializeSigner deserializes the signer to a byte slice - we use that
// type top insert it into the database.
// The deserialization prepends the slice with two bytes as signer type indicator -
// that is used when the Signer is serialized back.
func (s *Signer) Serialize() ([]byte, error) {
switch s.GetSignerType() {
case SignerTypePubKey:
if s.Signer != nil {
pk, _ := s.Signer.(*SignerPubKey)
if pk.PubKey != nil {
return pk.Serialize(), nil
}
}
return nil, errors.ErrSignerIsEmpty
case SignerTypeEthAddress:
if s.Signer != nil {
ea, _ := s.Signer.(*SignerETHAddress)
if ea.ETHAddress != nil {
return ea.Serialize(), nil
}
}
return nil, errors.ErrSignerIsEmpty
}
// If the signer type is not among the ones we know, we do not care to
// encode or decode it.
return nil, errors.ErrSignerUnknownType
}
func DeserializeSigner(content []byte) *Signer {
if len(content) > 0 {
switch content[0] {
case SignerPubKeyPrepend:
return &Signer{
Signer: DeserializePubKey(content[1:]),
}
case ETHAddressPrepend:
return &Signer{
Signer: DeserializeETHAddress(content[1:]),
}
}
}
// If the signer type is not among the ones we know, we do not care to
// encode or decode it.
return &Signer{Signer: nil}
}
func NewSigner(t SignerType) *Signer {
switch t {
case SignerTypePubKey:
return &Signer{
Signer: &SignerPubKey{},
}
case SignerTypeEthAddress:
return &Signer{
Signer: &SignerETHAddress{},
}
}
// No indication if the given type is unknown.
return nil
}
type SpecSigners []*Signer
func (s SpecSigners) String() string {
allSigners := []string{}
for _, signer := range s {
allSigners = append(allSigners, signer.String())
}
return "[" + strings.Join(allSigners, ", ") + "]"
}