-
Notifications
You must be signed in to change notification settings - Fork 1
/
accessors_core_dkg_private_key.go
58 lines (51 loc) · 1.43 KB
/
accessors_core_dkg_private_key.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
package rawdb
import (
"bytes"
coreDKG "github.com/dexon-foundation/dexon-consensus/core/crypto/dkg"
"github.com/dexon-foundation/dexon/log"
"github.com/dexon-foundation/dexon/rlp"
)
type dkgPrivateKey struct {
PK *coreDKG.PrivateKey
Reset uint64
}
func ReadCoreDKGPrivateKeyRLP(db DatabaseReader, round uint64) rlp.RawValue {
data, _ := db.Get(coreDKGPrivateKeyKey(round))
return data
}
func WriteCoreDKGPrivateKeyRLP(db DatabaseWriter, round uint64, rlp rlp.RawValue) error {
err := db.Put(coreDKGPrivateKeyKey(round), rlp)
if err != nil {
log.Crit("Failed to store core DKG private key", "err", err, "round", round)
}
return err
}
func ReadCoreDKGPrivateKey(db DatabaseReader, round, reset uint64) *coreDKG.PrivateKey {
data := ReadCoreDKGPrivateKeyRLP(db, round)
if len(data) == 0 {
return nil
}
key := &dkgPrivateKey{
PK: new(coreDKG.PrivateKey),
}
if err := rlp.Decode(bytes.NewReader(data), key); err != nil {
log.Error("Invalid core DKG private key RLP", "round", round, "err", err)
return nil
}
if key.Reset != reset {
return nil
}
return key.PK
}
func WriteCoreDKGPrivateKey(db DatabaseWriter, round, reset uint64, pk *coreDKG.PrivateKey) error {
key := &dkgPrivateKey{
PK: pk,
Reset: reset,
}
data, err := rlp.EncodeToBytes(key)
if err != nil {
log.Crit("Failed to RLP encode core DKG private key", "round", round, "err", err)
return err
}
return WriteCoreDKGPrivateKeyRLP(db, round, data)
}