ttransaction: impl tx.to_qr_data(): move logic from GUI to tx class - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit c4c22312c4bb2e9953448e2bdc6a29892746cf91 DIR parent 7b91da9966fcef4b2c9b743e38178cd2fd8c20ca HTML Author: SomberNight <somber.night@protonmail.com> Date: Wed, 23 Sep 2020 14:50:35 +0200 ttransaction: impl tx.to_qr_data(): move logic from GUI to tx class Diffstat: M electrum/gui/kivy/uix/dialogs/tx_d… | 11 ++--------- M electrum/gui/qt/transaction_dialog… | 9 ++------- M electrum/transaction.py | 13 ++++++++++++- 3 files changed, 16 insertions(+), 17 deletions(-) --- DIR diff --git a/electrum/gui/kivy/uix/dialogs/tx_dialog.py b/electrum/gui/kivy/uix/dialogs/tx_dialog.py t@@ -271,16 +271,9 @@ class TxDialog(Factory.Popup): self.app.broadcast(self.tx) def show_qr(self): - from electrum.bitcoin import base_encode, bfh original_raw_tx = str(self.tx) - tx = copy.deepcopy(self.tx) # make copy as we mutate tx - if isinstance(tx, PartialTransaction): - # this makes QR codes a lot smaller (or just possible in the first place!) - tx.convert_all_utxos_to_witness_utxos() - - text = tx.serialize_as_bytes() - text = base_encode(text, base=43) - self.app.qr_dialog(_("Raw Transaction"), text, text_for_clipboard=original_raw_tx) + qr_data = self.tx.to_qr_data() + self.app.qr_dialog(_("Raw Transaction"), qr_data, text_for_clipboard=original_raw_tx) def remove_local_tx(self): txid = self.tx.txid() DIR diff --git a/electrum/gui/qt/transaction_dialog.py b/electrum/gui/qt/transaction_dialog.py t@@ -288,14 +288,9 @@ class BaseTxDialog(QDialog, MessageBoxMixin): def show_qr(self, *, tx: Transaction = None): if tx is None: tx = self.tx - tx = copy.deepcopy(tx) # make copy as we mutate tx - if isinstance(tx, PartialTransaction): - # this makes QR codes a lot smaller (or just possible in the first place!) - tx.convert_all_utxos_to_witness_utxos() - text = tx.serialize_as_bytes() - text = base_encode(text, base=43) + qr_data = tx.to_qr_data() try: - self.main_window.show_qrcode(text, 'Transaction', parent=self) + self.main_window.show_qrcode(qr_data, 'Transaction', parent=self) except qrcode.exceptions.DataOverflowError: self.show_error(_('Failed to display QR code.') + '\n' + _('Transaction is too large in size.')) DIR diff --git a/electrum/transaction.py b/electrum/transaction.py t@@ -38,6 +38,7 @@ from collections import defaultdict from enum import IntEnum import itertools import binascii +import copy from . import ecc, bitcoin, constants, segwit_addr, bip32 from .bip32 import BIP32Node t@@ -46,7 +47,8 @@ from .bitcoin import (TYPE_ADDRESS, TYPE_SCRIPT, hash_160, hash160_to_p2sh, hash160_to_p2pkh, hash_to_segwit_addr, var_int, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC, COIN, int_to_hex, push_script, b58_address_to_hash160, - opcodes, add_number_to_script, base_decode, is_segwit_script_type) + opcodes, add_number_to_script, base_decode, is_segwit_script_type, + base_encode) from .crypto import sha256d from .logging import get_logger t@@ -832,6 +834,15 @@ class Transaction: else: return nVersion + txins + txouts + nLocktime + def to_qr_data(self) -> str: + """Returns tx as data to be put into a QR code. No side-effects.""" + tx = copy.deepcopy(self) # make copy as we mutate tx + if isinstance(tx, PartialTransaction): + # this makes QR codes a lot smaller (or just possible in the first place!) + tx.convert_all_utxos_to_witness_utxos() + tx_bytes = tx.serialize_as_bytes() + return base_encode(tx_bytes, base=43) + def txid(self) -> Optional[str]: if self._cached_txid is None: self.deserialize()