/
proposal_file.go
121 lines (94 loc) 路 2.4 KB
/
proposal_file.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package drand
import (
"encoding/hex"
"github.com/BurntSushi/toml"
drand "github.com/drand/drand/v2/protobuf/dkg"
)
type ProposalFileFormat struct {
Joining []*TomlParticipant
Leaving []*TomlParticipant
Remaining []*TomlParticipant
}
type ProposalFile struct {
Joining []*drand.Participant
Leaving []*drand.Participant
Remaining []*drand.Participant
}
func ParseProposalFile(filepath string) (*ProposalFile, error) {
proposalFile := ProposalFileFormat{}
_, err := toml.DecodeFile(filepath, &proposalFile)
if err != nil {
return nil, err
}
return &ProposalFile{
Joining: proposalFile.Joiners(),
Leaving: proposalFile.Leavers(),
Remaining: proposalFile.Remainers(),
}, nil
}
func (p *ProposalFileFormat) Joiners() []*drand.Participant {
out := make([]*drand.Participant, len(p.Joining))
for i, participant := range p.Joining {
out[i] = participant.Into()
}
return out
}
func (p *ProposalFileFormat) Leavers() []*drand.Participant {
out := make([]*drand.Participant, len(p.Leaving))
for i, participant := range p.Leaving {
out[i] = participant.Into()
}
return out
}
func (p *ProposalFileFormat) Remainers() []*drand.Participant {
out := make([]*drand.Participant, len(p.Remaining))
for i, participant := range p.Remaining {
out[i] = participant.Into()
}
return out
}
type TomlParticipant struct {
Address string
Key string
Signature string
}
func (t *TomlParticipant) Into() *drand.Participant {
return &drand.Participant{
Address: t.Address,
Key: decodeHexOrPanic(t.Key),
Signature: decodeHexOrPanic(t.Signature),
}
}
func (p *ProposalFile) TOML() ProposalFileFormat {
out := ProposalFileFormat{}
for _, j := range p.Joining {
p := encode(j)
out.Joining = append(out.Joining, &p)
}
for _, r := range p.Remaining {
p := encode(r)
out.Remaining = append(out.Remaining, &p)
}
for _, l := range p.Leaving {
p := encode(l)
out.Leaving = append(out.Leaving, &p)
}
return out
}
func encode(p *drand.Participant) TomlParticipant {
return TomlParticipant{
Address: p.Address,
Key: hex.EncodeToString(p.Key),
Signature: hex.EncodeToString(p.Signature),
}
}
func decodeHexOrPanic(input string) []byte {
// input lengths are u8 instead of hex, so /2
byteLength := len(input) / 2
out := make([]byte, byteLength)
_, err := hex.Decode(out, []byte(input))
if err != nil {
panic("Invalid hex in proposal file!")
}
return out
}