def on_qr(self, data): from electrum.bitcoin import base_decode, is_address data = data.strip() if is_address(data): self.set_URI(data) return if data.startswith('bitcoin:'): self.set_URI(data) return # try to decode transaction from electrum.transaction import Transaction try: text = base_decode(data, None, base=43).encode('hex') tx = Transaction(text) tx.deserialize() if self.wallet: my_coins = self.wallet.get_spendable_coins( None, self.electrum_config) my_outpoints = [ vin['prevout_hash'] + ':' + str(vin['prevout_n']) for vin in my_coins ] for i, txin in enumerate(tx.inputs()): outpoint = txin['prevout_hash'] + ':' + str( txin['prevout_n']) if outpoint in my_outpoints: my_index = my_outpoints.index(outpoint) tx._inputs[i]['value'] = my_coins[my_index]['value'] except: tx = None if tx: self.tx_dialog(tx) return # show error self.show_error("Unable to decode QR data")
def on_qr(self, data): from electrum.bitcoin import base_decode, is_address data = data.strip() if is_address(data): self.set_URI(data) return if data.startswith('bitcoin:'): self.set_URI(data) return if data.startswith('ln'): self.set_ln_invoice(data) return # try to decode transaction from electrum.transaction import Transaction from electrum.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 showTransaction_desc_(self, txraw, desc) -> None: tx = Transaction(txraw) tx.deserialize() tx_hash, status_, label_, can_broadcast, can_bump, amount, fee, height, conf, timestamp, exp_n = wallet( ).get_tx_info(tx) # print("send: status_",status_,"label_",label_,"amount",amount,"conf",conf) size = tx.estimated_size() conf = 0 if conf is None else conf timestamp = time.time() if timestamp is None else timestamp status, status_str = ( status_, _("Unsigned") ) # wallet().get_tx_status(tx_hash, height, conf, timestamp) doFX = fx() and fx().is_enabled() ccy = fx().get_currency() if doFX else None fiat_amount_str = str(self.fiat.text) if doFX else None # HistoryEntry = namedtuple("HistoryEntry", "tx tx_hash status_str label v_str balance_str date ts conf status value fiat_amount fiat_balance fiat_amount_str fiat_balance_str ccy status_image") entry = HistoryEntry( tx, tx_hash, status_str, str(desc), self.amt.text, "", timestamp_to_datetime(time.time() if conf <= 0 else timestamp), timestamp, conf, status, amount, None, None, fiat_amount_str, None, ccy, None) def newLabel(l): self.descDel.text = l self.navigationController.pushViewController_animated_( txdetail.CreateTxDetailWithEntry(entry, on_label=newLabel), True) # For modal do the following: # self.presentViewController_animated_completion_(txdetail.CreateTxDetailWithEntry(entry, on_label = newLabel, asModalNav = True), True, None) '''
def do_paste(self): data = self.app._clipboard.paste().strip() if not data: self.app.show_info(_("Clipboard is empty")) return # try to decode as transaction try: raw_tx = tx_from_str(data) tx = Transaction(raw_tx) tx.deserialize() except: tx = None if tx: self.app.tx_dialog(tx) return lower = data.lower() if lower.startswith('lightning:ln'): lower = lower[10:] # try to decode as URI/address if lower.startswith('ln'): self.set_ln_invoice(lower) else: self.set_URI(data) # save automatically self.save_invoice()
def on_qr(self, data): from electrum.bitcoin import base_decode, is_p2pkh, is_address data = data.strip() if is_p2pkh(data): uri = "vipstoken:{}?to_addr={}".format(self.contract_addr, data) self.set_URI(uri) return if is_address(data): self.app.show_info(_("QR data isn't p2pkh address.")) return if self.match_token(data): self.app.show_info(_("QR data is Contract Address.")) return if data.startswith('vipstoken:'): self.set_URI(data) return if data.startswith('vipstarcoin:'): self.set_URI(data) return # try to decode transaction from electrum.transaction import Transaction from electrum.util import bh2u try: text = bh2u(base_decode(data, None, base=43)) tx = Transaction(text) tx.deserialize() except: tx = None if tx: self.app.show_info(_("QR data is transaction.")) return # show error self.app.show_error(_("Unable to decode QR data"))
def sign_tx(self, tx): """tx should be a serialized hex tx. If self.password is correctly set, will return the raw transaction with all inputs from this wallet signed. """ if not self.password: raise Exception("No password, cannot sign") from electrum.transaction import Transaction etx = Transaction(tx) etx.deserialize() self.ewallet.sign_transaction(etx, self.password) return etx.raw
def sign_tx(self, tx): """tx should be a serialized hex tx. If self.password is correctly set, will return the raw transaction with all inputs from this wallet signed. """ if not self.password: raise Exception("No password, cannot sign") from electrum.transaction import Transaction etx = Transaction(tx) etx.deserialize() self.ewallet.sign_transaction(etx, self.password) return etx.raw
def do_paste(self): data = self.app._clipboard.paste() if not data: self.app.show_info(_("Clipboard is empty")) return # try to decode as transaction try: raw_tx = tx_from_str(data) tx = Transaction(raw_tx) tx.deserialize() except: tx = None if tx: self.app.tx_dialog(tx) return # try to decode as URI/address self.set_URI(data)
def pushtx(self, txhex, timeout=10): #synchronous send log.debug("About to push over electrum") from electrum.transaction import Transaction etx = Transaction(txhex) etx.deserialize() tx_hash = etx.hash() try: retval = self.wallet.network.synchronous_get( ('blockchain.transaction.broadcast', [str(etx)]), timeout) except: log.debug("Failed electrum push") return False if retval != tx_hash: log.debug("Pushtx over Electrum returned wrong value: " + str(retval)) return False log.debug("Pushed via Electrum successfully, hash: " + tx_hash) return True
def pushtx(self, txhex, timeout=10): #synchronous send from electrum.transaction import Transaction etx = Transaction(txhex) etx.deserialize() tx_hash = etx.hash() try: retval = self.wallet.network.synchronous_get( ('blockchain.transaction.broadcast', [str(etx)]), timeout) except: log.debug("Failed electrum push") return False if retval != tx_hash: log.debug("Pushtx over Electrum returned wrong value: " + str(retval)) return False log.debug("Pushed via Electrum successfully, hash: " + tx_hash) return True
def recover_tx_from_psbt(first: BasicPSBT, wallet: Abstract_Wallet) -> Transaction: # Take a PSBT object and re-construct the Electrum transaction object. # - does not include signatures, see merge_sigs_from_psbt # - any PSBT in the group could be used for this purpose; all must share tx details tx = Transaction(first.txn.hex()) tx.deserialize(force_full_parse=True) # .. add back some data that's been preserved in the PSBT, but isn't part of # of the unsigned bitcoin txn tx.is_partial_originally = True for idx, inp in enumerate(tx.inputs()): scr = first.inputs[idx].redeem_script or first.inputs[ idx].witness_script # XXX should use transaction.py parse_scriptSig() here! if scr: try: M, N, __, pubkeys, __ = parse_redeemScript_multisig(scr) except NotRecognizedRedeemScript: # limitation: we can only handle M-of-N multisig here raise ValueError("Cannot handle non M-of-N multisig input") inp['pubkeys'] = pubkeys inp['x_pubkeys'] = pubkeys inp['num_sig'] = M inp['type'] = 'p2wsh' if first.inputs[ idx].witness_script else 'p2sh' # bugfix: transaction.py:parse_input() puts empty dict here, but need a list inp['signatures'] = [None] * N if 'prev_tx' not in inp: # fetch info about inputs' previous txn wallet.add_hw_info(tx) if 'value' not in inp: # we'll need to know the value of the outpts used as part # of the witness data, much later... inp['value'] = inp['prev_tx'].outputs()[inp['prevout_n']].value return tx
def sign_tx(self, tx, addrs): """tx should be a serialized hex tx. If self.password is correctly set, will return the raw transaction with all inputs from this wallet signed. """ if not self.password: raise Exception("No password, cannot sign") from electrum.transaction import Transaction etx = Transaction(tx) etx.deserialize() for i in addrs.keys(): del etx._inputs[i]['scriptSig'] del etx._inputs[i]['pubkeys'] self.ewallet.add_input_sig_info(etx._inputs[i], addrs[i]) etx._inputs[i]['address'] = addrs[i] etx._inputs[i]['type'] = 'p2pkh' self.ewallet.sign_transaction(etx, self.password) return etx.raw
def on_qr(self, data): from electrum.bitcoin import base_decode, is_address data = data.strip() if is_address(data): self.set_URI(data) return if data.startswith('bitcoin:'): self.set_URI(data) return # try to decode transaction from electrum.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 on_qr(self, data): from electrum.bitcoin import base_decode, is_address data = data.strip() if is_address(data) or data.startswith('vipstarcoin:'): self.app.show_info(_("QR data is bitcoin URI.")) return if self.search_token(data) or data.startswith('vipstoken:'): self.set_URI(data) return # try to decode transaction from electrum.transaction import Transaction from electrum.util import bh2u try: text = bh2u(base_decode(data, None, base=43)) tx = Transaction(text) tx.deserialize() except: tx = None if tx: self.app.show_info(_("QR data is transaction.")) return # show error self.app.show_error(_("Unable to decode QR data"))
def on_qr(self, data): from electrum.bitcoin import base_decode, is_address data = data.strip() if is_address(data): self.set_URI(data) return if data.startswith(constants.net.URI_SCHEME + ':'): self.set_URI(data) return # try to decode transaction from electrum.transaction import Transaction from electrum.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 main(self): add_menu() welcome = "Use the menu to scan a transaction." droid.fullShow(qr_layout(welcome)) while True: event = droid.eventWait().result if not event: continue elif event["name"] == "key": if event["data"]["key"] == "4": if self.qr_data: self.show_qr(None) self.show_title(welcome) else: break elif event["name"] == "seed": password = self.get_password() if password is False: modal_dialog("Error", "incorrect password") continue seed = wallet.get_mnemonic(password) modal_dialog("Your seed is", seed) elif event["name"] == "password": self.change_password_dialog() elif event["name"] == "xpub": mpk = wallet.get_master_public_key() self.show_qr(mpk) self.show_title("master public key") elif event["name"] == "scan": r = droid.scanBarcode() r = r.result if not r: continue data = r["extras"]["SCAN_RESULT"] data = base_decode(data.encode("utf8"), None, base=43) data = "".join(chr(ord(b)) for b in data).encode("hex") tx = Transaction.deserialize(data) # except: # modal_dialog('Error', 'Cannot parse transaction') # continue if not wallet.can_sign(tx): modal_dialog("Error", "Cannot sign this transaction") continue lines = map(lambda x: x[0] + u"\t\t" + format_satoshis(x[1]) if x[1] else x[0], tx.get_outputs()) if not modal_question("Sign?", "\n".join(lines)): continue password = self.get_password() if password is False: modal_dialog("Error", "incorrect password") continue droid.dialogCreateSpinnerProgress("Signing") droid.dialogShow() wallet.sign_transaction(tx, password) droid.dialogDismiss() data = base_encode(str(tx).decode("hex"), base=43) self.show_qr(data) self.show_title("Signed Transaction") droid.makeToast("Bye!")
def main(self): add_menu() welcome = 'Use the menu to scan a transaction.' droid.fullShow(qr_layout(welcome)) while True: event = droid.eventWait().result if not event: continue elif event["name"] == "key": if event["data"]["key"] == '4': if self.qr_data: self.show_qr(None) self.show_title(welcome) else: break elif event["name"] == "seed": password = self.get_password() if password is False: modal_dialog('Error', 'incorrect password') continue seed = wallet.get_mnemonic(password) modal_dialog('Your seed is', seed) elif event["name"] == "password": self.change_password_dialog() elif event["name"] == "xpub": mpk = wallet.get_master_public_key() self.show_qr(mpk) self.show_title('master public key') elif event["name"] == "scan": r = droid.scanBarcode() r = r.result if not r: continue data = r['extras']['SCAN_RESULT'] data = base_decode(data.encode('utf8'), None, base=43) data = ''.join(chr(ord(b)) for b in data).encode('hex') tx = Transaction.deserialize(data) #except: # modal_dialog('Error', 'Cannot parse transaction') # continue if not wallet.can_sign(tx): modal_dialog('Error', 'Cannot sign this transaction') continue lines = map( lambda x: x[0] + u'\t\t' + format_satoshis(x[1]) if x[1] else x[0], tx.get_outputs()) if not modal_question('Sign?', '\n'.join(lines)): continue password = self.get_password() if password is False: modal_dialog('Error', 'incorrect password') continue droid.dialogCreateSpinnerProgress("Signing") droid.dialogShow() wallet.sign_transaction(tx, password) droid.dialogDismiss() data = base_encode(str(tx).decode('hex'), base=43) self.show_qr(data) self.show_title('Signed Transaction') droid.makeToast("Bye!")