<p>Really, the only way to detect the type is to parse it as a program.</p>

<p>All scripts Bitcoin are written in the Bitcoin Script language. Much like any other programming language, certain symbols have special meaning, and correspond to specific op codes, which appear in the raw hex form. You can find a list of op codes on the Bitcoin Wiki: <a href="https://en.bitcoin.it/wiki/Script#Opcodes" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Script#Opcodes</a></p>

<p>The standardized scripts, which are usually what we see presented as addresses, are really just specific snippets of Bitcoin Script programs. For example, a P2PKH "address" is really just half a program in the form of:</p>

<pre><code>OP_DUP OP_HASH160 &lt;pubKeyHash&gt; OP_EQUALVERIFY OP_CHECKSIG
</code></pre>

<p>When combined with the remaining half of the program, which is provided as the ScriptSig when spending the coins, you get a complete program which can be evaluated.</p>

<p>To determine the script type, you really just need to parse the program (or partial program, in the event of an output) and see if it matches a known, standardized type.</p>

CC BY-SA 4.0
2019-06-16T08:48:51.237Z
I mean how I can parse a scriptsig which is like this :

`scriptSig: <sig> <pubKey>`

in other words, i want to find it's public key to generate related address.
Saeed
CC BY-SA 4.0
2019-06-16T09:44:15.030Z
A script sig is just a bitcoin script fragment - even simpler, they are restricted to only use OP_PUSH, so to read the signature and public key, you read the first byte for the push length, then read that many bytes as data, then read the next byte, and read that many bytes as data again - The first chunk of data is a DER encoded signature, and the second chunk is the public key
Raghav Sood
CC BY-SA 4.0
2019-06-16T09:45:19.830Z
Naturally, the above only works for P2PKH/P2PK addresses -  P2SH and segwit addresses will require more effort
Raghav Sood
CC BY-SA 4.0
2019-06-16T10:06:32.513Z
Thank you a lot.
I have another question. you explained how I can parse DER encoded signature and public key. in the tx output address i can generate output address after detection of type of address(P2PKH or P2PK  or  P2SH  or ...)
because it is detectable from it's op code. but what about input script? 
Now with your guidance i can extract public key from input tx. how can i understand structure of producing address from this public key?
Saeed
CC BY-SA 4.0
2019-06-16T10:37:18.527Z
As an exapmle, i have some tx messages which it's signature script length is zero and did'nt have any script.
in other example i have a complete script with 23 byte length, as you saied before, I assumed first byte of it for push length, but first byte is equal to 22, so it means that it is not any public key. what is the reason?
Saeed
CC BY-SA 4.0
2019-06-16T11:52:21.357Z
is it some exceptions about your rule for parsing public key?
Saeed
CC BY-SA 4.0
2019-06-17T02:35:09.897Z
@Saeed You can only ever determine the address from the output being spent. You *cannot* do this from the scriptSig alone.
Pieter Wuille
CC BY-SA 4.0
2019-06-17T04:50:59.633Z
@PieterWuille So if I can't determine address from scripsig, then how miners or everyone can evaluate reliability of that transaction? and ensure that sender have enough credit to create this transaction?
Saeed
CC BY-SA 4.0
2019-06-17T05:07:04.640Z
Because they also look at the output being spent. That's how validation works. That's what you need to do to get any insight in what's going on. Also, addresses are not relevant for balance validation: the question is simply whether or not the output being spent was previously spent or not already, and if not, what value it had. Addresses are a human-level abstraction into the authentication mechanism, but don't exist at all at the protocol level. In particular, there is no such thing as an address balance. Just UTXOs.
Pieter Wuille
CC BY-SA 4.0
2019-06-17T05:10:45.667Z
As I advised you elsewhere, stop looking at protocol dumps and trying to match that information up with block explorers. If you want to understand what's going on, read how transactions and other things in the protocol *work*.
Pieter Wuille
CC BY-SA 4.0
2019-06-16T08:23:19.990Z
2019-06-16T08:23:19.990Z
Raghav Sood
<p>As explained in </p>

<p><a href="https://en.bitcoin.it/wiki/Script" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Script</a></p>

<p>there are many types of scriptPubKey and scriptSig like:</p>

