-
Notifications
You must be signed in to change notification settings - Fork 179
/
packer.go
55 lines (47 loc) · 1.67 KB
/
packer.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
package packer
import (
"bytes"
"fmt"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/encoding/rlp"
)
// SigDataPacker implements logic for encoding/decoding SignatureData using RLP encoding.
type SigDataPacker struct {
codec rlp.Codec // rlp encoder is used in order to ensure deterministic encoding
}
// SignatureData is a compact data type for encoding the block signature data
type SignatureData struct {
// bit-vector indicating type of signature for each signer.
// the order of each sig type matches the order of corresponding signer IDs
SigType []byte
AggregatedStakingSig []byte
AggregatedRandomBeaconSig []byte
ReconstructedRandomBeaconSig crypto.Signature
}
// Encode performs encoding of SignatureData
func (p *SigDataPacker) Encode(sigData *SignatureData) ([]byte, error) {
var buf bytes.Buffer
encoder := p.codec.NewEncoder(&buf)
err := encoder.Encode(sigData)
return buf.Bytes(), err
}
// Decode performs decoding of SignatureData
func (p *SigDataPacker) Decode(data []byte) (*SignatureData, error) {
bs := bytes.NewReader(data)
decoder := p.codec.NewDecoder(bs)
var sigData SignatureData
err := decoder.Decode(&sigData)
return &sigData, err
}
// UnpackRandomBeaconSig takes sigData previously packed by packer,
// decodes it and extracts random beacon signature
func UnpackRandomBeaconSig(sigData []byte) (crypto.Signature, error) {
// decode into typed data
packer := SigDataPacker{}
sig, err := packer.Decode(sigData)
if err != nil {
return nil, fmt.Errorf("could not decode sig data %s: %w", err, model.ErrInvalidFormat)
}
return sig.ReconstructedRandomBeaconSig, nil
}