forked from vuvuzela/vuvuzela
/
settings.go
67 lines (56 loc) · 1.61 KB
/
settings.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
// Copyright 2017 David Lazar. All rights reserved.
// Use of this source code is governed by the GNU AGPL
// license that can be found in the LICENSE file.
package mixnet
import (
"bytes"
"encoding/json"
"vuvuzela.io/alpenhorn/errors"
pb "vuvuzela.io/vuvuzela/mixnet/convopb"
)
type RoundSettings struct {
// Service is the name of the mixnet service.
Service string
// Round is the round that these settings correspond to.
Round uint32
// OnionKeys are the encryption keys in mixnet order.
OnionKeys []*[32]byte
// RawServiceData is the extra data required by the mixnet service.
RawServiceData []byte
// ServiceData is the result of parsing the RawServiceData;
// it is not included in the signing message.
ServiceData interface{} `json:"-"`
}
func (s RoundSettings) SigningMessage() []byte {
buf := new(bytes.Buffer)
buf.WriteString("RoundSettings")
json.NewEncoder(buf).Encode(s)
return buf.Bytes()
}
func (s *RoundSettings) FromProto(pbs *pb.RoundSettings) error {
s.Service = pbs.Service
s.Round = pbs.Round
s.OnionKeys = make([]*[32]byte, len(pbs.OnionKeys))
for i := range s.OnionKeys {
key := new([32]byte)
n := copy(key[:], pbs.OnionKeys[i])
if n != 32 {
return errors.New("wrong size for key %d: got %d, want %d", i, n, 32)
}
s.OnionKeys[i] = key
}
s.RawServiceData = pbs.ServiceData
return nil
}
func (s RoundSettings) Proto() *pb.RoundSettings {
pbs := &pb.RoundSettings{
Service: s.Service,
Round: s.Round,
OnionKeys: make([][]byte, len(s.OnionKeys)),
ServiceData: s.RawServiceData,
}
for i, key := range s.OnionKeys {
pbs.OnionKeys[i] = key[:]
}
return pbs
}