|
| 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 | +} |
0 commit comments