You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

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