From 2d80e984b99f0dfad356925dea8c560be499ca72 Mon Sep 17 00:00:00 2001 From: Madhur Shrimal Date: Mon, 30 Jan 2023 16:06:22 -0800 Subject: [PATCH] add key gen command (#388) --- Dockerfile | 2 +- cmd/README.md | 6 +++++- cmd/key_gen.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 16 ++++++++++++++- 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 cmd/key_gen.go diff --git a/Dockerfile b/Dockerfile index 07d7bbdc..a6bba933 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,7 +31,7 @@ RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH" WORKDIR /go/src -ARG VERSION=v0.10.2 +ARG VERSION=v0.10.3 RUN git clone https://github.com/coinbase/rosetta-cli.git && \ cd rosetta-cli && \ git fetch --all --tags && \ diff --git a/cmd/README.md b/cmd/README.md index 01297daf..0c3bd6cf 100644 --- a/cmd/README.md +++ b/cmd/README.md @@ -4,6 +4,11 @@ Rosetta CLI has a key sign tool, which you can use to sign and verify various cu by rosetta-specifications. This should only be used for local development. Never share private keys anywhere. ### Usage +#### Key Generate +``` +rosetta-cli key:gen --curve-type secp256k1 +``` +Curve Type options are specified by [rosetta-specifications](https://github.com/coinbase/rosetta-specifications/blob/master/models/CurveType.yaml) #### Sign ``` rosetta-cli key:sign --configuration-file config.json @@ -28,6 +33,5 @@ Required fields includes - `signing_payload` - `signature` - ### Troubleshoot - `account_identifier` field in `signing_payload` field should've a dummy address for providing valid payload. \ No newline at end of file diff --git a/cmd/key_gen.go b/cmd/key_gen.go new file mode 100644 index 00000000..49834644 --- /dev/null +++ b/cmd/key_gen.go @@ -0,0 +1,56 @@ +// Copyright 2023 Coinbase, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "encoding/hex" + "errors" + + "github.com/coinbase/rosetta-sdk-go/keys" + "github.com/coinbase/rosetta-sdk-go/types" + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +var ( + keyGenCmd = &cobra.Command{ + Use: "key:gen", + Short: "Used to generate a public private key pair", + Long: `Used to generate a public private key pair + It supports Keypair specified by https://github.com/coinbase/rosetta-specifications + Please provide valid CurveType`, + RunE: runKeyGenCmd, + } +) + +func runKeyGenCmd(_ *cobra.Command, _ []string) error { + if len(curveType) == 0 { + color.Red("please provide a non-empty curve type") + return errors.New("invalid curve-type string") + } + + curve := types.CurveType(curveType) + + color.Yellow("Generating new %s keypair...", curve) + keyPair, err := keys.GenerateKeypair(curve) + if err != nil { + color.Red("failed to generate keypair with error %#v", err) + } + + color.Green("CurveType: %s", curve) + color.Green("Public Key (hex): %s", hex.EncodeToString(keyPair.PublicKey.Bytes)) + color.Green("Private Key (hex): %s", hex.EncodeToString(keyPair.PrivateKey)) + return nil +} diff --git a/cmd/root.go b/cmd/root.go index 40b930eb..5a1b52e3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,6 +17,7 @@ package cmd import ( "context" "fmt" + "github.com/coinbase/rosetta-sdk-go/types" "log" "os" "os/signal" @@ -101,6 +102,10 @@ var ( // which has caused production incidents in the past. This can be used for both check:data // and check:construction. asserterConfigurationFile string + + // curveType is used to specify curve type to generate a keypair using rosetta-cli key:gen + // command + curveType string ) // rootPreRun is executed before the root command runs and sets up cpu @@ -381,6 +386,15 @@ default values.`, // Key Verify command rootCmd.AddCommand(keyVerifyCmd) + + keyGenCmd.Flags().StringVar( + &curveType, + "curve-type", + string(types.Secp256k1), + "curve type used to generate the public/private keypair", + ) + // Key Gen command + rootCmd.AddCommand(keyGenCmd) } func initConfig() { @@ -494,6 +508,6 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "Print rosetta-cli version", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("v0.10.2") + fmt.Println("v0.10.3") }, }