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

Update auth cli commands #6717

Merged
merged 72 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
9b4a4a1
add utils
sahith-narahari Jul 13, 2020
eaeef54
update sign cmd
sahith-narahari Jul 14, 2020
b2c8997
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 14, 2020
5abd7f1
update multisign cmd
sahith-narahari Jul 14, 2020
0a80caa
Merge branch 'sahith/update-auth-cli' of github.com:cosmos/cosmos-sdk…
sahith-narahari Jul 14, 2020
c2b4cc7
update sign batch cmd
sahith-narahari Jul 14, 2020
d9bbef8
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 15, 2020
d1eb8c1
update genutil cmd
sahith-narahari Jul 15, 2020
2d5335b
add wrap tx builder
sahith-narahari Jul 16, 2020
cc2b7e0
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 18, 2020
688e9cd
update gentx cli
sahith-narahari Jul 19, 2020
d4cbf42
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 19, 2020
425cbfe
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 19, 2020
27a57a5
Merge branch 'sahith/update-auth-cli' of github.com:cosmos/cosmos-sdk…
sahith-narahari Jul 19, 2020
64f7aaf
update validate sigs cmd
sahith-narahari Jul 19, 2020
987af37
fix lint
sahith-narahari Jul 19, 2020
167df15
add flag reader to cli
sahith-narahari Jul 19, 2020
8f82734
update marshaler for batchscan
sahith-narahari Jul 19, 2020
baebff4
add register query server
sahith-narahari Jul 19, 2020
ed36414
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 19, 2020
3122b59
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 20, 2020
57c8798
update to master
sahith-narahari Jul 20, 2020
39bc7f4
remove depricated methods
sahith-narahari Jul 20, 2020
1afa4d3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 20, 2020
d101c34
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 21, 2020
012e320
fix keyring issue
sahith-narahari Jul 21, 2020
e955ca6
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 23, 2020
3dec8f7
update wraptx
sahith-narahari Jul 23, 2020
7a42cfe
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 23, 2020
ac46abe
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 24, 2020
ff733fc
Fix batch scan
sahith-narahari Jul 24, 2020
30bca25
fix register interfaces issue
sahith-narahari Jul 24, 2020
b390579
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 25, 2020
695b77d
update printOutput
sahith-narahari Jul 27, 2020
4be2e51
Update Validate Sigs test
sahith-narahari Jul 27, 2020
b4f8c77
Merge branch 'master' into sahith/update-auth-cli
aaronc Jul 27, 2020
6fe5838
Merge branch 'master' into sahith/update-auth-cli
aaronc Jul 27, 2020
467d692
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into sa…
aaronc Jul 27, 2020
3a0efa5
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 27, 2020
6d3297e
WIP on signature JSON
aaronc Jul 27, 2020
15613cf
Merge remote-tracking branch 'origin/sahith/update-auth-cli' into sah…
aaronc Jul 27, 2020
4f2b71a
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into sa…
aaronc Jul 27, 2020
819aa66
Cleanup
aaronc Jul 27, 2020
07f22b2
Cleanup
aaronc Jul 27, 2020
e004e1b
Test fixes
aaronc Jul 27, 2020
1fe8088
Test fixes
aaronc Jul 27, 2020
2e2099d
Merge branch 'master' into sahith/update-auth-cli
aaronc Jul 27, 2020
88bdb62
Fixes
aaronc Jul 27, 2020
9f04601
WIP on tests
aaronc Jul 28, 2020
013c481
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/upd…
sahith-narahari Jul 28, 2020
1252fbf
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 28, 2020
72d3982
Fix gov tests
sahith-narahari Jul 28, 2020
f95810a
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 28, 2020
28bbc0f
fix lint
sahith-narahari Jul 28, 2020
5aa68e7
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 28, 2020
62ff121
fix MultiSign tests
sahith-narahari Jul 28, 2020
5b3c906
Merge branch 'sahith/update-auth-cli' of github.com:cosmos/cosmos-sdk…
sahith-narahari Jul 28, 2020
6c1a0fa
fix tests
sahith-narahari Jul 28, 2020
29dab69
refactor tests
sahith-narahari Jul 28, 2020
750c5ac
Cleanup
aaronc Jul 29, 2020
074ee7b
Merge branch 'master' into sahith/update-auth-cli
aaronc Jul 29, 2020
0c94f78
Merge branch 'master' into sahith/update-auth-cli
sahith-narahari Jul 29, 2020
7771328
Address review comments
sahith-narahari Jul 29, 2020
423cbcb
Update encode
sahith-narahari Jul 29, 2020
d9b15df
Merge branch 'master' into sahith/update-auth-cli
aaronc Jul 29, 2020
0136c11
Test fix
aaronc Jul 29, 2020
2073e3f
Rename SignData func
sahith-narahari Jul 29, 2020
af89c16
Fix lint
sahith-narahari Jul 29, 2020
c5d7181
Merge branch 'master' into sahith/update-auth-cli
aaronc Jul 29, 2020
c6a7353
Fix lint
sahith-narahari Jul 29, 2020
ecf0679
Revert ReadTxCommandFlags
aaronc Jul 29, 2020
8a4c583
Merge remote-tracking branch 'origin/sahith/update-auth-cli' into sah…
aaronc Jul 29, 2020
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
50 changes: 33 additions & 17 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,47 @@ func ReadTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
return clientCtx, err
}

