2019-07-24 18:59:15 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
|
2019-12-19 16:03:00 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-12-19 18:27:27 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-07-24 18:59:15 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidKeystoreExtendedKey is returned when the decrypted keystore file
|
|
|
|
// contains some old Status keys.
|
|
|
|
// The old version used to store the BIP44 account at index 0 as PrivateKey,
|
|
|
|
// and the BIP44 account at index 1 as ExtendedKey.
|
|
|
|
// The current version stores the same key as PrivateKey and ExtendedKey.
|
|
|
|
ErrInvalidKeystoreExtendedKey = errors.New("PrivateKey and ExtendedKey are different")
|
|
|
|
ErrInvalidMnemonicPhraseLength = errors.New("invalid mnemonic phrase length; valid lengths are 12, 15, 18, 21, and 24")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateKeystoreExtendedKey validates the keystore keys, checking that
|
|
|
|
// ExtendedKey is the extended key of PrivateKey.
|
2019-12-19 18:27:27 +00:00
|
|
|
func ValidateKeystoreExtendedKey(key *types.Key) error {
|
2019-07-24 18:59:15 +00:00
|
|
|
if key.ExtendedKey.IsZeroed() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(crypto.FromECDSA(key.PrivateKey), crypto.FromECDSA(key.ExtendedKey.ToECDSA())) {
|
|
|
|
return ErrInvalidKeystoreExtendedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MnemonicPhraseLengthToEntropyStrength returns the entropy strength for a given mnemonic length
|
|
|
|
func MnemonicPhraseLengthToEntropyStrength(length int) (extkeys.EntropyStrength, error) {
|
|
|
|
if length < 12 || length > 24 || length%3 != 0 {
|
|
|
|
return 0, ErrInvalidMnemonicPhraseLength
|
|
|
|
}
|
|
|
|
|
|
|
|
bitsLength := length * 11
|
|
|
|
checksumLength := bitsLength % 32
|
|
|
|
|
|
|
|
return extkeys.EntropyStrength(bitsLength - checksumLength), nil
|
|
|
|
}
|