def new_tx(utxo=None, target_sigops=20000, target_tx_size=ONE_MEGABYTE): padding_size = calc_needed_data_size([OP_CHECKSIG] * target_sigops, target_tx_size) if utxo != None: tx_to_spend = utxo['txid'] vout = utxo['vout'] value = utxo['amount'] tx = CTransaction() script = CScript([OP_CHECKSIG] * target_sigops) tx.vout.append(CTxOut(200000000, script)) tx.vout.append(CTxOut(200000000, CScript([OP_DUP, OP_HASH160, hex_str_to_bytes( "ab812dc588ca9d5787dde7eb29569da63c3a238c"), OP_EQUALVERIFY, OP_CHECKSIG]))) tx.vout.append(CTxOut(150000000, CScript([OP_FALSE, OP_RETURN] + [bytes(5)[:1] * padding_size]))) if utxo == None: txHex = node.fundrawtransaction(ToHex(tx), {'feeRate': 2, 'changePosition': len(tx.vout)})[ 'hex'] else: tx.vin.append(CTxIn(COutPoint(uint256_from_str(hex_str_to_bytes(tx_to_spend)[::-1]), vout))) txHex = ToHex(tx) txHex = node.signrawtransaction(txHex)['hex'] tx = FromHex(CTransaction(), txHex) tx.rehash() return tx
def new_tx(utxo=None, target_script_size=20000, op_codes=[OP_TRUE], elem=[b"a" * 499, OP_DROP], target_tx_size=None, simple=False, lock_script=None, unlock_script=b""): if utxo != None: tx_to_spend = utxo['txid'] vout = utxo['vout'] value = utxo['amount'] if simple: tx = create_transaction(tx_to_spend, vout, unlock_script, value - target_script_size - 2000) tx.rehash() return tx tx = CTransaction() if (lock_script != None): tx.vout.append(CTxOut(200000000, lock_script)) else: script = make_script(op_codes=op_codes, elem=elem, target_script_size=target_script_size) tx.vout.append(CTxOut(200000000, script)) tx.vout.append( CTxOut( 200000000, CScript([ OP_DUP, OP_HASH160, hex_str_to_bytes( "ab812dc588ca9d5787dde7eb29569da63c3a238c"), OP_EQUALVERIFY, OP_CHECKSIG ]))) if target_tx_size != None: padding_size = calc_needed_data_size(script, target_tx_size) tx.vout.append( CTxOut( 50000000, CScript([OP_FALSE, OP_RETURN] + [bytes(5)[:1] * padding_size]))) if utxo == None: txHex = node.fundrawtransaction(ToHex(tx), { 'feeRate': 2, 'changePosition': len(tx.vout) })['hex'] else: tx.vin.append( CTxIn( COutPoint( uint256_from_str( hex_str_to_bytes(tx_to_spend)[::-1]), vout))) txHex = ToHex(tx) txHex = node.signrawtransaction(txHex)['hex'] tx = FromHex(CTransaction(), txHex) tx.rehash() return tx
def fill_outputs(self, tx, n_outputs, script_op_codes, fund, total_bytes): for amount, n_bytes in zip(self._split_int(fund, n_outputs), self._split_int(total_bytes, n_outputs)): script = CScript( script_op_codes + [b"a" * calc_needed_data_size(script_op_codes, n_bytes)]) assert len(script) == n_bytes tx.vout.append(CTxOut(amount, script))
def make_script(op_codes=[OP_TRUE], elem=[b"a" * 499, OP_DROP], target_script_size=MAX_SCRIPT_SIZE_BEFORE_GENESIS): elem_len = len(CScript(elem)) max_size = calc_needed_data_size(op_codes, target_script_size) remainder = (max_size - len(CScript(op_codes + elem * (int)(floor(max_size / elem_len))))) script = CScript(op_codes + elem * (int)(floor(max_size / elem_len)) + [b"a" * remainder, OP_DROP]) if len(script) > target_script_size: remainder -= (len(script) - target_script_size) script = CScript(op_codes + elem * (int)(floor(max_size / elem_len)) + elem) return script
def make_unlock_script(elem=[b"a" * 500], target_script_size=MAX_SCRIPT_SIZE_BEFORE_GENESIS): elem_len = len(CScript(elem)) max_size = calc_needed_data_size([], (target_script_size)) remainder = (max_size - len(CScript(elem * (int)(floor(max_size / elem_len))))) script = CScript([] + elem * (int)(floor(max_size / elem_len)) + [b"a" * remainder, b""]) if len(script) > target_script_size: remainder -= (len(script) - target_script_size) script = CScript([] + elem * (int)(floor(max_size / elem_len)) + [b"a" * remainder, b""]) return script