github.com/opzlabs/tendermint@v0.34.27-terra.rc.2/types/canonical.go (about) 1 package types 2 3 import ( 4 "time" 5 6 cmtproto "github.com/tendermint/tendermint/proto/tendermint/types" 7 cmttime "github.com/tendermint/tendermint/types/time" 8 ) 9 10 // Canonical* wraps the structs in types for amino encoding them for use in SignBytes / the Signable interface. 11 12 // TimeFormat is used for generating the sigs 13 const TimeFormat = time.RFC3339Nano 14 15 //----------------------------------- 16 // Canonicalize the structs 17 18 func CanonicalizeBlockID(bid cmtproto.BlockID) *cmtproto.CanonicalBlockID { 19 rbid, err := BlockIDFromProto(&bid) 20 if err != nil { 21 panic(err) 22 } 23 var cbid *cmtproto.CanonicalBlockID 24 if rbid == nil || rbid.IsZero() { 25 cbid = nil 26 } else { 27 cbid = &cmtproto.CanonicalBlockID{ 28 Hash: bid.Hash, 29 PartSetHeader: CanonicalizePartSetHeader(bid.PartSetHeader), 30 } 31 } 32 33 return cbid 34 } 35 36 // CanonicalizeVote transforms the given PartSetHeader to a CanonicalPartSetHeader. 37 func CanonicalizePartSetHeader(psh cmtproto.PartSetHeader) cmtproto.CanonicalPartSetHeader { 38 return cmtproto.CanonicalPartSetHeader(psh) 39 } 40 41 // CanonicalizeVote transforms the given Proposal to a CanonicalProposal. 42 func CanonicalizeProposal(chainID string, proposal *cmtproto.Proposal) cmtproto.CanonicalProposal { 43 return cmtproto.CanonicalProposal{ 44 Type: cmtproto.ProposalType, 45 Height: proposal.Height, // encoded as sfixed64 46 Round: int64(proposal.Round), // encoded as sfixed64 47 POLRound: int64(proposal.PolRound), 48 BlockID: CanonicalizeBlockID(proposal.BlockID), 49 Timestamp: proposal.Timestamp, 50 ChainID: chainID, 51 } 52 } 53 54 // CanonicalizeVote transforms the given Vote to a CanonicalVote, which does 55 // not contain ValidatorIndex and ValidatorAddress fields. 56 func CanonicalizeVote(chainID string, vote *cmtproto.Vote) cmtproto.CanonicalVote { 57 return cmtproto.CanonicalVote{ 58 Type: vote.Type, 59 Height: vote.Height, // encoded as sfixed64 60 Round: int64(vote.Round), // encoded as sfixed64 61 BlockID: CanonicalizeBlockID(vote.BlockID), 62 Timestamp: vote.Timestamp, 63 ChainID: chainID, 64 } 65 } 66 67 // CanonicalTime can be used to stringify time in a canonical way. 68 func CanonicalTime(t time.Time) string { 69 // Note that sending time over amino resets it to 70 // local time, we need to force UTC here, so the 71 // signatures match 72 return cmttime.Canonical(t).Format(TimeFormat) 73 }