decodescript - Bitcoin

This method decodes a hex-encoded script and returns details about it, including its assembly, type, and the addresses it pays to.

Parameters

ParameterTypeRequiredDescription
hexstringstringYesThe script hex

Request

bashcurl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "decodescript",
    "params": ["76a914d0f172a0ecb48aee1be1f2687d2963ae33f71a1088ac"],
    "id": "getblock.io"
}'

example.js

javascriptconst axios = require('axios');

const response = await axios.post('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/', {
    jsonrpc: '2.0',
    method: 'decodescript',
    params: ["76a914d0f172a0ecb48aee1be1f2687d2963ae33f71a1088ac"],
    id: 'getblock.io'
}, {
    headers: { 'Content-Type': 'application/json' }
});

console.log(response.data.result);

example.py

pythonimport requests

response = requests.post(
    'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/',
    headers={'Content-Type': 'application/json'},
    json={
        'jsonrpc': '2.0',
        'method': 'decodescript',
        'params': ["76a914d0f172a0ecb48aee1be1f2687d2963ae33f71a1088ac"],
        'id': 'getblock.io'
    }
)

print(response.json())

example.rs

rustuse reqwest::Client;
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();

    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "decodescript",
            "params": ["76a914d0f172a0ecb48aee1be1f2687d2963ae33f71a1088ac"],
            "id": "getblock.io"
        }))
        .send()
        .await?
        .json::<Value>()
        .await?;

    println!("Result: {}", response["result"]);
    Ok(())
}

Response

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": {
        "asm": "OP_DUP OP_HASH160 d0f172a0ecb48aee1be1f2687d2963ae33f71a10 OP_EQUALVERIFY OP_CHECKSIG",
        "desc": "addr(bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq)#checksum",
        "type": "pubkeyhash",
        "address": "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
        "p2sh": "3P14159f73E4gFr7JterCCQh9QjiTjiZrG"
    }
}

Response Parameters

ParameterTypeDescription
jsonrpcstringJSON-RPC protocol version ("2.0")
idstringRequest identifier matching the request
resultobjectDecoded script object

Result Object

FieldTypeDescription
asmstringScript in assembly notation
typestringOutput type (pubkeyhash, scripthash, witness_v0_keyhash, and others)
addressstringAddress the script pays to, when applicable
p2shstringThe P2SH address wrapping this script

Use Cases

  • Script Analysis: Understand a locking script
  • Address Derivation: Read the address a script encodes
  • Debugging: Inspect custom scripts
  • Tooling: Decode scripts in explorers

Error Handling

Error CodeMessageDescription
403ForbiddenMissing or invalid ACCESS-TOKEN
-22Decode failedThe transaction or script could not be decoded
-8Invalid parameterA parameter is out of range or malformed
-32602Invalid paramsA parameter is missing or has the wrong type