Exemple #1
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)
		}

	}
}
Exemple #2
0
func TestChainDerivation2(t *testing.T) {
	seed := mnemonic.SeedFromWordsPassword(
		strings.Split("avoid element lobster scout spare actor page woman "+
			"clog street include proud", " "), "")
	key := NewMasterKey(seed)
	recv, err := key.Chain("m/ 44'/ 0'/ 0'/ 0/ 0")
	if err != nil {
		t.Fatalf("Chain derivation failed: %v", err)
	}
	if recv.PublicKeyHashEncode() != "1APWnkAgU5iaiJtz7Ga7i3pGA127oQnnTG" {
		t.Errorf("Chain derivation yielded wrong result: %s",
			recv.PublicKeyHash())
	}

	change, err := key.Chain("M/44'  /0' /0 ' /1  /10")
	if err != nil {
		t.Fatalf("Chain derivation failed: %v", err)
	}
	if change.PublicKeyHashEncode() != "17pZuDHfXNLKxyA7hsd1cGrSSPtUrhnxTq" {
		t.Errorf("Chain derivation yielded wrong result: %s",
			change.PublicKeyHash())
	}
}