github.com/decred/politeia@v1.4.0/politeiad/backend/gitbe/decredplugin/decredplugin.go (about)

     1  // Copyright (c) 2017-2021 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package decredplugin
     6  
     7  import (
     8  	"encoding/json"
     9  )
    10  
    11  // Plugin settings, kinda doesn;t go here but for now it is fine
    12  const (
    13  	Version          = "1"
    14  	ID               = "decred"
    15  	CmdBestBlock     = "bestblock"
    16  	CmdNewComment    = "newcomment"
    17  	CmdCensorComment = "censorcomment"
    18  	CmdGetComments   = "getcomments"
    19  )
    20  
    21  // ErrorStatusT represents decredplugin errors that result from casting a vote.
    22  //
    23  // These are part of the www/v1 API and must stay in until the deprecated cast
    24  // votes route is removed.
    25  type ErrorStatusT int
    26  
    27  const (
    28  	ErrorStatusInvalid          ErrorStatusT = 0
    29  	ErrorStatusInternalError    ErrorStatusT = 1
    30  	ErrorStatusProposalNotFound ErrorStatusT = 2
    31  	ErrorStatusInvalidVoteBit   ErrorStatusT = 3
    32  	ErrorStatusVoteHasEnded     ErrorStatusT = 4
    33  	ErrorStatusDuplicateVote    ErrorStatusT = 5
    34  	ErrorStatusIneligibleTicket ErrorStatusT = 6
    35  	ErrorStatusLast             ErrorStatusT = 7
    36  )
    37  
    38  var (
    39  	// ErrorStatus converts error status codes to human readable text.
    40  	ErrorStatus = map[ErrorStatusT]string{
    41  		ErrorStatusInvalid:          "invalid error status",
    42  		ErrorStatusInternalError:    "internal error",
    43  		ErrorStatusProposalNotFound: "proposal not found",
    44  		ErrorStatusInvalidVoteBit:   "invalid vote bit",
    45  		ErrorStatusVoteHasEnded:     "vote has ended",
    46  		ErrorStatusDuplicateVote:    "duplicate vote",
    47  		ErrorStatusIneligibleTicket: "ineligbile ticket",
    48  	}
    49  )
    50  
    51  // Comment is the structure that describes the full server side content.  It
    52  // includes server side meta-data as well. Note that the receipt is the server
    53  // side.
    54  type Comment struct {
    55  	// Data generated by client
    56  	Token     string `json:"token"`     // Censorship token
    57  	ParentID  string `json:"parentid"`  // Parent comment ID
    58  	Comment   string `json:"comment"`   // Comment
    59  	Signature string `json:"signature"` // Client Signature of Token+ParentID+Comment
    60  	PublicKey string `json:"publickey"` // Pubkey used for Signature
    61  
    62  	// Metadata generated by decred plugin
    63  	CommentID   string `json:"commentid"`   // Comment ID
    64  	Receipt     string `json:"receipt"`     // Server signature of the client Signature
    65  	Timestamp   int64  `json:"timestamp"`   // Received UNIX timestamp
    66  	TotalVotes  uint64 `json:"totalvotes"`  // Total number of up/down votes
    67  	ResultVotes int64  `json:"resultvotes"` // Vote score
    68  	Censored    bool   `json:"censored"`    // Has this comment been censored
    69  }
    70  
    71  // EncodeComment encodes Comment into a JSON byte slice.
    72  func EncodeComment(c Comment) ([]byte, error) {
    73  	return json.Marshal(c)
    74  }
    75  
    76  // DecodeComment decodes a JSON byte slice into a Comment
    77  func DecodeComment(payload []byte) (*Comment, error) {
    78  	var c Comment
    79  
    80  	err := json.Unmarshal(payload, &c)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return &c, nil
    86  }
    87  
    88  // NewComment sends a comment from a user to a specific proposal.  Note that
    89  // the user is implied by the session.
    90  type NewComment struct {
    91  	Token     string `json:"token"`     // Censorship token
    92  	ParentID  string `json:"parentid"`  // Parent comment ID
    93  	Comment   string `json:"comment"`   // Comment
    94  	Signature string `json:"signature"` // Signature of Token+ParentID+Comment
    95  	PublicKey string `json:"publickey"` // Pubkey used for Signature
    96  }
    97  
    98  // EncodeNewComment encodes NewComment into a JSON byte slice.
    99  func EncodeNewComment(nc NewComment) ([]byte, error) {
   100  	return json.Marshal(nc)
   101  }
   102  
   103  // DecodeNewComment decodes a JSON byte slice into a NewComment
   104  func DecodeNewComment(payload []byte) (*NewComment, error) {
   105  	var nc NewComment
   106  
   107  	err := json.Unmarshal(payload, &nc)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	return &nc, nil
   113  }
   114  
   115  // NewCommentReply returns the metadata generated by decred plugin for the new
   116  // comment.
   117  type NewCommentReply struct {
   118  	CommentID string `json:"commentid"` // Comment ID
   119  	Receipt   string `json:"receipt"`   // Server signature of the client Signature
   120  	Timestamp int64  `json:"timestamp"` // Received UNIX timestamp
   121  }
   122  
   123  // EncodeNewCommentReply encodes NewCommentReply into a JSON byte slice.
   124  func EncodeNewCommentReply(ncr NewCommentReply) ([]byte, error) {
   125  	return json.Marshal(ncr)
   126  }
   127  
   128  // DecodeNewCommentReply decodes a JSON byte slice into a NewCommentReply.
   129  func DecodeNewCommentReply(payload []byte) (*NewCommentReply, error) {
   130  	var ncr NewCommentReply
   131  
   132  	err := json.Unmarshal(payload, &ncr)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	return &ncr, nil
   138  }
   139  
   140  // CensorComment is a journal entry for a censored comment.  The signature and
   141  // public key are from the admin that censored this comment.
   142  type CensorComment struct {
   143  	Token     string `json:"token"`     // Proposal censorship token
   144  	CommentID string `json:"commentid"` // Comment ID
   145  	Reason    string `json:"reason"`    // Reason comment was censored
   146  	Signature string `json:"signature"` // Client signature of Token+CommentID+Reason
   147  	PublicKey string `json:"publickey"` // Pubkey used for signature
   148  
   149  	// Generated by decredplugin
   150  	Receipt   string `json:"receipt,omitempty"`   // Server signature of client signature
   151  	Timestamp int64  `json:"timestamp,omitempty"` // Received UNIX timestamp
   152  }
   153  
   154  // EncodeCensorComment encodes CensorComment into a JSON byte slice.
   155  func EncodeCensorComment(cc CensorComment) ([]byte, error) {
   156  	return json.Marshal(cc)
   157  }
   158  
   159  // DecodeCensorComment decodes a JSON byte slice into a CensorComment.
   160  func DecodeCensorComment(payload []byte) (*CensorComment, error) {
   161  	var cc CensorComment
   162  	err := json.Unmarshal(payload, &cc)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  	return &cc, nil
   167  }
   168  
   169  // CommentCensorReply returns the receipt for the censoring action. The
   170  // receipt is the server side signature of CommentCensor.Signature.
   171  type CensorCommentReply struct {
   172  	Receipt string `json:"receipt"` // Server signature of client signature
   173  }
   174  
   175  // EncodeCensorCommentReply encodes CensorCommentReply into a JSON byte slice.
   176  func EncodeCensorCommentReply(ccr CensorCommentReply) ([]byte, error) {
   177  	return json.Marshal(ccr)
   178  }
   179  
   180  // DecodeCensorComment decodes a JSON byte slice into a CensorCommentReply.
   181  func DecodeCensorCommentReply(payload []byte) (*CensorCommentReply, error) {
   182  	var ccr CensorCommentReply
   183  	err := json.Unmarshal(payload, &ccr)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	return &ccr, nil
   188  }
   189  
   190  // GetComments retrieve all comments for a given proposal. This call returns
   191  // the cooked comments; deleted/censored comments are not returned.
   192  type GetComments struct {
   193  	Token string `json:"token"` // Proposal ID
   194  }
   195  
   196  // EncodeGetComments encodes GetCommentsReply into a JSON byte slice.
   197  func EncodeGetComments(gc GetComments) ([]byte, error) {
   198  	return json.Marshal(gc)
   199  }
   200  
   201  // DecodeGetComments decodes a JSON byte slice into a GetComments.
   202  func DecodeGetComments(payload []byte) (*GetComments, error) {
   203  	var gc GetComments
   204  
   205  	err := json.Unmarshal(payload, &gc)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  
   210  	return &gc, nil
   211  }
   212  
   213  // GetCommentsReply returns the provided number of comments.
   214  type GetCommentsReply struct {
   215  	Comments []Comment `json:"comments"` // Comments
   216  }
   217  
   218  // EncodeGetCommentsReply encodes GetCommentsReply into a JSON byte slice.
   219  func EncodeGetCommentsReply(gcr GetCommentsReply) ([]byte, error) {
   220  	return json.Marshal(gcr)
   221  }
   222  
   223  // DecodeGetCommentsReply decodes a JSON byte slice into a GetCommentsReply.
   224  func DecodeGetCommentsReply(payload []byte) (*GetCommentsReply, error) {
   225  	var gcr GetCommentsReply
   226  
   227  	err := json.Unmarshal(payload, &gcr)
   228  	if err != nil {
   229  		return nil, err
   230  	}
   231  
   232  	return &gcr, nil
   233  }
   234  
   235  // BestBlock is a command to request the best block data.
   236  type BestBlock struct{}
   237  
   238  // EncodeBestBlock encodes an BestBlock into a JSON byte slice.
   239  func EncodeBestBlock(bb BestBlock) ([]byte, error) {
   240  	return json.Marshal(bb)
   241  }
   242  
   243  // DecodeBestBlock decodes a JSON byte slice into a BestBlock.
   244  func DecodeBestBlock(payload []byte) (*BestBlock, error) {
   245  	var bb BestBlock
   246  	err := json.Unmarshal(payload, &bb)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  	return &bb, nil
   251  }
   252  
   253  // BestBlockReply is the reply to the BestBlock command.
   254  type BestBlockReply struct {
   255  	Height uint32 `json:"height"`
   256  }
   257  
   258  // EncodeBestBlockReply encodes an BestBlockReply into a JSON byte slice.
   259  func EncodeBestBlockReply(bbr BestBlockReply) ([]byte, error) {
   260  	return json.Marshal(bbr)
   261  }
   262  
   263  // DecodeBestBlockReply decodes a JSON byte slice into a BestBlockReply.
   264  func DecodeBestBlockReply(payload []byte) (*BestBlockReply, error) {
   265  	var bbr BestBlockReply
   266  	err := json.Unmarshal(payload, &bbr)
   267  	if err != nil {
   268  		return nil, err
   269  	}
   270  	return &bbr, nil
   271  }