crypto: code polish + ensure key file contents match with name

This commit is contained in:
Péter Szilágyi 2016-02-17 00:38:34 +02:00
parent cda91ee180
commit 0969b35eec

@ -34,7 +34,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"reflect"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/randentropy" "github.com/ethereum/go-ethereum/crypto/randentropy"
@ -157,8 +156,7 @@ func DecryptKey(keyjson []byte, auth string) (*Key, error) {
keyBytes, keyId []byte keyBytes, keyId []byte
err error err error
) )
v := reflect.ValueOf(m["version"]) if version, ok := m["version"].(string); ok && version == "1" {
if v.Kind() == reflect.String && v.String() == "1" {
k := new(encryptedKeyJSONV1) k := new(encryptedKeyJSONV1)
if err := json.Unmarshal(keyjson, k); err != nil { if err := json.Unmarshal(keyjson, k); err != nil {
return nil, err return nil, err
@ -183,12 +181,21 @@ func DecryptKey(keyjson []byte, auth string) (*Key, error) {
}, nil }, nil
} }
func decryptKeyFromFile(keysDirPath string, keyAddr common.Address, auth string) (key *Key, err error) { func decryptKeyFromFile(keysDirPath string, keyAddr common.Address, auth string) (*Key, error) {
// Load the key from the keystore and decrypt its contents
keyjson, err := getKeyFile(keysDirPath, keyAddr) keyjson, err := getKeyFile(keysDirPath, keyAddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return DecryptKey(keyjson, auth) key, err := DecryptKey(keyjson, auth)
if err != nil {
return nil, err
}
// Make sure we're really operating on the requested key (no swap attacks)
if keyAddr != key.Address {
return nil, fmt.Errorf("key content mismatch: have account %x, want %x", key.Address, keyAddr)
}
return key, nil
} }
func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byte, keyId []byte, err error) { func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byte, keyId []byte, err error) {