I'm using bitcoin-cli and I need to get the public key of one of my SegWit addresses. My first attempt was to use 'validateaddress' command and take the 'scriptPubKey' field but that doesn't seem to be the public key of the address. Any ideas?
Because a "segwit address" starting with a
3 is actually a specific type of P2SH, the scriptPubKey will just be a normal P2SH scriptPubKey, which doesn't reveal anything about the script itself (only a hash of the script). You need the redeem script, which will be a null byte followed by a push of the public key hash. From what I can recall, the script is output in the
hex field of the validateaddress result.
Again, the public key hash doesn't actually reveal the public key, so once you have that there will probably be an extra step required, perhaps conversion of the public key hash into an address starting with a
1 and then validateaddress on that.
It is unfortunate that the support for segwit addresses is not great in version 0.15.x, it'll be much better in 0.16 when Peter Wuille's segwit wallet PR is merged