diff --git a/.gitignore b/.gitignore index e46a30a0..8a99150f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ lino linocli bin/ dist/ +*.key diff --git a/client/common.go b/client/common.go index a20cecc1..899bec48 100644 --- a/client/common.go +++ b/client/common.go @@ -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] == '@' { diff --git a/client/misc.go b/client/misc.go index 312c7e7c..c2196183 100644 --- a/client/misc.go +++ b/client/misc.go @@ -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 ", diff --git a/cmd/linocli/main.go b/cmd/linocli/main.go index 09758e58..ac5c3324 100644 --- a/cmd/linocli/main.go +++ b/cmd/linocli/main.go @@ -60,6 +60,7 @@ func main() { linoclient.GetNowCmd(cdc), linoclient.GetGenAddrCmd(), linoclient.GetAddrOfCmd(), + linoclient.GetPubKeyOfCmd(), linoclient.GetEncryptPrivKey(), client.LineBreak, ) diff --git a/x/account/client/cli/tx.go b/x/account/client/cli/tx.go index 2cc74f2c..7beb36ff 100644 --- a/x/account/client/cli/tx.go +++ b/x/account/client/cli/tx.go @@ -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 { @@ -41,6 +43,7 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { getCmdRegister(cdc), getCmdTransferV2(cdc), getCmdBind(cdc), + getCmdRecover(cdc), )...) return cmd @@ -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 --new-tx-priv --new-sign-pub ", + Long: "recover --new-tx-priv --new-sign-pub ", + 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") { diff --git a/x/account/types/msg.go b/x/account/types/msg.go index 457bfabe..cc2a4502 100644 --- a/x/account/types/msg.go +++ b/x/account/types/msg.go @@ -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())} } @@ -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())} } @@ -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()),