<p>As specified in <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="nofollow noreferrer">BIP-141</a>, the witness for a P2WPKH spend is as follows:</p>
<blockquote>
<p>The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a signature, and the second one a public key.</p>
</blockquote>
<p>The signature consists of the usual R and s values that you're looking for, as before.</p>
<hr />
<p>Let's look at <a href="https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494" rel="nofollow noreferrer">this transaction</a> you keep linking to. Let's look at the first input.</p>
<p>We can see the scriptPubKey is</p>
<pre><code>OP_HASH160 fcebfb01198e90e99875a913dd592650fd2df667 OP_EQUAL
</code></pre>
<p>This means it is a P2SH spend, where the P2SH redeem script has the hash <code>fcebfb0....67</code></p>
<p>Let's look at that redeem script then:</p>
<pre><code>0020d687015ac2304c3213491ff4fc71ea15e9c170fddedd8bed92be2d191f8adddd
</code></pre>
<p>This is OP_0 followed by a push of 32-bytes. That means it's a P2WSH spend. Because this was wrapped inside P2SH, it's called P2SH-P2WSH. But there is no public key or signature involved so far, just scripts.</p>
<p>The witness consists of three pushes onto the stack. This includes the witness script:</p>
<pre><code>OP_PUSHNUM_2 OP_PUSHBYTES_33 035e1822f3a6eec3911545188bb0e6df3826eef8b630a77d1e344ba5954d9534a8 OP_PUSHBYTES_33 03694100a1f02854db3acac05c878053aba59fedfe3ebb1b7e094051aa61da6ba8 OP_PUSHBYTES_33 03e9509f6b6ad39542aa816c24d50d8ca7d83cfa6b430f0c366184957961a07fd6 OP_PUSHNUM_3 OP_CHECKMULTISIG
</code></pre>
<p>In other words, this is a 2-of-3 multisig. The other two items on the witness stack are therefore signatures. But this is going to be different for every possible type of script, so there isn't going to be a way to just &quot;extract&quot; the signature from P2WSH spends. Best to focus just on the key-based spends.</p>

CC BY-SA 4.0
2022-01-15T12:20:27.273Z
please see this it has long witness ,https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494
Ramin Najafi
CC BY-SA 4.0
2022-01-15T19:58:27.687Z
That's because that transaction is a P2SH-P2WSH transaction, meaning a script is used rather than a key and signature.
meshcollider
CC BY-SA 4.0
2022-01-16T09:48:38.283Z
Extract what? Just use the same logic as rscan.py that you mentioned, but look at the witness field as I described.
meshcollider
CC BY-SA 4.0
2022-01-16T10:12:07.507Z
Which TX? The P2WSH one? I told you, it is a script spend, not a key + signature spend.
meshcollider
CC BY-SA 4.0
2022-01-16T10:48:52.193Z
https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494
Ramin Najafi
CC BY-SA 4.0
2022-01-16T20:07:00.713Z
Yes, that's a P2SH-P2SWH transaction. I already told you that. It isn't a key + signature transaction (P2WPKH). I've added an explanation to my answer.
meshcollider
CC BY-SA 4.0
2022-01-15T09:12:27.960Z
2022-01-16T20:30:12.010Z
2022-01-16T20:30:12.010Z
meshcollider
<p>How can I calculate the R, S, and Z values from a raw bitcoin transaction? I can extract them with <code>rscan.py</code>, but that doesn't work for new transactions.</p>

<signature><raw-transaction><python><nodejs>
How to calculate R, S, and Z from raw bitcoin transaction with witness?
Ramin Najafi
# How to calculate R, S, and Z from raw bitcoin transaction with witness?

(Asked by: username_0 on 2022-01-15)

How can I calculate the R, S, and Z values from a raw bitcoin transaction? I can extract them with `rscan.py`, but that doesn't work for new transactions.

**Tags:** signature, raw-transaction, python, nodejs


---

## 1 answers

### Answer by username_1 on 2022-01-15. Score: 1 | Accepted answer

As specified in [BIP-141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki), the witness for a P2WPKH spend is as follows:

> The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a signature, and the second one a public key.

The signature consists of the usual R and s values that you're looking for, as before.

---

Let's look at [this transaction](https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494) you keep linking to. Let's look at the first input.

We can see the scriptPubKey is

```
OP_HASH160 fcebfb01198e90e99875a913dd592650fd2df667 OP_EQUAL

```

This means it is a P2SH spend, where the P2SH redeem script has the hash `fcebfb0....67`

Let's look at that redeem script then:

```
0020d687015ac2304c3213491ff4fc71ea15e9c170fddedd8bed92be2d191f8adddd

```

This is OP\_0 followed by a push of 32-bytes. That means it's a P2WSH spend. Because this was wrapped inside P2SH, it's called P2SH-P2WSH. But there is no public key or signature involved so far, just scripts.

The witness consists of three pushes onto the stack. This includes the witness script:

```
OP_PUSHNUM_2 OP_PUSHBYTES_33 035e1822f3a6eec3911545188bb0e6df3826eef8b630a77d1e344ba5954d9534a8 OP_PUSHBYTES_33 03694100a1f02854db3acac05c878053aba59fedfe3ebb1b7e094051aa61da6ba8 OP_PUSHBYTES_33 03e9509f6b6ad39542aa816c24d50d8ca7d83cfa6b430f0c366184957961a07fd6 OP_PUSHNUM_3 OP_CHECKMULTISIG

```

In other words, this is a 2-of-3 multisig. The other two items on the witness stack are therefore signatures. But this is going to be different for every possible type of script, so there isn't going to be a way to just "extract" the signature from P2WSH spends. Best to focus just on the key-based spends.

> **Comments:**
> **username_0**: please see this it has long witness ,https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494  
> **username_1**: That's because that transaction is a P2SH-P2WSH transaction, meaning a script is used rather than a key and signature.  
> **username_1**: Extract what? Just use the same logic as rscan.py that you mentioned, but look at the witness field as I described.  
> **username_1**: Which TX? The P2WSH one? I told you, it is a script spend, not a key + signature spend.  
> **username_0**: https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494  
> **username_1**: Yes, that's a P2SH-P2SWH transaction. I already told you that. It isn't a key + signature transaction (P2WPKH). I've added an explanation to my answer.  

---

