Skip to content

Commit

Permalink
Merge branch 'develop' into drop-peers-rate-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Dec 16, 2019
2 parents 48671b6 + 2d2f2fb commit 7c6f153
Show file tree
Hide file tree
Showing 42 changed files with 897 additions and 1,383 deletions.
74 changes: 67 additions & 7 deletions extkeys/mnemonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
// https://github.com/trezor/trezor-crypto/blob/master/bip39.c
// https://github.com/trezor/python-mnemonic/blob/master/mnemonic/mnemonic.py
// https://github.com/bitpay/bitcore-mnemonic/blob/master/lib/mnemonic.js (used in eth-lightwallet.js)
// https://github.com/tyler-smith/go-bip39/blob/master/bip39.go

const defaultSalt = "mnemonic"

Expand Down Expand Up @@ -73,6 +74,21 @@ var (
rightShift11BitsDivider = big.NewInt(2048)
bigOne = big.NewInt(1)
bigTwo = big.NewInt(2)

wordLengthChecksumMasksMapping = map[int]*big.Int{
12: big.NewInt(15),
15: big.NewInt(31),
18: big.NewInt(63),
21: big.NewInt(127),
24: big.NewInt(255),
}

wordLengthChecksumShiftMapping = map[int]*big.Int{
12: big.NewInt(16),
15: big.NewInt(8),
18: big.NewInt(4),
21: big.NewInt(2),
}
)

// Language is language identifier
Expand Down Expand Up @@ -201,11 +217,11 @@ func (m *Mnemonic) MnemonicPhrase(strength EntropyStrength, language Language) (
return strings.Join(words, wordSeperator), nil
}

// ValidMnemonic validates mnemonic string
func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
// ValidateMnemonic validates that all words from a mnemonic string are in wordlist and that checksum is valid
func (m *Mnemonic) ValidateMnemonic(mnemonic string, language Language) error {
wordList, err := m.WordList(language)
if err != nil {
return false
return errors.New("invalid language specified")
}

// Create a list of all the words in the mnemonic sentence
Expand All @@ -216,17 +232,61 @@ func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {

// The number of words should be 12, 15, 18, 21 or 24
if numOfWords%3 != 0 || numOfWords < 12 || numOfWords > 24 {
return false
return errors.New("mnemonic contains an invalid number of words")
}

// Create reverse lookup map for dictionary
wordMap := map[string]int{}
for i, v := range wordList {
wordMap[v] = i
}

b := big.NewInt(0)

// Check if all words belong in the wordlist
for i := 0; i < numOfWords; i++ {
if !contains(wordList, words[i]) {
return false

wordIndex, ok := wordMap[words[i]]
if !ok {
return fmt.Errorf("word %s not found in the dictionary", words[i])
}
var wordBytes [2]byte
binary.BigEndian.PutUint16(wordBytes[:], uint16(wordIndex))
b = b.Mul(b, rightShift11BitsDivider)
b = b.Or(b, big.NewInt(0).SetBytes(wordBytes[:]))
}

checksum := big.NewInt(0)
checksumMask := wordLengthChecksumMasksMapping[numOfWords]
checksum = checksum.And(b, checksumMask)

b.Div(b, big.NewInt(0).Add(checksumMask, bigOne))

// Calculate entropy from mnemonic
entropy := b.Bytes()
entropy = padByteSlice(entropy, len(words)/3*4)

// Calculate checksum from entropy derived above
hasher := sha256.New()
hasher.Write(entropy)
computedChecksumBytes := hasher.Sum(nil)
computedChecksum := big.NewInt(int64(computedChecksumBytes[0]))

if l := len(words); l != 24 {
checksumShift := wordLengthChecksumShiftMapping[l]
computedChecksum.Div(computedChecksum, checksumShift)
}

return true
if checksum.Cmp(computedChecksum) != 0 {
return errors.New("checksum for mnemonic seed is invalid")
}

return nil
}

// ValidMnemonic validates mnemonic string
func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
return m.ValidateMnemonic(mnemonic, language) == nil
}

// WordList returns list of words for a given language
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ require (
github.com/syndtr/goleveldb v1.0.0
go.uber.org/zap v1.13.0
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba
golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/tools v0.0.0-20191213032237-7093a17b0467 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.29.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@ golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3 h1:6KET3Sqa7fkVfD63QnAM81ZeYg5n4HwApOJkufONnHA=
golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -728,6 +730,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -772,6 +776,8 @@ golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101 h1:LCmXVkvpQCDj724eX6irUTPCJP5GelFHxqGSWL2D1R0=
golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191213032237-7093a17b0467 h1:Jybbe55FT+YYZIJGWmJIA4ZGcglFuZOduakIW3+gHXY=
golang.org/x/tools v0.0.0-20191213032237-7093a17b0467/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.3.2/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
Expand Down
7 changes: 7 additions & 0 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/status-im/status-go/api"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/exportlogs"
"github.com/status-im/status-go/extkeys"
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/params"
Expand Down Expand Up @@ -705,3 +706,9 @@ func Identicon(pk string) string {
identicon, _ := protocol.Identicon(pk)
return identicon
}

func ValidateMnemonic(mnemonic string) string {
m := extkeys.NewMnemonic()
err := m.ValidateMnemonic(mnemonic, extkeys.Language(0))
return makeJSONResponse(err)
}
74 changes: 67 additions & 7 deletions vendor/github.com/status-im/status-go/extkeys/mnemonic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vendor/golang.org/x/net/internal/socket/zsys_aix_ppc64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vendor/golang.org/x/net/internal/socket/zsys_linux_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/golang.org/x/net/internal/socket/zsys_linux_arm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/golang.org/x/net/internal/socket/zsys_linux_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/golang.org/x/net/internal/socket/zsys_linux_mips.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/golang.org/x/net/internal/socket/zsys_linux_mips64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7c6f153

Please # to comment.