def test_from_seed_and_path(self): for test in TEST_VECT_BIP32: # Create from seed bip32_ctx = Bip32.FromSeedAndPath(binascii.unhexlify(test["seed"]), "m") # Test master key self.assertEqual(test["master"]["ex_pub"] , bip32_ctx.PublicKey().ToExtended()) self.assertEqual(test["master"]["ex_priv"], bip32_ctx.PrivateKey().ToExtended()) # Test derivation paths for chain in test["der_paths"]: # Try to build from path and test again bip32_from_path = Bip32.FromSeedAndPath(binascii.unhexlify(test["seed"]), chain["path"]) # Test keys self.assertEqual(chain["ex_pub"] , bip32_from_path.PublicKey().ToExtended()) self.assertEqual(chain["ex_priv"], bip32_from_path.PrivateKey().ToExtended())
def from_mnemonic(words: List[str], init_path=VET_EXTERNAL_PATH): ''' Construct an HD Node from a set of words. The init_path is m/44'/818'/0'/0 by default on VeChain. Note ---- The words will generate a seed, which will be further developed into a "m" secret key and "chain code". Parameters ---------- words : List[str] Mnemonic words, usually 12 words. init_path : str, optional The initial derivation path, by default VET_EXTERNAL_PATH Returns ------- HDNode A new HDNode. ''' seed = derive_seed(words) # 64 bytes bip32_ctx = Bip32.FromSeedAndPath(seed, init_path) return HDNode(bip32_ctx)
def from_seed(seed: bytes, init_path=VET_EXTERNAL_PATH): ''' Construct an HD Node from a seed (64 bytes). The init_path is m/44'/818'/0'/0 for starting. or you can simply put in 44'/818'/0'/0 Note ---- The seed will be further developed into a "m" secret key and "chain code". Parameters ---------- seed : bytes Seed itself. init_path : str, optional The derive path, by default VET_EXTERNAL_PATH Returns ------- HDNode A new HDNode. ''' bip32_ctx = Bip32.FromSeedAndPath(seed, init_path) return HDNode(bip32_ctx)
def get_addresses_from(mnemonic): seed_bytes = Bip39SeedGenerator(mnemonic).Generate() addresses = [] # TODO: manually derive last index from one parent to save some time, allow other derive paths for i in range(0, options.indices - 1): bip32_ctx = Bip32.FromSeedAndPath(seed_bytes, f"m/0'/2'/{i}") key = Key(bip32_ctx.PrivateKey().Raw().ToHex()) addresses.append(key.address) return addresses
def get_account_from_words(words: str, index: int = 0, hd_path: str = ETHEREUM_PATH) -> Account: """ :param words: Mnemonic words generated using Bip39 :param index: Index of account :param hd_path: Bip44 Path. By default Ethereum is used :return: List of ethereum public addresses """ seed = Bip39SeedGenerator(words).Generate() bip32_ctx = Bip32.FromSeedAndPath(seed, hd_path) return Account.from_key( bip32_ctx.ChildKey(index).PrivateKey().Raw().ToBytes())
def get_address_from_words(words: str, index: int = 0, hd_path: str = ETHEREUM_PATH) -> str: """ :param words: Mnemonic words generated using Bip39 :param index: Index of account :param hd_path: Bip44 Path. By default Ethereum is used :return: List of ethereum public addresses """ seed = Bip39SeedGenerator(words).Generate() bip32_ctx = Bip32.FromSeedAndPath(seed, hd_path) pub_key = bip32_ctx.ChildKey(index).m_ver_key.pubkey return checksum_encode( sha3( encode_int32(pub_key.point.x()) + encode_int32(pub_key.point.y()))[12:])
def generate_bitcoin_wallet(query_params={}): if blockchain_validator.generate_litecoin_wallet(query_params): if query_params != {}: mnemonic = query_params['mnemonic'] else: mnemonic = wallet.generate_mnemonic(strength=256) if Bip39MnemonicValidator(mnemonic).Validate(): seed_bytes = Bip39SeedGenerator(mnemonic).Generate() bip32_ctx = Bip32.FromSeedAndPath(seed_bytes, "m/44'/0'/0'/0") return { "xpriv": bip32_ctx.PrivateKey().ToExtended(), "xpub": bip32_ctx.PublicKey().ToExtended(), "mnemonic": mnemonic } else: return 'Mnemonic is not valid!'
def derive_private_key(words: List[str], index: int = 0) -> bytes: ''' Get a private key from the mnemonic wallet, default to the 0 index of the deviration. (first key) Parameters ---------- words : List[str] A list of english words. index : int, optional The private key index, first private key., by default 0 Returns ------- bytes [description] ''' seed = derive_seed(words) bip32_ctx = Bip32.FromSeedAndPath(seed, _get_vet_key_path(index)) return bip32_ctx.PrivateKey().Raw().ToBytes()
from avaxpython.utils.formatting.encoding import Encoding from bip_utils import Bip32 import ref import json sample_wallet = next(generator.generate(1, Config.KEY_SIZE)) word_length = len(sample_wallet.split(" ")) if len(sys.argv) != word_length+1: print("Invalid phrase length provided. Got {} expected {}.".format(len(sys.argv)-1, word_length)) exit(1) seed = Mnemonic("english").to_seed(" ".join(sys.argv[1:]), passphrase="") masterHdKey = Bip32.FromSeed(seed) accountHdKey = Bip32.FromSeedAndPath(seed, WalletConfig.AVA_ACCOUNT_PATH) # the first private key generated by an HD wallet seeded from this mnemonic phrase firstHDKey = BIP32.derive_master_key(accountHdKey, '0/0') masterKey = masterHdKey.PrivateKey().Raw().ToBytes() accountKey = accountHdKey.PrivateKey().Raw().ToBytes() firstKey = firstHDKey.PrivateKey().Raw().ToBytes() enc = Encoding() pkey_prefix = "PrivateKey-{}" masterKeyEncoded = pkey_prefix.format(enc.Encode(masterKey)) accountKeyEncoded = pkey_prefix.format(enc.Encode(accountKey)) firstKeyEncoded = pkey_prefix.format(enc.Encode(firstKey))
print("No mnemonic given") exit(1) mnemonic = sys.argv[1] words = mnemonic.split(" ") print("BIP39 mnemonic starts with: {} {} ...".format(words[0], words[1])) seed_bytes = Bip39SeedGenerator(mnemonic).Generate() print("BIP39 seed: {}".format(hexlify(seed_bytes).decode("utf-8"))) bip32 = Bip32.FromSeed(seed_bytes) print("BIP32 root key: {}".format(bip32.PrivateKey().ToExtended())) derivation_path = "m/44'/60'/0'/0" extended = Bip32.FromSeedAndPath(seed_bytes, derivation_path) print("BIP32 extended public key: {}".format( extended.PublicKey().ToExtended())) print("BIP32 extended private key: {}".format( extended.PrivateKey().ToExtended())) derived_0_path = f"{derivation_path}/0" derived_0_address = extended.DerivePath("0") print("Derived {} public key: {}".format( derived_0_path, derived_0_address.EcdsaPublicKey().RawCompressed().ToHex())) print("Derived {} private key: {}".format( derived_0_path, derived_0_address.EcdsaPrivateKey().Raw().ToHex()))