forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
historicKey.go
85 lines (66 loc) · 1.68 KB
/
historicKey.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package identity
import (
"fmt"
"os"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/common/primitives/random"
)
type HistoricKey struct {
ActiveDBHeight uint32
SigningKey primitives.PublicKey
}
var _ interfaces.BinaryMarshallable = (*HistoricKey)(nil)
func RandomHistoricKey() *HistoricKey {
hk := new(HistoricKey)
hk.ActiveDBHeight = random.RandUInt32()
hk.SigningKey = *primitives.RandomPrivateKey().Pub
return hk
}
func (e *HistoricKey) IsSameAs(b *HistoricKey) bool {
if e.ActiveDBHeight != b.ActiveDBHeight {
return false
}
if e.SigningKey.IsSameAs(&b.SigningKey) == false {
return false
}
return true
}
func (e *HistoricKey) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "HistoricKey.MarshalBinary err:%v", *pe)
}
}(&err)
buf := primitives.NewBuffer(nil)
err = buf.PushUInt32(e.ActiveDBHeight)
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(&e.SigningKey)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (e *HistoricKey) UnmarshalBinaryData(p []byte) (newData []byte, err error) {
newData = p
buf := primitives.NewBuffer(p)
e.ActiveDBHeight, err = buf.PopUInt32()
if err != nil {
return
}
err = buf.PopBinaryMarshallable(&e.SigningKey)
if err != nil {
return
}
newData = buf.DeepCopyBytes()
return
}
func (e *HistoricKey) UnmarshalBinary(p []byte) error {
_, err := e.UnmarshalBinaryData(p)
return err
}