Skip to content

Commit 85d246b

Browse files
committed
refactor hwdevice prepare_transfer_tx_bulk
and fix user-votes summary in proposal details dialog
1 parent 44abec3 commit 85d246b

3 files changed

Lines changed: 55 additions & 128 deletions

File tree

src/hwdevice.py

Lines changed: 45 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -127,99 +127,52 @@ def getStatus(self):
127127

128128

129129

130-
131130
@process_ledger_exceptions
132-
def prepare_transfer_tx(self, caller, bip32_path, utxos_to_spend, dest_address, tx_fee, useSwiftX=False):
133-
with self.lock:
134-
# For each UTXO create a Ledger 'trusted input'
135-
self.trusted_inputs = []
136-
# https://klmoney.wordpress.com/bitcoin-dissecting-transactions-part-2-building-a-transaction-by-hand)
137-
self.arg_inputs = []
138-
self.amount = 0
131+
def append_inputs_to_TX(self, utxo, bip32_path):
132+
self.amount += int(utxo['value'])
133+
raw_tx = bytearray.fromhex(utxo['raw_tx'])
139134

140-
num_of_sigs = len(utxos_to_spend)
141-
curr_utxo_checked = 0
142-
for idx, utxo in enumerate(utxos_to_spend):
143-
144-
self.amount += int(utxo['value'])
145-
raw_tx = bytearray.fromhex(utxo['raw_tx'])
146-
147-
# parse the raw transaction, so that we can extract the UTXO locking script we refer to
148-
prev_transaction = bitcoinTransaction(raw_tx)
149-
150-
utxo_tx_index = utxo['tx_ouput_n']
151-
if utxo_tx_index < 0 or utxo_tx_index > len(prev_transaction.outputs):
152-
raise Exception('Incorrect value of outputIndex for UTXO %s' % str(idx))
153-
154-
155-
trusted_input = self.chip.getTrustedInput(prev_transaction, utxo_tx_index)
156-
157-
self.trusted_inputs.append(trusted_input)
158-
159-
# Hash check
160-
curr_pubkey = compress_public_key(self.chip.getWalletPublicKey(bip32_path)['publicKey'])
161-
pubkey_hash = bin_hash160(curr_pubkey)
162-
pubkey_hash_from_script = extract_pkh_from_locking_script(prev_transaction.outputs[utxo_tx_index].script)
163-
if pubkey_hash != pubkey_hash_from_script:
164-
text = "Error: The hashes for the public key for the BIP32 path, and the UTXO locking script do not match."
165-
text += "Your signed transaction will not be validated by the network.\n"
166-
text += "pubkey_hash: %s\n" % pubkey_hash.hex()
167-
text += "pubkey_hash_from_script: %s\n" % pubkey_hash_from_script.hex()
168-
printDbg(text)
169-
170-
self.arg_inputs.append({
171-
'locking_script': prev_transaction.outputs[utxo['tx_ouput_n']].script,
172-
'pubkey': curr_pubkey,
173-
'bip32_path': bip32_path,
174-
'outputIndex': utxo['tx_ouput_n'],
175-
'txid': utxo['tx_hash']
176-
})
177-
178-
# completion percent emitted
179-
curr_utxo_checked += 1
180-
completion = int(95*curr_utxo_checked / num_of_sigs)
181-
self.tx_progress.emit(completion)
182-
183-
self.amount -= int(tx_fee)
184-
self.amount = int(self.amount)
185-
arg_outputs = [{'address': dest_address, 'valueSat': self.amount}] # there will be multiple outputs soon
186-
self.new_transaction = bitcoinTransaction() # new transaction object to be used for serialization at the last stage
187-
self.new_transaction.version = bytearray([0x01, 0x00, 0x00, 0x00])
188-
189-
# completion percent emitted
190-
self.tx_progress.emit(99)
135+
# parse the raw transaction, so that we can extract the UTXO locking script we refer to
136+
prev_transaction = bitcoinTransaction(raw_tx)
191137

