def FromHex(obj, hex_string): obj.deserialize(BytesIO(hex_str_to_bytes(hex_string))) return obj
def create_tampered_rawtx_cbh(node_from, node_to, tx_amount, fee, mode): genesis_block_hash = node_from.getblock(str(0))['hash'] # select necessary UTXOs usp = node_from.listunspent() assert (len(usp) != 0) amount = Decimal('0') inputs = [] for x in usp: amount += Decimal(x['amount']) inputs.append({"txid": x['txid'], "vout": x['vout']}) if amount >= tx_amount + fee: break outputs = { node_from.getnewaddress(): (Decimal(amount) - tx_amount - fee), node_to.getnewaddress(): tx_amount } rawTx = node_from.createrawtransaction(inputs, outputs) # build an object from the raw Tx in order to be able to modify it tx_01 = CTransaction() f = StringIO(unhexlify(rawTx)) tx_01.deserialize(f) # corrupt vouts in this Tx for vout_idx in range(len(tx_01.vout)): decodedScriptOrig = node_from.decodescript( hexlify(tx_01.vout[vout_idx].scriptPubKey)) scriptOrigAsm = decodedScriptOrig['asm'] params = scriptOrigAsm.split() hash160 = hex_str_to_bytes(params[2]) original_height = int(params[6]) original_hash = hex_str_to_bytes(params[5]) if mode == MODE_HEIGHT: # new referenced block height evil_height = -1 # new referenced block hash modTargetHash = hex_str_to_bytes(swap_bytes(genesis_block_hash)) # build modified script: CScript is putting a 4f (OP_NEGATE) for our -1 # edit script.py to send different stuff for the -1 value (like ff itself!) modScriptPubKey = CScript([ OP_DUP, OP_HASH160, hash160, OP_EQUALVERIFY, OP_CHECKSIG, modTargetHash, evil_height, OP_CHECKBLOCKATHEIGHT ]) elif mode == MODE_SWAP_ARGS: modScriptPubKey = CScript([ OP_DUP, OP_HASH160, hash160, OP_EQUALVERIFY, OP_CHECKSIG, original_height, original_hash, OP_CHECKBLOCKATHEIGHT ]) elif mode == MODE_NON_MIN_ENC: non_min_h = hex_str_to_bytes("07000000") modScriptPubKey = CScript([ OP_DUP, OP_HASH160, hash160, OP_EQUALVERIFY, OP_CHECKSIG, original_hash, non_min_h, OP_CHECKBLOCKATHEIGHT ]) else: assert (False) tx_01.vout[vout_idx].scriptPubKey = modScriptPubKey tx_01.rehash() signedRawTx = node_from.signrawtransaction(ToHex(tx_01)) return signedRawTx