Skip to content
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

add sign tool #386

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ default values.`,
`Verify both minimum and Coinbase spec requirements`,
)
rootCmd.AddCommand(checkSpecCmd)

// Sign command
rootCmd.AddCommand(signCmd)
}

func initConfig() {
Expand Down
68 changes: 68 additions & 0 deletions cmd/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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"
"fmt"

"github.com/coinbase/rosetta-sdk-go/keys"
"github.com/fatih/color"
"github.com/spf13/cobra"
)

var (
signCmd = &cobra.Command{
Use: "sign",
Short: "Sign an unsigned payload with given private key",
Long: `Sign an unsigned payload with given private key
It supports Keypair specified by https://github.com/coinbase/rosetta-specifications`,
RunE: runSignCmd,
}
)

func runSignCmd(_ *cobra.Command, _ []string) error {
if Config.Sign == nil {
return errors.New("sign configuration is missing")
}

keyPair, err := keys.ImportPrivateKey(Config.Sign.PrivateKey, Config.Sign.PubKey.CurveType)

err = keyPair.IsValid()
if err != nil {
fmt.Println(fmt.Errorf("keypair invalid with err %#v", err))
return err
}

signer, err := keyPair.Signer()
if err != nil {
fmt.Println(fmt.Errorf("signer invalid with err %#v", err))
return err
}

signingPayload := Config.Sign.SigningPayload
signatureType := Config.Sign.SigningPayload.SignatureType

sign, err := signer.Sign(signingPayload, signatureType)
if err != nil {
fmt.Println(fmt.Errorf("unable to sign with err %#v", err))
return err
}

hexSig := hex.EncodeToString(sign.Bytes)
color.Blue(hexSig)
return nil
}
14 changes: 10 additions & 4 deletions configuration/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ type Configuration struct {
// InfoMetaData is a string, rosetta-cli will convert it into a map[string]string
// key-value are separated by ":"
// different key-value pairs are separated by ","
// an example: if users want to record "instance_name" as "1234", and "blockchain_name" as "Bitcoin",
// an example: if users want to record "instance_name" as "1234", and "blockchain_name" as "Bitcoin",
// this field would be "instance_name:1234,blockchain_name:Bitcoin"
// if adding spaces before and after ":" and ",", it will be trimmed when building map
// " instance_name : xxxx , blockchain_name : xxxx " will be recorded same as
Expand All @@ -474,11 +474,17 @@ type Configuration struct {
Construction *ConstructionConfiguration `json:"construction"`
Data *DataConfiguration `json:"data"`
Perf *CheckPerfConfiguration `json:"perf"`
Sign *SignConfiguration `json:"sign"`
}

//********************//
// Check Perf configs //
//********************//
// SignConfiguration configuration for signing
type SignConfiguration struct {
PubKey *types.PublicKey `json:"pub_key"`
PrivateKey string `json:"private_key"`
SigningPayload *types.SigningPayload `json:"signing_payload"`
}

// CheckPerfConfiguration configuration for check perf
type CheckPerfConfiguration struct {

// StartBlock is the starting block for running check:perf.
Expand Down
13 changes: 13 additions & 0 deletions examples/configuration/sign.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"sign": {
"pub_key": {
"curve_type": "secp256k1",
"hex_bytes": "03c7e625aa08cad8f257d9ee2b9b7a0214f19f981afd5b498c728ad7ed6c0c3df6"
},
"private_key": "",
"signing_payload": {
"hex_bytes": "370e74254e8cbaa343af3564901456082ec7af967e45ff24ba061233b1a1b04f",
"signature_type": "ecdsa"
}
}
}