Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
add back account recovery cli. (#451)
Browse files Browse the repository at this point in the history
* account recover cli

* fix signature

* fmt

* add pubkey_of cmd
  • Loading branch information
Stumble committed Dec 19, 2019
1 parent d9d0ba7 commit 6aba3c2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ lino
linocli
bin/
dist/
*.key
9 changes: 9 additions & 0 deletions client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import (

var ValidateCmd = cosmoscli.ValidateCmd

func ParsePubKey(key string) (crypto.PubKey, error) {
pubKeyBytes, err := hex.DecodeString(key)
if err != nil {
return nil, err
}

return cryptoAmino.PubKeyFromBytes(pubKeyBytes)
}

func ParsePrivKey(key string) (crypto.PrivKey, error) {
// @ tag means that priv-key is encrypted in the file.
if key[0] == '@' {
Expand Down
17 changes: 17 additions & 0 deletions client/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ func GetAddrOfCmd() *cobra.Command {
}
}

func GetPubKeyOfCmd() *cobra.Command {
return &cobra.Command{
Use: "pubkey-of <@keyfile>",
Short: "pubkey-of <@keyfile> prints the hex-encoded string of pubkey, @ included",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
file := args[0]
priv, err := ParsePrivKey(file)
if err != nil {
return err
}
fmt.Printf("pubkey: %s\n", hex.EncodeToString(priv.PubKey().Bytes()))
return nil
},
}
}

func GetEncryptPrivKey() *cobra.Command {
return &cobra.Command{
Use: "encrypt-key <file>",
Expand Down
1 change: 1 addition & 0 deletions cmd/linocli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {
linoclient.GetNowCmd(cdc),
linoclient.GetGenAddrCmd(),
linoclient.GetAddrOfCmd(),
linoclient.GetPubKeyOfCmd(),
linoclient.GetEncryptPrivKey(),
client.LineBreak,
)
Expand Down
41 changes: 41 additions & 0 deletions x/account/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
FlagAddr = "addr"
FlagAddrSeq = "addr-seq"
FlagAddrPrivKey = "addr-priv-key"
FlagNewTxKey = "new-tx-priv"
FlagNewSignKey = "new-sign-pub"
)

func GetTxCmd(cdc *codec.Codec) *cobra.Command {
Expand All @@ -41,6 +43,7 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
getCmdRegister(cdc),
getCmdTransferV2(cdc),
getCmdBind(cdc),
getCmdRecover(cdc),
)...)

return cmd
Expand Down Expand Up @@ -186,6 +189,44 @@ func getCmdTransferV2(cdc *codec.Codec) *cobra.Command {
return cmd
}

// getCmdRecover -
func getCmdRecover(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "recover",
Short: "recover <username> --new-tx-priv <new-priv-key> --new-sign-pub <pubkey-hex>",
Long: "recover <username> --new-tx-priv <new-priv-key> --new-sign-pub <pubkey-hex>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := client.NewCoreContextFromViper().WithTxEncoder(linotypes.TxEncoder(cdc))
user := args[0]
if !linotypes.AccountKey(user).IsValid() {
return fmt.Errorf("invalid username: %s", user)
}

txPrivKey, err := client.ParsePrivKey(viper.GetString(FlagNewTxKey))
if err != nil {
return fmt.Errorf("invalid new tx key: %s", err)
}

signPubKey, err := client.ParsePubKey(viper.GetString(FlagNewSignKey))
if err != nil {
return fmt.Errorf("invalid new sign key: %s", err)
}

msg := types.NewRecoverMsg(user, txPrivKey.PubKey(), signPubKey)
return ctx.DoTxPrintResponse(msg, client.OptionalSigner{
PrivKey: txPrivKey,
Seq: 0,
})
},
}
cmd.Flags().String(FlagNewTxKey, "", "new transaction private key")
cmd.Flags().String(FlagNewSignKey, "", "new signing key")
_ = cmd.MarkFlagRequired(FlagNewTxKey)
_ = cmd.MarkFlagRequired(FlagNewSignKey)
return cmd
}

func parseAccOrAddr(s string) (rst linotypes.AccOrAddr, err error) {
comps := strings.Split(s, ":")
if len(comps) != 2 || !(comps[0] == "addr" || comps[0] == "user") {
Expand Down
3 changes: 0 additions & 3 deletions x/account/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ func (msg TransferV2Msg) GetSignBytes() []byte {
}

// GetSigners - implements sdk.Msg
// SHOULD NOT BE USED.
func (msg TransferV2Msg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Sender.String())}
}
Expand Down Expand Up @@ -200,7 +199,6 @@ func (msg RecoverMsg) GetSignBytes() []byte {
}

// GetSigners - implements sdk.Msg
// SHOULD NOT BE USED.
func (msg RecoverMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Username), sdk.AccAddress(msg.NewTxPubKey.Address())}
}
Expand Down Expand Up @@ -337,7 +335,6 @@ func (msg RegisterV2Msg) GetSignBytes() []byte {
}

// GetSigners - implements sdk.Msg
// SHOULD NOT BE USED
func (msg RegisterV2Msg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{
sdk.AccAddress(msg.Referrer.String()),
Expand Down

0 comments on commit 6aba3c2

Please sign in to comment.