github.com/decred/politeia@v1.4.0/politeiawww/cmd/cmswww/censorcomment.go (about) 1 // Copyright (c) 2017-2020 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 main 6 7 import ( 8 "encoding/hex" 9 "fmt" 10 11 "github.com/decred/politeia/politeiad/api/v1/identity" 12 v1 "github.com/decred/politeia/politeiawww/api/www/v1" 13 "github.com/decred/politeia/politeiawww/cmd/shared" 14 "github.com/decred/politeia/util" 15 ) 16 17 // CensorCommentCmd censors a proposal comment. 18 type CensorCommentCmd struct { 19 Args struct { 20 Token string `positional-arg-name:"token"` // Censorship token 21 CommentID string `positional-arg-name:"commentID"` // Comment ID 22 Reason string `positional-arg-name:"reason"` // Reason for censoring 23 } `positional-args:"true" required:"true"` 24 } 25 26 // Execute executes the censor comment command. 27 func (cmd *CensorCommentCmd) Execute(args []string) error { 28 token := cmd.Args.Token 29 commentID := cmd.Args.CommentID 30 reason := cmd.Args.Reason 31 32 // Check for user identity 33 if cfg.Identity == nil { 34 return shared.ErrUserIdentityNotFound 35 } 36 37 // Get server public key 38 vr, err := client.Version() 39 if err != nil { 40 return err 41 } 42 43 // Setup censor comment request 44 s := cfg.Identity.SignMessage([]byte(token + commentID + reason)) 45 signature := hex.EncodeToString(s[:]) 46 cc := &v1.CensorComment{ 47 Token: token, 48 CommentID: commentID, 49 Reason: reason, 50 Signature: signature, 51 PublicKey: hex.EncodeToString(cfg.Identity.Public.Key[:]), 52 } 53 54 // Print request details 55 err = shared.PrintJSON(cc) 56 if err != nil { 57 return err 58 } 59 60 // Send request 61 ccr, err := client.WWWCensorComment(cc) 62 if err != nil { 63 return err 64 } 65 66 // Validate censor comment receipt 67 serverID, err := identity.PublicIdentityFromString(vr.PubKey) 68 if err != nil { 69 return err 70 } 71 receiptB, err := util.ConvertSignature(ccr.Receipt) 72 if err != nil { 73 return err 74 } 75 if !serverID.VerifyMessage([]byte(signature), receiptB) { 76 return fmt.Errorf("could not verify receipt signature") 77 } 78 79 // Print response details 80 return shared.PrintJSON(ccr) 81 } 82 83 // censorCommentHelpMsg is the output of the help command when 'censorcomment' 84 // is specified. 85 const censorCommentHelpMsg = `censorcomment "token" "commentID" "reason" 86 87 Censor a user comment. Requires admin privileges. 88 89 Arguments: 90 1. token (string, required) Proposal censorship token 91 2. commentID (string, required) Id of the comment 92 3. reason (string, required) Reason for censoring the comment 93 `