192-
for o in arg_outputs:
193-
output = bitcoinOutput()
194-
output.script = compose_tx_locking_script(o['address'])
195-
output.amount = int.to_bytes(o['valueSat'], 8, byteorder='little')
196-
self.new_transaction.outputs.append(output)
197-
198-
# completion percent emitted
199-
self.tx_progress.emit(100)
200-
201-
# join all outputs - will be used by Ledger for signing transaction
202-
self.all_outputs_raw = self.new_transaction.serializeOutputs()
203-
204-
self.mBox2 = QMessageBox(caller)
205-
self.messageText = "<p>Confirm transaction on your device, with the following details:</p>"
206-
#messageText += "From bip32_path: <b>%s</b><br><br>" % str(bip32_path)
207-
self.messageText += "<p>Payment to:<br><b>%s</b></p>" % dest_address
208-
self.messageText += "<p>Net amount:<br><b>%s</b> PIV</p>" % str(round(self.amount / 1e8, 8))
209-
if useSwiftX:
210-
self.messageText += "<p>Fees (SwiftX flat rate):<br><b>%s</b> PIV<p>" % str(round(int(tx_fee) / 1e8, 8))
211-
else:
212-
self.messageText += "<p>Fees:<br><b>%s</b> PIV<p>" % str(round(int(tx_fee) / 1e8, 8))
213-
messageText = self.messageText + "Signature Progress: 0 %"
214-
self.mBox2.setText(messageText)
215-
self.mBox2.setText(messageText)
216-
self.mBox2.setIconPixmap(caller.tabMain.ledgerImg.scaledToHeight(200, Qt.SmoothTransformation))
217-
self.mBox2.setWindowTitle("CHECK YOUR LEDGER")
218-
self.mBox2.setStandardButtons(QMessageBox.NoButton)
219-
self.mBox2.setMaximumWidth(500)
220-
self.mBox2.show()
138+
utxo_tx_index = utxo['tx_ouput_n']
139+
if utxo_tx_index < 0 or utxo_tx_index > len(prev_transaction.outputs):
140+
raise Exception('Incorrect value of outputIndex for UTXO %s-%d' %
141+
(utxo['raw_tx'], utxo['tx_ouput_n']))
221142

222-
ThreadFuns.runInThread(self.signTxSign, (), self.signTxFinish)
143+
trusted_input = self.chip.getTrustedInput(prev_transaction, utxo_tx_index)
144+
self.trusted_inputs.append(trusted_input)
145+
146+
# Hash check
147+
curr_pubkey = compress_public_key(self.chip.getWalletPublicKey(bip32_path)['publicKey'])
148+
pubkey_hash = bin_hash160(curr_pubkey)
149+
pubkey_hash_from_script = extract_pkh_from_locking_script(prev_transaction.outputs[utxo_tx_index].script)
150+
if pubkey_hash != pubkey_hash_from_script:
151+
text = "Error: The hashes for the public key for the BIP32 path, and the UTXO locking script do not match."
152+
text += "Your signed transaction will not be validated by the network.\n"
153+
text += "pubkey_hash: %s\n" % pubkey_hash.hex()
154+
text += "pubkey_hash_from_script: %s\n" % pubkey_hash_from_script.hex()
155+
printDbg(text)
156+
157+
self.arg_inputs.append({
158+
'locking_script': prev_transaction.outputs[utxo['tx_ouput_n']].script,
159+
'pubkey': curr_pubkey,
160+
'bip32_path': bip32_path,
161+
'outputIndex': utxo['tx_ouput_n'],
162+
'txid': utxo['tx_hash']
163+
})
164+
165+
166+
167+
168+
@process_ledger_exceptions
169+
def prepare_transfer_tx(self, caller, bip32_path, utxos_to_spend, dest_address, tx_fee, useSwiftX=False):
170+
rewardsArray = []
171+
mnode = {}
172+
mnode['path'] = bip32_path
173+
mnode['utxos'] = utxos_to_spend
174+
rewardsArray.append(mnode)
175+
self.prepare_transfer_tx_bulk(caller, rewardsArray, dest_address, tx_fee, useSwiftX)
223176

224177

225178

