def test_sign_transaction(self): tx = Tx(version=1, in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("d2a42ebcb98b598ddcb6d430ce9061dba76804a97f7c1413dd3faef744f909a8"),index=0), script=Script(instructions=[]), sequence=4294967295),TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("2be8e319be1d15c1ba54708bdd7969b638e7e6fa2154819136e363ea6d33664a"),index=1), script=Script(instructions=[]), sequence=4294967295)], out_list=[TxOut(value=3315075843, script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20, decodehexstr("297c5a2ee31a1ab721115722d83f8654ca21d5df")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)])),TxOut(value=2971972133, script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20, decodehexstr("7d5feab86e31e8fc99d8e735d56226de9043e5fc")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)])),TxOut(value=1046301823, script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20, decodehexstr("fd91232a2fa0c389fa0188efde26c8a6165f4c50")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)]))], locktime=0) outscript0 = Script(instructions=[Instruction(33, decodehexstr("03409fe679bdff9e801692c999b86d0c47b62dc02cdd10591ee70ca8056cd05023")),Instruction(OP_CHECKSIG)]) outscript1 = Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20, decodehexstr("5fb7fdb3d1ab0f3fec90b38417cca8ab736b10c6")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)]) # Sign TX_PUBKEY sign_transaction(tx, [TxOut(None, outscript0), TxOut(None, outscript1)], [decodehexstr("f006b27418527b1c400bbc434a3f22ee57c376bd4819cfe2a1162682788ae714"), decodehexstr("c693115901c2840badd1e404706a4866a90d3afa16a542c1a3d0de9aad0875fe")]) vm = TxValidationVM() valid, reason = vm.validate(tx, 0, outscript0, tx.in_list[0].script) if not valid: raise Exception(reason) valid, reason = vm.validate(tx, 1, outscript1, tx.in_list[1].script) if not valid: raise Exception(reason)
def send_transaction(self, planned_tx, passphrases): try: self.wallet.unlock(passphrases) privkey_list = [] for outpoint, txout in planned_tx.selected_outputs: privkey_list.append( self.wallet.get_txout_private_key_secret(txout)) finally: self.wallet.lock() sign_transaction( planned_tx.tx, [txout for outpoint, txout in planned_tx.selected_outputs], privkey_list) txhash = hash_tx(planned_tx.tx) self.log.info( "Sending %f to %s (fee:%f), change address: %s, hash:%s" % (planned_tx.amount, str(planned_tx.address), planned_tx.fee, str(planned_tx.change_address), str(txhash))) #Initially, create an empty MerkleTx (the tx is not yet in a block) merkle_tx = MerkleTx(planned_tx.tx, Uint256.zero(), [], 4294967295) self.wallet.begin_updates() self.wallet.allocate_key(planned_tx.change_public_key, ischange=True) #Set the spend flags for the input transactions for outpoint, txout in planned_tx.selected_outputs: input_wallet_tx = self.wallet.get_transaction(outpoint.hash) input_wallet_tx.set_spent(outpoint.index) self.wallet.set_transaction(outpoint.hash, input_wallet_tx) #Add the wallet_tx (contains supporting transations) txtime = int(time.time()) wallet_tx = create_wallet_tx(self.blockchain, merkle_tx, txtime) self.wallet.add_transaction(txhash, wallet_tx) self.wallet.commit_updates() self.fire(self.EVT_NEW_TRANSACTION_ITEM, item=(planned_tx.tx, txhash, txtime, planned_tx.address, "", -planned_tx.amount, False)) self.compute_balances() # we could only compute delta here self.fire(self.EVT_PUBLISH_TRANSACTION, txhash=txhash, tx=planned_tx.tx) self.last_tx_publish[txhash] = txtime #update description of change address new_description = self.wallet.get_address_description( planned_tx.change_public_key) self.fire(self.EVT_NEW_ADDRESS_DESCRIPTION, public_key=planned_tx.change_public_key, description=new_description)
def send_transaction(self, planned_tx, passphrases): try: self.wallet.unlock(passphrases) privkey_list = [] for outpoint, txout in planned_tx.selected_outputs: privkey_list.append(self.wallet.get_txout_private_key_secret(txout)) finally: self.wallet.lock() sign_transaction(planned_tx.tx, [txout for outpoint, txout in planned_tx.selected_outputs], privkey_list) txhash = hash_tx(planned_tx.tx) self.log.info( "Sending %f to %s (fee:%f), change address: %s, hash:%s" % (planned_tx.amount, str(planned_tx.address), planned_tx.fee, str(planned_tx.change_address), str(txhash)) ) # Initially, create an empty MerkleTx (the tx is not yet in a block) merkle_tx = MerkleTx(planned_tx.tx, Uint256.zero(), [], 4294967295) self.wallet.begin_updates() self.wallet.allocate_key(planned_tx.change_public_key, ischange=True) # Set the spend flags for the input transactions for outpoint, txout in planned_tx.selected_outputs: input_wallet_tx = self.wallet.get_transaction(outpoint.hash) input_wallet_tx.set_spent(outpoint.index) self.wallet.set_transaction(outpoint.hash, input_wallet_tx) # Add the wallet_tx (contains supporting transations) txtime = int(time.time()) wallet_tx = create_wallet_tx(self.blockchain, merkle_tx, txtime) self.wallet.add_transaction(txhash, wallet_tx) self.wallet.commit_updates() self.fire( self.EVT_NEW_TRANSACTION_ITEM, item=(planned_tx.tx, txhash, txtime, planned_tx.address, "", -planned_tx.amount, False), ) self.compute_balances() # we could only compute delta here self.fire(self.EVT_PUBLISH_TRANSACTION, txhash=txhash, tx=planned_tx.tx) self.last_tx_publish[txhash] = txtime # update description of change address new_description = self.wallet.get_address_description(planned_tx.change_public_key) self.fire( self.EVT_NEW_ADDRESS_DESCRIPTION, public_key=planned_tx.change_public_key, description=new_description )
from coinpy.model.constants.bitcoin import COIN from coinpy.lib.vm.vm import TxValidationVM import os runmode = TESTNET wallet_filename = "D:\\repositories\\coinpy\\coinpy-client\\src\\data\\testnet\\wallet_testnet.dat" directory, filename = os.path.split(wallet_filename) bsddb_env = BSDDBEnv(directory) wallet = Wallet(None, BSDDBWalletDatabase(bsddb_env, filename), runmode) selector = CoinSelector() amount, fee = 145.0065 * COIN, 0 * COIN #list of (txhash, tx, index, txout) outputs = list(wallet.iter_my_outputs()) selected_outputs = selector.select_coins(outputs, amount + fee) tx = create_pubkeyhash_transaction( selected_outputs, decode_base58check("n4MsBRWD7VxKGsqYRSLaFZC6hQrsrKLaZo"), decode_base58check("n4MsBRWD7VxKGsqYRSLaFZC6hQrsrKLaZo"), amount, fee) sign_transaction(tx, selected_outputs) vm = TxValidationVM() print tx print len(selected_outputs) print vm.validate(tx, 0, selected_outputs[0].txout.script, tx.in_list[0].script) print vm.validate(tx, 1, selected_outputs[1].txout.script, tx.in_list[1].script) print vm.validate(tx, 2, selected_outputs[2].txout.script, tx.in_list[2].script)
from coinpy.model.constants.bitcoin import COIN from coinpy.lib.vm.vm import TxValidationVM import os runmode = TESTNET wallet_filename = "D:\\repositories\\coinpy\\coinpy-client\\src\\data\\testnet\\wallet_testnet.dat" directory, filename = os.path.split(wallet_filename) bsddb_env = BSDDBEnv(directory) wallet = Wallet(None, BSDDBWalletDatabase(bsddb_env, filename), runmode) selector = CoinSelector() amount, fee = 145.0065 * COIN, 0 * COIN # list of (txhash, tx, index, txout) outputs = list(wallet.iter_my_outputs()) selected_outputs = selector.select_coins(outputs, amount + fee) tx = create_pubkeyhash_transaction( selected_outputs, decode_base58check("n4MsBRWD7VxKGsqYRSLaFZC6hQrsrKLaZo"), decode_base58check("n4MsBRWD7VxKGsqYRSLaFZC6hQrsrKLaZo"), amount, fee, ) sign_transaction(tx, selected_outputs) vm = TxValidationVM() print tx print len(selected_outputs) print vm.validate(tx, 0, selected_outputs[0].txout.script, tx.in_list[0].script) print vm.validate(tx, 1, selected_outputs[1].txout.script, tx.in_list[1].script) print vm.validate(tx, 2, selected_outputs[2].txout.script, tx.in_list[2].script)