keys: change to []bytes (#4950)

This commit is contained in:
Marko
2020-06-04 15:32:42 +02:00
committed by GitHub
parent d53a8d0377
commit 7c576f02ab
21 changed files with 152 additions and 160 deletions

View File

@@ -1,7 +1,6 @@
package cryptoamino
import (
"os"
"reflect"
"testing"
@@ -17,17 +16,20 @@ import (
"github.com/tendermint/tendermint/crypto/sr25519"
)
type byter interface {
Bytes() []byte
type AminoMarshal interface {
AminoMarshal() ([]byte, error)
AminoUnmarshal([]byte) error
}
func checkAminoBinary(t *testing.T, src, dst interface{}, size int) {
// Marshal to binary bytes.
bz, err := cdc.MarshalBinaryBare(src)
require.Nil(t, err, "%+v", err)
if byterSrc, ok := src.(byter); ok {
if byterSrc, ok := src.(AminoMarshal); ok {
// Make sure this is compatible with current (Bytes()) encoding.
assert.Equal(t, byterSrc.Bytes(), bz, "Amino binary vs Bytes() mismatch")
bza, err := byterSrc.AminoMarshal()
assert.NoError(t, err)
assert.Equal(t, bza, bz, "Amino binary vs Bytes() mismatch")
}
// Make sure we have the expected length.
assert.Equal(t, size, len(bz), "Amino binary size mismatch")
@@ -52,21 +54,6 @@ func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool)
require.Nil(t, err, "%+v", err)
}
// ExamplePrintRegisteredTypes refers to unknown identifier: PrintRegisteredTypes
//nolint:govet
func ExamplePrintRegisteredTypes() {
cdc.PrintTypes(os.Stdout)
// Output: | Type | Name | Prefix | Length | Notes |
//| ---- | ---- | ------ | ----- | ------ |
//| 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) {
cases := []struct {
privKey crypto.PrivKey

View File

@@ -16,7 +16,7 @@ func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
case ed25519.PubKey:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Ed25519{
Ed25519: k[:],
Ed25519: k,
},
}
default:
@@ -33,8 +33,8 @@ func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeySize)
}
var pk ed25519.PubKey
copy(pk[:], k.Ed25519)
pk := make(ed25519.PubKey, ed25519.PubKeySize)
copy(pk, k.Ed25519)
return pk, nil
default:
return nil, fmt.Errorf("fromproto: key type %v is not supported", k)
@@ -48,7 +48,7 @@ func PrivKeyToProto(k crypto.PrivKey) (pc.PrivateKey, error) {
case ed25519.PrivKey:
kp = pc.PrivateKey{
Sum: &pc.PrivateKey_Ed25519{
Ed25519: k[:],
Ed25519: k,
},
}
default:
@@ -66,8 +66,8 @@ func PrivKeyFromProto(k pc.PrivateKey) (crypto.PrivKey, error) {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeySize)
}
var pk ed25519.PrivKey
copy(pk[:], k.Ed25519)
pk := make(ed25519.PrivKey, ed25519.PrivateKeySize)
copy(pk, k.Ed25519)
return pk, nil
default:
return nil, errors.New("fromproto: key type not supported")