Skip to content

Commit

Permalink
add sign tool (#386)
Browse files Browse the repository at this point in the history
* add sign tool

* add ex

* make fmt

* log error
  • Loading branch information
shrimalmadhur authored Jan 27, 2023
1 parent 87f7b06 commit 1188c48
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 41 deletions.
4 changes: 2 additions & 2 deletions cmd/check_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/coinbase/rosetta-sdk-go/fetcher"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/coinbase/rosetta-sdk-go/utils"
"github.com/fatih/color"
"github.com/fatih/color"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -171,7 +171,7 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {

g, ctx := errgroup.WithContext(ctx)
ctx = logger.AddMetadataMapToContext(ctx, metadataMap)

g.Go(func() error {
return dataTester.StartPeriodicLogger(ctx)
})
Expand Down
5 changes: 4 additions & 1 deletion 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 Expand Up @@ -451,7 +454,7 @@ func initConfig() {
}

if len(InfoMetaData) != 0 {
Config.InfoMetaData = InfoMetaData
Config.InfoMetaData = InfoMetaData
}
}

Expand Down
72 changes: 72 additions & 0 deletions cmd/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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)
if err != nil {
fmt.Println(fmt.Errorf("unable to import private keys %#v", err))
return err
}

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
}
24 changes: 12 additions & 12 deletions cmd/utils_asserter_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ var (

allowedErrors = []*types.Error{
{
Code: 4,
Message: "Block not found",
Code: 4,
Message: "Block not found",
Retriable: false,
},
{
Code: 0,
Message: "Endpoint not implemented",
Code: 0,
Message: "Endpoint not implemented",
Retriable: false,
},
{
Code: 3,
Message: "Bitcoind error",
Code: 3,
Message: "Bitcoind error",
Retriable: false,
},
}
Expand Down Expand Up @@ -93,18 +93,18 @@ func TestSortArrayFields(t *testing.T) {
}, clientConfiguration.AllowedOperationStatuses)
assert.Equal([]*types.Error{
{
Code: 0,
Message: "Endpoint not implemented",
Code: 0,
Message: "Endpoint not implemented",
Retriable: false,
},
{
Code: 3,
Message: "Bitcoind error",
Code: 3,
Message: "Bitcoind error",
Retriable: false,
},
{
Code: 4,
Message: "Block not found",
Code: 4,
Message: "Block not found",
Retriable: false,
},
}, clientConfiguration.AllowedErrors)
Expand Down
16 changes: 8 additions & 8 deletions cmd/validate_asserter_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package cmd
import (
"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/types"
"testing"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMatch(t *testing.T) {
Expand Down Expand Up @@ -104,10 +104,10 @@ func generateNetworkAllowAndAsserterConfiguration() (
) {
var tsi int64 = 5
allow := &types.Allow{
OperationStatuses: generateOperationStatuses(),
OperationTypes: generateOperationTypes(),
Errors: generateErrors(),
TimestampStartIndex: &tsi,
OperationStatuses: generateOperationStatuses(),
OperationTypes: generateOperationTypes(),
Errors: generateErrors(),
TimestampStartIndex: &tsi,
}
config := &asserter.Configuration{
AllowedOperationStatuses: generateOperationStatuses(),
Expand All @@ -127,7 +127,7 @@ func generateOperationStatuses() []*types.OperationStatus {
return []*types.OperationStatus{
{
Successful: true,
Status: "status0",
Status: "status0",
},
{
// Successful: false
Expand All @@ -139,11 +139,11 @@ func generateOperationStatuses() []*types.OperationStatus {
func generateErrors() []*types.Error {
return []*types.Error{
{
Code: 1,
Code: 1,
Message: "message1",
},
{
Code: 2,
Code: 2,
Message: "message2",
},
}
Expand Down
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
3 changes: 2 additions & 1 deletion examples/configuration/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
"pruning_balance_disabled": false,
"initial_balance_fetch_disabled": false
},
"perf": null
"perf": null,
"sign": null
}
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"
}
}
}
16 changes: 8 additions & 8 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewLogger(
logReconciliation bool,
checkType CheckType,
network *types.NetworkIdentifier,
logMetadataMap map[string]string,
logMetadataMap map[string]string,
fields ...zap.Field,
) (*Logger, error) {
zapLogger, err := buildZapLogger(checkType, network, fields...)
Expand Down Expand Up @@ -217,7 +217,7 @@ func (l *Logger) LogConstructionStatus(
}

statsMessage = AddMetadata(statsMessage, l.logMetadataMap)

l.lastStatsMessage = statsMessage
color.Cyan(statsMessage)
}
Expand Down Expand Up @@ -487,7 +487,7 @@ func (l *Logger) ReconcileSuccessStream(
)
reconciliationSuccessString = AddMetadata(reconciliationSuccessString, l.logMetadataMap)
color.Cyan(reconciliationSuccessString)

_, err = f.WriteString(reconciliationSuccessString)
if err != nil {
err = fmt.Errorf("failed to write reconciliation success string %s: %w", reconciliationSuccessString, err)
Expand Down Expand Up @@ -601,14 +601,14 @@ func (l *Logger) Fatal(msg string, fields ...zap.Field) {
}

// return a string of metadata
func (l *Logger) GetMetadata() string{
func (l *Logger) GetMetadata() string {
metadatMap := l.logMetadataMap
metadata := ConvertMapToString(metadatMap)
return metadata
}

// return a map of metadatMap
func (l *Logger) GetMetadataMap() map[string]string{
func (l *Logger) GetMetadataMap() map[string]string {
metadatMap := l.logMetadataMap
return metadatMap
}
Expand Down Expand Up @@ -683,7 +683,7 @@ func ConvertStringToMap(metadata string) map[string]string {
pairs := strings.Split(metadata, ",")
for _, pair := range pairs {
kv := strings.Split(pair, ":")
if(len(kv) != 2) {
if len(kv) != 2 {
log := fmt.Sprintf("the %s from %s could be transfer to key value pair", pair, metadata)
color.Yellow(log)
} else {
Expand All @@ -694,7 +694,7 @@ func ConvertStringToMap(metadata string) map[string]string {
}

// add requesrUUID to metadataMap
func AddRequestUUIDToMap(metadataMap map[string]string, requestUUID string) map[string]string{
func AddRequestUUIDToMap(metadataMap map[string]string, requestUUID string) map[string]string {
if len(requestUUID) > 0 {
metadataMap["RequestID"] = requestUUID
}
Expand All @@ -711,5 +711,5 @@ func ConvertMapToString(metadataMap map[string]string) string {
}
}
}
return metadata;
return metadata
}
6 changes: 3 additions & 3 deletions pkg/processor/balance_storage_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewBalanceStorageHelper(
// Pre-process exemptAccounts on initialization
// to provide fast lookup while syncing.
for _, account := range exemptAccounts {
// if users do not specify Currency, we add the address
// if users do not specify Currency, we add the address
// by this, all the Currencies in this address will be skipped
if account.Currency == nil {
exemptMap[account.Account.Address] = struct{}{}
Expand Down Expand Up @@ -150,12 +150,12 @@ func (h *BalanceStorageHelper) ExemptFunc() parser.ExemptOperation {
return true
}
}
// if exemptAccounts have the Account address means all the
// if exemptAccounts have the Account address means all the
// currencies in this Account address need to be skipped
_, existsAddress := h.exemptAccounts[op.Account.Address]
if existsAddress {
return existsAddress
}
}

thisAcct := types.Hash(&types.AccountCurrency{
Account: op.Account,
Expand Down
4 changes: 2 additions & 2 deletions pkg/tester/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const (

//MaxTableSize unit is MB
MaxValueLogFileSize = int64(2048)

// empty requestUUID
EmptyRequestUUID = ""
)
Expand Down Expand Up @@ -1106,7 +1106,7 @@ func (t *DataTester) HandleErr(err error, sigListeners *[]context.CancelFunc) er
}

return t.FindMissingOps(
ctx,
ctx,
err,
sigListeners,
)
Expand Down

0 comments on commit 1188c48

Please sign in to comment.