Proto Tx with Any (#7276)
* WIP on protobuf keys * Use Type() and Bytes() in sr25519 pub key Equals * Add tests * Add few more tests * Update other pub/priv key types Equals * Fix PrivKey's Sign method * Rename variables in tests * Fix infinite recursive calls * Use tm ed25519 keys * Add Sign and VerifySignature tests * Remove ed25519 and sr25519 references * proto linting * Add proto crypto file * Implement some of the new multisig proto type methods * Add tests for MultisigThresholdPubKey * Add tests for pubkey pb/amino conversion functions * Move crypto types.go and register new proto pubkeys * Add missing pointer ref * Address review comments * panic in MultisigThresholdPubKey VerifySignature * Use internal crypto.PubKey in multisig * Add tests for MultisigThresholdPubKey VerifyMultisignature * Only keep LegacyAminoMultisigThresholdPubKey and move to proto keys to v1 * Remove conversion functions and introduce internal PubKey type * Override Amino marshaling for proto pubkeys * Merge master * Make proto-gen * Start removal of old PubKeyMultisigThreshold references * Fix tests * Fix solomachine * Fix ante handler tests * Pull latest go-amino * Remove ed25519 * Remove old secp256k1 PubKey and PrivKey * Uncomment test case * Fix linting issues * More linting * Revert tests keys values * Add Amino overrides to proto keys * Add pubkey test * Fix tests * Use threshold isntead of K * Standardize Type * Revert standardize types commit * Fix build * Fix lint * Fix lint * Add comment * Register crypto.PubKey * Add empty key in BuildSimTx * Simplify proto names * Unpack interfaces for signing desc * Fix IBC tests? * Format proto * Use secp256k1 in ibc * Fixed merge issues * Uncomment tests * Update x/ibc/testing/solomachine.go * UnpackInterfaces for solomachine types * Remove old multisig * Add amino marshal for multisig * Fix lint * Correctly register amino * One test left! * Remove old struct * Fix test * Fix test * Unpack into tmcrypto * Remove old threshold pubkey tests * Fix register amino * Fix lint * Use sdk crypto PubKey in multisig UnpackInterfaces * Potential fix? * Register LegacyAminoPubKey * Register our own PubKey * Register tmcrypto PubKey * Register both PubKeys * Register interfaces in test * Refactor fiels * Add comments * Use anil's suggestion * Add norace back * Check nil * Address comments * FIx lint * Add tests for solomachine unpack interfaces * Fix query tx by hash * Better name in amino register * Display StdTx instead of proto Tx * Remove useless check Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com> Co-authored-by: blushi <marie.gauthier63@gmail.com> Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
This commit is contained in:
co-authored by
Aaron Craelius
blushi
Alexander Bezobchuk
colin axnér
parent
535510be1f
commit
7cd25abb87
+4
-2
@@ -9,15 +9,17 @@ import (
|
||||
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
||||
)
|
||||
|
||||
// RegisterLegacyAminoCodec registers types with the Amino codec.
|
||||
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
|
||||
vesting.RegisterLegacyAminoCodec(cdc)
|
||||
sdk.RegisterLegacyAminoCodec(cdc)
|
||||
cryptocodec.RegisterCrypto(cdc)
|
||||
vesting.RegisterLegacyAminoCodec(cdc)
|
||||
}
|
||||
|
||||
// RegisterInterfaces registers Interfaces from sdk/types and vesting
|
||||
// RegisterInterfaces registers Interfaces from sdk/types, vesting, crypto, tx.
|
||||
func RegisterInterfaces(interfaceRegistry types.InterfaceRegistry) {
|
||||
sdk.RegisterInterfaces(interfaceRegistry)
|
||||
txtypes.RegisterInterfaces(interfaceRegistry)
|
||||
cryptocodec.RegisterInterfaces(interfaceRegistry)
|
||||
vesting.RegisterInterfaces(interfaceRegistry)
|
||||
}
|
||||
|
||||
-109
@@ -1,109 +0,0 @@
|
||||
package std
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
"github.com/tendermint/tendermint/crypto/sr25519"
|
||||
|
||||
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
)
|
||||
|
||||
// DefaultPublicKeyCodec implements the standard PublicKeyCodec for the SDK which
|
||||
// supports a standard set of public key types
|
||||
type DefaultPublicKeyCodec struct{}
|
||||
|
||||
var _ types.PublicKeyCodec = DefaultPublicKeyCodec{}
|
||||
|
||||
// Decode implements the PublicKeyCodec.Decode method
|
||||
// TODO To be removed in https://github.com/cosmos/cosmos-sdk/pull/7276
|
||||
func (cdc DefaultPublicKeyCodec) Decode(key *types.PublicKey) (crypto.PubKey, error) {
|
||||
// key being nil is allowed as all fields in proto are optional
|
||||
if key == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if key.Sum == nil {
|
||||
return nil, nil
|
||||
}
|
||||
switch key := key.Sum.(type) {
|
||||
case *types.PublicKey_Secp256K1:
|
||||
n := len(key.Secp256K1)
|
||||
if n != secp256k1.PubKeySize {
|
||||
return nil, fmt.Errorf("wrong length %d for secp256k1 public key", n)
|
||||
}
|
||||
|
||||
res := make([]byte, secp256k1.PubKeySize)
|
||||
copy(res, key.Secp256K1)
|
||||
return &secp256k1.PubKey{Key: res}, nil
|
||||
case *types.PublicKey_Ed25519:
|
||||
n := len(key.Ed25519)
|
||||
if n != ed25519.PubKeySize {
|
||||
return nil, fmt.Errorf("wrong length %d for ed25519 public key", n)
|
||||
}
|
||||
|
||||
res := make(ed25519.PubKey, ed25519.PubKeySize)
|
||||
copy(res, key.Ed25519)
|
||||
return res, nil
|
||||
case *types.PublicKey_Sr25519:
|
||||
n := len(key.Sr25519)
|
||||
if n != sr25519.PubKeySize {
|
||||
return nil, fmt.Errorf("wrong length %d for sr25519 public key", n)
|
||||
}
|
||||
|
||||
res := make(sr25519.PubKey, sr25519.PubKeySize)
|
||||
copy(res, key.Sr25519)
|
||||
|
||||
return res, nil
|
||||
case *types.PublicKey_Multisig:
|
||||
pubKeys := key.Multisig.PubKeys
|
||||
resKeys := make([]crypto.PubKey, len(pubKeys))
|
||||
for i, k := range pubKeys {
|
||||
dk, err := cdc.Decode(k)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resKeys[i] = dk
|
||||
}
|
||||
|
||||
return kmultisig.NewLegacyAminoPubKey(int(key.Multisig.K), resKeys), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("can't decode PubKey of type %T. Use a custom PublicKeyCodec instead", key)
|
||||
}
|
||||
}
|
||||
|
||||
// Encode implements the PublicKeyCodec.Encode method
|
||||
// TODO To be removed in https://github.com/cosmos/cosmos-sdk/pull/7276
|
||||
|
||||
func (cdc DefaultPublicKeyCodec) Encode(key crypto.PubKey) (*types.PublicKey, error) {
|
||||
if key == nil {
|
||||
return &types.PublicKey{}, nil
|
||||
}
|
||||
switch key := key.(type) {
|
||||
case *secp256k1.PubKey:
|
||||
return &types.PublicKey{Sum: &types.PublicKey_Secp256K1{Secp256K1: key.Key}}, nil
|
||||
case ed25519.PubKey:
|
||||
return &types.PublicKey{Sum: &types.PublicKey_Ed25519{Ed25519: key}}, nil
|
||||
case sr25519.PubKey:
|
||||
return &types.PublicKey{Sum: &types.PublicKey_Sr25519{Sr25519: key}}, nil
|
||||
case *kmultisig.LegacyAminoPubKey:
|
||||
pubKeys := key.PubKeys
|
||||
resKeys := make([]*types.PublicKey, len(pubKeys))
|
||||
for i, any := range pubKeys {
|
||||
k := any.GetCachedValue().(crypto.PubKey)
|
||||
dk, err := cdc.Encode(k)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resKeys[i] = dk
|
||||
}
|
||||
return &types.PublicKey{Sum: &types.PublicKey_Multisig{Multisig: &types.PubKeyMultisigThreshold{
|
||||
K: key.Threshold,
|
||||
PubKeys: resKeys,
|
||||
}}}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("can't encode PubKey of type %T. Use a custom PublicKeyCodec instead", key)
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package std_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
|
||||
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
"github.com/cosmos/cosmos-sdk/std"
|
||||
)
|
||||
|
||||
func roundTripTest(t *testing.T, pubKey crypto.PubKey) {
|
||||
cdc := std.DefaultPublicKeyCodec{}
|
||||
|
||||
pubKeyEnc, err := cdc.Encode(pubKey)
|
||||
require.NoError(t, err)
|
||||
pubKeyDec, err := cdc.Decode(pubKeyEnc)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, pubKey, pubKeyDec)
|
||||
}
|
||||
|
||||
func TestDefaultPublicKeyCodec(t *testing.T) {
|
||||
roundTripTest(t, nil)
|
||||
|
||||
roundTripTest(t, crypto.PubKey(nil))
|
||||
|
||||
pubKeySecp256k1 := secp256k1.GenPrivKey().PubKey()
|
||||
roundTripTest(t, pubKeySecp256k1)
|
||||
|
||||
pubKeyMultisig := kmultisig.NewLegacyAminoPubKey(2, []crypto.PubKey{
|
||||
pubKeySecp256k1, secp256k1.GenPrivKey().PubKey(), secp256k1.GenPrivKey().PubKey(),
|
||||
})
|
||||
roundTripTest(t, pubKeyMultisig)
|
||||
}
|
||||
Reference in New Issue
Block a user