github.com/decred/politeia@v1.4.0/politeiawww/cmd/shared/userkeyupdate.go (about)

     1  // Copyright (c) 2017-2019 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 shared
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  
    11  	v1 "github.com/decred/politeia/politeiawww/api/www/v1"
    12  )
    13  
    14  // UserKeyUpdateCmd creates a new identity for the logged in user.
    15  type UserKeyUpdateCmd struct {
    16  	NoSave bool `long:"nosave"` // Don't save new identity to disk
    17  }
    18  
    19  // Execute executes the update user key command.
    20  func (cmd *UserKeyUpdateCmd) Execute(args []string) error {
    21  	// Get the logged in user's username. We need
    22  	// this when we save the new identity to disk.
    23  	me, err := client.Me()
    24  	if err != nil {
    25  		return fmt.Errorf("Me: %v", err)
    26  	}
    27  
    28  	// Create new identity
    29  	id, err := NewIdentity()
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	// Update user key
    35  	uuk := &v1.UpdateUserKey{
    36  		PublicKey: hex.EncodeToString(id.Public.Key[:]),
    37  	}
    38  
    39  	err = PrintJSON(uuk)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	uukr, err := client.UpdateUserKey(uuk)
    45  	if err != nil {
    46  		return fmt.Errorf("UpdateUserKey: %v", err)
    47  	}
    48  
    49  	// Verify update user key
    50  	sig := id.SignMessage([]byte(uukr.VerificationToken))
    51  	vuuk := &v1.VerifyUpdateUserKey{
    52  		VerificationToken: uukr.VerificationToken,
    53  		Signature:         hex.EncodeToString(sig[:]),
    54  	}
    55  
    56  	vuukr, err := client.VerifyUpdateUserKey(vuuk)
    57  	if err != nil {
    58  		return fmt.Errorf("VerifyUpdateUserKey: %v", err)
    59  	}
    60  
    61  	// Save the new identity to disk
    62  	if !cmd.NoSave {
    63  		return cfg.SaveIdentity(me.Username, id)
    64  	}
    65  
    66  	// Print response details
    67  	return PrintJSON(vuukr)
    68  }
    69  
    70  // UserKeyUpdateHelpMsg is the output of the help command when 'userkeyupdate'
    71  // is specified.
    72  const UserKeyUpdateHelpMsg = `userkeyupdate
    73  
    74  Generate a new public key for the currently logged in user. 
    75  
    76  Arguments:
    77  None`