<p>This is just a matter of notation.</p>
<p><code>&lt;20-byte value&gt;</code> is a human-readable way of writing &quot;the script that pushes said 20-byte value onto the stack&quot;. Practically that's encoded using 0x14 followed by those 20 bytes. Some sources will refer to that 0x14 byte as &quot;OP_PUSHBYTES_20&quot;, some just print the hex bytes, while others denote it by putting <code>&lt;&gt;</code> around the thing being pushed.</p>
<p>To answer your titular question</p>
<blockquote>
<p>What is the content of ScriptPubKey for P2WPKH?</p>
</blockquote>
<p>It is a 22-byte script, whose first byte is 0x00 (&quot;OP_0&quot;), whose second byte is 0x14 (&quot;push the 20 bytes that follow&quot;, or &quot;OP_PUSHBYTES_20&quot;), and whose last 20 bytes are the Hash160 (SHA256 + RIPEMD160) of the receiver's public key.</p>
<p>Certain frameworks for operating on Bitcoin Script represent scripts naturally in a parsed format, where the 0x14 byte is implicitly interpreted as &quot;the next 20 bytes form a unit&quot; rather than being explicitly represented as an opcode. In such a framework, a P2WPKH scriptPubKey would be represented as the 2-element sequence <code>(OP_0, the 20 byte hash)</code>. This is by the way the case inside the Bitcoin Core source code too; it does not treat the 0x14 as a separate opcode.</p>

CC BY-SA 4.0
2023-08-30T19:54:02.310Z
Well yes, but the problem is that Jimmy in his `is_p2wpkh_script_pubkey` function checks only if the first "element" is 0x00 and if the second is of length 20. He is not checking if the second element is 0x14 and then the third element of length 20. It looks like this function won't work as it should.
dassd
CC BY-SA 4.0
2023-08-30T19:56:39.693Z
@joke Presumably it's working in a framework where the script is represented in parsed form rather than in byte-serialized form. In parsed form, it'd be represented as just the sequence (OP_0, the 20-byte-value). In the byte serialized, the 0x14 before the 20-byte value is implicit (added when serializing, and just used as indicator that a 20-byte value follows when parsing).
Pieter Wuille
CC BY-SA 4.0
2023-08-30T19:59:35.477Z
Yes, I guess that's the case here.
dassd
CC BY-SA 4.0
2023-08-30T20:08:05.300Z
I expanded my answer a bit to include this.
Pieter Wuille
CC BY-SA 4.0
2023-08-30T19:48:44.473Z
2023-08-31T10:40:38.700Z
2023-08-31T10:40:38.700Z
Pieter Wuille
<p>What is the content of ScriptPubKey for P2WPKH?</p>
<p>Master bitcoin book 2end edition says the following (<a href="https://en.bitcoin.it/wiki/Protocol_documentation#tx" rel="nofollow noreferrer">link</a>):</p>
<blockquote>
<p>Example P2WPKH output script</p>
<p><code>0 ab68025513c3dbd2f7b92a94e0581f5d50f654e7</code></p>
</blockquote>
<p>In programming Bitcoin by Jimmy Song, it says the following:</p>
<blockquote>
<p>The ScriptPubKey for p2wpkh is OP_0 &lt;20-byte hash&gt;.</p>
</blockquote>
<p>It also provides the following image:</p>
<p><a href="https://i.sstatic.net/pPUP4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pPUP4.png" alt="enter image description here" /></a></p>
<p>Additionally, when Jimmy Song is checking if the scriptPubKey is of P2WPKH type, he is using the following code:</p>
<p><a href="https://i.sstatic.net/eSGWy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/eSGWy.png" alt="enter image description here" /></a></p>
<p>So my question is: <strong>Where is OP_PUSHBYTES_20 here? Okay, maybe in the examples we can omit it and just say OP_0 20-byte hash, but how can we omit it in a program checking if the script is P2WPKH as Jimmy Song did?</strong></p>
<p>I thought that I missed something, so I went into Bitcoin block explorer to find any P2WPKH scriptPubKey and I found it with OP_PUSHBYTES_20. Here is the <a href="https://mempool.space/tx/4b56d9d549e5f1075b663ac5b72976bf1aa2540459efe69fff84bb6b0add4d00" rel="nofollow noreferrer">link</a> for it.</p>
<p>So, should we consider the P2WPKH output is: <strong>OP_0 OP_PUSHBYTES_20 &lt;20-byte hash&gt;</strong> and not just <strong>OP_0 &lt;20-byte hash&gt;</strong>. Does it mean Jimmy Song made a mistake in his program?</p>

