From 933d042d9e06569cbcf28cb08290c23687cc9440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Toledano?= Date: Thu, 7 Sep 2023 22:47:31 +0200 Subject: [PATCH] feat: add base64 Pubkeys from cli (#17639) Co-authored-by: Aleksandr Bezobchuk --- CHANGELOG.md | 1 + client/keys/add.go | 58 ++++++++++++++++++++++++++++++++++------- client/keys/add_test.go | 19 ++++++++++++++ 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 609a765f0f1f..f9a6f041be5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (client/keys) [#17639](https://github.com/cosmos/cosmos-sdk/pull/17639) Allows using and saving public keys encoded as base64 * (client) [#17513](https://github.com/cosmos/cosmos-sdk/pull/17513) Allow overwritting `client.toml`. Use `client.CreateClientConfig` in place of `client.ReadFromClientConfig` and provide a custom template and a custom config. * (x/bank) [#14224](https://github.com/cosmos/cosmos-sdk/pull/14224) Allow injection of restrictions on transfers using `AppendSendRestriction` or `PrependSendRestriction`. * (genutil) [#17571](https://github.com/cosmos/cosmos-sdk/pull/17571) Allow creation of `AppGenesis` without a file lookup. diff --git a/client/keys/add.go b/client/keys/add.go index ef304296c7fb..4ff3b5af2819 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -3,6 +3,7 @@ package keys import ( "bufio" "bytes" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -15,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/input" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" @@ -23,15 +25,16 @@ import ( ) const ( - flagInteractive = "interactive" - flagRecover = "recover" - flagNoBackup = "no-backup" - flagCoinType = "coin-type" - flagAccount = "account" - flagIndex = "index" - flagMultisig = "multisig" - flagNoSort = "nosort" - flagHDPath = "hd-path" + flagInteractive = "interactive" + flagRecover = "recover" + flagNoBackup = "no-backup" + flagCoinType = "coin-type" + flagAccount = "account" + flagIndex = "index" + flagMultisig = "multisig" + flagNoSort = "nosort" + flagHDPath = "hd-path" + flagPubKeyBase64 = "pubkey-base64" // DefaultKeyPass contains the default key password for genesis transactions DefaultKeyPass = "12345678" @@ -69,6 +72,7 @@ Example: f.Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig") f.Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied") f.String(FlagPublicKey, "", "Parse a public key in JSON format and saves key info to file.") + f.String(flagPubKeyBase64, "", "Parse a public key in base64 format and saves key info.") f.BoolP(flagInteractive, "i", false, "Interactively prompt user for BIP39 passphrase and mnemonic") f.Bool(flags.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device") f.Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating") @@ -189,6 +193,10 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf } pubKey, _ := cmd.Flags().GetString(FlagPublicKey) + pubKeyBase64, _ := cmd.Flags().GetString(flagPubKeyBase64) + if pubKey != "" && pubKeyBase64 != "" { + return fmt.Errorf(`flags %s and %s cannot be used simultaneously`, FlagPublicKey, flagPubKeyBase64) + } if pubKey != "" { var pk cryptotypes.PubKey if err = ctx.Codec.UnmarshalInterfaceJSON([]byte(pubKey), &pk); err != nil { @@ -202,6 +210,38 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf return printCreate(ctx, cmd, k, false, "", outputFormat) } + if pubKeyBase64 != "" { + b64, err := base64.StdEncoding.DecodeString(pubKeyBase64) + if err != nil { + return err + } + + var pk cryptotypes.PubKey + // create an empty pubkey in order to get the algo TypeUrl. + tempAny, err := codectypes.NewAnyWithValue(algo.Generate()([]byte{}).PubKey()) + if err != nil { + return err + } + + jsonPub, err := json.Marshal(struct { + Type string `json:"@type,omitempty"` + Key string `json:"key,omitempty"` + }{tempAny.TypeUrl, string(b64)}) + if err != nil { + return fmt.Errorf("failed to JSON marshal typeURL and base64 key: %w", err) + } + + if err = ctx.Codec.UnmarshalInterfaceJSON(jsonPub, &pk); err != nil { + return err + } + + k, err := kb.SaveOfflineKey(name, pk) + if err != nil { + return fmt.Errorf("failed to save offline key: %w", err) + } + + return printCreate(ctx, cmd, k, false, "", outputFormat) + } coinType, _ := cmd.Flags().GetUint32(flagCoinType) account, _ := cmd.Flags().GetUint32(flagAccount) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index d49084210e6a..a10da1c5a408 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -130,6 +130,7 @@ func Test_runAddCmdBasic(t *testing.T) { func Test_runAddCmdDryRun(t *testing.T) { pubkey1 := `{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtObiFVE4s+9+RX5SP8TN9r2mxpoaT4eGj9CJfK7VRzN"}` pubkey2 := `{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A/se1vkqgdQ7VJQCM4mxN+L+ciGhnnJ4XYsQCRBMrdRi"}` + b64Pubkey := "QWhnOHhpdXBJcGZ2UlR2ak5la1ExclROUThTOW96YjdHK2RYQmFLVjl4aUo=" cdc := moduletestutil.MakeTestEncodingConfig().Codec testData := []struct { @@ -189,6 +190,24 @@ func Test_runAddCmdDryRun(t *testing.T) { }, added: false, }, + { + name: "base64 pubkey account is added", + args: []string{ + "testkey", + fmt.Sprintf("--%s=%s", flags.FlagDryRun, "false"), + fmt.Sprintf("--%s=%s", flagPubKeyBase64, b64Pubkey), + }, + added: true, + }, + { + name: "base64 pubkey account is not added with dry run", + args: []string{ + "testkey", + fmt.Sprintf("--%s=%s", flags.FlagDryRun, "true"), + fmt.Sprintf("--%s=%s", flagPubKeyBase64, b64Pubkey), + }, + added: false, + }, } for _, tt := range testData { tt := tt