What is 'ScriptCode'?
The ScriptCode is the script that is evaluated for a witness program type. Each witness program type has a specific template for the ScriptCode. That is simply part of its definition.

For a Pay-to-Witness-Pubkey-Hash output the ScriptCode is

OP_DUP OP_HASH160 &lt;pubkey hash&gt; OP_EQUALVERIFY OP_CHECKSIG


The &lt;pubkey hash&gt; for the ScriptCode is taken from the witness program.