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