def main(): k_m = KeyManager() um = UTXOManager(k_m.my_address()) i_k_m = KeyManager() u_k_m = KeyManager() t1 = CoinbaseTransaction(k_m.my_address()) t2 = CoinbaseTransaction(k_m.my_address()) t3 = CoinbaseTransaction(k_m.my_address()) t4 = Transaction([TransactionInput(t1.to_dict(), 0)], [ TransactionOutput(u_k_m.my_address(), 10.0), TransactionOutput(i_k_m.my_address(), 20.0) ]) transactions = [] transactions.append(t1.to_dict()) transactions.append(t2.to_dict()) transactions.append(t3.to_dict()) transactions.append(t4.to_dict()) um.extract_utxos(transactions) balance = um.my_balance print(balance)
'''tx = Transaction(5,3,'54450450e24286143a35686ad77a7c851ada01a0', 1, '192.152.0.0/16', [2, '2001:cdba:9abc:5678::', 20, 230, 1, '5.5.5.5', 45, 50])''' '''tx = Transaction(5,3,'54450450e24286143a35686ad77a7c851ada01a0', 2, '2001:db8::1/16', [2, '2001:cdba:9abc:5678::', 20, 230])''' tx.sign(key) print "Tx ID", tx.hash.encode('hex') print tx.ip_network #print utils.ip_to_bytes('192.152.0.0/16').encode('hex') assert tx.sender == addr print "Tx Sender OK" assert tx.v in (27, 28) print "V OK" #print tx.metadata print tx.to_dict() rawtx = rlp.encode(tx).encode('hex') print '\n', rawtx, '\n' tx2 = rlp.decode(rawtx.decode('hex'), Transaction) #print tx2.sender.encode('hex') print tx2.to_dict() assert rlp.encode(tx).encode('hex') == rlp.encode(tx2).encode('hex') print "Tx Encode = Tx Decode: OK" '''rawtx = ('f8cd05029454450450e24286143a35686ad77a7c851ada01a00291' '20010db800000000000000000000000110f85d0184010101019454' 'dbb737eac5007103e729e9ab7ce64a6850a31002902001cdba0000' '000000000000325796529489b44e4d3c81ede05d0f5de8d1a68f75' '4d73d997018403030303943a1e02d9ea25349c47fe2e94f4265cd2' '61c5c7ca801ca047a05dcbc30b8cc7b14510c2b9e92364bab43ee7' 'a317c0681a26f5136ccbe0aaa06bfd3b1751e4327095a85e93edce' 'beb3266b7dca59369729cee2bd482e9ed3f3')
#!/usr/bin/python3 from chain import BlockChain from wallet import Wallet from transactions import Transaction bch = BlockChain() w1 = Wallet("Abdou") w2 = Wallet("Aymen") tr = Transaction(w1, w2, "50000") late = bch.latest_block bch.construct_block(late.proof_nonce, late.prev_hash, tr.to_dict(), { w1.user_id: "00", w2.user_id: "00" }) tr1 = Transaction(w2, w1, "50000") late = bch.latest_block bch.construct_block(late.proof_nonce, late.prev_hash, tr1.to_dict(), { w1.user_id: "00", w2.user_id: "00" }) w3 = Wallet("Ons") bch.construct_block(late.proof_nonce, late.prev_hash, [], { w1.user_id: "00", w2.user_id: "00", w3.user_id: "00" }) print(bch.chain)
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()