def _make_wallet_cashacct_pseudo_contacts(self, exclude_contacts=[] ) -> List[Contact]: ''' Returns a list of 'fake' contacts that come from the wallet's own registered Vitae IDs. These contacts do not exist in the wallet.contacts object but are created on-the-fly from the wallet.cashacct list of registered & verified Vitae IDs. The creation of this list is relatively cheap and scales as the lookups are O(logN) in the cashaccts caches. This is a convenience so that the Contacts tab shows "my" cash accounts after registration as well as external Vitae IDs. Note that the "mine" entries won't be shown if the user explicitly added his own as "external"... ''' try: excl_chk = set((c.name, Address.from_string(c.address)) for c in exclude_contacts if c.type == 'cashacct') except: # Hmm.. invalid address? excl_chk = set() wallet_cashaccts = [] v_txids = set() # Add the [Mine] pseudo-contacts for ca_info in self.wallet.cashacct.get_wallet_cashaccounts(): v_txids.add(ca_info.txid) name = self.wallet.cashacct.fmt_info(ca_info, emoji=False) if (name, ca_info.address) in excl_chk: continue wallet_cashaccts.append( Contact(name=name, address=ca_info.address.to_ui_string(), type='cashacct_W')) # Add the [Pend] pseudo-contacts for txid, tup in self._ca_pending_conf.copy().items(): if txid in v_txids or self.wallet.cashacct.is_verified(txid): self._ca_pending_conf.pop(txid, None) continue if tup in excl_chk: continue name, address = tup wallet_cashaccts.append( Contact(name=name, address=address.to_ui_string(), type='cashacct_T')) return wallet_cashaccts
def resolve(self): self.is_alias = False if self.hasFocus(): return if self.is_multiline(): # only supports single line entries atm return if self.is_pr: return key = str(self.toPlainText()) if key == self.previous_payto: return self.previous_payto = key if not (('.' in key) and (not '<' in key) and (not ' ' in key)): return parts = key.split(sep=',') # assuming single lie if parts and len(parts) > 0 and Address.is_valid(parts[0]): return try: data = self.win.contacts.resolve(key) except: return if not data: return self.is_alias = True address = data.get('address') name = data.get('name') new_url = key + ' <' + address + '>' self.setText(new_url) self.previous_payto = new_url if isinstance(self.win.contacts, dict): # old contacts API self.win.contacts[key] = ('openalias', name) else: try: from electroncash.contacts import Contact self.win.contacts.add(Contact(name=name, address=key, type='openalias'), unique=True) except Exception as e: print_error( "[Custom PayToEdit] Could not determine contacts API, giving up:", repr(e)) self.win.contact_list.on_update() self.setFrozen(True) if data.get('type') == 'openalias': self.validated = data.get('validated') if self.validated: self.setGreen() else: self.setExpired() else: self.validated = None
def _make_wallet_lns_pseudo_contacts(self, exclude_contacts = []) -> List[Contact]: ''' Returns a list of 'fake' contacts that come from the wallet's own registered LNS Names. These contacts do not exist in the wallet.contacts object but are created on-the-fly from the wallet.lns list of LNS Names. The creation of this list is relatively cheap and scales as the lookups are O(logN) in the LNS caches. This is a convenience so that the Contacts tab shows "my" LNS Names after registration as well as external LNS Names. Note that the "mine" entries won't be shown if the user explicitly added his own as "external"... ''' try: excl_chk = set((c.name, Address.from_string(c.address)) for c in exclude_contacts if c.type == 'lns') except: # Hmm.. invalid address? excl_chk = set() wallet_lns_names = [] need_save = False # Add the [Mine] pseudo-contacts for lns_info in self.wallet.lns.get_wallet_lns_names(): name = self.wallet.lns.fmt_info(lns_info) if (name, lns_info.address) in excl_chk: continue wallet_lns_names.append(Contact( name = name, address = lns_info.address.to_ui_string(), type = 'lns_W' )) my_lns = Contact(name=name, address=lns_info.address.to_ui_string(), type='lns') if not self.wallet.contacts.has(my_lns): # HACK: Force-add this contract to the "saved" list since it is "mine" but it wasn't in the list before # if we got here. self.wallet.contacts.add(my_lns, unique=True, save=False) need_save = True if need_save: self.wallet.contacts.save() return wallet_lns_names
def add_contact(entry: ContactsEntry, do_write=True) -> bool: parent = gui.ElectrumGui.gui wallet = parent.wallet if wallet is None: utils.NSLog("add_contact: wallent was None, returning early") return False c = wallet.contacts if c is None: utils.NSLog("add_contact: contacts was None, returning early") return False n = c.num c.add(Contact(name=entry.name, address=entry.address_str, type="address"), unique=True) n2 = c.num c.save() if do_write: c.storage.write() ret = n2 - n utils.NSLog("added %d contact(s)", ret) return bool(ret)
def delete_contact(entry: ContactsEntry, do_write=True) -> int: parent = gui.ElectrumGui.gui wallet = parent.wallet if wallet is None: utils.NSLog("delete_contacts: wallent was None, returning early") return None wc = wallet.contacts if not wc: # paranoia return None n = wc.num wc.remove( Contact(name=entry.name, address=entry.address_str, type='address')) history.delete_contact_history(entry.address) n2 = wc.num if n2 < n: wc.save() if do_write: wc.storage.write() ret = n - n2 utils.NSLog("deleted %d contact(s)", ret) return ret
def _resolve_open_alias(self, *, force_if_has_focus=False): prev_vals = self.is_alias, self.validated # used only if early return due to unchanged text below self.is_alias, self.validated = False, False if not force_if_has_focus and self.hasFocus(): return if self.is_multiline(): # only supports single line entries atm return if self.is_pr: return key = str(self.toPlainText()) key = key.strip() # strip whitespaces if key == self.previous_payto: # unchanged, restore previous state, abort early. self.is_alias, self.validated = prev_vals return self.is_alias self.previous_payto = key if '.' not in key or '<' in key or ' ' in key: # not an openalias or an openalias with extra info in it, bail..! return parts = key.split(sep=',') # assuming single line if parts and len(parts) > 0 and Address.is_valid(parts[0]): return try: data = self.win.contacts.resolve(key) except Exception as e: self.print_error(f'error resolving alias: {repr(e)}') return if not data: return address = data.get('address') name = data.get('name') _type = data.get('type') if _type != 'openalias': return address_str = None if isinstance(address, str): address_str = address elif isinstance(address, Address): address_str = address.to_ui_string() else: raise RuntimeError('unknown address type') self.is_alias = True new_url = key + ' <' + address_str + '>' self.setText(new_url) self.previous_payto = new_url self.win.contacts.add(Contact(name=name, address=key, type='openalias'), unique=True) self.setFrozen(True) self.validated = bool(data.get('validated')) if self.validated: self.setGreen() else: self.setExpired() return True