0

I'm trying to write a simple python implementation using bitcoinrpc to extract the address receiving a transaction but...

  1. I'm not sure why certain fields are only accessible inconsistently. E.g. tx['vout']['scriptPubKey']['address'] is accessible 43 times in transactions in the first 10,000 blocks (but doesn't exist on the other 10,075 occasions). The documentation mentions I should even find a tx['vout']['scriptPubKey']['addresses'] (plural) field but I can access that 0 times in the first 10,000 blocks. I understand that there are different transaction/script types and that may impact things, for example the 43 times I found an 'address' field, the tx['vout']['scriptPubKey']['asm'] field for that transaction had this identical structure/opcodes: OP_DUP OP_HASH160 12ab8dc588ca9d5787dde7eb29569da63c3a238c OP_EQUALVERIFY OP_CHECKSIG. I also notice that fields like tx['vout']['scriptPubKey']['asm'] and tx['vout']['scriptPubKey']['desc'] are at least always present so I'm hoping it's possible to extract the receiver address from them somehow? At least for a subset of the possible types of scripts? If so, does anyone know the best place to find a definitive list of the valid types of scripts and for which I could extract the addresses?

  2. The "desc" field has a structure like 'desc': 'pk(04b0db59592289864233ce40cf1dc7420e8b5e290b4402bd203545d330ece6ba841ffef40fa62b76596688a46e70b284f57566766b7d8bf75274b343164bafc8a3)#ef9sgeu5' but I'm struggling to find a description of the format used - e.g. does pk stand for public key?

Thanks for any thoughts at all

1 Answer 1

3

The "address" field in the Bitcoin Core RPC output is present for all transaction outputs for which a corresponding address exists. In the first 10000 blocks there are lots of pay-to-public-key (P2PK) outputs, which while possible to construct internally in wallet software, don't have a corresponding address format that allows instructing a payer to create such an output.

The existing address types in Bitcoin today are:

  • pay-to-pubkey-hash (P2PKH)
  • pay-to-script-hash (P2SH)
  • native witness addresses, for:
    • pay-to-witness-public-key-hash (P2WPKH)
    • pay-to-witness-script-hash (P2WSH)
    • pay-to-taproot (P2TR)

Whenever you inspect any output whose scriptPubKey matches any of these address types, the RPC output will report the address. But scriptPubKeys are free form byte arrays, and there is no strict requirement that they follow the structure encoded by any of the defined address types.

The "addresses" is older, and was deprecated and removed. Its purpose was listing the individual participants in bare multisig outputs. Such outputs predate the introduction of P2SH in 2012, and were never widespread. Today they're effectively unusable.

For documentation of Bitcoin Core's RPC, see the help RPC command, or https://bitcoincore.org. The https://bitcoin.org website is unrelated to Bitcoin Core, and its information is often outdated.

The "desc" field reports information about the output in "output descriptor" notation. This format is described in BIP380 and the following BIPs, and Bitcoin Core documentation about it can be found in its doc directory: https://github.com/bitcoin/bitcoin/tree/master/doc/descriptors.md. And indeed, pk() stands for public key, indicating a P2PK output with the argument as public key.

2
  • thanks that's incredibly helpful! I also just realised I could use bitcoinrpc's deriveaddresses(tx['vout']['scriptPubKey']['desc']) which seems to either return addresses or throw an JSONRPCException if no addresses are associated to the vout
    – d3wannabe
    Feb 18, 2023 at 16:53
  • 1
    You can also check if the descriptor is of the form addr(), pkh(), wpkh(), wsh(), sh(),or tr(). Those have associated addresses. Feb 18, 2023 at 17:50

Your Answer

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

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