def sign_transaction(self, tx, password): if tx.is_complete(): return client = self.get_client() inputs = [] inputsPaths = [] pubKeys = [] chipInputs = [] redeemScripts = [] signatures = [] preparedTrustedInputs = [] changePath = "" changeAmount = None output = None outputAmount = None p2shTransaction = False segwitTransaction = False pin = "" self.get_client() # prompt for the PIN before displaying the dialog if necessary # Fetch inputs of the transaction to sign derivations = self.get_tx_derivations(tx) for txin in tx.inputs(): if txin['type'] == 'coinbase': self.give_error("Coinbase not supported") # should never happen if txin['type'] in ['p2sh']: p2shTransaction = True if txin['type'] in ['p2wpkh-p2sh', 'p2wsh-p2sh']: if not self.get_client_electrum().supports_segwit(): self.give_error(MSG_NEEDS_FW_UPDATE_SEGWIT) segwitTransaction = True if txin['type'] in ['p2wpkh', 'p2wsh']: if not self.get_client_electrum().supports_native_segwit(): self.give_error(MSG_NEEDS_FW_UPDATE_SEGWIT) segwitTransaction = True pubkeys, x_pubkeys = tx.get_sorted_pubkeys(txin) for i, x_pubkey in enumerate(x_pubkeys): if x_pubkey in derivations: signingPos = i s = derivations.get(x_pubkey) hwAddress = "%s/%d/%d" % (self.get_derivation()[2:], s[0], s[1]) break else: self.give_error("No matching x_key for sign_transaction") # should never happen redeemScript = Transaction.get_preimage_script(txin) if txin.get('prev_tx') is None: # and not Transaction.is_segwit_input(txin): # note: offline signing does not work atm even with segwit inputs for ledger raise Exception(_('Offline signing with {} is not supported.').format(self.device)) inputs.append([txin['prev_tx'].raw, txin['prevout_n'], redeemScript, txin['prevout_hash'], signingPos, txin.get('sequence', 0xffffffff - 1) ]) inputsPaths.append(hwAddress) pubKeys.append(pubkeys) # Sanity check if p2shTransaction: for txin in tx.inputs(): if txin['type'] != 'p2sh': self.give_error("P2SH / regular input mixed in same transaction not supported") # should never happen txOutput = var_int(len(tx.outputs())) for txout in tx.outputs(): output_type, addr, amount = txout txOutput += int_to_hex(amount, 8) script = tx.pay_script(output_type, addr) txOutput += var_int(len(script)//2) txOutput += script txOutput = bfh(txOutput) # Recognize outputs - only one output and one change is authorized if not p2shTransaction: if not self.get_client_electrum().supports_multi_output(): if len(tx.outputs()) > 2: self.give_error("Transaction with more than 2 outputs not supported") for _type, address, amount in tx.outputs(): assert _type == TYPE_ADDRESS info = tx.output_info.get(address) if (info is not None) and len(tx.outputs()) > 1 \ and info[0][0] == 1: # "is on 'change' branch" index, xpubs, m = info changePath = self.get_derivation()[2:] + "/%d/%d"%index changeAmount = amount else: output = address outputAmount = amount self.handler.show_message(_("Confirm Transaction on your Ledger device...")) try: # Get trusted inputs from the original transactions for utxo in inputs: sequence = int_to_hex(utxo[5], 4) if segwitTransaction: txtmp = bitcoinTransaction(bfh(utxo[0])) tmp = bfh(utxo[3])[::-1] tmp += bfh(int_to_hex(utxo[1], 4)) tmp += txtmp.outputs[utxo[1]].amount chipInputs.append({'value' : tmp, 'witness' : True, 'sequence' : sequence}) redeemScripts.append(bfh(utxo[2])) elif not p2shTransaction: txtmp = bitcoinTransaction(bfh(utxo[0])) trustedInput = self.get_client().getTrustedInput(txtmp, utxo[1]) trustedInput['sequence'] = sequence chipInputs.append(trustedInput) redeemScripts.append(txtmp.outputs[utxo[1]].script) else: tmp = bfh(utxo[3])[::-1] tmp += bfh(int_to_hex(utxo[1], 4)) chipInputs.append({'value' : tmp, 'sequence' : sequence}) redeemScripts.append(bfh(utxo[2])) # Sign all inputs firstTransaction = True inputIndex = 0 rawTx = tx.serialize() self.get_client().enableAlternate2fa(False) if segwitTransaction: self.get_client().startUntrustedTransaction(True, inputIndex, chipInputs, redeemScripts[inputIndex]) if changePath: # we don't set meaningful outputAddress, amount and fees # as we only care about the alternateEncoding==True branch outputData = self.get_client().finalizeInput(b'', 0, 0, changePath, bfh(rawTx)) else: outputData = self.get_client().finalizeInputFull(txOutput) outputData['outputData'] = txOutput transactionOutput = outputData['outputData'] if outputData['confirmationNeeded']: outputData['address'] = output self.handler.finished() pin = self.handler.get_auth( outputData ) # does the authenticate dialog and returns pin if not pin: raise UserWarning() if pin != 'paired': self.handler.show_message(_("Confirmed. Signing Transaction...")) while inputIndex < len(inputs): singleInput = [ chipInputs[inputIndex] ] self.get_client().startUntrustedTransaction(False, 0, singleInput, redeemScripts[inputIndex]) inputSignature = self.get_client().untrustedHashSign(inputsPaths[inputIndex], pin, lockTime=tx.locktime) inputSignature[0] = 0x30 # force for 1.4.9+ signatures.append(inputSignature) inputIndex = inputIndex + 1 else: while inputIndex < len(inputs): self.get_client().startUntrustedTransaction(firstTransaction, inputIndex, chipInputs, redeemScripts[inputIndex]) if changePath: # we don't set meaningful outputAddress, amount and fees # as we only care about the alternateEncoding==True branch outputData = self.get_client().finalizeInput(b'', 0, 0, changePath, bfh(rawTx)) else: outputData = self.get_client().finalizeInputFull(txOutput) outputData['outputData'] = txOutput if firstTransaction: transactionOutput = outputData['outputData'] if outputData['confirmationNeeded']: outputData['address'] = output self.handler.finished() pin = self.handler.get_auth( outputData ) # does the authenticate dialog and returns pin if not pin: raise UserWarning() if pin != 'paired': self.handler.show_message(_("Confirmed. Signing Transaction...")) else: # Sign input with the provided PIN inputSignature = self.get_client().untrustedHashSign(inputsPaths[inputIndex], pin, lockTime=tx.locktime) inputSignature[0] = 0x30 # force for 1.4.9+ signatures.append(inputSignature) inputIndex = inputIndex + 1 if pin != 'paired': firstTransaction = False except UserWarning: self.handler.show_error(_('Cancelled by user')) return except BTChipException as e: if e.sw == 0x6985: # cancelled by user return else: traceback.print_exc(file=sys.stderr) self.give_error(e, True) except BaseException as e: traceback.print_exc(file=sys.stdout) self.give_error(e, True) finally: self.handler.finished() for i, txin in enumerate(tx.inputs()): signingPos = inputs[i][4] txin['signatures'][signingPos] = bh2u(signatures[i]) tx.raw = tx.serialize()
def sign_transaction(self, tx: Transaction, password): if tx.is_complete(): return client = self.get_client() inputs = [] inputsPaths = [] pubKeys = [] chipInputs = [] redeemScripts = [] signatures = [] changePath = "" output = None p2shTransaction = False segwitTransaction = False pin = "" self.get_client( ) # prompt for the PIN before displaying the dialog if necessary # Fetch inputs of the transaction to sign derivations = self.get_tx_derivations(tx) for txin in tx.inputs(): if txin['type'] == 'coinbase': self.give_error( "Coinbase not supported") # should never happen if txin['type'] in ['p2sh']: p2shTransaction = True if txin['type'] in ['p2wpkh-p2sh', 'p2wsh-p2sh']: if not self.get_client_electrum().supports_segwit(): self.give_error(MSG_NEEDS_FW_UPDATE_SEGWIT) segwitTransaction = True if txin['type'] in ['p2wpkh', 'p2wsh']: if not self.get_client_electrum().supports_native_segwit(): self.give_error(MSG_NEEDS_FW_UPDATE_SEGWIT) segwitTransaction = True pubkeys, x_pubkeys = tx.get_sorted_pubkeys(txin) for i, x_pubkey in enumerate(x_pubkeys): if x_pubkey in derivations: signingPos = i s = derivations.get(x_pubkey) hwAddress = "%s/%d/%d" % (self.get_derivation()[2:], s[0], s[1]) break else: self.give_error("No matching x_key for sign_transaction" ) # should never happen redeemScript = Transaction.get_preimage_script(txin) txin_prev_tx = txin.get('prev_tx') if txin_prev_tx is None and not Transaction.is_segwit_input(txin): raise UserFacingException( _('Offline signing with {} is not supported for legacy inputs.' ).format(self.device)) txin_prev_tx_raw = txin_prev_tx.raw if txin_prev_tx else None inputs.append([ txin_prev_tx_raw, txin['prevout_n'], redeemScript, txin['prevout_hash'], signingPos, txin.get('sequence', 0xffffffff - 1), txin.get('value') ]) inputsPaths.append(hwAddress) pubKeys.append(pubkeys) # Sanity check if p2shTransaction: for txin in tx.inputs(): if txin['type'] != 'p2sh': self.give_error( "P2SH / regular input mixed in same transaction not supported" ) # should never happen txOutput = var_int(len(tx.outputs())) for o in tx.outputs(): output_type, addr, amount = o.type, o.address, o.value txOutput += int_to_hex(amount, 8) script = tx.pay_script(output_type, addr) txOutput += var_int(len(script) // 2) txOutput += script txOutput = bfh(txOutput) # Recognize outputs # - only one output and one change is authorized (for hw.1 and nano) # - at most one output can bypass confirmation (~change) (for all) if not p2shTransaction: if not self.get_client_electrum().supports_multi_output(): if len(tx.outputs()) > 2: self.give_error( "Transaction with more than 2 outputs not supported") has_change = False any_output_on_change_branch = is_any_tx_output_on_change_branch(tx) for o in tx.outputs(): assert o.type == TYPE_ADDRESS info = tx.output_info.get(o.address) if (info is not None) and len(tx.outputs()) > 1 \ and not has_change: index = info.address_index # prioritise hiding outputs on the 'change' branch from user # because no more than one change address allowed if info.is_change == any_output_on_change_branch: changePath = self.get_derivation( )[2:] + "/%d/%d" % index has_change = True else: output = o.address else: output = o.address self.handler.show_message( _("Confirm Transaction on your Ledger device...")) try: # Get trusted inputs from the original transactions for utxo in inputs: sequence = int_to_hex(utxo[5], 4) if segwitTransaction: tmp = bfh(utxo[3])[::-1] tmp += bfh(int_to_hex(utxo[1], 4)) tmp += bfh(int_to_hex(utxo[6], 8)) # txin['value'] chipInputs.append({ 'value': tmp, 'witness': True, 'sequence': sequence }) redeemScripts.append(bfh(utxo[2])) elif not p2shTransaction: txtmp = bitcoinTransaction(bfh(utxo[0])) trustedInput = self.get_client().getTrustedInput( txtmp, utxo[1]) trustedInput['sequence'] = sequence chipInputs.append(trustedInput) redeemScripts.append(txtmp.outputs[utxo[1]].script) else: tmp = bfh(utxo[3])[::-1] tmp += bfh(int_to_hex(utxo[1], 4)) chipInputs.append({'value': tmp, 'sequence': sequence}) redeemScripts.append(bfh(utxo[2])) # Sign all inputs firstTransaction = True inputIndex = 0 rawTx = tx.serialize_to_network() self.get_client().enableAlternate2fa(False) if segwitTransaction: self.get_client().startUntrustedTransaction( True, inputIndex, chipInputs, redeemScripts[inputIndex], version=tx.version) # we don't set meaningful outputAddress, amount and fees # as we only care about the alternateEncoding==True branch outputData = self.get_client().finalizeInput( b'', 0, 0, changePath, bfh(rawTx)) outputData['outputData'] = txOutput if outputData['confirmationNeeded']: outputData['address'] = output self.handler.finished() pin = self.handler.get_auth( outputData ) # does the authenticate dialog and returns pin if not pin: raise UserWarning() self.handler.show_message( _("Confirmed. Signing Transaction...")) while inputIndex < len(inputs): singleInput = [chipInputs[inputIndex]] self.get_client().startUntrustedTransaction( False, 0, singleInput, redeemScripts[inputIndex], version=tx.version) inputSignature = self.get_client().untrustedHashSign( inputsPaths[inputIndex], pin, lockTime=tx.locktime) inputSignature[0] = 0x30 # force for 1.4.9+ signatures.append(inputSignature) inputIndex = inputIndex + 1 else: while inputIndex < len(inputs): self.get_client().startUntrustedTransaction( firstTransaction, inputIndex, chipInputs, redeemScripts[inputIndex], version=tx.version) # we don't set meaningful outputAddress, amount and fees # as we only care about the alternateEncoding==True branch outputData = self.get_client().finalizeInput( b'', 0, 0, changePath, bfh(rawTx)) outputData['outputData'] = txOutput if outputData['confirmationNeeded']: outputData['address'] = output self.handler.finished() pin = self.handler.get_auth( outputData ) # does the authenticate dialog and returns pin if not pin: raise UserWarning() self.handler.show_message( _("Confirmed. Signing Transaction...")) else: # Sign input with the provided PIN inputSignature = self.get_client().untrustedHashSign( inputsPaths[inputIndex], pin, lockTime=tx.locktime) inputSignature[0] = 0x30 # force for 1.4.9+ signatures.append(inputSignature) inputIndex = inputIndex + 1 firstTransaction = False except UserWarning: self.handler.show_error(_('Cancelled by user')) return except BTChipException as e: if e.sw in (0x6985, 0x6d00): # cancelled by user return elif e.sw == 0x6982: raise # pin lock. decorator will catch it else: self.logger.exception('') self.give_error(e, True) except BaseException as e: self.logger.exception('') self.give_error(e, True) finally: self.handler.finished() for i, txin in enumerate(tx.inputs()): signingPos = inputs[i][4] tx.add_signature_to_txin(i, signingPos, bh2u(signatures[i])) tx.raw = tx.serialize()
def sign_transaction( self, keystore: Hardware_KeyStore, tx: PartialTransaction, wallet: Deterministic_Wallet, ): if tx.is_complete(): return if self.bitbox02_device is None: raise Exception( "Need to setup communication first before attempting any BitBox02 calls" ) coin = self._get_coin() tx_script_type = None # Build BTCInputType list inputs = [] for txin in tx.inputs(): my_pubkey, full_path = keystore.find_my_pubkey_in_txinout(txin) if full_path is None: raise Exception( "A wallet owned pubkey was not found in the transaction input to be signed" ) prev_tx = txin.utxo if prev_tx is None: raise UserFacingException(_('Missing previous tx.')) prev_inputs: List[bitbox02.BTCPrevTxInputType] = [] prev_outputs: List[bitbox02.BTCPrevTxOutputType] = [] for prev_txin in prev_tx.inputs(): prev_inputs.append({ "prev_out_hash": prev_txin.prevout.txid[::-1], "prev_out_index": prev_txin.prevout.out_idx, "signature_script": prev_txin.script_sig, "sequence": prev_txin.nsequence, }) for prev_txout in prev_tx.outputs(): prev_outputs.append({ "value": prev_txout.value, "pubkey_script": prev_txout.scriptpubkey, }) inputs.append({ "prev_out_hash": txin.prevout.txid[::-1], "prev_out_index": txin.prevout.out_idx, "prev_out_value": txin.value_sats(), "sequence": txin.nsequence, "keypath": full_path, "script_config_index": 0, "prev_tx": { "version": prev_tx.version, "locktime": prev_tx.locktime, "inputs": prev_inputs, "outputs": prev_outputs, }, }) if tx_script_type == None: tx_script_type = txin.script_type elif tx_script_type != txin.script_type: raise Exception("Cannot mix different input script types") if tx_script_type == "p2wpkh": tx_script_type = bitbox02.btc.BTCScriptConfig( simple_type=bitbox02.btc.BTCScriptConfig.P2WPKH) elif tx_script_type == "p2wpkh-p2sh": tx_script_type = bitbox02.btc.BTCScriptConfig( simple_type=bitbox02.btc.BTCScriptConfig.P2WPKH_P2SH) elif tx_script_type in ("p2wsh-p2sh", "p2wsh"): if type(wallet) is Multisig_Wallet: tx_script_type = self.btc_multisig_config( coin, full_path, wallet, tx_script_type) else: raise Exception( "Can only use p2wsh-p2sh or p2wsh with multisig wallets") else: raise UserFacingException( "invalid input script type: {} is not supported by the BitBox02" .format(tx_script_type)) # Build BTCOutputType list outputs = [] for txout in tx.outputs(): assert txout.address # check for change if txout.is_change: my_pubkey, change_pubkey_path = keystore.find_my_pubkey_in_txinout( txout) outputs.append( bitbox02.BTCOutputInternal( keypath=change_pubkey_path, value=txout.value, script_config_index=0, )) else: addrtype, payload = bitcoin.address_to_payload(txout.address) if addrtype == OnchainOutputType.P2PKH: output_type = bitbox02.btc.P2PKH elif addrtype == OnchainOutputType.P2SH: output_type = bitbox02.btc.P2SH elif addrtype == OnchainOutputType.WITVER0_P2WPKH: output_type = bitbox02.btc.P2WPKH elif addrtype == OnchainOutputType.WITVER0_P2WSH: output_type = bitbox02.btc.P2WSH elif addrtype == OnchainOutputType.WITVER1_P2TR: output_type = bitbox02.btc.P2TR else: raise UserFacingException( "Received unsupported output type during transaction signing: {} is not supported by the BitBox02" .format(addrtype)) outputs.append( bitbox02.BTCOutputExternal( output_type=output_type, output_payload=payload, value=txout.value, )) keypath_account = full_path[:-2] sigs = self.bitbox02_device.btc_sign( coin, [ bitbox02.btc.BTCScriptConfigWithKeypath( script_config=tx_script_type, keypath=keypath_account, ) ], inputs=inputs, outputs=outputs, locktime=tx.locktime, version=tx.version, ) # Fill signatures if len(sigs) != len(tx.inputs()): raise Exception( "Incorrect number of inputs signed.") # Should never occur signatures = [ bh2u(ecc.der_sig_from_sig_string(x[1])) + "01" for x in sigs ] tx.update_signatures(signatures)
def __init__(self, window: 'ElectrumWindow', chan_id: bytes): super().__init__(window) # initialize instance fields self.window = window chan = self.chan = window.wallet.lnworker.channels[chan_id] self.format = lambda msat: window.format_amount_and_units(msat / 1000) # connect signals with slots self.ln_payment_completed.connect(self.do_ln_payment_completed) self.htlc_added.connect(self.do_htlc_added) # register callbacks for updating window.network.register_callback(self.ln_payment_completed.emit, ['ln_payment_completed']) window.network.register_callback(self.htlc_added.emit, ['htlc_added']) # set attributes of QDialog self.setWindowTitle(_('Channel Details')) self.setMinimumSize(800, 400) # add layouts vbox = QtWidgets.QVBoxLayout(self) form_layout = QtWidgets.QFormLayout(None) vbox.addLayout(form_layout) # add form content form_layout.addRow(_('Node ID:'), SelectableLabel(bh2u(chan.node_id))) form_layout.addRow(_('Channel ID:'), SelectableLabel(bh2u(chan.channel_id))) funding_label_text = f'<a href=click_destination>{chan.funding_outpoint.txid}</a>:{chan.funding_outpoint.output_index}' form_layout.addRow(_('Funding Outpoint:'), LinkedLabel(funding_label_text, self.show_tx)) form_layout.addRow( _('Short Channel ID:'), SelectableLabel(format_short_channel_id(chan.short_channel_id))) self.received_label = SelectableLabel() form_layout.addRow(_('Received (mSAT):'), self.received_label) self.sent_label = SelectableLabel() form_layout.addRow(_('Sent (mSAT):'), self.sent_label) self.htlc_minimum_msat = SelectableLabel( str(chan.config[REMOTE].htlc_minimum_msat)) form_layout.addRow(_('Minimum HTLC value accepted by peer (mSAT):'), self.htlc_minimum_msat) self.max_htlcs = SelectableLabel( str(chan.config[REMOTE].max_accepted_htlcs)) form_layout.addRow( _('Maximum number of concurrent HTLCs accepted by peer:'), self.max_htlcs) self.max_htlc_value = SelectableLabel( self.window.format_amount_and_units( chan.config[REMOTE].max_htlc_value_in_flight_msat / 1000)) form_layout.addRow( _('Maximum value of in-flight HTLCs accepted by peer:'), self.max_htlc_value) self.dust_limit = SelectableLabel( self.window.format_amount_and_units( chan.config[REMOTE].dust_limit_sat)) form_layout.addRow(_('Remote dust limit:'), self.dust_limit) self.reserve = SelectableLabel( self.window.format_amount_and_units( chan.config[REMOTE].reserve_sat)) form_layout.addRow(_('Remote channel reserve:'), self.reserve) # add htlc tree view to vbox (wouldn't scale correctly in QFormLayout) form_layout.addRow(_('Payments (HTLCs):'), None) w = QtWidgets.QTreeView(self) htlc_dict = chan.get_payments() w.setModel(self.make_model(htlc_dict)) w.header().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents) vbox.addWidget(w) # initialize sent/received fields self.update_sent_received()
def __init__(self, window: 'ElectrumWindow', chan_id: bytes): super().__init__(window) # initialize instance fields self.window = window self.wallet = window.wallet chan = self.chan = window.wallet.lnworker.channels[chan_id] self.format_msat = lambda msat: window.format_amount_and_units(msat / 1000) # connect signals with slots self.ln_payment_completed.connect(self.do_ln_payment_completed) self.ln_payment_failed.connect(self.do_ln_payment_failed) self.state_changed.connect(self.do_state_changed) self.htlc_added.connect(self.do_htlc_added) # register callbacks for updating util.register_callback(self.ln_payment_completed.emit, ['ln_payment_completed']) util.register_callback(self.ln_payment_failed.emit, ['ln_payment_failed']) util.register_callback(self.htlc_added.emit, ['htlc_added']) util.register_callback(self.state_changed.emit, ['channel']) # set attributes of QDialog self.setWindowTitle(_('Channel Details')) self.setMinimumSize(800, 400) # add layouts vbox = QtWidgets.QVBoxLayout(self) vbox.addWidget(QLabel(_('Remote Node ID:'))) remote_id_e = ButtonsLineEdit(bh2u(chan.node_id)) remote_id_e.addCopyButton(self.window.app) remote_id_e.setReadOnly(True) vbox.addWidget(remote_id_e) funding_label_text = f'<a href=click_destination>{chan.funding_outpoint.txid}</a>:{chan.funding_outpoint.output_index}' vbox.addWidget(QLabel(_('Funding Outpoint:'))) vbox.addWidget(LinkedLabel(funding_label_text, self.show_tx)) form_layout = QtWidgets.QFormLayout(None) # add form content form_layout.addRow( _('Channel ID:'), SelectableLabel( f"{chan.channel_id.hex()} (Short: {chan.short_channel_id})")) form_layout.addRow(_('State:'), SelectableLabel(chan.get_state_for_GUI())) self.initiator = 'Local' if chan.constraints.is_initiator else 'Remote' form_layout.addRow(_('Initiator:'), SelectableLabel(self.initiator)) self.capacity = self.window.format_amount_and_units( chan.constraints.capacity) form_layout.addRow(_('Capacity:'), SelectableLabel(self.capacity)) self.can_send_label = SelectableLabel() self.can_receive_label = SelectableLabel() form_layout.addRow(_('Can send:'), self.can_send_label) form_layout.addRow(_('Can receive:'), self.can_receive_label) self.received_label = SelectableLabel() form_layout.addRow(_('Received:'), self.received_label) self.sent_label = SelectableLabel() form_layout.addRow(_('Sent:'), self.sent_label) #self.htlc_minimum_msat = SelectableLabel(str(chan.config[REMOTE].htlc_minimum_msat)) #form_layout.addRow(_('Minimum HTLC value accepted by peer (mSAT):'), self.htlc_minimum_msat) #self.max_htlcs = SelectableLabel(str(chan.config[REMOTE].max_accepted_htlcs)) #form_layout.addRow(_('Maximum number of concurrent HTLCs accepted by peer:'), self.max_htlcs) #self.max_htlc_value = SelectableLabel(self.window.format_amount_and_units(chan.config[REMOTE].max_htlc_value_in_flight_msat / 1000)) #form_layout.addRow(_('Maximum value of in-flight HTLCs accepted by peer:'), self.max_htlc_value) self.dust_limit = SelectableLabel( self.window.format_amount_and_units( chan.config[REMOTE].dust_limit_sat)) form_layout.addRow(_('Remote dust limit:'), self.dust_limit) self.remote_reserve = self.window.format_amount_and_units( chan.config[REMOTE].reserve_sat) form_layout.addRow(_('Remote reserve:'), SelectableLabel(self.remote_reserve)) vbox.addLayout(form_layout) # add htlc tree view to vbox (wouldn't scale correctly in QFormLayout) vbox.addWidget(QLabel(_('Payments (HTLCs):'))) w = QtWidgets.QTreeView(self) htlc_dict = chan.get_payments() w.setModel(self.make_model(htlc_dict)) w.header().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents) vbox.addWidget(w) vbox.addLayout(Buttons(CloseButton(self))) # initialize sent/received fields self.update()
def on_suggest(): remote_nodeid.setText(bh2u(lnworker.suggest_peer() or b'')) remote_nodeid.repaint() # macOS hack for #6269
def new_channel_dialog(self): lnworker = self.parent.wallet.lnworker d = WindowModalDialog(self.parent, _('Open Channel')) vbox = QVBoxLayout(d) vbox.addWidget( QLabel(_('Enter Remote Node ID or connection string or invoice'))) local_nodeid = FreezableLineEdit() local_nodeid.setMinimumWidth(700) local_nodeid.setText(bh2u(lnworker.node_keypair.pubkey)) local_nodeid.setFrozen(True) local_nodeid.setCursorPosition(0) remote_nodeid = QLineEdit() remote_nodeid.setMinimumWidth(700) amount_e = BTCAmountEdit(self.parent.get_decimal_point) # max button def spend_max(): amount_e.setFrozen(max_button.isChecked()) if not max_button.isChecked(): return make_tx = self.parent.mktx_for_open_channel('!') try: tx = make_tx(None) except (NotEnoughFunds, NoDynamicFeeEstimates) as e: max_button.setChecked(False) amount_e.setFrozen(False) self.main_window.show_error(str(e)) return amount = tx.output_value() amount = min(amount, LN_MAX_FUNDING_SAT) amount_e.setAmount(amount) max_button = EnterButton(_("Max"), spend_max) max_button.setFixedWidth(100) max_button.setCheckable(True) suggest_button = QPushButton(d, text=_('Suggest')) def on_suggest(): remote_nodeid.setText(bh2u(lnworker.suggest_peer() or b'')) remote_nodeid.repaint() # macOS hack for #6269 suggest_button.clicked.connect(on_suggest) clear_button = QPushButton(d, text=_('Clear')) def on_clear(): amount_e.setText('') amount_e.setFrozen(False) amount_e.repaint() # macOS hack for #6269 remote_nodeid.setText('') remote_nodeid.repaint() # macOS hack for #6269 max_button.setChecked(False) max_button.repaint() # macOS hack for #6269 clear_button.clicked.connect(on_clear) h = QGridLayout() h.addWidget(QLabel(_('Your Node ID')), 0, 0) h.addWidget(local_nodeid, 0, 1, 1, 3) h.addWidget(QLabel(_('Remote Node ID')), 1, 0) h.addWidget(remote_nodeid, 1, 1, 1, 3) h.addWidget(suggest_button, 2, 1) h.addWidget(clear_button, 2, 2) h.addWidget(QLabel('Amount'), 3, 0) h.addWidget(amount_e, 3, 1) h.addWidget(max_button, 3, 2) vbox.addLayout(h) ok_button = OkButton(d) ok_button.setDefault(True) vbox.addLayout(Buttons(CancelButton(d), ok_button)) if not d.exec_(): return if max_button.isChecked( ) and amount_e.get_amount() < LN_MAX_FUNDING_SAT: # if 'max' enabled and amount is strictly less than max allowed, # that means we have fewer coins than max allowed, and hence we can # spend all coins funding_sat = '!' else: funding_sat = amount_e.get_amount() connect_str = str(remote_nodeid.text()).strip() if not connect_str or not funding_sat: return self.parent.open_channel(connect_str, funding_sat, 0)
def __init__(self, window: 'ElectrumWindow', chan_id: bytes): super().__init__(window) # initialize instance fields self.window = window self.wallet = window.wallet chan = self.chan = window.wallet.lnworker.channels[chan_id] self.format_msat = lambda msat: window.format_amount_and_units(msat / 1000) self.format_sat = lambda sat: window.format_amount_and_units(sat) # connect signals with slots self.htlc_fulfilled.connect(self.on_htlc_fulfilled) self.htlc_failed.connect(self.on_htlc_failed) self.state_changed.connect(self.do_state_changed) self.htlc_added.connect(self.on_htlc_added) # register callbacks for updating util.register_callback(self.htlc_fulfilled.emit, ['htlc_fulfilled']) util.register_callback(self.htlc_failed.emit, ['htlc_failed']) util.register_callback(self.htlc_added.emit, ['htlc_added']) util.register_callback(self.state_changed.emit, ['channel']) # set attributes of QDialog self.setWindowTitle(_('Channel Details')) self.setMinimumSize(800, 400) # add layouts vbox = QtWidgets.QVBoxLayout(self) vbox.addWidget(QLabel(_('Remote Node ID:'))) remote_id_e = ButtonsLineEdit(bh2u(chan.node_id)) remote_id_e.addCopyButton(self.window.app) remote_id_e.setReadOnly(True) vbox.addWidget(remote_id_e) funding_label_text = f'<a href=click_destination>{chan.funding_outpoint.txid}</a>:{chan.funding_outpoint.output_index}' vbox.addWidget(QLabel(_('Funding Outpoint:'))) vbox.addWidget(LinkedLabel(funding_label_text, self.show_tx)) hbox_stats = QHBoxLayout() # channel stats left column form_layout_left = QtWidgets.QFormLayout(None) form_layout_left.addRow( _('Channel ID:'), WWLabel( f"{chan.channel_id.hex()} (Short: {chan.short_channel_id})")) form_layout_left.addRow(_('State:'), SelectableLabel(chan.get_state_for_GUI())) self.initiator = 'Local' if chan.constraints.is_initiator else 'Remote' form_layout_left.addRow(_('Initiator:'), SelectableLabel(self.initiator)) self.capacity = self.format_sat(chan.get_capacity()) form_layout_left.addRow(_('Capacity:'), SelectableLabel(self.capacity)) self.can_send_label = SelectableLabel() self.can_receive_label = SelectableLabel() form_layout_left.addRow(_('Can send:'), self.can_send_label) form_layout_left.addRow(_('Can receive:'), self.can_receive_label) #self.htlc_minimum_msat = SelectableLabel(str(chan.config[REMOTE].htlc_minimum_msat)) #form_layout_left.addRow(_('Minimum HTLC value accepted by peer (mSAT):'), self.htlc_minimum_msat) #self.max_htlcs = SelectableLabel(str(chan.config[REMOTE].max_accepted_htlcs)) #form_layout_left.addRow(_('Maximum number of concurrent HTLCs accepted by peer:'), self.max_htlcs) #self.max_htlc_value = SelectableLabel(self.format_sat(chan.config[REMOTE].max_htlc_value_in_flight_msat / 1000)) #form_layout_left.addRow(_('Maximum value of in-flight HTLCs accepted by peer:'), self.max_htlc_value) dust_limit_label = SelectableLabel("{}, {}".format( self.format_sat(chan.config[REMOTE].dust_limit_sat), self.format_sat(chan.config[LOCAL].dust_limit_sat), )) form_layout_left.addRow(_('Dust limit:'), dust_limit_label) chan_reserve_label = SelectableLabel("{}, {}".format( self.format_sat(chan.config[REMOTE].reserve_sat), self.format_sat(chan.config[LOCAL].reserve_sat), )) form_layout_left.addRow(_('Channel reserve:'), chan_reserve_label) form_layout_left.addRow( _('Channel type:'), SelectableLabel(chan.storage['channel_type'].name_minimal)) hbox_stats.addLayout(form_layout_left, 50) # vertical line separator line_separator = QtWidgets.QFrame() line_separator.setFrameShape(QtWidgets.QFrame.VLine) line_separator.setFrameShadow(QtWidgets.QFrame.Sunken) line_separator.setLineWidth(1) hbox_stats.addWidget(line_separator) # channel stats right column form_layout_right = QtWidgets.QFormLayout(None) self.local_balance_label = SelectableLabel() self.remote_balance_label = SelectableLabel() form_layout_right.addRow(_('Local balance:'), self.local_balance_label) form_layout_right.addRow(_('Remote balance:'), self.remote_balance_label) self.received_label = SelectableLabel() self.sent_label = SelectableLabel() form_layout_right.addRow(_('Total received so far:'), self.received_label) form_layout_right.addRow(_('Total sent so far:'), self.sent_label) hbox_stats.addLayout(form_layout_right, 50) vbox.addLayout(hbox_stats) # add htlc tree view to vbox (wouldn't scale correctly in QFormLayout) vbox.addWidget(QLabel(_('Payments (HTLCs):'))) w = QtWidgets.QTreeView(self) htlc_dict = chan.get_payments() htlc_list = [] for rhash, plist in htlc_dict.items(): for htlc_with_status in plist: htlc_list.append(htlc_with_status) w.setModel(self.make_model(htlc_list)) w.header().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents) vbox.addWidget(w) vbox.addLayout(Buttons(CloseButton(self))) # initialize sent/received fields self.update()