def test_is_valid_address(self): for priv_details in self.priv_pub_addr: addr = priv_details['address'] self.assertFalse(is_address(priv_details['priv'])) self.assertFalse(is_address(priv_details['pub'])) self.assertTrue(is_address(addr)) is_enc_b58 = priv_details['addr_encoding'] == 'base58' self.assertEqual(is_enc_b58, is_b58_address(addr)) self.assertFalse(is_address("not an address"))
def do_send(self): if not is_address(self.str_recipient): print(_('Invalid Dash address')) return try: amount = int(Decimal(self.str_amount) * COIN) except Exception: print(_('Invalid Amount')) return try: fee = int(Decimal(self.str_fee) * COIN) except Exception: print(_('Invalid Fee')) return if self.wallet.has_password(): password = self.password_dialog() if not password: return else: password = None c = "" while c != "y": c = input("ok to send (y/n)?") if c == "n": return try: tx = self.wallet.mktx(outputs=[ PartialTxOutput.from_address_and_value(self.str_recipient, amount) ], password=password, fee=fee) except Exception as e: print(repr(e)) return if self.str_description: self.wallet.set_label(tx.txid(), self.str_description) print(_("Please wait...")) try: coro = self.wallet.psman.broadcast_transaction(tx) self.network.run_from_another_thread(coro) except TxBroadcastError as e: msg = e.get_message_for_gui() print(msg) except BestEffortRequestFailed as e: msg = repr(e) print(msg) else: print(_('Payment sent.'))
def show_address_helper(self, wallet, address, keystore=None): if keystore is None: keystore = wallet.get_keystore() if not is_address(address): keystore.handler.show_error(_('Invalid Dash Address')) return False if not wallet.is_mine(address): keystore.handler.show_error(_('Address not in wallet.')) return False if type(keystore) != self.keystore_class: return False return True
def do_send(self): if not is_address(self.str_recipient): print(_('Invalid Dash address')) return try: amount = int(Decimal(self.str_amount) * COIN) except Exception: print(_('Invalid Amount')) return try: fee = int(Decimal(self.str_fee) * COIN) except Exception: print(_('Invalid Fee')) return if self.wallet.has_password(): password = self.password_dialog() if not password: return else: password = None c = "" while c != "y": c = input("ok to send (y/n)?") if c == "n": return try: tx = self.wallet.mktx( [TxOutput(TYPE_ADDRESS, self.str_recipient, amount)], password, self.config, fee) except Exception as e: print(str(e)) return if self.str_description: self.wallet.labels[tx.txid()] = self.str_description print(_("Please wait...")) status, msg = self.network.broadcast_transaction(tx) if status: print(_('Payment sent.')) #self.do_clear() #self.update_contacts_tab() else: display_msg = _( 'The server returned an error when broadcasting the transaction.' ) display_msg += '\n' + repr(e) print(display_msg)
def do_send(self): if not is_address(self.str_recipient): print(_('Invalid PACGlobal address')) return try: amount = int(Decimal(self.str_amount) * COIN) except Exception: print(_('Invalid Amount')) return try: fee = int(Decimal(self.str_fee) * COIN) except Exception: print(_('Invalid Fee')) return if self.wallet.has_password(): password = self.password_dialog() if not password: return else: password = None c = "" while c != "y": c = input("ok to send (y/n)?") if c == "n": return try: tx = self.wallet.mktx( [TxOutput(TYPE_ADDRESS, self.str_recipient, amount)], password, self.config, fee) except Exception as e: print(str(e)) return if self.str_description: self.wallet.labels[tx.txid()] = self.str_description print(_("Please wait...")) try: self.network.run_from_another_thread( self.network.broadcast_transaction(tx)) except TxBroadcastError as e: msg = e.get_message_for_gui() print(msg) except BestEffortRequestFailed as e: msg = repr(e) print(msg) else: print(_('Payment sent.'))
def create_menu(self, position): from electrum_dash.wallet import Multisig_Wallet is_multisig = isinstance(self.wallet, Multisig_Wallet) can_delete = self.wallet.can_delete_address() selected = self.selectedItems() multi_select = len(selected) > 1 addrs = [item.text(1) for item in selected] if not addrs: return if not multi_select: item = self.itemAt(position) col = self.currentColumn() if not item: return addr = addrs[0] if not is_address(addr): item.setExpanded(not item.isExpanded()) return menu = QMenu() if not multi_select: column_title = self.headerItem().text(col) copy_text = item.text(col) menu.addAction(_("Copy {}").format(column_title), lambda: self.parent.app.clipboard().setText(copy_text)) menu.addAction(_('Details'), lambda: self.parent.show_address(addr)) if col in self.editable_columns: menu.addAction(_("Edit {}").format(column_title), lambda: self.editItem(item, col)) menu.addAction(_("Request payment"), lambda: self.parent.receive_at(addr)) if self.wallet.can_export(): menu.addAction(_("Private key"), lambda: self.parent.show_private_key(addr)) if not is_multisig and not self.wallet.is_watching_only(): menu.addAction(_("Sign/verify message"), lambda: self.parent.sign_verify_message(addr)) menu.addAction(_("Encrypt/decrypt message"), lambda: self.parent.encrypt_message(addr)) if can_delete: menu.addAction(_("Remove from wallet"), lambda: self.parent.remove_address(addr)) addr_URL = block_explorer_URL(self.config, 'addr', addr) if addr_URL: menu.addAction(_("View on block explorer"), lambda: webbrowser.open(addr_URL)) if not self.wallet.is_frozen(addr): menu.addAction(_("Freeze"), lambda: self.parent.set_frozen_state([addr], True)) else: menu.addAction(_("Unfreeze"), lambda: self.parent.set_frozen_state([addr], False)) coins = self.wallet.get_utxos(addrs) if coins: menu.addAction(_("Spend from"), lambda: self.parent.spend_coins(coins)) run_hook('receive_menu', menu, addrs, self.wallet) menu.exec_(self.viewport().mapToGlobal(position))
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()) key = key.strip() # strip whitespaces 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 line if parts and len(parts) > 0 and bitcoin.is_address(parts[0]): return try: data = self.win.contacts.resolve(key) except Exception as e: self.print_error(f'error resolving address/alias: {repr(e)}') 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 self.win.config.get('openalias_autoadd') == 'checked': self.win.contacts[key] = ('openalias', name) self.win.contact_list.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 do_send(self): if not is_address(self.str_recipient): print(_('Invalid Dash address')) return try: amount = int(Decimal(self.str_amount) * COIN) except Exception: print(_('Invalid Amount')) return try: fee = int(Decimal(self.str_fee) * COIN) except Exception: print(_('Invalid Fee')) return if self.wallet.has_password(): password = self.password_dialog() if not password: return else: password = None c = "" while c != "y": c = input("ok to send (y/n)?") if c == "n": return try: tx = self.wallet.mktx([TxOutput(TYPE_ADDRESS, self.str_recipient, amount)], password, self.config, fee) except Exception as e: print(str(e)) return if self.str_description: self.wallet.labels[tx.txid()] = self.str_description print(_("Please wait...")) status, msg = self.network.broadcast_transaction(tx) if status: print(_('Payment sent.')) #self.do_clear() #self.update_contacts_tab() else: print(_('Error'))
def do_send(self): if not is_address(self.str_recipient): self.show_message(_('Invalid Dash address')) return try: amount = int(Decimal(self.str_amount) * COIN) except Exception: self.show_message(_('Invalid Amount')) return try: fee = int(Decimal(self.str_fee) * COIN) except Exception: self.show_message(_('Invalid Fee')) return if self.wallet.has_password(): password = self.password_dialog() if not password: return else: password = None try: tx = self.wallet.mktx( [TxOutput(TYPE_ADDRESS, self.str_recipient, amount)], password, self.config, fee) except Exception as e: self.show_message(str(e)) return if self.str_description: self.wallet.labels[tx.txid()] = self.str_description self.show_message(_("Please wait..."), getchar=False) try: self.network.run_from_another_thread( self.network.broadcast_transaction(tx)) except TxBroadcastError as e: msg = e.get_message_for_gui() self.show_message(msg) except BestEffortRequestFailed as e: msg = repr(e) self.show_message(msg) else: self.show_message(_('Payment sent.')) self.do_clear()
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 line if parts and len(parts) > 0 and bitcoin.is_address(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 self.win.config.get('openalias_autoadd') == 'checked': self.win.contacts[key] = ('openalias', name) 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_new_contact(): code = droid.scanBarcode() r = code.result if r: data = str(r['extras']['SCAN_RESULT']).strip() if data: if re.match('^dash:', data): out = util.parse_URI(data) address = out.get('address') elif is_address(data): address = data else: address = None if address: if modal_question('Add to contacts?', address): # fixme: ask for key contacts[address] = ('address', address) else: modal_dialog('Invalid address', data)
def do_send(self): if not is_address(self.str_recipient): self.show_message(_('Invalid Dash address')) return try: amount = int(Decimal(self.str_amount) * COIN) except Exception: self.show_message(_('Invalid Amount')) return try: fee = int(Decimal(self.str_fee) * COIN) except Exception: self.show_message(_('Invalid Fee')) return if self.wallet.has_password(): password = self.password_dialog() if not password: return else: password = None try: tx = self.wallet.mktx([(TYPE_ADDRESS, self.str_recipient, amount)], password, self.config, fee) except Exception as e: self.show_message(str(e)) return if self.str_description: self.wallet.labels[tx.hash()] = self.str_description self.show_message(_("Please wait..."), getchar=False) status, msg = self.network.broadcast(tx) if status: self.show_message(_('Payment sent.')) self.do_clear() #self.update_contacts_tab() else: self.show_message(_('Error'))
def on_qr(self, data): from electrum_dash.bitcoin import base_decode, is_address data = data.strip() if is_address(data): self.set_URI(data) return if data.startswith('dash:'): self.set_URI(data) return # try to decode transaction from electrum_dash.transaction import Transaction try: text = base_decode(data, None, base=43).encode('hex') tx = Transaction(text) tx.deserialize() except: tx = None if tx: self.tx_dialog(tx) return # show error self.show_error("Unable to decode QR data")
def do_send(self): if self.screen.is_pr: if self.payment_request.has_expired(): self.app.show_error(_('Payment request has expired')) return outputs = self.payment_request.get_outputs() else: address = str(self.screen.address) if not address: self.app.show_error(_('Recipient not specified.') + ' ' + _('Please scan a Dash address or a payment request')) return if not bitcoin.is_address(address): self.app.show_error(_('Invalid Dash Address') + ':\n' + address) return try: amount = self.app.get_amount(self.screen.amount) except: self.app.show_error(_('Invalid amount') + ':\n' + self.screen.amount) return outputs = [TxOutput(bitcoin.TYPE_ADDRESS, address, amount)] message = self.screen.message amount = sum(map(lambda x:x[2], outputs)) self._do_send(amount, message, outputs)
def on_qr(self, data): from electrum_dash.bitcoin import base_decode, is_address data = data.strip() if is_address(data): self.set_URI(data) return if data.startswith('dash:'): self.set_URI(data) return # try to decode transaction from electrum_dash.transaction import Transaction from electrum_dash.util import bh2u try: text = bh2u(base_decode(data, None, base=43)) tx = Transaction(text) tx.deserialize() except: tx = None if tx: self.tx_dialog(tx) return # show error self.show_error("Unable to decode QR data")
def read_invoice(self): address = str(self.address) if not address: self.app.show_error( _('Recipient not specified.') + ' ' + _('Please scan a Dash address or a payment request')) return if not self.amount: self.app.show_error(_('Please enter an amount')) return if self.is_max: amount = '!' else: try: amount = self.app.get_amount(self.amount) except: self.app.show_error(_('Invalid amount') + ':\n' + self.amount) return message = self.message try: if self.payment_request: outputs = self.payment_request.get_outputs() else: if not bitcoin.is_address(address): self.app.show_error( _('Invalid Dash Address') + ':\n' + address) return outputs = [ PartialTxOutput.from_address_and_value(address, amount) ] return self.app.wallet.create_invoice(outputs=outputs, message=message, pr=self.payment_request, URI=self.parsed_URI) except InvoiceError as e: self.app.show_error(_('Error creating payment') + ':\n' + str(e))
def parse_address(self, line): r = line.strip() m = re.match('^'+RE_ALIAS+'$', r) address = m.group(2) if m else r assert bitcoin.is_address(address) return address
def payto_loop(): global recipient if recipient: droid.fullSetProperty("recipient","text",recipient) recipient = None out = None while out is None: event = droid.eventWait().result if not event: continue print "got event in payto loop", event if event == 'OK': continue if not event.get("name"): continue if event["name"] == "click": id = event["data"]["id"] if id=="buttonPay": droid.fullQuery() recipient = droid.fullQueryDetail("recipient").result.get('text') message = droid.fullQueryDetail("message").result.get('text') amount = droid.fullQueryDetail('amount').result.get('text') if not is_address(recipient): modal_dialog('Error','Invalid Dash address') continue try: amount = int(COIN * Decimal(amount)) except Exception: modal_dialog('Error','Invalid amount') continue result = pay_to(recipient, amount, message) if result: out = 'main' elif id=="buttonContacts": addr = select_from_contacts() droid.fullSetProperty("recipient", "text", addr) elif id=="buttonQR": code = droid.scanBarcode() r = code.result if r: data = str(r['extras']['SCAN_RESULT']).strip() if data: print "data", data if re.match('^dash:', data): payto, amount, label, message, _ = util.parse_URI(data) if amount: amount = str(amount / COIN) droid.fullSetProperty("recipient", "text", payto) droid.fullSetProperty("amount", "text", amount) droid.fullSetProperty("message", "text", message) elif is_address(data): droid.fullSetProperty("recipient", "text", data) else: modal_dialog('Error','cannot parse QR code\n'+data) elif event["name"] in menu_commands: out = event["name"] elif event["name"]=="key": if event["data"]["key"] == '4': out = 'main' #elif event["name"]=="screen": # if event["data"]=="destroy": # out = 'main' return out
def payto_loop(): global recipient if recipient: droid.fullSetProperty("recipient", "text", recipient) recipient = None out = None while out is None: event = droid.eventWait().result if not event: continue print "got event in payto loop", event if event == 'OK': continue if not event.get("name"): continue if event["name"] == "click": id = event["data"]["id"] if id == "buttonPay": droid.fullQuery() recipient = droid.fullQueryDetail("recipient").result.get( 'text') message = droid.fullQueryDetail("message").result.get('text') amount = droid.fullQueryDetail('amount').result.get('text') if not is_address(recipient): modal_dialog('Error', 'Invalid Dash address') continue try: amount = int(COIN * Decimal(amount)) except Exception: modal_dialog('Error', 'Invalid amount') continue result = pay_to(recipient, amount, message) if result: out = 'main' elif id == "buttonContacts": addr = select_from_contacts() droid.fullSetProperty("recipient", "text", addr) elif id == "buttonQR": code = droid.scanBarcode() r = code.result if r: data = str(r['extras']['SCAN_RESULT']).strip() if data: print "data", data if re.match('^dash:', data): payto, amount, label, message, _ = util.parse_URI( data) if amount: amount = str(amount / COIN) droid.fullSetProperty("recipient", "text", payto) droid.fullSetProperty("amount", "text", amount) droid.fullSetProperty("message", "text", message) elif is_address(data): droid.fullSetProperty("recipient", "text", data) else: modal_dialog('Error', 'cannot parse QR code\n' + data) elif event["name"] in menu_commands: out = event["name"] elif event["name"] == "key": if event["data"]["key"] == '4': out = 'main' #elif event["name"]=="screen": # if event["data"]=="destroy": # out = 'main' return out
def parse_address(self, line): r = line.strip() m = re.match('^'+RE_ALIAS+'$', r) address = str(m.group(2) if m else r) assert bitcoin.is_address(address) return address
def test_is_address_bad_checksums(self): self.assertTrue(is_address('XhgzhL7rvJQV4MzRuis6btAyyDAhoMcGoA')) self.assertFalse(is_address('1819s5TxxbBtuRPr3qYskMVC8sb1pqapWw')) self.assertTrue(is_address('7maNAh4xVptDJRKzT2brX42iePPFbFGKfz')) self.assertFalse(is_address('3LrjLVnngqnaJeo3BQwMBg34iqYsjZjQUd'))