Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: minerdole on July 09, 2024, 11:10:18 AM



Title: Get pubKey for an address using bitcoin-cli
Post by: minerdole on July 09, 2024, 11:10:18 AM
Howdy tech folks,

I would greatly value your expertise in retrieving my wallet's public key for a given address.

What I currently understand:
  • The scriptPubKey no longer contains the public key for Taproot addresses.
  • The bitcoin-cli commands `validateaddress` and `getaddressinfo` no longer return the pubKey.
  • The addresses in question have been receive addresses so far and have not submitted any transactions from which I could retrieve the pubKey.

The only way I can currently identify is to dump the private key from bitcoin-cli, import it into bitcoin-js, and then get the pubKey. But I really, really want to avoid this.

I appreciate any advice or hints.

Thanks a lot! BTC



Title: Re: Get pubKey for an address using bitcoin-cli
Post by: nc50lc on July 09, 2024, 01:04:20 PM
The only way I can currently identify is to dump the private key from bitcoin-cli -snip-
Since you're talking about a wallet with TapRoot, it must be a descriptor wallet.
So, how are you able to export its private key? I'm curious.

Anyways, you may use getaddressinfo, then take note of the extended public key in its "parent_desc" and the derivation path's internal/external chain index.
(the number after the "/" on its right, '0' for receiving addresses, '1' for change addresses)
And also the "address_index" in its descriptor - "desc:". (the last digit on its derivation path)
Then use bitcoin-js or other tool to derive the public key from the xpub.

e.g. (RegTest):
getaddressinfo bcrt1pg9aez39kk2w5qucfy5da340amz3ajzug7zs4xetpkhzcr5maxwgqfw43k0
...
"desc": "tr([de651e9f/86h/1h/0h/0/3]6dd8531afdb9ce153cd8a053cc94e125ed7efc2be0e2c451b57104330274a595)#hskruc03",
"parent_desc": "tr([de651e9f/86h/1h/0h]tpubDCjhgDSSsWLW5xD6nFUQswRHkdJrdAmfJqu8jewwoWYgXvvdA5jr2qqEZc38UVynn1zPMJv7vw8 hPXuhfd7yZhuLh5Fbq2mPb4CVrcE5To4/0/*)#l6qxnyxd",
...

From the example; (external) chain index = 0 & address_index = 3.
So derive the public key at tpub/0/3 which should be the public key of the address.
Public key: 036dd8531afdb9ce153cd8a053cc94e125ed7efc2be0e2c451b57104330274a595 (I used IanColeman's BIP39 tool for this example, I don't use bitcoin-js)