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.
1751lasttxid = coinbase.txid_int1752amount = 50000000001753for i, spk in enumerate(old_spks + tap_spks):1754val = 42000000 * (i + 7)1755tx = CTransaction()1756tx.version = 11757tx.vin = [CTxIn(COutPoint(lasttxid, i & 1), CScript([]), SEQUENCE_FINAL)]1758tx.vout = [CTxOut(val, spk), CTxOut(amount - val, CScript([OP_1]))]1759if i & 1:1760tx.vout = list(reversed(tx.vout))1761tx.nLockTime = 01762amount -= val1763lasttxid = tx.txid_int1764txn.append(tx)1765spend_info[spk]['prevout'] = COutPoint(tx.txid_int, i & 1)1766spend_info[spk]['utxo'] = CTxOut(val, spk)1767# Mine those transactions1768self.init_blockinfo(self.nodes[0])1769self.block_submit(self.nodes[0], txn, "Crediting txn", None, sigops_weight=10, accept=True)1771# scriptPubKey computation1772tests = {"version": 1}1773spk_tests = tests.setdefault("scriptPubKey", [])1774for i, tap in enumerate(taps):1775test_case = {}1776given = test_case.setdefault("given", {})1777given['internalPubkey'] = tap.internal_pubkey.hex()1779def pr(node):1780if node is None:1781return None1782elif isinstance(node, tuple):1783return {"id": int(node[0]), "script": node[1].hex(), "leafVersion": node[2]}1784elif len(node) == 1:1785return pr(node[0])1786elif len(node) == 2:1787return [pr(node[0]), pr(node[1])]1788else:1789assert False1791given['scriptTree'] = pr(script_lists[i])1792intermediary = test_case.setdefault("intermediary", {})1793if len(tap.leaves):1794leafhashes = intermediary.setdefault('leafHashes', [None] * len(tap.leaves))1795for leaf in tap.leaves:1796leafhashes[int(leaf)] = tap.leaves[leaf].leaf_hash.hex()1797intermediary['merkleRoot'] = tap.merkle_root.hex() if tap.merkle_root else None1798intermediary['tweak'] = tap.tweak.hex()1799intermediary['tweakedPubkey'] = tap.output_pubkey.hex()1800expected = test_case.setdefault("expected", {})1801expected['scriptPubKey'] = tap.scriptPubKey.hex()1802expected['bip350Address'] = program_to_witness(1, bytes(tap.output_pubkey), True)1803if len(tap.leaves):1804control_blocks = expected.setdefault("scriptPathControlBlocks", [None] * len(tap.leaves))1805for leaf in tap.leaves:1806ctx = {**DEFAULT_CONTEXT, 'tap': tap, 'leaf': leaf}1807control_blocks[int(leaf)] = get(ctx, "controlblock").hex()1808spk_tests.append(test_case)1810# Construct a deterministic transaction spending all outputs created above.1811tx = CTransaction()1812tx.version = 21813tx.vin = []1814inputs = []1815input_spks = [tap_spks[0], tap_spks[1], old_spks[0], tap_spks[2], tap_spks[5], old_spks[2], tap_spks[6], tap_spks[3], tap_spks[4]]1816sequences = [0, SEQUENCE_FINAL, SEQUENCE_FINAL, 0xfffffffe, 0xfffffffe, 0, 0, SEQUENCE_FINAL, SEQUENCE_FINAL]1817hashtypes = [SIGHASH_SINGLE, SIGHASH_SINGLE|SIGHASH_ANYONECANPAY, SIGHASH_ALL, SIGHASH_ALL, SIGHASH_DEFAULT, SIGHASH_ALL, SIGHASH_NONE, SIGHASH_NONE|SIGHASH_ANYONECANPAY, SIGHASH_ALL|SIGHASH_ANYONECANPAY]1818for i, spk in enumerate(input_spks):1819tx.vin.append(CTxIn(spend_info[spk]['prevout'], CScript(), sequences[i]))1820inputs.append(spend_info[spk]['utxo'])1821tx.vout.append(CTxOut(1000000000, old_spks[1]))1822tx.vout.append(CTxOut(3410000000, pubs[98]))1823tx.nLockTime = 5000000001824precomputed = {1825"hashAmounts": BIP341_sha_amounts(inputs),1826"hashPrevouts": BIP341_sha_prevouts(tx),1827"hashScriptPubkeys": BIP341_sha_scriptpubkeys(inputs),1828"hashSequences": BIP341_sha_sequences(tx),1829"hashOutputs": BIP341_sha_outputs(tx)1830}1831keypath_tests = tests.setdefault("keyPathSpending", [])1832tx_test = {}1833global_given = tx_test.setdefault("given", {})1834global_given['rawUnsignedTx'] = tx.serialize().hex()1835utxos_spent = global_given.setdefault("utxosSpent", [])1836for i in range(len(input_spks)):1837utxos_spent.append({"scriptPubKey": inputs[i].scriptPubKey.hex(), "amountSats": inputs[i].nValue})1838global_intermediary = tx_test.setdefault("intermediary", {})1839for key in sorted(precomputed.keys()):1840global_intermediary[key] = precomputed[key].hex()1841test_list = tx_test.setdefault('inputSpending', [])1842for i in range(len(input_spks)):1843ctx = {1844**DEFAULT_CONTEXT,1845**spend_info[input_spks[i]],1846'tx': tx,1847'utxos': inputs,1848'idx': i,1849'hashtype': hashtypes[i],1850'deterministic': True1851}1852if ctx['mode'] == 'taproot':1853test_case = {}1854given = test_case.setdefault("given", {})1855given['txinIndex'] = i1856given['internalPrivkey'] = get(ctx, 'key').hex()1857if get(ctx, "tap").merkle_root != bytes():1858given['merkleRoot'] = get(ctx, "tap").merkle_root.hex()1859else:1860given['merkleRoot'] = None1861given['hashType'] = get(ctx, "hashtype")1862intermediary = test_case.setdefault("intermediary", {})1863intermediary['internalPubkey'] = get(ctx, "tap").internal_pubkey.hex()1864intermediary['tweak'] = get(ctx, "tap").tweak.hex()1865intermediary['tweakedPrivkey'] = get(ctx, "key_tweaked").hex()1866sigmsg = get(ctx, "sigmsg")1867intermediary['sigMsg'] = sigmsg.hex()1868intermediary['precomputedUsed'] = [key for key in sorted(precomputed.keys()) if sigmsg.count(precomputed[key])]1869intermediary['sigHash'] = get(ctx, "sighash").hex()1870expected = test_case.setdefault("expected", {})1871expected['witness'] = [get(ctx, "sign").hex()]1872test_list.append(test_case)1873tx.wit.vtxinwit.append(CTxInWitness())1874tx.vin[i].scriptSig = CScript(flatten(get(ctx, "scriptsig")))1875tx.wit.vtxinwit[i].scriptWitness.stack = flatten(get(ctx, "witness"))1876aux = tx_test.setdefault("auxiliary", {})1877aux['fullySignedTx'] = tx.serialize().hex()1878keypath_tests.append(tx_test)1879assert_equal(hashlib.sha256(tx.serialize()).hexdigest(), "24bab662cb55a7f3bae29b559f651674c62bcc1cd442d44715c0133939107b38")1880# Mine the spending transaction1881self.block_submit(self.nodes[0], [tx], "Spending txn", None, sigops_weight=10000, accept=True, witness=True)1883if GEN_TEST_VECTORS:1884print(json.dumps(tests, indent=4, sort_keys=False))1886def run_test(self):1887self.nodesigner = NodeSigner(self.nodes[0])1888self.gen_test_vectors()1890self.log.info("Post-activation tests...")1892# New sub-tests not checking standardness can be added to consensus_spenders1893# to allow for increased coverage across input types.1894# See sample_spenders for a minimal example1895consensus_spenders = sample_spenders()1896consensus_spenders += spenders_taproot_active()1897self.test_spenders(self.nodes[0], consensus_spenders, input_counts=[1, 2, 2, 2, 2, 3])1899# Run each test twice; once in isolation, and once combined with others. Testing in isolation1900# means that the standardness is verified in every test (as combined transactions are only standard1901# when all their inputs are standard).1902nonstd_spenders = spenders_taproot_nonstandard()1903self.test_spenders(self.nodes[0], nonstd_spenders, input_counts=[1])1904self.test_spenders(self.nodes[0], nonstd_spenders, input_counts=[2, 3])1907if __name__ == '__main__':1908TaprootTest(__file__).main()
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.