Skip to content

Commit

Permalink
add key gen command (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur authored Jan 31, 2023
1 parent 679464c commit 2d80e98
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
6 changes: 5 additions & 1 deletion cmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
56 changes: 56 additions & 0 deletions cmd/key_gen.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 15 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"context"
"fmt"
"github.com/coinbase/rosetta-sdk-go/types"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
},
}

0 comments on commit 2d80e98

Please sign in to comment.