0

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.

Murch
  • 71,155
  • 33
  • 180
  • 600

1 Answers1

1

As specified in BIP-141, 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 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.

meshcollider
  • 11,695
  • 4
  • 24
  • 52
  • please see this it has long witness ,https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494 – Ramin Najafi Jan 15 '22 at 12:20
  • That's because that transaction is a P2SH-P2WSH transaction, meaning a script is used rather than a key and signature. – meshcollider Jan 15 '22 at 19:58
  • Extract what? Just use the same logic as rscan.py that you mentioned, but look at the witness field as I described. – meshcollider Jan 16 '22 at 09:48
  • Which TX? The P2WSH one? I told you, it is a script spend, not a key + signature spend. – meshcollider Jan 16 '22 at 10:12
  • https://www.blockchain.com/btc/tx/9ae7ce79fa8501dd92ade347fae7c1737ccefa6db204a2fae485634c265d5494 – Ramin Najafi Jan 16 '22 at 10:48
  • 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 Jan 16 '22 at 20:07