<pre><code>&lt;sig&gt; &lt;pubKey&gt;
&lt;sig&gt; &lt;pubKey&gt; &lt;pubKey&gt; 
&lt;sig&gt; &lt;pubKey&gt; &lt;pubHashA&gt; 
&lt;sig&gt; &lt;pubKey&gt; &lt;pubHashA&gt; &lt;pubKeyHash&gt;
...
</code></pre>

<p>how can we detect type of ScriptSig and separate each part?
In other words is there a length variable or something like that to parse each part separately?</p>

<script><public-key><input-script>
Detect types of ScriptSig and parse each part
Saeed
# Detect types of ScriptSig and parse each part

(Asked by: username_0 on 2019-06-16)

As explained in

<https://en.bitcoin.it/wiki/Script>

there are many types of scriptPubKey and scriptSig like:

```
<sig> <pubKey>
<sig> <pubKey> <pubKey> 
<sig> <pubKey> <pubHashA> 
<sig> <pubKey> <pubHashA> <pubKeyHash>
...

```

how can we detect type of ScriptSig and separate each part?
In other words is there a length variable or something like that to parse each part separately?

**Tags:** script, public-key, input-script


---

## 1 answers

### Answer by username_1 on 2019-06-16. Score: 1

Really, the only way to detect the type is to parse it as a program.

All scripts Bitcoin are written in the Bitcoin Script language. Much like any other programming language, certain symbols have special meaning, and correspond to specific op codes, which appear in the raw hex form. You can find a list of op codes on the Bitcoin Wiki: <https://en.bitcoin.it/wiki/Script#Opcodes>

The standardized scripts, which are usually what we see presented as addresses, are really just specific snippets of Bitcoin Script programs. For example, a P2PKH "address" is really just half a program in the form of:

```
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

```

When combined with the remaining half of the program, which is provided as the ScriptSig when spending the coins, you get a complete program which can be evaluated.

To determine the script type, you really just need to parse the program (or partial program, in the event of an output) and see if it matches a known, standardized type.

> **Comments:**
> **username_0**: I mean how I can parse a scriptsig which is like this :  `scriptSig: <sig> <pubKey>`  in other words, i want to find it's public key to generate related address.  
> **username_1**: A script sig is just a bitcoin script fragment - even simpler, they are restricted to only use OP_PUSH, so to read the signature and public key, you read the first byte for the push length, then read that many bytes as data, then read the next byte, and read that many bytes as data again - The first chunk of data is a DER encoded signature, and the second chunk is the public key  
> **username_1**: Naturally, the above only works for P2PKH/P2PK addresses -  P2SH and segwit addresses will require more effort  
> **username_0**: Thank you a lot. I have another question. you explained how I can parse DER encoded signature and public key. in the tx output address i can generate output address after detection of type of address(P2PKH or P2PK  or  P2SH  or ...) because it is detectable from it's op code. but what about input script?  Now with your guidance i can extract public key from input tx. how can i understand structure of producing address from this public key?  
> **username_0**: As an exapmle, i have some tx messages which it's signature script length is zero and did'nt have any script. in other example i have a complete script with 23 byte length, as you saied before, I assumed first byte of it for push length, but first byte is equal to 22, so it means that it is not any public key. what is the reason?  
> **username_0**: is it some exceptions about your rule for parsing public key?  
> **username_2**: @username_0 You can only ever determine the address from the output being spent. You *cannot* do this from the scriptSig alone.  
> **username_0**: @username_2 So if I can't determine address from scripsig, then how miners or everyone can evaluate reliability of that transaction? and ensure that sender have enough credit to create this transaction?  
> **username_2**: Because they also look at the output being spent. That's how validation works. That's what you need to do to get any insight in what's going on. Also, addresses are not relevant for balance validation: the question is simply whether or not the output being spent was previously spent or not already, and if not, what value it had. Addresses are a human-level abstraction into the authentication mechanism, but don't exist at all at the protocol level. In particular, there is no such thing as an address balance. Just UTXOs.  
> **username_2**: As I advised you elsewhere, stop looking at protocol dumps and trying to match that information up with block explorers. If you want to understand what's going on, read how transactions and other things in the protocol *work*.  

---

