github.com/koko1123/flow-go-1@v0.29.6/consensus/hotstuff/model/signature_data.go (about)

     1  package model
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/onflow/flow-go/crypto"
     7  	"github.com/koko1123/flow-go-1/model/encoding/rlp"
     8  )
     9  
    10  // SigDataPacker implements logic for encoding/decoding SignatureData using RLP encoding.
    11  type SigDataPacker struct {
    12  	codec rlp.Codec // rlp encoder is used in order to ensure deterministic encoding
    13  }
    14  
    15  // SignatureData is a compact data type for encoding the block signature data
    16  type SignatureData struct {
    17  	// bit-vector indicating type of signature for each signer.
    18  	// the order of each sig type matches the order of corresponding signer IDs
    19  	SigType []byte
    20  
    21  	AggregatedStakingSig         []byte
    22  	AggregatedRandomBeaconSig    []byte
    23  	ReconstructedRandomBeaconSig crypto.Signature
    24  }
    25  
    26  // Encode performs encoding of SignatureData
    27  func (p *SigDataPacker) Encode(sigData *SignatureData) ([]byte, error) {
    28  	var buf bytes.Buffer
    29  	encoder := p.codec.NewEncoder(&buf)
    30  	err := encoder.Encode(sigData)
    31  	return buf.Bytes(), err
    32  }
    33  
    34  // Decode performs decoding of SignatureData
    35  func (p *SigDataPacker) Decode(data []byte) (*SignatureData, error) {
    36  	bs := bytes.NewReader(data)
    37  	decoder := p.codec.NewDecoder(bs)
    38  	var sigData SignatureData
    39  	err := decoder.Decode(&sigData)
    40  	return &sigData, err
    41  }
    42  
    43  // UnpackRandomBeaconSig takes sigData previously packed by packer,
    44  // decodes it and extracts random beacon signature
    45  func UnpackRandomBeaconSig(sigData []byte) (crypto.Signature, error) {
    46  	// decode into typed data
    47  	packer := SigDataPacker{}
    48  	sig, err := packer.Decode(sigData)
    49  	if err != nil {
    50  		return nil, NewInvalidFormatErrorf("could not decode sig data: %w", err)
    51  	}
    52  	return sig.ReconstructedRandomBeaconSig, nil
    53  }