-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat: add base64 Pubkeys from cli #17639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
97569c3
ae3f37b
31e5315
54876fa
b4cf069
8549694
71f7288
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <name> 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,27 @@ 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 := fmt.Sprintf(`{"@type":"%s","key":"%s"}`, tempAny.TypeUrl, b64) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to avoid manual JSON templating?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this is susceptible to some sneak attacks, we can invoke this jsonPubB, err := json.Marshal(struct{
Type string `json:"@type,omitempty"`
Key string `json:"key,omitempty"`
}{tempAny.TypeURL, b64})
if err != nil {
return fmt.Errorf("failed to JSON marshal typeURL and base64 key: %w", err)
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch guys!! Resolved in last commit! |
||
| if err = ctx.Codec.UnmarshalInterfaceJSON([]byte(jsonPub), &pk); err != nil { | ||
| return err | ||
| } | ||
| k, err := kb.SaveOfflineKey(name, pk) | ||
| if err != nil { | ||
| return err | ||
|
JulianToledano marked this conversation as resolved.
Outdated
|
||
| } | ||
| return printCreate(ctx, cmd, k, false, "", outputFormat) | ||
|
JulianToledano marked this conversation as resolved.
|
||
| } | ||
|
|
||
| coinType, _ := cmd.Flags().GetUint32(flagCoinType) | ||
| account, _ := cmd.Flags().GetUint32(flagAccount) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.