Skip to content

Commit 2d80e98

Browse files
add key gen command (#388)
1 parent 679464c commit 2d80e98

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
3131

3232
WORKDIR /go/src
3333

34-
ARG VERSION=v0.10.2
34+
ARG VERSION=v0.10.3
3535
RUN git clone https://github.com/coinbase/rosetta-cli.git && \
3636
cd rosetta-cli && \
3737
git fetch --all --tags && \

cmd/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Rosetta CLI has a key sign tool, which you can use to sign and verify various cu
44
by rosetta-specifications. This should only be used for local development. Never share private keys anywhere.
55

66
### Usage
7+
#### Key Generate
8+
```
9+
rosetta-cli key:gen --curve-type secp256k1
10+
```
11+
Curve Type options are specified by [rosetta-specifications](https://github.com/coinbase/rosetta-specifications/blob/master/models/CurveType.yaml)
712
#### Sign
813
```
914
rosetta-cli key:sign --configuration-file config.json
@@ -28,6 +33,5 @@ Required fields includes
2833
- `signing_payload`
2934
- `signature`
3035

31-
3236
### Troubleshoot
3337
- `account_identifier` field in `signing_payload` field should've a dummy address for providing valid payload.

cmd/key_gen.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2023 Coinbase, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"encoding/hex"
19+
"errors"
20+
21+
"github.com/coinbase/rosetta-sdk-go/keys"
22+
"github.com/coinbase/rosetta-sdk-go/types"
23+
"github.com/fatih/color"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
var (
28+
keyGenCmd = &cobra.Command{
29+
Use: "key:gen",
30+
Short: "Used to generate a public private key pair",
31+
Long: `Used to generate a public private key pair
32+
It supports Keypair specified by https://github.com/coinbase/rosetta-specifications
33+
Please provide valid CurveType`,
34+
RunE: runKeyGenCmd,
35+
}
36+
)
37+
38+
func runKeyGenCmd(_ *cobra.Command, _ []string) error {
39+
if len(curveType) == 0 {
40+
color.Red("please provide a non-empty curve type")
41+
return errors.New("invalid curve-type string")
42+
}
43+
44+
curve := types.CurveType(curveType)
45+
46+
color.Yellow("Generating new %s keypair...", curve)
47+
keyPair, err := keys.GenerateKeypair(curve)
48+
if err != nil {
49+
color.Red("failed to generate keypair with error %#v", err)
50+
}
51+
52+
color.Green("CurveType: %s", curve)
53+
color.Green("Public Key (hex): %s", hex.EncodeToString(keyPair.PublicKey.Bytes))
54+
color.Green("Private Key (hex): %s", hex.EncodeToString(keyPair.PrivateKey))
55+
return nil
56+
}

cmd/root.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717
import (
1818
"context"
1919
"fmt"
20+
"github.com/coinbase/rosetta-sdk-go/types"
2021
"log"
2122
"os"
2223
"os/signal"
@@ -101,6 +102,10 @@ var (
101102
// which has caused production incidents in the past. This can be used for both check:data
102103
// and check:construction.
103104
asserterConfigurationFile string
105+
106+
// curveType is used to specify curve type to generate a keypair using rosetta-cli key:gen
107+
// command
108+
curveType string
104109
)
105110

106111
// rootPreRun is executed before the root command runs and sets up cpu
@@ -381,6 +386,15 @@ default values.`,
381386

382387
// Key Verify command
383388
rootCmd.AddCommand(keyVerifyCmd)
389+
390+
keyGenCmd.Flags().StringVar(
391+
&curveType,
392+
"curve-type",
393+
string(types.Secp256k1),
394+
"curve type used to generate the public/private keypair",
395+
)
396+
// Key Gen command
397+
rootCmd.AddCommand(keyGenCmd)
384398
}
385399

386400
func initConfig() {
@@ -494,6 +508,6 @@ var versionCmd = &cobra.Command{
494508
Use: "version",
495509
Short: "Print rosetta-cli version",
496510
Run: func(cmd *cobra.Command, args []string) {
497-
fmt.Println("v0.10.2")
511+
fmt.Println("v0.10.3")
498512
},
499513
}

0 commit comments

Comments
 (0)