<p2wpkh><output-script><programming-bitcoin-book>
What is the content of ScriptPubKey for P2WPKH?
dassd
# What is the content of ScriptPubKey for P2WPKH?

(Asked by: username_0 on 2023-08-30)

What is the content of ScriptPubKey for P2WPKH?

Master bitcoin book 2end edition says the following ([link](https://en.bitcoin.it/wiki/Protocol_documentation#tx)):

> Example P2WPKH output script
>
> `0 ab68025513c3dbd2f7b92a94e0581f5d50f654e7`

In programming Bitcoin by Jimmy Song, it says the following:

> The ScriptPubKey for p2wpkh is OP\_0 <20-byte hash>.

It also provides the following image:

[![enter image description here](https://i.sstatic.net/pPUP4.png)](https://i.sstatic.net/pPUP4.png)

Additionally, when Jimmy Song is checking if the scriptPubKey is of P2WPKH type, he is using the following code:

[![enter image description here](https://i.sstatic.net/eSGWy.png)](https://i.sstatic.net/eSGWy.png)

So my question is: **Where is OP\_PUSHBYTES\_20 here? Okay, maybe in the examples we can omit it and just say OP\_0 20-byte hash, but how can we omit it in a program checking if the script is P2WPKH as Jimmy Song did?**

I thought that I missed something, so I went into Bitcoin block explorer to find any P2WPKH scriptPubKey and I found it with OP\_PUSHBYTES\_20. Here is the [link](https://mempool.space/tx/4b56d9d549e5f1075b663ac5b72976bf1aa2540459efe69fff84bb6b0add4d00) for it.

So, should we consider the P2WPKH output is: **OP\_0 OP\_PUSHBYTES\_20 <20-byte hash>** and not just **OP\_0 <20-byte hash>**. Does it mean Jimmy Song made a mistake in his program?

**Tags:** p2wpkh, output-script, programming-bitcoin-book


---

## 1 answers

### Answer by username_1 on 2023-08-30. Score: 4 | Accepted answer

This is just a matter of notation.

`<20-byte value>` is a human-readable way of writing "the script that pushes said 20-byte value onto the stack". Practically that's encoded using 0x14 followed by those 20 bytes. Some sources will refer to that 0x14 byte as "OP\_PUSHBYTES\_20", some just print the hex bytes, while others denote it by putting `<>` around the thing being pushed.

To answer your titular question

> What is the content of ScriptPubKey for P2WPKH?

It is a 22-byte script, whose first byte is 0x00 ("OP\_0"), whose second byte is 0x14 ("push the 20 bytes that follow", or "OP\_PUSHBYTES\_20"), and whose last 20 bytes are the Hash160 (SHA256 + RIPEMD160) of the receiver's public key.

Certain frameworks for operating on Bitcoin Script represent scripts naturally in a parsed format, where the 0x14 byte is implicitly interpreted as "the next 20 bytes form a unit" rather than being explicitly represented as an opcode. In such a framework, a P2WPKH scriptPubKey would be represented as the 2-element sequence `(OP_0, the 20 byte hash)`. This is by the way the case inside the Bitcoin Core source code too; it does not treat the 0x14 as a separate opcode.

> **Comments:**
> **username_0**: Well yes, but the problem is that Jimmy in his `is_p2wpkh_script_pubkey` function checks only if the first "element" is 0x00 and if the second is of length 20. He is not checking if the second element is 0x14 and then the third element of length 20. It looks like this function won't work as it should.  
> **username_1**: @joke Presumably it's working in a framework where the script is represented in parsed form rather than in byte-serialized form. In parsed form, it'd be represented as just the sequence (OP_0, the 20-byte-value). In the byte serialized, the 0x14 before the 20-byte value is implicit (added when serializing, and just used as indicator that a 20-byte value follows when parsing).  
> **username_0**: Yes, I guess that's the case here.  
> **username_1**: I expanded my answer a bit to include this.  

---

