2020-06-04 03:38:24 -07:00
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2020-09-25 01:41:16 -07:00
|
|
|
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
|
2020-06-04 03:38:24 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/sr25519"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-09-25 01:41:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
2020-09-16 04:08:55 -07:00
|
|
|
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
2020-09-18 02:40:39 -07:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2020-06-04 03:38:24 -07:00
|
|
|
)
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
var amino *codec.LegacyAmino
|
2020-06-04 03:38:24 -07:00
|
|
|
|
|
|
|
func init() {
|
2020-09-07 07:47:12 -07:00
|
|
|
amino = codec.NewLegacyAmino()
|
2020-06-04 03:38:24 -07:00
|
|
|
RegisterCrypto(amino)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterCrypto registers all crypto dependency types with the provided Amino
|
|
|
|
// codec.
|
2020-08-10 12:41:21 -07:00
|
|
|
func RegisterCrypto(cdc *codec.LegacyAmino) {
|
2020-09-18 02:40:39 -07:00
|
|
|
// TODO We now register both Tendermint's PubKey and our own PubKey. In the
|
|
|
|
// long-term, we should move away from Tendermint's PubKey, and delete this
|
|
|
|
// first line.
|
2020-06-04 03:38:24 -07:00
|
|
|
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
|
2020-09-18 02:40:39 -07:00
|
|
|
cdc.RegisterInterface((*cryptotypes.PubKey)(nil), nil)
|
2020-08-14 10:58:53 -07:00
|
|
|
cdc.RegisterConcrete(sr25519.PubKey{},
|
|
|
|
sr25519.PubKeyName, nil)
|
2020-09-25 01:41:16 -07:00
|
|
|
// TODO Same as above, for ED25519
|
|
|
|
cdc.RegisterConcrete(tmed25519.PubKey{},
|
|
|
|
tmed25519.PubKeyName, nil)
|
|
|
|
cdc.RegisterConcrete(&ed25519.PubKey{},
|
|
|
|
ed25519.PubKeyName, nil)
|
2020-09-16 04:08:55 -07:00
|
|
|
cdc.RegisterConcrete(&secp256k1.PubKey{},
|
2020-08-14 10:58:53 -07:00
|
|
|
secp256k1.PubKeyName, nil)
|
2020-09-16 04:08:55 -07:00
|
|
|
cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
|
2020-09-18 02:40:39 -07:00
|
|
|
kmultisig.PubKeyAminoRoute, nil)
|
2020-06-04 03:38:24 -07:00
|
|
|
|
|
|
|
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
|
2020-08-14 10:58:53 -07:00
|
|
|
cdc.RegisterConcrete(sr25519.PrivKey{},
|
|
|
|
sr25519.PrivKeyName, nil)
|
2020-09-25 01:41:16 -07:00
|
|
|
// TODO Same as above
|
|
|
|
cdc.RegisterConcrete(tmed25519.PrivKey{},
|
|
|
|
tmed25519.PrivKeyName, nil)
|
|
|
|
cdc.RegisterConcrete(&ed25519.PrivKey{},
|
|
|
|
ed25519.PrivKeyName, nil)
|
2020-09-16 04:08:55 -07:00
|
|
|
cdc.RegisterConcrete(&secp256k1.PrivKey{},
|
2020-08-14 10:58:53 -07:00
|
|
|
secp256k1.PrivKeyName, nil)
|
2020-06-04 03:38:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey
|
|
|
|
func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) {
|
|
|
|
err = amino.UnmarshalBinaryBare(privKeyBytes, &privKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PubKeyFromBytes unmarshals public key bytes and returns a PubKey
|
|
|
|
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) {
|
|
|
|
err = amino.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
|
|
|
|
return
|
|
|
|
}
|