2016-09-08 09:45:12 +00:00
|
|
|
package extkeys_test
|
2016-07-08 20:26:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-09-08 09:45:12 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
2016-07-08 20:26:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type VectorsFile struct {
|
|
|
|
Data map[string][][6]string
|
|
|
|
vectors []*Vector
|
|
|
|
}
|
|
|
|
|
|
|
|
type Vector struct {
|
|
|
|
language, salt, password, input, mnemonic, seed, xprv string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMnemonicPhrase
|
|
|
|
func TestMnemonicPhrase(t *testing.T) {
|
|
|
|
|
2016-09-08 09:45:12 +00:00
|
|
|
mnemonic := extkeys.NewMnemonic(extkeys.Salt)
|
2016-07-08 20:26:09 +00:00
|
|
|
|
|
|
|
// test mnemonic generation
|
2016-09-08 09:45:12 +00:00
|
|
|
t.Log("Test mnemonic generation:")
|
2016-07-08 20:26:09 +00:00
|
|
|
for _, language := range mnemonic.AvailableLanguages() {
|
|
|
|
phrase, err := mnemonic.MnemonicPhrase(128, language)
|
2016-09-08 09:45:12 +00:00
|
|
|
t.Logf("Mnemonic (%s): %s", extkeys.Languages[language], phrase)
|
2016-07-08 20:26:09 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not create seed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mnemonic.ValidMnemonic(phrase, language) {
|
|
|
|
t.Error("Seed is not valid Mnenomic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run against test vectors
|
|
|
|
vectorsFile, err := LoadVectorsFile("mnemonic_vectors.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-09-08 09:45:12 +00:00
|
|
|
t.Log("Test against pre-computed seed vectors:")
|
2016-07-08 20:26:09 +00:00
|
|
|
stats := map[string]int{}
|
|
|
|
for _, vector := range vectorsFile.vectors {
|
|
|
|
stats[vector.language] += 1
|
2016-09-08 09:45:12 +00:00
|
|
|
mnemonic := extkeys.NewMnemonic(vector.salt)
|
2016-07-08 20:26:09 +00:00
|
|
|
seed := mnemonic.MnemonicSeed(vector.mnemonic, vector.password)
|
|
|
|
if fmt.Sprintf("%x", seed) != vector.seed {
|
|
|
|
t.Errorf("Test failed (%s): incorrect seed (%x) generated (expected: %s)", vector.language, seed, vector.seed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//t.Logf("Test passed: correct seed (%x) generated (expected: %s)", seed, vector.seed)
|
|
|
|
}
|
|
|
|
for language, count := range stats {
|
|
|
|
t.Logf("[%s]: %d tests completed", language, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadVectorsFile(path string) (*VectorsFile, error) {
|
|
|
|
fp, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Test failed: cannot open vectors file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var vectorsFile VectorsFile
|
|
|
|
if err := json.NewDecoder(fp).Decode(&vectorsFile); err != nil {
|
|
|
|
return nil, fmt.Errorf("Test failed: cannot parse vectors file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// load data into Vector structs
|
|
|
|
for language, data := range vectorsFile.Data {
|
|
|
|
for _, item := range data {
|
|
|
|
vectorsFile.vectors = append(vectorsFile.vectors, &Vector{language, item[0], item[1], item[2], item[3], item[4], item[5]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &vectorsFile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Vector) String() string {
|
|
|
|
return fmt.Sprintf("{salt: %s, password: %s, input: %s, mnemonic: %s, seed: %s, xprv: %s}",
|
|
|
|
v.salt, v.password, v.input, v.mnemonic, v.seed, v.xprv)
|
|
|
|
}
|