diff --git a/CHANGELOG.md b/CHANGELOG.md index a7fbb35..e70ebc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ dev: + - support additional mnemonic word list languages - increase minimum timeout for commands that fetch all validators to 2 minutes 1.27.1: diff --git a/docs/changingwithdrawalcredentials.md b/docs/changingwithdrawalcredentials.md index de256d8..5d1f3d1 100644 --- a/docs/changingwithdrawalcredentials.md +++ b/docs/changingwithdrawalcredentials.md @@ -16,7 +16,16 @@ Withdrawal credentials, held as part of a validator's on-chain definition, defin A private key is a hexadecimal string (_e.g._ 0x010203…a1a2a3) that can be used to generate a public key and (in the case of the execution chain) Ethereum address. ### Mnemonic -A mnemonic is a 24-word phrase that can be used to generate multiple private keys with the use of _paths_. +A mnemonic is a 24-word phrase that can be used to generate multiple private keys with the use of _paths_. Mnemonics are supported in the following languages: +* chinese simplified +* chinese traditional +* czech +* english +* french +* italian +* japanese +* korean +* spanish ### Path A path is a string starting with "m" and containing a number of components separated by "/", for example "m/12381/3600/0/0". The process to obtain a key from a mnemonic and path is known as "hierarchical derivation". diff --git a/util/epoch_test.go b/util/epoch_test.go index 73990c4..98cbb24 100644 --- a/util/epoch_test.go +++ b/util/epoch_test.go @@ -1,4 +1,4 @@ -// Copyright © 2032 Weald Technology Trading. +// Copyright © 2023 Weald Technology Trading. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at diff --git a/util/mnemonic.go b/util/mnemonic.go index 2d8e98a..4f49e32 100644 --- a/util/mnemonic.go +++ b/util/mnemonic.go @@ -1,4 +1,4 @@ -// Copyright © 2020, 2022 Weald Technology Trading +// Copyright © 2020 - 2023 Weald Technology Trading // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -19,12 +19,25 @@ import ( "github.com/pkg/errors" "github.com/tyler-smith/go-bip39" + "github.com/tyler-smith/go-bip39/wordlists" "golang.org/x/text/unicode/norm" ) // hdPathRegex is the regular expression that matches an HD path. var hdPathRegex = regexp.MustCompile("^m/[0-9]+/[0-9]+(/[0-9+])+") +var mnemonicWordLists = [][]string{ + wordlists.English, + wordlists.ChineseSimplified, + wordlists.ChineseTraditional, + wordlists.Czech, + wordlists.French, + wordlists.Italian, + wordlists.Japanese, + wordlists.Korean, + wordlists.Spanish, +} + // SeedFromMnemonic creates a seed from a mnemonic. func SeedFromMnemonic(mnemonic string) ([]byte, error) { // If there are more than 24 words we treat the additional characters as the passphrase. @@ -38,10 +51,14 @@ func SeedFromMnemonic(mnemonic string) ([]byte, error) { mnemonic = string(norm.NFKD.Bytes([]byte(mnemonic))) mnemonicPassphrase = string(norm.NFKD.Bytes([]byte(mnemonicPassphrase))) - if !bip39.IsMnemonicValid(mnemonic) { - return nil, errors.New("mnemonic is invalid") + // Try with the various word lists. + for _, wl := range mnemonicWordLists { + bip39.SetWordList(wl) + seed, err := bip39.NewSeedWithErrorChecking(mnemonic, mnemonicPassphrase) + if err == nil { + return seed, nil + } } - // Create seed from mnemonic and passphrase. - return bip39.NewSeed(mnemonic, mnemonicPassphrase), nil + return nil, errors.New("mnemonic is invalid") } diff --git a/util/mnemonic_test.go b/util/mnemonic_test.go new file mode 100644 index 0000000..7f2d56d --- /dev/null +++ b/util/mnemonic_test.go @@ -0,0 +1,72 @@ +// Copyright © 2023 Weald Technology Trading. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package util_test + +import ( + "encoding/hex" + "strings" + "testing" + + "github.com/stretchr/testify/require" + "github.com/wealdtech/ethdo/util" +) + +func bytesStr(input string) []byte { + bytes, err := hex.DecodeString(strings.TrimPrefix(input, "0x")) + if err != nil { + panic(err) + } + return bytes +} + +func TestSeedFromMnemonic(t *testing.T) { + tests := []struct { + name string + mnemonic string + seed []byte + err string + }{ + { + name: "Empty", + err: "mnemonic is invalid", + }, + { + name: "Default", + mnemonic: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art", + seed: bytesStr("0x408b285c123836004f4b8842c89324c1f01382450c0d439af345ba7fc49acf705489c6fc77dbd4e3dc1dd8cc6bc9f043db8ada1e243c4a0eafb290d399480840"), + }, + { + name: "English", + mnemonic: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art", + seed: bytesStr("0x408b285c123836004f4b8842c89324c1f01382450c0d439af345ba7fc49acf705489c6fc77dbd4e3dc1dd8cc6bc9f043db8ada1e243c4a0eafb290d399480840"), + }, + { + name: "Spanish", + mnemonic: "ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ábaco ancla", + seed: bytesStr("0x1e0de8aa97db3c7988f692d9c6151968be89debdbd71b1e34cab15d15ec10eed33412891129e1274fb84624565fd835f7e56df22a997439fca3da05c9c82a156"), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + seed, err := util.SeedFromMnemonic(test.mnemonic) + if test.err != "" { + require.EqualError(t, err, test.err) + } else { + require.NoError(t, err) + require.Equal(t, test.seed, seed) + } + }) + } +}