forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activateproducer.go
79 lines (63 loc) · 1.72 KB
/
activateproducer.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
// Copyright (c) 2017-2019 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//
package payload
import (
"bytes"
"errors"
"io"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/crypto"
)
const (
ActivateProducerVersion byte = 0x00
)
type ActivateProducer struct {
NodePublicKey []byte
Signature []byte
}
func (a *ActivateProducer) Data(version byte) []byte {
buf := new(bytes.Buffer)
if err := a.Serialize(buf, version); err != nil {
return []byte{0}
}
return buf.Bytes()
}
func (a *ActivateProducer) Serialize(w io.Writer, version byte) error {
err := a.SerializeUnsigned(w, version)
if err != nil {
return err
}
err = common.WriteVarBytes(w, a.Signature)
if err != nil {
return errors.New("[ActivateProducer], signature serialize failed")
}
return nil
}
func (a *ActivateProducer) SerializeUnsigned(w io.Writer, version byte) error {
err := common.WriteVarBytes(w, a.NodePublicKey)
if err != nil {
return errors.New("[ActivateProducer], write owner public key failed")
}
return nil
}
func (a *ActivateProducer) Deserialize(r io.Reader, version byte) error {
err := a.DeserializeUnsigned(r, version)
if err != nil {
return err
}
a.Signature, err = common.ReadVarBytes(r, crypto.SignatureLength, "signature")
if err != nil {
return errors.New("[ActivateProducer], signature deserialize failed")
}
return nil
}
func (a *ActivateProducer) DeserializeUnsigned(r io.Reader, version byte) error {
var err error
a.NodePublicKey, err = common.ReadVarBytes(r, crypto.NegativeBigLength, "public key")
if err != nil {
return errors.New("[ActivateProducer], read owner public key failed")
}
return err
}