def sendCoins(self): sendAtp = self.amountBox.get() recipientKey = self.recipient_pubkey.get() sendFee = self.feeBox.get() if not sendAtp: messagebox.showwarning('Warning', 'Please enter the Amount to pay.') elif len(recipientKey) <= 1: messagebox.showwarning('Warning', 'Please enter the Recipient Address.') elif not sendFee: sendFee = 0 else: result = messagebox.askyesno( 'Confirmation', 'Sending {} SimpleBitcoins to :\n {}'.format( sendAtp, recipientKey)) if result: if 0 < len(self.um.utxo_txs): print('Sending {} SimpleBitcoins to reciever:\n {}'.format( sendAtp, recipientKey)) else: messagebox.showwarning('Short of Coin.', 'Not enough coin to be sent...') return utxo, idx = self.um.get_utxo_tx(0) t = Transaction([TransactionInput(utxo, idx)], [TransactionOutput(recipientKey, sendAtp)]) counter = 1 # TransactionInputが送信額を超えるまで繰り返して取得しTransactionとして完成させる while t.is_enough_inputs(sendFee) is not True: new_utxo, new_idx = self.um.get_utxo_tx(counter) t.inputs.append(TransactionInput(new_utxo, new_idx)) counter += 1 if counter > len(self.um.utxo_txs): messagebox.showwarning('Short of Coin.', 'Not enough coin to be sent...') break # 正常なTransactionが生成できた時だけ秘密鍵で署名を実行する if t.is_enough_inputs(sendFee) is True: # まずお釣り用Transactionを作る change = t.compute_change(sendFee) t.outputs.append( TransactionOutput(self.km.my_address(), change)) to_be_signed = json.dumps(t.to_dict(), sort_keys=True) signed = self.km.compute_digital_signature(to_be_signed) new_tx = json.loads(to_be_signed) new_tx['signature'] = signed # TODO: 本来はここで出来上がったTransactionを送信する処理を入れる print('signed new_tx:', json.dumps(new_tx)) # 実験的にお釣り分の勘定のため新しく生成したTransactionをUTXOとして追加しておくが # 本来はブロックチェーンの更新に合わせて再計算した方が適切 self.um.put_utxo_tx(t.to_dict()) to_be_deleted = 0 del_list = [] while to_be_deleted < counter: del_tx = self.um.get_utxo_tx(to_be_deleted) del_list.append(del_tx) to_be_deleted += 1 for dx in del_list: self.um.remove_utxo_tx(dx) self.amountBox.delete(0, END) self.feeBox.delete(0, END) self.recipient_pubkey.delete(0, END) self.update_balance()