cosmos-sdk/std/pubkey.go
Marko 8de96d16f9
tendermint: update to rc3 (#6892)
* modify light imports

* change abci.header to tmproto.header

* use rc

* rc

* fix import

* Merge PR #6893: fix key imports

* fix rc2

* tendermint: update 3 (#6899)

* tendermint: update 4 (#6919)

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>

* tendermint: update 5 (#6923)

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* bump to latest master

* tendermint: update (#6972)

Co-authored-by: Aleksandr Bezobchuk <aleks.bezobchuk@gmail.com>
Co-authored-by: Cory Levinson <cjlevinson@gmail.com>

* Update x/ibc/07-tendermint/types/test_utils.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* address comment

* go mod

* bring back things

* fix test

* update tm proto files

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <aleks.bezobchuk@gmail.com>
Co-authored-by: Cory Levinson <cjlevinson@gmail.com>
Co-authored-by: Federico Kunze <federico.kunze94@gmail.com>
2020-08-14 13:58:53 -04:00

96 lines
2.9 KiB
Go

package std
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
"github.com/tendermint/tendermint/crypto"
ed255192 "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sr25519"
)
// 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
func (cdc DefaultPublicKeyCodec) Decode(key *types.PublicKey) (crypto.PubKey, error) {
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(secp256k1.PubKey, secp256k1.PubKeySize)
copy(res, key.Secp256K1)
return res, nil
case *types.PublicKey_Ed25519:
n := len(key.Ed25519)
if n != ed255192.PubKeySize {
return nil, fmt.Errorf("wrong length %d for ed25519 public key", n)
}
res := make(ed255192.PubKey, ed255192.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 multisig.NewPubKeyMultisigThreshold(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
func (cdc DefaultPublicKeyCodec) Encode(key crypto.PubKey) (*types.PublicKey, error) {
switch key := key.(type) {
case secp256k1.PubKey:
return &types.PublicKey{Sum: &types.PublicKey_Secp256K1{Secp256K1: key}}, nil
case ed255192.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 multisig.PubKeyMultisigThreshold:
pubKeys := key.PubKeys
resKeys := make([]*types.PublicKey, len(pubKeys))
for i, k := range pubKeys {
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: uint32(key.K),
PubKeys: resKeys,
}}}, nil
default:
return nil, fmt.Errorf("can't encode PubKey of type %T. Use a custom PublicKeyCodec instead", key)
}
}