aboutsummaryrefslogtreecommitdiffstats
path: root/core/rawdb/accessors_core_dkg_private_key.go
blob: ac51ca5ecb6e0fdcbec7514356bbf876dd61d704 (plain) (blame)
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
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"
)

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 HasCoreDKGPrivateKey(db DatabaseReader, round uint64) (bool, error) {
    return db.Has(coreDKGPrivateKeyKey(round))
}

func ReadCoreDKGPrivateKey(db DatabaseReader, round uint64) *coreDKG.PrivateKey {
    data := ReadCoreDKGPrivateKeyRLP(db, round)
    if len(data) == 0 {
        return nil
    }
    key := 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
    }
    return key
}

func WriteCoreDKGPrivateKey(db DatabaseWriter, round uint64, key *coreDKG.PrivateKey) error {
    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)
}