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.
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 – 2019-06-16T08:48:51.237
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 – 2019-06-16T09:44:15.030
Naturally, the above only works for P2PKH/P2PK addresses - P2SH and segwit addresses will require more effort – Raghav Sood – 2019-06-16T09:45:19.830
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 – 2019-06-16T10:06:32.513
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 – 2019-06-16T10:37:18.527
is it some exceptions about your rule for parsing public key? – Saeed – 2019-06-16T11:52:21.357
@Saeed You can only ever determine the address from the output being spent. You cannot do this from the scriptSig alone. – Pieter Wuille – 2019-06-17T02:35:09.897
@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 – 2019-06-17T04:50:59.633
1Because 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 – 2019-06-17T05:07:04.640
1As 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 – 2019-06-17T05:10:45.667