def test_invalid_params(self): self.assertRaises(TypeError, ElectrumV2MnemonicGenerator, ElectrumV2MnemonicTypes.STANDARD, 0) self.assertRaises(TypeError, ElectrumV2MnemonicGenerator, 0, ElectrumV2Languages.ENGLISH) self.assertRaises(TypeError, ElectrumV2MnemonicValidator, ElectrumV2MnemonicTypes.STANDARD, 0) self.assertRaises(TypeError, ElectrumV2MnemonicValidator, 0, ElectrumV2Languages.ENGLISH) self.assertRaises(TypeError, ElectrumV2SeedGenerator, "", 0) # Fail to generate a valid mnemonic (entropy with too few bits) self.assertRaises(ValueError, ElectrumV2MnemonicGenerator(ElectrumV2MnemonicTypes.STANDARD).FromEntropy, binascii.unhexlify(b"00000000000000000000000000000000"))
def test_entropy_invalid_bitlen(self): for test_bit_len in ElectrumV2EntropyBitLen: self.assertRaises(ValueError, ElectrumV2EntropyGenerator, test_bit_len - ElectrumV2MnemonicConst.WORD_BIT_LEN - 1) self.assertRaises(ValueError, ElectrumV2EntropyGenerator, test_bit_len + 1) # Build a dummy entropy with invalid bit length dummy_ent = b"\x00" * ((test_bit_len - 8) // 8) self.assertRaises(ValueError, ElectrumV2MnemonicGenerator(ElectrumV2MnemonicTypes.STANDARD).FromEntropy, dummy_ent)
def test_entropy_valid_bitlen(self): for test_bit_len in ElectrumV2EntropyBitLen: # Test generator entropy = ElectrumV2EntropyGenerator(test_bit_len).Generate() entropy_bit_len = math.ceil(math.log(BytesUtils.ToInteger(entropy), 2)) self.assertTrue(test_bit_len - ElectrumV2MnemonicConst.WORD_BIT_LEN <= entropy_bit_len <= test_bit_len) # Compute the expected mnemonic length mnemonic_len = test_bit_len // 11 # Generate mnemonic with checksum mnemonic = ElectrumV2MnemonicGenerator(ElectrumV2MnemonicTypes.STANDARD).FromEntropy(entropy) # Test generated mnemonic length self.assertEqual(mnemonic.WordsCount(), mnemonic_len)
def test_vector(self): for test in TEST_VECT: lang = test["lang"] # Test mnemonic generator mnemonic = ElectrumV2MnemonicGenerator(test["mnemonic_type"], lang).FromEntropy(binascii.unhexlify(test["entropy"])) self.assertEqual(test["mnemonic"], mnemonic.ToStr()) self.assertEqual(test["mnemonic"], str(mnemonic)) self.assertEqual(test["mnemonic"].split(" "), mnemonic.ToList()) self.assertEqual(len(test["mnemonic"].split(" ")), mnemonic.WordsCount()) # Test mnemonic validator (language specified, all mnemonic types) mnemonic_validator = ElectrumV2MnemonicValidator(lang=lang) self.assertTrue(mnemonic_validator.IsValid(mnemonic)) # Test mnemonic validator (automatic language detection, all mnemonic types) mnemonic_validator = ElectrumV2MnemonicValidator() self.assertTrue(mnemonic_validator.IsValid(mnemonic)) # Test mnemonic validator (mnemonic type specified) mnemonic_validator = ElectrumV2MnemonicValidator(mnemonic_type=test["mnemonic_type"]) self.assertTrue(mnemonic_validator.IsValid(mnemonic)) # Test decoder (language specified, all mnemonic types) entropy = ElectrumV2MnemonicDecoder(lang=lang).Decode(mnemonic) self.assertEqual(test["entropy_final"], binascii.hexlify(entropy)) # Test decoder (automatic language detection, all mnemonic types) entropy = ElectrumV2MnemonicDecoder().Decode(mnemonic) self.assertEqual(test["entropy_final"], binascii.hexlify(entropy)) # Test decoder (mnemonic type specified) entropy = ElectrumV2MnemonicDecoder(mnemonic_type=test["mnemonic_type"]).Decode(mnemonic) self.assertEqual(test["entropy_final"], binascii.hexlify(entropy)) # Test seed generator seed = ElectrumV2SeedGenerator(mnemonic, lang).Generate() self.assertEqual(test["seed"], binascii.hexlify(seed)) # Test address if test["mnemonic_type"] == ElectrumV2MnemonicTypes.STANDARD: self.assertEqual(test["address"], ElectrumV2Standard.FromSeed(seed).GetAddress(0, 0)) elif test["mnemonic_type"] == ElectrumV2MnemonicTypes.SEGWIT: self.assertEqual(test["address"], ElectrumV2Segwit.FromSeed(seed).GetAddress(0, 0))
print( f"Master private key: {priv_to_wif(electrum_v1.MasterPrivateKey(), WifPubKeyModes.UNCOMPRESSED)}" ) # Derive V1 addresses print("Addresses:") for i in range(ADDR_NUM): print( f" {i}. Private key: {priv_to_wif(electrum_v1.GetPrivateKey(0, i), WifPubKeyModes.UNCOMPRESSED)}" ) print(f" {i}. Address: {electrum_v1.GetAddress(0, i)}") print("") print("V2 Wallet (standard)") # Generate random standard mnemonic v2_standard_mnemonic = ElectrumV2MnemonicGenerator( ElectrumV2MnemonicTypes.STANDARD).FromWordsNumber( ElectrumV2WordsNum.WORDS_NUM_12) print(f"Mnemonic: {v2_standard_mnemonic}") # Generate seed from mnemonic v2_standard_seed_bytes = ElectrumV2SeedGenerator( v2_standard_mnemonic).Generate() print(f"Seed: {binascii.hexlify(v2_standard_seed_bytes)}") # Construct from seed electrum_v2_standard = ElectrumV2Standard.FromSeed(v2_standard_seed_bytes) # Print master key print( f"Master private key: {priv_to_wif(electrum_v2_standard.MasterPrivateKey().KeyObject())}" ) # Derive standard addresses print("Addresses:") for i in range(ADDR_NUM):
def test_from_invalid_words_num(self): for test_words_num in ElectrumV2WordsNum: self.assertRaises(ValueError, ElectrumV2MnemonicGenerator(ElectrumV2MnemonicTypes.STANDARD).FromWordsNumber, test_words_num - 1) self.assertRaises(ValueError, ElectrumV2MnemonicGenerator(ElectrumV2MnemonicTypes.STANDARD).FromWordsNumber, test_words_num + 1)
def test_from_valid_words_num(self): for test_words_num in ElectrumV2WordsNum: mnemonic = ElectrumV2MnemonicGenerator(ElectrumV2MnemonicTypes.STANDARD).FromWordsNumber(test_words_num) self.assertEqual(mnemonic.WordsCount(), test_words_num)