0
I am creating a block parser and just learned about Scripts in Bitcoin yesterday which is kind of confusing so far. I want to extract the addresses involved in each transaction. Right now, I am able to read the script for each output. To extract the address, am I correct in thinking that my code will need to handle all of the different formats of scriptPubKeys? Is there any sample code out there to guide me? If I am completely wrong, how should I do it?
Thanks a lot Andrew. I'm not great at C++ (only been programming for a year) but I think I can use it as a guide. One more question: why would a scriptPubKey not map to an address? For the purposes of my block parser, would I just discard it? – Johnny – 2019-07-13T05:56:05.133
A scriptPubKey can be literally any sequence of bytes; it doesn't have to be a valid script, the output will just be unspendable. Those standard scriptPubKeys that don't have addresses occur because there was/is no reason for them to have addresses. A very common one that you will see are Pay to Pubkey outputs. These are in basically all of the early blocks because the original Bitcoin client created coinbase transactions with P2PK outputs. Some block explorers will convert these to addresses, but that is incorrect. – Andrew Chow – 2019-07-13T06:02:14.917
I recommend that you do not discard them because you will be losing information. It is useful to know exactly what the scriptPubKey for an output is. So you should still just store them. Maybe decode them into opcode notation instead of storing the bytes themselves, but you should still keep the scripts around in some form. – Andrew Chow – 2019-07-13T06:03:21.163
Regarding the handling of segwit txns, is my understanding correct?
1) Check if it's non-native or native.
2) If the former, check if it's p2sh-p2wpkh or p2sh-p2wsh and then handle it like any other p2sh.
3) If the latter, determine if it's p2wpkh or p2wsh.
4) After extracting the script hash, perform the appropriate hashing and base58check encoding to extract the addresses? – Johnny – 2019-07-15T06:56:54.307
You won't be able to tell if an output is non-native segwit. The scriptPubKey will not contain the segwit script at all, you will just see a p2sh output. So you should treat all p2sh outputs the same, because, as outputs, they are. – Andrew Chow – 2019-07-15T13:38:06.137