Browse Source

TESTS: Improve test coverage.

pull/40/head
Tyler Smith 5 months ago
parent
commit
5c4bc8a996
No known key found for this signature in database GPG Key ID: CA38F1A9BE0EC890
3 changed files with 27 additions and 38 deletions
  1. +1
    -1
      Makefile
  2. +11
    -36
      bip39.go
  3. +15
    -1
      bip39_test.go

+ 1
- 1
Makefile View File

@ -5,7 +5,7 @@ tests: ## Run tests with coverage
@go test -v -cover ./...
profile_tests: ## Run tests and output coverage profiling
@go test -v -coverprofile=coverage.out .
@go test -v -coverprofile=coverage.out ./...
@go tool cover -html=coverage.out
build_check: ## Checks build and tests

+ 11
- 36
bip39.go View File

@ -216,50 +216,25 @@ func NewMnemonic(entropy []byte) (string, error) {
// An error is returned if the mnemonic is invalid.
func MnemonicToByteArray(mnemonic string, raw ...bool) ([]byte, error) {
var (
mnemonicSlice = strings.Split(mnemonic, " ")
entropyBitSize = len(mnemonicSlice) * 11
checksumBitSize = entropyBitSize % 32
fullByteSize = (entropyBitSize-checksumBitSize)/8 + 1
checksumByteSize = fullByteSize - (fullByteSize % 4)
mnemonicSlice = strings.Split(mnemonic, " ")
entropyBitSize = len(mnemonicSlice) * 11
checksumBitSize = entropyBitSize % 32
fullByteSize = (entropyBitSize-checksumBitSize)/8 + 1
)
// Pre validate that the mnemonic is well formed and only contains words that
// are present in the word list.
if !IsMnemonicValid(mnemonic) {
return nil, ErrInvalidMnemonic
}
// Convert word indices to a big.Int representing the entropy.
checksummedEntropy := big.NewInt(0)
modulo := big.NewInt(2048)
for _, v := range mnemonicSlice {
index := big.NewInt(int64(wordMap[v]))
checksummedEntropy.Mul(checksummedEntropy, modulo)
checksummedEntropy.Add(checksummedEntropy, index)
}
// Calculate the unchecksummed entropy so we can validate that the checksum is
// correct.
checksumModulo := big.NewInt(0).Exp(bigTwo, big.NewInt(int64(checksumBitSize)), nil)
rawEntropy := big.NewInt(0).Div(checksummedEntropy, checksumModulo)
// Convert big.Ints to byte padded byte slices.
rawEntropyBytes := padByteSlice(rawEntropy.Bytes(), checksumByteSize)
checksummedEntropyBytes := padByteSlice(checksummedEntropy.Bytes(), fullByteSize)
// Validate that the checksum is correct.
newChecksummedEntropyBytes := padByteSlice(addChecksum(rawEntropyBytes), fullByteSize)
if !compareByteSlices(checksummedEntropyBytes, newChecksummedEntropyBytes) {
return nil, ErrChecksumIncorrect
// Turn into raw entropy.
rawEntropyBytes, err := EntropyFromMnemonic(mnemonic)
if err != nil {
return nil, err
}
// If we want the raw entropy then we're done.
if len(raw) > 0 && raw[0] {
return rawEntropyBytes, nil
}
return checksummedEntropyBytes, nil
// Otherwise add the checksum before returning
return padByteSlice(addChecksum(rawEntropyBytes), fullByteSize), nil
}
// NewSeedWithErrorChecking creates a hashed seed output given the mnemonic string and a password.

+ 15
- 1
bip39_test.go View File

@ -73,7 +73,17 @@ func TestIsMnemonicValid(t *testing.T) {
}
}
func TestMnemonicToByteArrayInvalidMnemonic(t *testing.T) {
func TestMnemonicToByteArrayWithRawIsEqualToEntropyFromMnemonic(t *testing.T) {
for _, vector := range testVectors() {
rawEntropy, err := MnemonicToByteArray(vector.mnemonic, true)
assert.Nil(t, err)
rawEntropy2, err := EntropyFromMnemonic(vector.mnemonic)
assert.Nil(t, err)
assert.True(t, bytes.Equal(rawEntropy, rawEntropy2))
}
}
func TestMnemonicToByteArrayInvalidMnemonics(t *testing.T) {
for _, vector := range badMnemonicSentences() {
_, err := MnemonicToByteArray(vector.mnemonic)
assert.NotNil(t, err)
@ -81,6 +91,10 @@ func TestMnemonicToByteArrayInvalidMnemonic(t *testing.T) {
_, err := MnemonicToByteArray("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon yellow")
assert.NotNil(t, err)
assertEqual(t, err, ErrChecksumIncorrect)
_, err = MnemonicToByteArray("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon angry")
assert.NotNil(t, err)
assertEqual(t, err, ErrInvalidMnemonic)
}

Loading…
Cancel
Save