add entropyStrength type

This commit is contained in:
Andrea Franz 2018-03-14 10:42:55 +01:00
parent 3d71a3868a
commit a1da71e968
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
3 changed files with 19 additions and 4 deletions

View File

@ -41,6 +41,21 @@ const (
totalAvailableLanguages
)
type entropyStrength int
const (
// EntropyStrength128 defines an entropy strength of 128 bits
EntropyStrength128 = entropyStrength(128)
// EntropyStrength160 defines an entropy strength of 160 bits
EntropyStrength160 = entropyStrength(160)
// EntropyStrength192 defines an entropy strength of 192 bits
EntropyStrength192 = entropyStrength(192)
// EntropyStrength224 defines an entropy strength of 224 bits
EntropyStrength224 = entropyStrength(224)
// EntropyStrength256 defines an entropy strength of 256 bits
EntropyStrength256 = entropyStrength(256)
)
// Languages is a list of supported languages for which mnemonic keys can be generated
var Languages = [...]string{
"English",
@ -123,7 +138,7 @@ func (m *Mnemonic) MnemonicSeed(mnemonic string, password string) []byte {
}
// MnemonicPhrase returns a human readable seed for BIP32 Hierarchical Deterministic Wallets
func (m *Mnemonic) MnemonicPhrase(strength int, language Language) (string, error) {
func (m *Mnemonic) MnemonicPhrase(strength entropyStrength, language Language) (string, error) {
wordList, err := m.WordList(language)
if err != nil {
return "", err

View File

@ -22,7 +22,7 @@ func TestMnemonicPhrase(t *testing.T) {
mnemonic := NewMnemonic(Salt)
// test strength validation
strengths := []int{127, 129, 257}
strengths := []entropyStrength{127, 129, 257}
for _, s := range strengths {
_, err := mnemonic.MnemonicPhrase(s, EnglishLanguage)
if err != ErrInvalidEntropyStrength {
@ -33,7 +33,7 @@ func TestMnemonicPhrase(t *testing.T) {
// test mnemonic generation
t.Log("Test mnemonic generation:")
for _, language := range mnemonic.AvailableLanguages() {
phrase, err := mnemonic.MnemonicPhrase(128, language)
phrase, err := mnemonic.MnemonicPhrase(EntropyStrength128, language)
t.Logf("Mnemonic (%s): %s", Languages[language], phrase)
if err != nil {

View File

@ -48,7 +48,7 @@ func NewManager(nodeManager common.NodeManager) *Manager {
func (m *Manager) CreateAccount(password string) (address, pubKey, mnemonic string, err error) {
// generate mnemonic phrase
mn := extkeys.NewMnemonic(extkeys.Salt)
mnemonic, err = mn.MnemonicPhrase(128, extkeys.EnglishLanguage)
mnemonic, err = mn.MnemonicPhrase(extkeys.EntropyStrength128, extkeys.EnglishLanguage)
if err != nil {
return "", "", "", fmt.Errorf("can not create mnemonic seed: %v", err)
}