3

I am currently working on a tool parsing every transaction of the bitcoin blockchain, using the getrawtransaction RPC call in verbose mode. My goal is to store them all in a database, while keeping some useful information about them (i.e., not everything that is given by the RPC call).

Something I am having trouble to understand though is how to precisely "tag" an input with a type? The outputs all have a "type" string that allows me to tag them as P2SH, P2PK, P2WSH, etc, but the inputs don't.

What I tried up to now is to parse the content of the scriptSig attribute of the inputs. For example, to know if an input is P2PK, I read somewhere that the ASM code of the scriptSig of the input should start with 30. When I compare with some online tools that have already tagged every transaction inputs/outputs, it looks like it's accurate, but I don't have any proof that what I am doing is right.

Is there some kind of online resources with precise specifications allowing me to identify an input? I cannot manage to find any.

Another idea I had is that each output is, obviously, linked to an output. Could I suppose that the type of input is the same as the one of the output it is linked to?

It would make sense to me, but I would rather not jump to conclusions; I still have a lot to learn about Bitcoin.

Thank you very much!

1 Answer 1

3

Looking at the transaction in raw (serialized) format, neither inputs nor outputs have their own type. In other words, it is not indicated anywhere what type they are. However, there are certain standard output formats such as P2PKH, P2SH, P2WPKH etc. You can see the complete list of all standard formats at the following link. What getrawtransaction (like many other functions within Bitcoin RPC) does is generate additional fields in the JSON result based on the actual content of the transaction to make it easier for end users. Unfortunately, one of those additional fields is not an input type.

The way to see what type of output a certain input unlocks (and therefore implicitly what type that input is) is to look at a pair of transaction-output referenced by a certain input and based on the content/format of that referenced output (link above) determine the type of input.

2
  • 1
    Thank you very much for your answer, Filip, it will be very useful to me. However, one of the additional JSON fields in the outputs of a transaction is type, and after testing with a few transactions, using the table you provided me, this field seems accurate (e.g., a P2WSH output will have the type witness_v0_scripthash and the format is the one from the table). Do you know how this type field is generated? Would the other fields be parsed to deduce the type?
    – Lev
    Sep 28 at 9:02
  • 2
    @Lev The type field is not an actual part of the transaction, Bitcoin RPC determined this field based on the content of the output script (scriptPubKey). For example, if it sees the content OP_HASH160 OP_PUSHBYTES_20 32-bytes OP_EQUAL in the output script (scriptPubKey), it knows that the type is P2SH, etc. No, it is only necessary to read the content of the output script, ie. the content of scriptPubKey inside the output, to determine its type.
    – dassd
    Sep 28 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.