Skip to content

Commit

Permalink
Update auth cli commands (#6717)
Browse files Browse the repository at this point in the history
* add utils

* update sign cmd

* update multisign cmd

* update sign batch cmd

* update genutil cmd

* add wrap tx builder

* update gentx cli

* update validate sigs cmd

* fix lint

* add flag reader to cli

* update marshaler for batchscan

* add register query server

* update to master

* remove depricated methods

* fix keyring issue

* update wraptx

* Fix batch scan

* fix register interfaces issue

* update printOutput

* Update Validate Sigs test

* WIP on signature JSON

* Cleanup

* Cleanup

* Test fixes

* Test fixes

* Fixes

* WIP on tests

* Fix gov tests

* fix lint

* fix MultiSign tests

* fix tests

* refactor tests

* Cleanup

* Address review comments

* Update encode

* Test fix

* Rename SignData func

* Fix lint

* Fix lint

* Revert ReadTxCommandFlags

Co-authored-by: Aaron Craelius <aaron@regen.network>
Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 29, 2020
1 parent cf078c0 commit 72ebafe
Show file tree
Hide file tree
Showing 31 changed files with 1,985 additions and 536 deletions.
11 changes: 11 additions & 0 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ func (ctx Context) WithInterfaceRegistry(interfaceRegistry codectypes.InterfaceR
return ctx
}

// PrintString prints the raw string to ctx.Output or os.Stdout
func (ctx Context) PrintString(str string) error {
writer := ctx.Output
if writer == nil {
writer = os.Stdout
}

_, err := writer.Write([]byte(str))
return err
}

// PrintOutput outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
// will be JSON encoded using ctx.JSONMarshaler. An error is returned upon failure.
Expand Down
11 changes: 11 additions & 0 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,14 @@ func (f Factory) WithSimulateAndExecute(sim bool) Factory {
f.simulateAndExecute = sim
return f
}

// SignMode returns the sign mode configured in the Factory
func (f Factory) SignMode() signing.SignMode {
return f.signMode
}

// WithSignMode returns a copy of the Factory with an updated sign mode value.
func (f Factory) WithSignMode(mode signing.SignMode) Factory {
f.signMode = mode
return f
}
7 changes: 6 additions & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func GenerateTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
return err
}

return clientCtx.PrintOutput(tx.GetTx())
json, err := clientCtx.TxConfig.TxJSONEncoder()(tx.GetTx())
if err != nil {
return err
}

return clientCtx.PrintString(fmt.Sprintf("%s\n", json))
}

// BroadcastTx attempts to generate, sign and broadcast a transaction with the
Expand Down
2 changes: 2 additions & 0 deletions client/tx_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type (
TxDecoder() sdk.TxDecoder
TxJSONEncoder() sdk.TxEncoder
TxJSONDecoder() sdk.TxDecoder
MarshalSignatureJSON([]signingtypes.SignatureV2) ([]byte, error)
UnmarshalSignatureJSON([]byte) ([]signingtypes.SignatureV2, error)
}

// TxConfig defines an interface a client can utilize to generate an
Expand Down
4 changes: 3 additions & 1 deletion codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ type ProtoCodec struct {
anyUnpacker types.AnyUnpacker
}