genOnly, _ := flagSet.GetBool(flags.FlagGenerateOnly)
clientCtx = clientCtx.WithGenerateOnly(genOnly)
if flagSet.Changed(flags.FlagGenerateOnly) {
aaronc marked this conversation as resolved.
Show resolved Hide resolved
genOnly, _ := flagSet.GetBool(flags.FlagGenerateOnly)
clientCtx = clientCtx.WithGenerateOnly(genOnly)

dryRun, _ := flagSet.GetBool(flags.FlagDryRun)
clientCtx = clientCtx.WithSimulation(dryRun)
}

offline, _ := flagSet.GetBool(flags.FlagOffline)
clientCtx = clientCtx.WithOffline(offline)
if flagSet.Changed(flags.FlagDryRun) {
dryRun, _ := flagSet.GetBool(flags.FlagDryRun)
clientCtx = clientCtx.WithSimulation(dryRun)
}

useLedger, _ := flagSet.GetBool(flags.FlagUseLedger)
clientCtx = clientCtx.WithUseLedger(useLedger)
if flagSet.Changed(flags.FlagOffline) {
offline, _ := flagSet.GetBool(flags.FlagOffline)
clientCtx = clientCtx.WithOffline(offline)
}

bMode, _ := flagSet.GetString(flags.FlagBroadcastMode)
clientCtx = clientCtx.WithBroadcastMode(bMode)
if flagSet.Changed(flags.FlagUseLedger) {
useLedger, _ := flagSet.GetBool(flags.FlagUseLedger)
clientCtx = clientCtx.WithUseLedger(useLedger)
}

skipConfirm, _ := flagSet.GetBool(flags.FlagSkipConfirmation)
clientCtx = clientCtx.WithSkipConfirmation(skipConfirm)
if flagSet.Changed(flags.FlagBroadcastMode) {
bMode, _ := flagSet.GetString(flags.FlagBroadcastMode)
clientCtx = clientCtx.WithBroadcastMode(bMode)
}

if flagSet.Changed(flags.FlagSkipConfirmation) {
skipConfirm, _ := flagSet.GetBool(flags.FlagSkipConfirmation)
clientCtx = clientCtx.WithSkipConfirmation(skipConfirm)

from, _ := flagSet.GetString(flags.FlagFrom)
fromAddr, fromName, err := GetFromFields(clientCtx.Keyring, from, clientCtx.GenerateOnly)
if err != nil {
return clientCtx, err
}

clientCtx = clientCtx.WithFrom(from).WithFromAddress(fromAddr).WithFromName(fromName)
if flagSet.Changed(flags.FlagFrom) {
from, _ := flagSet.GetString(flags.FlagFrom)
fromAddr, fromName, err := GetFromFields(clientCtx.Keyring, from, clientCtx.GenerateOnly)
if err != nil {
return clientCtx, err
}

clientCtx = clientCtx.WithFrom(from).WithFromAddress(fromAddr).WithFromName(fromName)
}

return clientCtx, nil
}
Expand Down
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super similar to SignatureV2. Could we remove SignatureV2 and use this one everywhere?

Copy link
Member

@aaronc aaronc Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine we can. But it will probably be a decent size refactor. Are you fine with that in this PR?

// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have message ModeInfo, and if I understand this, this is ModeInfo+Signature. I'm still not 100% aware of protobuf extending types and such, but is there a way to use ModeInfo?

Copy link
Member

@aaronc aaronc Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not without adding a field on ModeInfo that will be unused in transactions. The challenge is that you still need to distinguish between single and multisigs.

// 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