Browse Source

Enable privkey validity checking on load

pull/1782/head
Ethan Frey 7 years ago
parent
commit
bce88a20df
2 changed files with 80 additions and 1 deletions
  1. +15
    -1
      priv_key.go
  2. +65
    -0
      priv_key_test.go

+ 15
- 1
priv_key.go View File

@ -13,13 +13,27 @@ import (
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
if err == nil {
// add support for a ValidateKey method on PrivKeys
// to make sure they load correctly
val, ok := privKey.Unwrap().(validatable)
if ok {
err = val.ValidateKey()
}
}
return
}
// validatable is an optional interface for keys that want to
// check integrity
type validatable interface {
ValidateKey() error
}
//----------------------------------------
// DO NOT USE THIS INTERFACE.
// You probably want to use PubKey
// You probably want to use PrivKey
// +gen wrapper:"PrivKey,Impl[PrivKeyEd25519,PrivKeySecp256k1],ed25519,secp256k1"
type PrivKeyInner interface {
AssertIsPrivKeyInner()


+ 65
- 0
priv_key_test.go View File

@ -0,0 +1,65 @@
package crypto
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
wire "github.com/tendermint/go-wire"
)
type BadKey struct {
PrivKeyEd25519
}
// Wrap fulfils interface for PrivKey struct
func (pk BadKey) Wrap() PrivKey {
return PrivKey{pk}
}
func (pk BadKey) Bytes() []byte {
return wire.BinaryBytes(pk.Wrap())
}
func (pk BadKey) ValidateKey() error {
return fmt.Errorf("fuggly key")
}
func init() {
PrivKeyMapper.
RegisterImplementation(BadKey{}, "bad", 0x66)
}
func TestReadPrivKey(t *testing.T) {
assert, require := assert.New(t), require.New(t)
// garbage in, garbage out
garbage := []byte("hjgewugfbiewgofwgewr")
_, err := PrivKeyFromBytes(garbage)
require.Error(err)
edKey := GenPrivKeyEd25519()
badKey := BadKey{edKey}
cases := []struct {
key PrivKey
valid bool
}{
{edKey.Wrap(), true},
{badKey.Wrap(), false},
}
for i, tc := range cases {
data := tc.key.Bytes()
key, err := PrivKeyFromBytes(data)
if tc.valid {
assert.NoError(err, "%d", i)
assert.Equal(tc.key, key, "%d", i)
} else {
assert.Error(err, "%d: %#v", i, key)
}
}
}

Loading…
Cancel
Save