/
convert.go
58 lines (50 loc) 路 1.46 KB
/
convert.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 chain
import (
"fmt"
"io"
"time"
json "github.com/nikkolasg/hexjson"
"github.com/drand/drand/key"
"github.com/drand/drand/protobuf/drand"
)
// InfoFromProto returns a Info from the protocol description
func InfoFromProto(p *drand.ChainInfoPacket) (*Info, error) {
public := key.KeyGroup.Point()
if err := public.UnmarshalBinary(p.PublicKey); err != nil {
return nil, err
}
return &Info{
PublicKey: public,
GenesisTime: p.GenesisTime,
Period: time.Duration(p.Period) * time.Second,
GroupHash: p.GroupHash,
}, nil
}
// ToProto returns the protobuf description of the chain info
func (c *Info) ToProto() *drand.ChainInfoPacket {
buff, _ := c.PublicKey.MarshalBinary()
return &drand.ChainInfoPacket{
PublicKey: buff,
GenesisTime: c.GenesisTime,
Period: uint32(c.Period.Seconds()),
Hash: c.Hash(),
GroupHash: c.GroupHash,
}
}
// InfoFromJSON returns a Info from JSON description in the given reader
func InfoFromJSON(buff io.Reader) (*Info, error) {
chainProto := new(drand.ChainInfoPacket)
if err := json.NewDecoder(buff).Decode(chainProto); err != nil {
return nil, fmt.Errorf("reading group file (%v)", err)
}
chainInfo, err := InfoFromProto(chainProto)
if err != nil {
return nil, fmt.Errorf("invalid chain info: %s", err)
}
return chainInfo, nil
}
// ToJSON provides a json serialization of an info packet
func (c *Info) ToJSON(w io.Writer) error {
info := c.ToProto()
return json.NewEncoder(w).Encode(info)
}