Exemplo n.º 1
0
func main() {
	flag.Parse()

	fmt.Printf("Generating seed words and key.\n")
	if *password != "" {
		fmt.Printf("Using password %q.\n", *password)
	} else {
		fmt.Printf("Not using any password.\n")
	}
	m := mnemonic.NewFromFileOrDie(*wordFile)

	var err error
	if *wordCount != 0 {
		_, err = m.GenerateWords(*wordCount)
	} else {
		_, err = m.GenerateEntropy(*entropySize)
	}
	if err != nil {
		log.Fatalf("Failed to generate seed words: %v\n", err)
	}
	words, key, err := m.GenerateSeedWithPassword(*password)
	if err != nil {
		log.Fatalf("Failed to generate seed: %v\n", err)
		log.Fatal(err)
	}
	fmt.Printf("Seed words (%d): %q\nKey (512 bits): %s\n",
		len(words), mnemonic.ListToString(words), hex.EncodeToString(key))
}
Exemplo n.º 2
0
func TestMnemonicGeneration(t *testing.T) {
	m := mnemonic.NewFromFileOrDie("wordlist.txt")
	file, err := ioutil.ReadFile("test_vectors.json")
	if err != nil {
		t.Fatalf("File error: %v\n", err)
	}

	var tests testSet
	err = json.Unmarshal(file, &tests)
	if err != nil {
		t.Fatalf("File parsing error: %v\n", err)
	}

	for i, test := range tests.English {
		data, err := hex.DecodeString(test[0])
		if err != nil {
			t.Fatalf("Test %d: Failed to decode hex encoded data %q: %v",
				i, test[0], err)
		}
		words, err := m.GenerateFromData(data)
		if err != nil {
			t.Fatalf("Test %d: Failed to generator words: %v", i, err)
		}
		str := mnemonic.ListToString(words)
		if str != test[1] {
			t.Errorf("Test %d: Words don't match: Got %q, expected %q.",
				i, str, test[1])
		}
		res, err := m.VerifyChecksum(strings.Split(str, " "))
		if err != nil {
			t.Fatalf("Test %d: Failed to verify checksum: %v", i, err)
		}
		if !res {
			t.Fatalf("Test %d: Checksum mismatch.", i)
		}

		words, key, err := m.GenerateSeedWithPassword("TREZOR")
		if err != nil {
			t.Fatalf("Test %d: Failed to generator seed: %v", i, err)
		}
		encoded := hex.EncodeToString(key)
		if encoded != test[2] {
			t.Errorf("Test %d: Key doesn't match: Got %q, expected %q.",
				i, encoded, test[2])
		}
		key2 := mnemonic.SeedFromWordsPassword(words, "TREZOR")
		encoded2 := hex.EncodeToString(key2)
		if encoded2 != encoded {
			t.Errorf("Test %d: Couldn't recover key: Got %x, expected %x.",
				i, key2, key)
		}

	}
}