def __init__(self, mnemonic, seed, nlocktime_file): logging.info( 'Reading nlocktime transactions from {}'.format(nlocktime_file)) self.compressed_zip = open(nlocktime_file, "rb").read() self.mnemonic = mnemonic self.seed = seed version = wally.BIP32_VER_MAIN_PRIVATE self.wallet = wally.bip32_key_from_seed(self.seed, version, wally.BIP32_FLAG_SKIP_HASH) chaincode = wally.bip32_key_get_chain_code(self.wallet) zipdata = gacommon._unzip(self.compressed_zip, chaincode) if len(zipdata) == 0: raise exceptions.GARecoveryError( 'The nlocktimes file "{}" contains no transactions'.format( nlocktime_file)) self.txdata = [ json.loads(txdata.decode("ascii")) for txdata in zipdata ] self.fixup_old_nlocktimes() inferred_network = self.infer_network() if inferred_network != clargs.args.network: msg = 'Specified network and network inferred from nlocktime file do not match' \ '(specified={}, inferred={})'.format(clargs.args.network, inferred_network) raise exceptions.InvalidNetwork(msg)
def gait_paths_from_seed(seed): """Get the paths for deriving the GreenAddress xpubs from a hex seed, rather than mnemonic This is an alternative derivation path used with hardware wallets where the mnemonic may not be available. It is based on a hardened public key derived from the backed up hw wallet seed. Returns two possible paths corresponding to two different client implementations. """ assert len(seed) == wally.BIP39_SEED_LEN_512 # Passing version=BIP32_VER_MAIN_PRIVATE here although it may be either MAIN or TEST # This version indicator only matters if you serialize the key version = wally.BIP32_VER_MAIN_PRIVATE root_key = wally.bip32_key_from_seed(seed, version, wally.BIP32_FLAG_SKIP_HASH) # path = m/18241' # 18241 = 0x4741 = 'GA' flags = wally.BIP32_FLAG_KEY_PUBLIC | wally.BIP32_FLAG_SKIP_HASH path = [gaconstants.HARDENED | 18241] derived_public_key = wally.bip32_key_from_parent_path(root_key, path, flags) chain_code = wally.bip32_key_get_chain_code(derived_public_key) pub_key = wally.bip32_key_get_pub_key(derived_public_key) # For historic reasons some old clients use a hexlified input path here - generate both path_input = chain_code + pub_key path_input_hex = bytearray(wally.hex_from_bytes(chain_code + pub_key), 'ascii') return [get_gait_path(path_input) for path_input in [path_input, path_input_hex]]
def get_recovery(options, mnemonic, seed): """Return an instance of either TwoOfTwo, TwoOfThree or LiquidRecovery, depending on options""" if options.recovery_mode == 'csv': if is_liquid(options.network): return LiquidRecovery(mnemonic) raise exceptions.InvalidNetwork( 'recovery method {} is not available for this network'.format( options.recovery_mode)) elif options.recovery_mode == '2of3': # Passing BIP32_VER_MAIN_PRIVATE although it may be on TEST. It doesn't make any difference # because they key is not going to be serialized version = wally.BIP32_VER_MAIN_PRIVATE wallet = wally.bip32_key_from_seed(seed, version, wally.BIP32_FLAG_SKIP_HASH) backup_wallet = None if not options.custom_xprv: recovery_mnemonic = get_recovery_mnemonic(options) backup_wallet = wallet_from_mnemonic(recovery_mnemonic) return TwoOfThree(mnemonic, wallet, backup_wallet, options.custom_xprv) elif options.recovery_mode == '2of2': return TwoOfTwo(mnemonic, seed, options.nlocktime_file) else: return TwoOfTwoCSV(mnemonic, seed)
def from_seed(cls, seed, is_testnet=False): """Create a bip32 key from a 128, 256, or 512 bit seed""" version = { True: wally.BIP32_VER_TEST_PRIVATE, False: wally.BIP32_VER_MAIN_PRIVATE }[is_testnet] extkey = wally.bip32_key_from_seed(seed, version, wally.BIP32_FLAG_SKIP_HASH) return cls(extkey)
def get_recovery(options, mnemonic, seed): """Return an instance of either TwoOfTwo or TwoOfThree, depending on options""" if options.recovery_mode == '2of3': # Passing BIP32_VER_MAIN_PRIVATE although it may be on TEST. It doesn't make any difference # because they key is not going to be serialized version = wally.BIP32_VER_MAIN_PRIVATE wallet = wally.bip32_key_from_seed(seed, version, wally.BIP32_FLAG_SKIP_HASH) backup_wallet = None if not options.custom_xprv: recovery_mnemonic = get_recovery_mnemonic(options) backup_wallet = wallet_from_mnemonic(recovery_mnemonic) return TwoOfThree(mnemonic, wallet, backup_wallet, options.custom_xprv) else: return TwoOfTwo(mnemonic, seed, options.nlocktime_file)
def __init__(self, mnemonic, seed, nlocktime_file): logging.info( 'Reading nlocktime transactions from {}'.format(nlocktime_file)) self.compressed_zip = open(nlocktime_file, "rb").read() self.mnemonic = mnemonic self.seed = seed version = wally.BIP32_VER_MAIN_PRIVATE self.wallet = wally.bip32_key_from_seed(self.seed, version, wally.BIP32_FLAG_SKIP_HASH) chaincode = wally.bip32_key_get_chain_code(self.wallet) zipdata = gacommon._unzip(self.compressed_zip, chaincode) if len(zipdata) == 0: raise exceptions.GARecoveryError( 'The nlocktimes file "{}" contains no transactions'.format( nlocktime_file)) self.txdata = [ json.loads(txdata.decode("ascii")) for txdata in zipdata ] self.fixup_old_nlocktimes() self.is_testnet = self._is_testnet()
def wallet_from_mnemonic(mnemonic_or_hex_seed, ver=wally.BIP32_VER_MAIN_PRIVATE): """Generate a BIP32 HD Master Key (wallet) from a mnemonic phrase or a hex seed""" seed, mnemonic = seed_from_mnemonic(mnemonic_or_hex_seed) return wally.bip32_key_from_seed(seed, ver, wally.BIP32_FLAG_SKIP_HASH)
def master_key(self): return wally.bip32_key_from_seed(self.seed, wally.BIP32_VER_TEST_PRIVATE, wally.BIP32_FLAG_KEY_PRIVATE)
def master_key(self): _, seed = wally.bip39_mnemonic_to_seed512(self._mnemonic, None) return wally.bip32_key_from_seed(seed, wally.BIP32_VER_TEST_PRIVATE, wally.BIP32_FLAG_KEY_PRIVATE)
import wallycore as wally import os address_prefix = wally.WALLY_CA_PREFIX_LIQUID_REGTEST network = wally.WALLY_NETWORK_LIQUID_REGTEST wif_prefix = wally.WALLY_ADDRESS_VERSION_WIF_TESTNET mnemonic = "supreme layer police brand month october rather rack proud strike receive joy limit random hill inside brand depend giant success quarter brain butter mechanic" # start-create_p2pkh_address _, seed = wally.bip39_mnemonic_to_seed512(mnemonic, '') wallet_master_key = wally.bip32_key_from_seed(seed, wally.BIP32_VER_TEST_PRIVATE, 0) wallet_derived_key = wally.bip32_key_from_parent(wallet_master_key, 1, wally.BIP32_FLAG_KEY_PRIVATE) address = wally.bip32_key_to_address( wallet_derived_key, wally.WALLY_ADDRESS_TYPE_P2PKH, wally.WALLY_ADDRESS_VERSION_P2PKH_LIQUID_REGTEST) # end-create_p2pkh_address # start-derive_blinding_key master_blinding_key = wally.asset_blinding_key_from_seed(seed) script_pubkey = wally.address_to_scriptpubkey( address, wally.WALLY_NETWORK_LIQUID_REGTEST) private_blinding_key = wally.asset_blinding_key_to_ec_private_key( master_blinding_key, script_pubkey) public_blinding_key = wally.ec_public_key_from_private_key( private_blinding_key) # end-derive_blinding_key # start-create_conf_address
while(True): i = i + 1 # get entropy entropy = os.urandom(32) # calculate mnemonic mnemonic = wally.bip39_mnemonic_from_bytes(None, entropy) # calculate the seed seed = bytearray(64) password = '' wally.bip39_mnemonic_to_seed(mnemonic, password, seed) # calculate master key master_key = wally.bip32_key_from_seed(seed, master_key_flags, wally.BIP32_FLAG_SKIP_HASH) if args.verbose > 1: print('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::') print('Seed: {}'.format(seed.hex())) print('Mnemonic: {}'.format(mnemonic)) print('Master key: {}'.format(wally.bip32_key_to_base58(master_key, 0))) print('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::') # derive a children found = False for x in range(0, args.children + 1): child = x if args.hardened == True: child = child + 0x80000000 derived = wally.bip32_key_from_parent_path(master_key, path + [child], wally.BIP32_FLAG_KEY_PRIVATE);
def from_seed(cls, seed: bytes): """Create a xprv from a 128, 256, or 512 bit seed""" extkey = wally.bip32_key_from_seed(seed, pywally.params.BIP32_PRIVKEY, 0) return cls(extkey)
import sys try: # Desktop: make test vectors from hashlib import sha256 import wallycore as w with open('test_hdnode_gen.py', 'wt') as fd: print("import gc, ngu # auto-gen", file=fd) print("HDNode = ngu.hdnode.HDNode", file=fd) print("for i in range(3):", file=fd) ms = b'1' * 32 for i in range(10): ms = sha256(ms).digest() node = w.bip32_key_from_seed(ms, 0x488ade4, 0) pub = w.bip32_key_get_pub_key(node) priv = w.bip32_key_get_priv_key(node) fp = bytearray(4) w.bip32_key_get_fingerprint(node, fp) cc = w.bip32_key_get_chain_code(node) xprv = w.base58check_from_bytes(w.bip32_key_serialize(node, 0)) xpub = w.base58check_from_bytes( w.bip32_key_serialize(node, w.BIP32_FLAG_KEY_PUBLIC)) addr = w.bip32_key_to_address(node, w.WALLY_ADDRESS_TYPE_P2PKH, 0) print(" a = HDNode(); a.from_master(%r)" % ms, file=fd) print(" assert a.pubkey() == %r" % pub, file=fd) print(" assert a.privkey() == %r" % priv, file=fd) print(" assert a.my_fp() == 0x%s" % fp.hex(), file=fd) print(" assert a.chain_code() == %r" % bytes(cc), file=fd)