github.com/status-im/status-go@v1.1.0/account/generator/utils.go (about)

     1  package generator
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  
     7  	"github.com/status-im/status-go/eth-node/crypto"
     8  	"github.com/status-im/status-go/eth-node/types"
     9  	"github.com/status-im/status-go/extkeys"
    10  )
    11  
    12  var (
    13  	// ErrInvalidKeystoreExtendedKey is returned when the decrypted keystore file
    14  	// contains some old Status keys.
    15  	// The old version used to store the BIP44 account at index 0 as PrivateKey,
    16  	// and the BIP44 account at index 1 as ExtendedKey.
    17  	// The current version stores the same key as PrivateKey and ExtendedKey.
    18  	ErrInvalidKeystoreExtendedKey  = errors.New("PrivateKey and ExtendedKey are different")
    19  	ErrInvalidMnemonicPhraseLength = errors.New("invalid mnemonic phrase length; valid lengths are 12, 15, 18, 21, and 24")
    20  )
    21  
    22  // ValidateKeystoreExtendedKey validates the keystore keys, checking that
    23  // ExtendedKey is the extended key of PrivateKey.
    24  func ValidateKeystoreExtendedKey(key *types.Key) error {
    25  	if key.ExtendedKey.IsZeroed() {
    26  		return nil
    27  	}
    28  
    29  	if !bytes.Equal(crypto.FromECDSA(key.PrivateKey), crypto.FromECDSA(key.ExtendedKey.ToECDSA())) {
    30  		return ErrInvalidKeystoreExtendedKey
    31  	}
    32  
    33  	return nil
    34  }
    35  
    36  // MnemonicPhraseLengthToEntropyStrength returns the entropy strength for a given mnemonic length
    37  func MnemonicPhraseLengthToEntropyStrength(length int) (extkeys.EntropyStrength, error) {
    38  	if length < 12 || length > 24 || length%3 != 0 {
    39  		return 0, ErrInvalidMnemonicPhraseLength
    40  	}
    41  
    42  	bitsLength := length * 11
    43  	checksumLength := bitsLength % 32
    44  
    45  	return extkeys.EntropyStrength(bitsLength - checksumLength), nil
    46  }