Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
252Optionally wrap the segwit output using P2SH."""253if use_p2wsh:254program = keys_to_multisig_script([pubkey])255addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)256else:257addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)258if not encode_p2sh:259assert_equal(address_to_scriptpubkey(addr).hex(), witness_script(use_p2wsh, pubkey))260return node.createrawtransaction([utxo], {addr: amount})262def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):263"""Create a transaction spending a given utxo to a segwit output.265The output corresponds to the given pubkey: use_p2wsh determines whether to266use P2WPKH or P2WSH; encode_p2sh determines whether to wrap in P2SH.267sign=True will have the given node sign the transaction.268insert_redeem_script will be added to the scriptSig, if given."""269tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)270if (sign):271signed = node.signrawtransactionwithwallet(tx_to_witness)272assert "errors" not in signed273return node.sendrawtransaction(signed["hex"])274else:275if (insert_redeem_script):276tx = tx_from_hex(tx_to_witness)277tx.vin[0].scriptSig += CScript([bytes.fromhex(insert_redeem_script)])278tx_to_witness = tx.serialize().hex()280return node.sendrawtransaction(tx_to_witness)282class TestFrameworkBlockTools(unittest.TestCase):283def test_create_block_prefers_explicit_height(self):284block = create_block(285hashprev=1,286tmpl={"height": 100},287height=200,288)289assert_equal(CScriptNum.decode(block.vtx[0].vin[0].scriptSig), 200)291def test_create_coinbase(self):292height = 20293coinbase_tx = create_coinbase(height=height)294assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), height)
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.