def tx_in(tx, inscript): #TODO change from 1 i/p to multi i/p to_spend = Transaction.unhexlify(tx) intxs = [ TxIn(txid=to_spend.txid, txout=0, sequence=Sequence.max(), script_sig=inscript) ] #ScriptSig.<something> return intxs
def make_raw_transaction( inputs: list, outputs: list, locktime: Locktime = Locktime(0), timestamp: int = int(time()), version: int = 1, ) -> Transaction: '''create raw transaction''' return Transaction(version=version, timestamp=timestamp, ins=inputs, outs=outputs, locktime=locktime)
def pay_order(order_id): pay_info = request.get_json() tx = Transaction.unhexlify(pay_info['transaction']) order = model.find_query_by_id(order_id) if not check_transaction(tx, order.amount): return jsonify(error='Destination or amount are wrong'), 400 if not model.check_transaction(tx.txid): return jsonify( error='This transaction already used for another order'), 400 post_transaction_to_bitcoin_network(tx) model.add_transaction_to_order(order_id, tx.txid) return '', 204
def test_all(self): global keys priv = ExtendedPrivateKey.decode(keys[0][1]).key pk = priv.pub() addr_string = str(pk.to_address()) utxo = [] for i in range(3): # create 3 tx to add to UTXO txid = regtest.send_rpc_cmd(['sendtoaddress', addr_string, '100'], 0) to_spend = Transaction.unhexlify( regtest.send_rpc_cmd(['getrawtransaction', txid, '0'], 0)) txout = None for out in to_spend.outs: if str(out.script_pubkey.address()) == addr_string: txout = out break assert txout is not None utxo.append({ 'txid': txid, 'txout': txout, 'solver': P2pkhSolver(priv), 'next_seq': Sequence.max(), 'next_locktime': Locktime(0) }) regtest.send_rpc_cmd(['generate', '100'], 0) generate = False next_locktime = Locktime(0) next_sequence = Sequence.max() i = 0 while i < len(self.all) - 2: print('{:04d}\r'.format(i), end='', flush=True) ins = [ MutableTxIn(unspent['txid'], unspent['txout'].n, ScriptSig.empty(), unspent['next_seq']) for unspent in utxo ] outs = [] prev_types = [] for j, (unspent, script) in enumerate(zip(utxo, self.all[i:i + 3])): outs.append( TxOut(unspent['txout'].value - 1000000, j, script[0])) prev_types.append(script[2]) tx = MutableTransaction( 2, ins, outs, min_locktime(unspent['next_locktime'] for unspent in utxo)) mutable = copy.deepcopy(tx) tx = tx.spend([unspent['txout'] for unspent in utxo], [unspent['solver'] for unspent in utxo]) # print('====================') # print('txid: {}'.format(tx.txid)) # print() # print(tx) # print() # print('raw: {}'.format(tx.hexlify())) # print('prev_scripts, amounts, solvers:') print('TX: {}'.format(i)) regtest.send_rpc_cmd(['sendrawtransaction', tx.hexlify()], 0) print('Mempool size: {}'.format( len(regtest.send_rpc_cmd(['getrawmempool'], 0)))) if cmdline_args.dumpfile is not None: with open(cmdline_args.dumpfile, 'a') as out: for j, unspent in enumerate(utxo): json.dump( self.json_dump(unspent, tx.ins[j], j, copy.deepcopy(mutable).to_segwit()), out) out.write('\n') utxo = [] for j, (output, prev_type) in enumerate(zip(tx.outs, prev_types)): if 'time' in prev_type: if 'absolute' in prev_type: next_locktime = Locktime(100) next_sequence = Sequence(0xfffffffe) if 'relative' in prev_type: next_sequence = Sequence(3) generate = True else: next_locktime = Locktime(0) next_sequence = Sequence.max() utxo.append({ 'txid': tx.txid, 'txout': output, 'solver': self.all[i + j][1][0], # solver 'next_seq': next_sequence, 'next_locktime': next_locktime }) if generate: regtest.send_rpc_cmd(['generate', '4'], 0) generate = False if not i % 10: print('generating 2') regtest.send_rpc_cmd(['generate', '2'], 0) i += 1 ins = [ MutableTxIn(unspent['txid'], unspent['txout'].n, ScriptSig.empty(), unspent['next_seq']) for unspent in utxo ] tx = MutableTransaction( 2, ins, [ TxOut( sum(unspent['txout'].value for unspent in utxo) - 1000000, 0, self.final['script']) ], min_locktime(unspent['next_locktime'] for unspent in utxo)) tx = tx.spend([unspent['txout'] for unspent in utxo], [unspent['solver'] for unspent in utxo]) # print('====================') # print('txid: {}'.format(tx.txid)) # print() # print(tx) # print() # print('raw: {}'.format(tx.hexlify())) # print('prev_scripts, amounts, solvers:') # for unspent in utxo: # print(unspent['txout'].script_pubkey, unspent['txout'].value, unspent['solver'].__class__.__name__) regtest.send_rpc_cmd(['sendrawtransaction', tx.hexlify()], 0) regtest.teardown()
def test_all(self): global keys priv = PrivateKey.from_bip32(keys[0][1]) pk = priv.pub() addr_string = str(pk.to_address()) utxo = [] for i in range(3): # create 3 tx to add to UTXO txid = regtest.send_rpc_cmd(['sendtoaddress', addr_string, '100'], 0) to_spend = Transaction.unhexlify( regtest.send_rpc_cmd(['getrawtransaction', txid, '0'], 0)) txout = None for out in to_spend.outs: if str(out.script_pubkey.address()) == addr_string: txout = out break assert txout is not None utxo.append({ 'txid': txid, 'txout': txout, 'solver': P2pkhSolver(priv), 'next_seq': Sequence.max(), 'next_locktime': Locktime(0) }) regtest.send_rpc_cmd(['generate', '100'], 0) generate = False next_locktime = Locktime(0) next_sequence = Sequence.max() i = 0 # 1785 # 382 # 2376 # 1180 # while i < len(self.all) - 2: print('{:04d}\r'.format(i), end='') ins = [ MutableTxIn(unspent['txid'], unspent['txout'].n, ScriptSig.empty(), unspent['next_seq']) for unspent in utxo ] outs = [] prev_types = [] for j, (unspent, script) in enumerate(zip(utxo, self.all[i:i + 3])): outs.append( TxOut(unspent['txout'].value - 1000000, j, script[0])) prev_types.append(script[2]) tx = MutableTransaction( 2, ins, outs, min_locktime(unspent['next_locktime'] for unspent in utxo)) tx = tx.spend([unspent['txout'] for unspent in utxo], [unspent['solver'] for unspent in utxo]) # print('====================') # print('txid: {}'.format(tx.txid)) # print() # print(tx) # print() # print('raw: {}'.format(tx.hexlify())) # print('prev_scripts, amounts, solvers:') for unspent in utxo: if isinstance(unspent['solver'], P2shSolver): if isinstance(unspent['solver'].redeem_script_solver, P2wshV0Solver): prev = unspent[ 'solver'].redeem_script_solver.witness_script else: prev = unspent['solver'].redeem_script elif isinstance(unspent['solver'], P2wshV0Solver): prev = unspent['solver'].witness_script else: prev = unspent['txout'].script_pubkey print(prev, unspent['txout'].value, unspent['solver'].__class__.__name__) regtest.send_rpc_cmd(['sendrawtransaction', tx.hexlify()], 0) utxo = [] for j, (output, prev_type) in enumerate(zip(tx.outs, prev_types)): if 'time' in prev_type: if 'absolute' in prev_type: next_locktime = Locktime(100) next_sequence = Sequence(0xfffffffe) if 'relative' in prev_type: next_sequence = Sequence(3) generate = True else: next_locktime = Locktime(0) next_sequence = Sequence.max() utxo.append({ 'txid': tx.txid, 'txout': output, 'solver': self.all[i + j][1][0], # solver 'next_seq': next_sequence, 'next_locktime': next_locktime }) if generate: regtest.send_rpc_cmd(['generate', '4'], 0) generate = False if not i % 10: regtest.send_rpc_cmd(['generate', '2'], 0) i += 1 ins = [ MutableTxIn(unspent['txid'], unspent['txout'].n, ScriptSig.empty(), unspent['next_seq']) for unspent in utxo ] tx = MutableTransaction( 2, ins, [ TxOut( sum(unspent['txout'].value for unspent in utxo) - 1000000, 0, self.final['script']) ], min_locktime(unspent['next_locktime'] for unspent in utxo)) tx = tx.spend([unspent['txout'] for unspent in utxo], [unspent['solver'] for unspent in utxo]) # print('====================') # print('txid: {}'.format(tx.txid)) # print() # print(tx) # print() # print('raw: {}'.format(tx.hexlify())) # print('prev_scripts, amounts, solvers:') for unspent in utxo: print(unspent['txout'].script_pubkey, unspent['txout'].value, unspent['solver'].__class__.__name__) regtest.send_rpc_cmd(['sendrawtransaction', tx.hexlify()], 0) regtest.teardown()