crypto: remove key suffixes (#4941)

## Description

- remove keyname suffix from keys


Closes: #2228
This commit is contained in:
Marko
2020-06-03 07:46:29 +02:00
committed by GitHub
parent 0da7d87351
commit 99d88cbe2f
28 changed files with 182 additions and 173 deletions

View File

@@ -17,7 +17,7 @@ var cdc = amino.NewCodec()
// nameTable is used to map public key concrete types back
// to their registered amino names. This should eventually be handled
// by amino. Example usage:
// nameTable[reflect.TypeOf(ed25519.PubKeyEd25519{})] = ed25519.PubKeyAminoName
// nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
var nameTable = make(map[reflect.Type]string, 3)
func init() {
@@ -31,10 +31,10 @@ func init() {
// TODO: Have amino provide a way to go from concrete struct to route directly.
// Its currently a private API
nameTable[reflect.TypeOf(ed25519.PubKeyEd25519{})] = ed25519.PubKeyAminoName
nameTable[reflect.TypeOf(sr25519.PubKeySr25519{})] = sr25519.PubKeyAminoName
nameTable[reflect.TypeOf(secp256k1.PubKeySecp256k1{})] = secp256k1.PubKeyAminoName
nameTable[reflect.TypeOf(multisig.PubKeyMultisigThreshold{})] = multisig.PubKeyMultisigThresholdAminoRoute
nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyAminoName
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyAminoName
nameTable[reflect.TypeOf(multisig.PubKey{})] = multisig.PubKeyAminoRoute
}
// PubkeyAminoName returns the amino route of a pubkey
@@ -49,21 +49,21 @@ func PubkeyAminoName(cdc *amino.Codec, key crypto.PubKey) (string, bool) {
func RegisterAmino(cdc *amino.Codec) {
// These are all written here instead of
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
cdc.RegisterConcrete(ed25519.PubKeyEd25519{},
cdc.RegisterConcrete(ed25519.PubKey{},
ed25519.PubKeyAminoName, nil)
cdc.RegisterConcrete(sr25519.PubKeySr25519{},
cdc.RegisterConcrete(sr25519.PubKey{},
sr25519.PubKeyAminoName, nil)
cdc.RegisterConcrete(secp256k1.PubKeySecp256k1{},
cdc.RegisterConcrete(secp256k1.PubKey{},
secp256k1.PubKeyAminoName, nil)
cdc.RegisterConcrete(multisig.PubKeyMultisigThreshold{},
multisig.PubKeyMultisigThresholdAminoRoute, nil)
cdc.RegisterConcrete(multisig.PubKey{},
multisig.PubKeyAminoRoute, nil)
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
cdc.RegisterConcrete(ed25519.PrivKeyEd25519{},
cdc.RegisterConcrete(ed25519.PrivKey{},
ed25519.PrivKeyAminoName, nil)
cdc.RegisterConcrete(sr25519.PrivKeySr25519{},
cdc.RegisterConcrete(sr25519.PrivKey{},
sr25519.PrivKeyAminoName, nil)
cdc.RegisterConcrete(secp256k1.PrivKeySecp256k1{},
cdc.RegisterConcrete(secp256k1.PrivKey{},
secp256k1.PrivKeyAminoName, nil)
}

View File

@@ -58,13 +58,13 @@ func ExamplePrintRegisteredTypes() {
cdc.PrintTypes(os.Stdout)
// Output: | Type | Name | Prefix | Length | Notes |
//| ---- | ---- | ------ | ----- | ------ |
//| PubKeyEd25519 | tendermint/PubKeyEd25519 | 0x1624DE64 | 0x20 | |
//| PubKeySr25519 | tendermint/PubKeySr25519 | 0x0DFB1005 | 0x20 | |
//| PubKeySecp256k1 | tendermint/PubKeySecp256k1 | 0xEB5AE987 | 0x21 | |
//| PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | |
//| PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 | |
//| PrivKeySr25519 | tendermint/PrivKeySr25519 | 0x2F82D78B | 0x20 | |
//| PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 | |
//| PubKey | tendermint/PubKeyEd25519 | 0x1624DE64 | 0x20 | |
//| PubKey | tendermint/PubKeySr25519 | 0x0DFB1005 | 0x20 | |
//| PubKey | tendermint/PubKeySecp256k1 | 0xEB5AE987 | 0x21 | |
//| PubKey | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | |
//| PrivKey | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 | |
//| PrivKey | tendermint/PrivKeySr25519 | 0x2F82D78B | 0x20 | |
//| PrivKey | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 | |
}
func TestKeyEncodings(t *testing.T) {
@@ -148,10 +148,10 @@ func TestPubkeyAminoName(t *testing.T) {
want string
found bool
}{
{ed25519.PubKeyEd25519{}, ed25519.PubKeyAminoName, true},
{sr25519.PubKeySr25519{}, sr25519.PubKeyAminoName, true},
{secp256k1.PubKeySecp256k1{}, secp256k1.PubKeyAminoName, true},
{multisig.PubKeyMultisigThreshold{}, multisig.PubKeyMultisigThresholdAminoRoute, true},
{ed25519.PubKey{}, ed25519.PubKeyAminoName, true},
{sr25519.PubKey{}, sr25519.PubKeyAminoName, true},
{secp256k1.PubKey{}, secp256k1.PubKeyAminoName, true},
{multisig.PubKey{}, multisig.PubKeyAminoRoute, true},
}
for i, tc := range tests {
got, found := PubkeyAminoName(cdc, tc.key)
@@ -229,8 +229,8 @@ func TestRegisterKeyType(t *testing.T) {
cdc = amino.NewCodec()
nameTable = make(map[reflect.Type]string, 3)
RegisterAmino(cdc)
nameTable[reflect.TypeOf(ed25519.PubKeyEd25519{})] = ed25519.PubKeyAminoName
nameTable[reflect.TypeOf(sr25519.PubKeySr25519{})] = sr25519.PubKeyAminoName
nameTable[reflect.TypeOf(secp256k1.PubKeySecp256k1{})] = secp256k1.PubKeyAminoName
nameTable[reflect.TypeOf(multisig.PubKeyMultisigThreshold{})] = multisig.PubKeyMultisigThresholdAminoRoute
nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyAminoName
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyAminoName
nameTable[reflect.TypeOf(multisig.PubKey{})] = multisig.PubKeyAminoRoute
}

View File

@@ -13,7 +13,7 @@ import (
func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
var kp pc.PublicKey
switch k := k.(type) {
case ed25519.PubKeyEd25519:
case ed25519.PubKey:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Ed25519{
Ed25519: k[:],
@@ -29,11 +29,11 @@ func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
switch k := k.Sum.(type) {
case *pc.PublicKey_Ed25519:
if len(k.Ed25519) != ed25519.PubKeyEd25519Size {
if len(k.Ed25519) != ed25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeyEd25519Size)
len(k.Ed25519), ed25519.PubKeySize)
}
var pk ed25519.PubKeyEd25519
var pk ed25519.PubKey
copy(pk[:], k.Ed25519)
return pk, nil
default:
@@ -45,7 +45,7 @@ func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
func PrivKeyToProto(k crypto.PrivKey) (pc.PrivateKey, error) {
var kp pc.PrivateKey
switch k := k.(type) {
case ed25519.PrivKeyEd25519:
case ed25519.PrivKey:
kp = pc.PrivateKey{
Sum: &pc.PrivateKey_Ed25519{
Ed25519: k[:],
@@ -62,11 +62,11 @@ func PrivKeyFromProto(k pc.PrivateKey) (crypto.PrivKey, error) {
switch k := k.Sum.(type) {
case *pc.PrivateKey_Ed25519:
if len(k.Ed25519) != ed25519.PubKeyEd25519Size {
if len(k.Ed25519) != ed25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeyEd25519Size)
len(k.Ed25519), ed25519.PubKeySize)
}
var pk ed25519.PrivKeyEd25519
var pk ed25519.PrivKey
copy(pk[:], k.Ed25519)
return pk, nil
default: