0

Given that address is a "convenience" field, it makes sense that it is not provided by default in many transactions. However, it seems to be possible to extract address from hex. I am wondering if I can get this address from bitcoin client applications (e.g., bitcoin-qt), ideally via the REST API.

An example block I am looking at is the block with height 100:

{
    "hash": "000000007bc154e0fa7ea32218a72fe2c1bb9f86cf8c9ebf9a715ed27fdb229a",
    "confirmations": 715831,
    "height": 100,
    "version": 1,
    "versionHex": "00000001",
    "merkleroot": "2d05f0c9c3e1c226e63b5fac240137687544cf631cd616fd34fd188fc9020866",
    "time": 1231660825,
    "mediantime": 1231656204,
    "nonce": 1573057331,
    "bits": "1d00ffff",
    "difficulty": 1,
    "chainwork": "0000000000000000000000000000000000000000000000000000006500650065",
    "nTx": 1,
    "previousblockhash": "00000000cd9b12643e6854cb25939b39cd7a1ad0af31a9bd8b2efe67854b1995",
    "nextblockhash": "00000000b69bd8e4dc60580117617a466d5c76ada85fb7b87e9baea01f9d9984",
    "strippedsize": 215,
    "size": 215,
    "weight": 860,
    "tx": [
        {
            "txid": "2d05f0c9c3e1c226e63b5fac240137687544cf631cd616fd34fd188fc9020866",
            "hash": "2d05f0c9c3e1c226e63b5fac240137687544cf631cd616fd34fd188fc9020866",
            "version": 1,
            "size": 134,
            "vsize": 134,
            "weight": 536,
            "locktime": 0,
            "vin": [
                {
                    "coinbase": "04ffff001d014d",
                    "sequence": 4294967295
                }
            ],
            "vout": [
                {
                    "value": 50.00000000,
                    "n": 0,
                    "scriptPubKey": {
                        "asm": "04e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93 OP_CHECKSIG",
                        "hex": "4104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac",
                        "type": "pubkey"
                    }
                }
            ],
            "hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d014dffffffff0100f2052a01000000434104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac00000000"
        }
    ]
}
2

Not all scriptPubKeys correspond to an address. The address field is provided for those that do, and is omitted for those that don't. Your example contains a P2PK scriptPubKey which does not have a corresponding address.

Some blockchain explorers may extract the pubkey from a P2PK output and compute a corresponding address, but that is not technically correct. Such an address would result in a P2PKH scriptPubKey, not P2PK.

4
  • 1
    Am I correct in assuming that if address is not provided, it means the address cannot be "constructed" correctly?
    – Hamed
    Dec 27 '21 at 7:16
  • @Hamed Yes. If address does not exist, the scriptPubKey has no corresponding address.
    – Andrew Chow
    Dec 27 '21 at 17:32
  • Thank you. Does this apply only to P2PK, or I can generalize it to P2SH, P2SHH, and P2WPKH?
    – Hamed
    Dec 27 '21 at 17:37
  • 1
    You can generalize it to all scriptPubKeys. If the scriptPubKey has a corresponding address, then there will be an address present. Note that there is no address for this P2PK scriptPubKey because all P2PK scriptPubKeys do not have addresses. Likewise, all P2PKH scriptPubKeys have addresses.
    – Andrew Chow
    Dec 27 '21 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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