@@ -234,41 +187,9 @@ def prepare_transfer_tx_bulk(self, caller, rewardsArray, dest_address, tx_fee, u
234187
num_of_sigs = sum([len(mnode['utxos']) for mnode in rewardsArray])
235188
curr_utxo_checked = 0
236189

237-
for i, mnode in enumerate(rewardsArray):
238-
for idx, utxo in enumerate(mnode['utxos']):
239-
240-
self.amount += int(utxo['value'])
241-
raw_tx = bytearray.fromhex(utxo['raw_tx'])
242-
243-
# parse the raw transaction, so that we can extract the UTXO locking script we refer to
244-
prev_transaction = bitcoinTransaction(raw_tx)
245-
246-
utxo_tx_index = utxo['tx_ouput_n']
247-
if utxo_tx_index < 0 or utxo_tx_index > len(prev_transaction.outputs):
248-
raise Exception('Incorrect value of outputIndex for UTXO %s' % str(idx))
249-
250-
trusted_input = self.chip.getTrustedInput(prev_transaction, utxo_tx_index)
251-
self.trusted_inputs.append(trusted_input)
252-
253-
# Hash check
254-
curr_pubkey = compress_public_key(self.chip.getWalletPublicKey(mnode['path'])['publicKey'])
255-
pubkey_hash = bin_hash160(curr_pubkey)
256-
pubkey_hash_from_script = extract_pkh_from_locking_script(prev_transaction.outputs[utxo_tx_index].script)
257-
if pubkey_hash != pubkey_hash_from_script:
258-
text = "Error: The hashes for the public key for the BIP32 path, and the UTXO locking script do not match."
259-
text += "Your signed transaction will not be validated by the network.\n"
260-
text += "pubkey_hash: %s\n" % pubkey_hash.hex()
261-
text += "pubkey_hash_from_script: %s\n" % pubkey_hash_from_script.hex()
262-
printDbg(text)
263-
264-
self.arg_inputs.append({
265-
'locking_script': prev_transaction.outputs[utxo['tx_ouput_n']].script,
266-
'pubkey': curr_pubkey,
267-
'bip32_path': mnode['path'],
268-
'outputIndex': utxo['tx_ouput_n'],
269-
'txid': utxo['tx_hash']
270-
})
271-
190+
for mnode in rewardsArray:
191+
for utxo in mnode['utxos']:
192+
self.append_inputs_to_TX(utxo, mnode['path'])
272193
# completion percent emitted
273194
curr_utxo_checked += 1
274195
completion = int(95*curr_utxo_checked / num_of_sigs)

src/qt/dlg_proposalDetails.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class ProposalDetails_dlg(QDialog):
1212
def __init__(self, main_wnd, proposal):
1313
QDialog.__init__(self, parent=main_wnd)
1414
self.data = proposal
15+
myVotes = main_wnd.caller.parent.db.getMyVotes(proposal.Hash)
16+
self.myYeas = [[v["mn_name"], v["time"]] for v in myVotes if v["vote"] == "YES"]
17+
self.myAbstains = [[v["mn_name"], v["time"]] for v in myVotes if v["vote"] == "ABSTAIN"]
18+
self.myNays = [[v["mn_name"], v["time"]] for v in myVotes if v["vote"] == "NO"]
1519
self.setWindowTitle('Proposal Details')
1620
self.setupUI()
1721

@@ -77,13 +81,13 @@ def setupUi(self, PropDetailsDlg):
7781
votes += "<span style='color: red'>%d NAYS</span>" % PropDetailsDlg.data.Nays
7882
body.addRow(QLabel("<b>Votes: </b>"), QLabel(votes))
7983
my_yeas = ["%s <em style='color: green'>(%s)</em>" % (x[0], strftime('%Y-%m-%d %H:%M:%S',
80-
gmtime(x[1][1]))) for x in PropDetailsDlg.data.MyYeas]
84+
gmtime(x[1]))) for x in PropDetailsDlg.myYeas]
8185
body.addRow(QLabel("<b>My Yeas: </b>"), self.scroll(my_yeas))
8286
my_abstains = ["%s <em style='color: orange'>(%s)</em>" % (x[0], strftime('%Y-%m-%d %H:%M:%S',
83-
gmtime(x[1][1]))) for x in PropDetailsDlg.data.MyAbstains]
87+
gmtime(x[1]))) for x in PropDetailsDlg.myAbstains]
8488
body.addRow(QLabel("<b>My Abstains: </b>"), self.scroll(my_abstains))
8589
my_nays = ["%s <em style='color: red'>(%s)</em>" % (x[0], strftime('%Y-%m-%d %H:%M:%S',
86-
gmtime(x[1][1]))) for x in PropDetailsDlg.data.MyNays]
90+
gmtime(x[1]))) for x in PropDetailsDlg.myNays]
8791
body.addRow(QLabel("<b>My Nays: </b>"), self.scroll(my_nays))
8892
layout.addLayout(body)
8993
self.okButton = QPushButton('OK')

src/tabRewards.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,10 @@ def FinishSend(self, serialized_tx, amount_to_send):
426426
mess2 = QMessageBox(QMessageBox.Information, 'transaction Sent', mess2_text)
427427
mess2.setDetailedText(txid)
428428
mess2.exec_()
429-
# remove spent rewards
429+
# remove spent rewards from DB
430430
self.removeSpentRewards()
431+
# reload utxos
432+
self.display_mn_utxos()
431433
self.onCancel()
432434

433435
else:

0 commit comments

Comments
 (0)