1

I would like to get the script bytes from the bech32 address of a script. Is this possible?

4 Answers 4

1

From some indexer, yes as it is common to support fetching script's information from it's hash. Here is an API endpoint for Blockfrost.

To obtain script's hash from its bech32 encoded address, you would first need to decode this bech32 encoding and then remove the 1 byte header (see CIP-19).

1

To expand on the previous answer, you can use https://docs.blockfrost.io/#tag/Cardano-Scripts/paths/%7E1scripts%7E1%7Bscript_hash%7D/get endpoint and a TypeScript conversion function from here https://github.com/blockfrost/blockfrost-utils/blob/40964699913a54417371c63739e6d0c19712dca1/src/validation.ts#L220-L235

1

Thought I'd give some context for others who might come across this.

The short answer:

No, you cannot derive it directly because the script address only exposes the script hash.
You can however find it using tools like this Blockfrost endpoint

Long answer:

Cardano addresses are made up of 3 parts; a header, payment part, and a delegation part.

   addr1  zxgx3far7qygq0k6epa0zcvcvrevmn0ypsnfsue94nsn3tvpw  288a4x0xf8pxgcntelxmyclq83s0ykeehchz2wtspks905plm
| header |                  payment part                    |                 delegation part                 |

If you decode to Hex you get

    11     9068a7a3f008803edac87af1619860f2cdcde40c26987325ace138ad  81728e7ed4cf324e1323135e7e6d931f01e30792d9cdf17129cb806d
| header |                      payment part                       |                     delegation part                     |

The payment part in the above tells a node how to validate the transaction:

  • Either it is a pubkeyhash from a wallet, in which case the node checks for that wallets signature
  • Or it is the hash of a script, in which case the node checks for an attached script and executes it

This means that the only information you know from a script address is the hash of the script, and there is no way to directly derive the script bytes from this due to the nature of hashing

If you use a tool like the Blockfrost endpoint above to get the script, you must provide the script hash which you can get from the payment part of the HEX representation of the address.

1

While all answers are insightful, here is an example that makes it possible to entirely get the jpg.store script bytecode from just its script hash obtained from the bech32 address, using the pycardano and python-blockfrost libraries:

from pycardano import (
    BlockFrostChainContext, 
    Address,
    PlutusV2Script
)
import cbor2
from blockfrost import ApiUrls


# ------- Chain context
chain_context = BlockFrostChainContext(
    project_id="mainnetslL7jFzg8p4Hxk8pcJCRIcWvcM6P6FZE",
    base_url=ApiUrls.mainnet.value
)
jpg_store_addr = Address.from_primitive("addr1zxgx3far7qygq0k6epa0zcvcvrevmn0ypsnfsue94nsn3tvpw288a4x0xf8pxgcntelxmyclq83s0ykeehchz2wtspks905plm")
script_hash = jpg_store_addr.payment_part
script_cbor = chain_context.api.script_cbor(str(script_hash)).cbor
script_bytes = PlutusV2Script(cbor2.loads(bytes.fromhex(script_cbor)))
print(script_bytes.hex())  # equals jpg.store script bytecode as shown on cardanoscan

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.