github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/types/signable.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	. "github.com/intfoundation/go-common"
     8  	"github.com/intfoundation/go-merkle"
     9  )
    10  
    11  // Signable is an interface for all signable things.
    12  // It typically removes signatures before serializing.
    13  type Signable interface {
    14  	WriteSignBytes(chainID string, w io.Writer, n *int, err *error)
    15  }
    16  
    17  // SignBytes is a convenience method for getting the bytes to sign of a Signable.
    18  func SignBytes(chainID string, o Signable) []byte {
    19  	buf, n, err := new(bytes.Buffer), new(int), new(error)
    20  	o.WriteSignBytes(chainID, buf, n, err)
    21  	if *err != nil {
    22  		PanicCrisis(err)
    23  	}
    24  	return buf.Bytes()
    25  }
    26  
    27  // HashSignBytes is a convenience method for getting the hash of the bytes of a signable
    28  func HashSignBytes(chainID string, o Signable) []byte {
    29  	return merkle.SimpleHashFromBinary(SignBytes(chainID, o))
    30  }