func NewProtoCodec(anyUnpacker types.AnyUnpacker) Marshaler {
var _ Marshaler = &ProtoCodec{}

func NewProtoCodec(anyUnpacker types.AnyUnpacker) *ProtoCodec {
return &ProtoCodec{anyUnpacker: anyUnpacker}
}

Expand Down
2 changes: 1 addition & 1 deletion codec/proto_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func mustAny(msg proto.Message) *types.Any {
}

func BenchmarkProtoCodecMarshalBinaryLengthPrefixed(b *testing.B) {
var pCdc = codec.NewProtoCodec(types.NewInterfaceRegistry()).(*codec.ProtoCodec)
var pCdc = codec.NewProtoCodec(types.NewInterfaceRegistry())
var msg = &testdata.HasAnimal{
X: 1000,
Animal: mustAny(&testdata.HasAnimal{
Expand Down
3 changes: 3 additions & 0 deletions crypto/types/multisig/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type PubKey interface {

// GetPubKeys returns the crypto.PubKey's nested within the multi-sig PubKey
GetPubKeys() []crypto.PubKey

// GetThreshold returns the threshold number of signatures that must be obtained to verify a signature.
GetThreshold() uint
}

// GetSignBytesFunc defines a function type which returns sign bytes for a given SignMode or an error.
Expand Down
5 changes: 5 additions & 0 deletions crypto/types/multisig/threshold_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,8 @@ func (pk PubKeyMultisigThreshold) Equals(other crypto.PubKey) bool {
}
return true
}

// GetThreshold implements the PubKey.GetThreshold method
func (pk PubKeyMultisigThreshold) GetThreshold() uint {
return pk.K
}
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4er
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -646,7 +644,6 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20u
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down Expand Up @@ -772,7 +769,5 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
48 changes: 48 additions & 0 deletions proto/cosmos/tx/signing/signing.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
syntax = "proto3";
package cosmos.tx.signing;

import "cosmos/crypto/crypto.proto";

option go_package = "github.com/cosmos/cosmos-sdk/types/tx/signing";

// SignMode represents a signing mode with its own security guarantees
Expand All @@ -20,3 +22,49 @@ enum SignMode {
// Amino JSON and will be removed in the future
SIGN_MODE_LEGACY_AMINO_JSON = 127;
}

// SignatureDescriptors wraps multiple SignatureDescriptor's.
message SignatureDescriptors {
// signatures are the signature descriptors
repeated SignatureDescriptor signatures = 1;
}

// SignatureDescriptor is a convenience type which represents the full data for a
// signature including the public key of the signer, signing modes and the signature
// itself. It is primarily used for coordinating signatures between clients.
message SignatureDescriptor {
// public_key is the public key of the signer
cosmos.crypto.PublicKey public_key = 1;

Data data = 2;

// Data represents signature data
message Data {
// sum is the oneof that specifies whether this represents single or multi-signature data
oneof sum {
// single represents a single signer
Single single = 1;

// multi represents a multisig signer
Multi multi = 2;
}

// Single is the signature data for a single signer
message Single {
// mode is the signing mode of the single signer
cosmos.tx.signing.SignMode mode = 1;

// signature is the raw signature bytes
bytes signature = 2;
}

// Multi is the signature data for a multisig public key
message Multi {
// bitarray specifies which keys within the multisig are signing
cosmos.crypto.CompactBitArray bitarray = 1;

// signatures is the signatures of the multi-signature
repeated Data signatures = 2;
}
}
}
8 changes: 4 additions & 4 deletions simapp/params/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (

// MakeEncodingConfig creates an EncodingConfig for an amino based test configuration.
func MakeEncodingConfig() EncodingConfig {
cdc := codec.New()
amino := codec.New()
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := codec.NewHybridCodec(cdc, interfaceRegistry)
txGen := tx.NewTxConfig(interfaceRegistry, std.DefaultPublicKeyCodec{}, tx.DefaultSignModeHandler())
marshaler := codec.NewHybridCodec(amino, interfaceRegistry)
txGen := tx.NewTxConfig(codec.NewProtoCodec(interfaceRegistry), std.DefaultPublicKeyCodec{}, tx.DefaultSignModeHandler())

return EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: txGen,
Amino: cdc,
Amino: amino,
}
}
3 changes: 1 addition & 2 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/client/tx"

"github.com/stretchr/testify/require"
tmcfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
Expand All @@ -29,6 +27,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
Expand Down
37 changes: 37 additions & 0 deletions types/tx/sig_desc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion types/tx/signing/signature.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package signing

import "github.com/tendermint/tendermint/crypto"
import (
"fmt"

"github.com/tendermint/tendermint/crypto"
)

// SignatureV2 is a convenience type that is easier to use in application logic
// than the protobuf SignerInfo's and raw signature bytes. It goes beyond the
Expand All @@ -15,3 +19,64 @@ type SignatureV2 struct {
// the signatures themselves for either single or multi-signatures.
Data SignatureData
}

// SignatureDataToProto converts a SignatureData to SignatureDescriptor_Data.
// SignatureDescriptor_Data is considered an encoding type whereas SignatureData is used for
// business logic.
func SignatureDataToProto(data SignatureData) *SignatureDescriptor_Data {
switch data := data.(type) {
case *SingleSignatureData:
return &SignatureDescriptor_Data{
Sum: &SignatureDescriptor_Data_Single_{
Single: &SignatureDescriptor_Data_Single{
Mode: data.SignMode,
Signature: data.Signature,
},
},
}
case *MultiSignatureData:
descDatas := make([]*SignatureDescriptor_Data, len(data.Signatures))

for j, d := range data.Signatures {
descDatas[j] = SignatureDataToProto(d)
}

return &SignatureDescriptor_Data{
Sum: &SignatureDescriptor_Data_Multi_{
Multi: &SignatureDescriptor_Data_Multi{
Bitarray: data.BitArray,
Signatures: descDatas,
},
},
}
default:
panic(fmt.Errorf("unexpected case %+v", data))
}
}

// SignatureDataFromProto converts a SignatureDescriptor_Data to SignatureData.
// SignatureDescriptor_Data is considered an encoding type whereas SignatureData is used for
// business logic.
func SignatureDataFromProto(descData *SignatureDescriptor_Data) SignatureData {
switch descData := descData.Sum.(type) {
case *SignatureDescriptor_Data_Single_:
return &SingleSignatureData{
SignMode: descData.Single.Mode,
Signature: descData.Single.Signature,
}
case *SignatureDescriptor_Data_Multi_:
multi := descData.Multi
datas := make([]SignatureData, len(multi.Signatures))

for j, d := range multi.Signatures {
datas[j] = SignatureDataFromProto(d)
}

return &MultiSignatureData{
BitArray: multi.Bitarray,
Signatures: datas,
}
default:
panic(fmt.Errorf("unexpected case %+v", descData))
}
}
Loading

0 comments on commit 72ebafe

Please sign in to comment.