2

Is there a way for me to translate the input and output scripts in a bitcoin transaction?

I am building an application that lets you query different stats from the Bitcoin-network, down the line I want it to be able to "trace addresses" as well.

I have tried to read up on Script and how a transaction works but I am a bit confused.

Since the built-in functions in bitcoin-cli (and most all block explorers) are able to find the addresses, I'm sure I should be able to parse and translate the scripts… or?

2
  • Welcome to Bitcoin.stackexchange.com. Do you simply want to know how, in principle, to convert any Bitcoin script (in Hex say) to a standard address for each of the transaction output types for which an address is well-defined? (If so, all the discussion of BigQuery may be a hindrance to getting an answer) Jul 5, 2022 at 10:12
  • Ah thanks for the input, I agree. Just edited
    – JVH
    Jul 5, 2022 at 11:18

1 Answer 1

2

An address is just a standardized way to encode an output script. They aren't part of the Bitcoin protocol, and only a few standard scripts have an associated address. As of now these are P2PKH, P2SH, P2WPKH, P2WSH and P2TR.

I won't go into exact details on how to convert each of these scripts into an address, but I'll give helpful links.

P2PKH (Pay to Public Key Hash)

A P2PKH output scripts looks like this:

OP_DUP OP_HASH160 OP_PUSHBYTES_20 <key_hash> OP_EQUALVERIFY OP_CHECKSIG

The <key_hash> part is a 160-bit hash (SHA-256 then RIPEMD-160) of a public key. The address always starts with a 1. More details here.

P2SH (Pay to Script Hash)

A P2SH output script looks like this:

OP_HASH160 OP_PUSHBYTES_20 <script_hash> OP_EQUAL

The <script_hash> part is a 160-bit hash of a redeem script. The address always starts with a 3 and is constructed similarly to P2PKH. More details in BIP13.

P2WPKH (Pay to Witness Public Key Hash)

A P2WPKH output script looks like this:

OP_0 OP_PUSHBYTES_20 <key_hash>

As in P2PKH, <key_hash> is a 160-bit hash of a public key. The address always starts with bc1q. More details in BIP173.

P2WSH (Pay to Witness Script Hash)

A P2WSH output script looks like this:

OP_0 OP_PUSHBYTES_32 <script_hash>

The <script_hash> part is a 256-bit hash (double SHA-256) of a redeem script. As with P2WPKH the address always starts with bc1q and is also specified in BIP173.

P2TR (Pay to Taproot)

A P2TR output script looks like this:

OP_1 OP_PUSHBYTES_32 <key>

The <key> part is a BIP340 public key. The address always starts with bc1p. More details in BIP350.

Your Answer

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

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