def create_wallet(path, password, max_mixdepth, wallet_cls=None, **kwargs): storage = Storage(path, password, create=True) wallet_cls = wallet_cls or get_wallet_cls() wallet_cls.initialize(storage, get_network(), max_mixdepth=max_mixdepth, **kwargs) storage.save() return wallet_cls(storage)
def open_wallet(path, ask_for_password=True, password=None, read_only=False, **kwargs): """ Open the wallet file at path and return the corresponding wallet object. params: path: str, full path to wallet file ask_for_password: bool, if False password is assumed unset and user will not be asked to type it password: password for storage, ignored if ask_for_password is True read_only: bool, if True, open wallet in read-only mode kwargs: additional options to pass to wallet's init method returns: wallet object """ if not os.path.isfile(path): raise Exception( "Failed to open wallet at '{}': not a file".format(path)) if not Storage.is_storage_file(path): raise Exception("Failed to open wallet at '{}': not a valid joinmarket" " wallet.\n\nIf this wallet is in the old json format " "you need to convert it using the conversion script" "at `scripts/convert_old_wallet.py`".format(path)) if ask_for_password and Storage.is_encrypted_storage_file(path): while True: try: # do not try empty password, assume unencrypted on empty password pwd = get_password( "Enter wallet decryption passphrase: ") or None storage = Storage(path, password=pwd, read_only=read_only) except StoragePasswordError: jmprint("Wrong password, try again.", "warning") continue except Exception as e: jmprint("Failed to load wallet, error message: " + repr(e), "error") raise e break else: storage = Storage(path, password, read_only=read_only) wallet_cls = get_wallet_cls_from_storage(storage) wallet = wallet_cls(storage, **kwargs) wallet_sanity_check(wallet) return wallet
def new_wallet_from_data(data, file_name): print("Creating new wallet file.") new_pw = cli_get_wallet_passphrase_check() if new_pw is False: return False storage = Storage(file_name, create=True, password=new_pw) wallet_cls = get_wallet_cls(wtype=get_configured_wallet_type(False)) kwdata = { 'entropy': data['entropy'], 'timestamp': data.get('creation_time'), 'max_mixdepth': get_max_mixdepth(data) } if 'entropy_ext' in data: kwdata['entropy_extension'] = data['entropy_ext'] wallet_cls.initialize(storage, data['network'], **kwdata) wallet = wallet_cls(storage) if 'index_cache' in data: for md, indices in enumerate(data['index_cache']): wallet.set_next_index(md, BaseWallet.ADDRESS_TYPE_EXTERNAL, indices[0], force=True) wallet.set_next_index(md, BaseWallet.ADDRESS_TYPE_INTERNAL, indices[1], force=True) if 'imported' in data: for md in data['imported']: for privkey in data['imported'][md]: privkey += b'\x01' wif = BTCEngine.privkey_to_wif(privkey) wallet.import_private_key(md, wif) wallet.save() wallet.close() return True
def new_wallet_from_data(data, file_name): print("Creating new wallet file.") new_pw = cli_get_wallet_passphrase_check() if new_pw is False: return False storage = Storage(file_name, create=True, password=new_pw) wallet_cls = get_wallet_cls() kwdata = { 'entropy': data['entropy'], 'timestamp': data.get('creation_time'), 'max_mixdepth': get_max_mixdepth(data) } if 'entropy_ext' in data: kwdata['entropy_extension'] = data['entropy_ext'] wallet_cls.initialize(storage, data['network'], **kwdata) wallet = wallet_cls(storage) if 'index_cache' in data: for md, indices in enumerate(data['index_cache']): wallet.set_next_index(md, 0, indices[0], force=True) wallet.set_next_index(md, 1, indices[1], force=True) if 'imported' in data: for md in data['imported']: for privkey in data['imported'][md]: privkey += b'\x01' wif = wif_compressed_privkey(hexlify(privkey).decode('ascii')) wallet.import_private_key(md, wif) wallet.save() wallet.close() return True