How to get addresses from scriptPubKey in segwit transactions?

10

6

I'm running through raw transaction data, and I'd like to be able to gather addresses (where possible).

So far, I've made this diagram to help me understand when it's possible to figure out addresses from the scriptPubKey of each output:

enter image description here

However, as you can see, I'm not sure how I can work out an address from a P2WSH.

Is it possible to get addresses from the scriptPubKey alone, or can I only get them when the witness is revealed when the output has been spent? (as shown here):

witness:      0 <signature1> <1 <pubkey1> <pubkey2> 2 CHECKMULTISIG>
scriptSig:    (empty)
scriptPubKey: 0 <32-byte-hash>
              (0x0020{32-byte-hash})

EDIT: Also, please let me know if you notice that any of my pattern matching in this diagram is incorrect (or if I'm missing any patterns that allow me to find out more addresses).

inersha

Posted 2016-11-12T12:55:56.147

Reputation: 2 236

Nitpick: there's a checksum for the address in P2PK/P2PKH/P2SH. https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses

Nick ODell 2016-11-12T17:31:23.657

Yes, thank you. To clarify for anyone reading this, it should be base58('00' + hash160 + checksum)inersha 2016-11-12T19:22:34.717

Answers

6

There currently exists no native address format for witness outputs, so the answer for P2WPKH and P2WSH is simply: you can't, and shouldn't. If you convert the 20-byte hash inside a P2WPKH output to a 1xxxxxx, and show the result, people may think that is an address more money can be sent to. However, that would result in a P2PKH output, which a pure segwit wallet may not understand.

However, most segwit outputs initially will use the P2SH wrapper (resulting in P2SH-P2WPKH or P2SH-P2WSH programs), which just look like (and behave like) P2SH addresses.

A new native segwit address format is proposed in https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki. That can only be used when both the receiver and sender software support it however.

Pieter Wuille

Posted 2016-11-12T12:55:56.147

Reputation: 54 032

Still ongoing? I want to know the latest trend. Is P2SH wrapper the defacto standard now? Seems bit142 is still deferred. https://github.com/bitcoin/bips/blob/master/bip-0142.mediawiki

zono 2017-06-30T16:06:08.697

BIP173 replaces it. It will take a long time to be adopted, however, so initially I expect segwit transfers to use the P2SH wrapper.Pieter Wuille 2017-06-30T16:12:51.110

2

I assembled this picure with the top ten PKscripts I found somewhere on a webpage... don't find the link. I'd also want to extend it by segwit outputs. I can share the "graphml" file if desired.

enter image description here

pebwindkraft

Posted 2016-11-12T12:55:56.147

Reputation: 4 568