0

I'm looking for an explanation on the difference between the bitcoin CLI for decoderawtransaction and decodescript please.

From what I know, decodetransaction will undo the hash and ECDSA elliptic curve calculations for the overall transaction data, and break it down into asm, hex, reqSigs, type of script & the original address. How is this different from decodescript & when should I be using one vs the other?

Thanks.

1 Answer 1

1

The Bitcoin protocol has its own serialization format for transactions, blocks, scripts etc. These formats optimize for space, which means they aren't particularly human-readable, so there are bitcoin-cli commands that can be used to decode them into a more human-readable form.

Consider the transaction 60267bfd9494e1a6a5218184c36bf4c97adccbbde40487a2f92fcfffc10d0500. Here is its serialization in hexadecimal:

010000000001018154f1677366e5799f61646e5cea1ea13ef23c0bd3ba4cc845dcdc3f061c47570100000000ffffffff01035a28000000000016001486918c31af903a4c7ed2dc8191156c0ef44a11b40141f33812c31e4c1ae9a46c55e1463121d42c1c5cd64afc86fe94d2a126b2e515823954b079e11bf1251aa9d585cf4a83a0473634f1c22891249dacf9f93c976ff40100000000

When you feed this into decoderawtransaction, you get this:

{
  "txid": "60267bfd9494e1a6a5218184c36bf4c97adccbbde40487a2f92fcfffc10d0500",
  "hash": "9aab2076ba5b8f2656d1a2b9f9928c6cb6a4d6f607ef88589ea6b6e842f3f320",
  "version": 1,
  "size": 151,
  "vsize": 100,
  "weight": 397,
  "locktime": 0,
  "vin": [
    {
      "txid": "57471c063fdcdc45c84cbad30b3cf23ea11eea5c6e64619f79e5667367f15481",
      "vout": 1,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "f33812c31e4c1ae9a46c55e1463121d42c1c5cd64afc86fe94d2a126b2e515823954b079e11bf1251aa9d585cf4a83a0473634f1c22891249dacf9f93c976ff401"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.02644483,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 86918c31af903a4c7ed2dc8191156c0ef44a11b4",
        "desc": "addr(bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs)#h6rkqret",
        "hex": "001486918c31af903a4c7ed2dc8191156c0ef44a11b4",
        "address": "bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs",
        "type": "witness_v0_keyhash"
      }
    }
  ]
}

Among many other things, this also decodes the transaction's output scripts (under scriptPubKey). However, if you have a serialized script outside the context of a transaction, which in this case would be 001486918c31af903a4c7ed2dc8191156c0ef44a11b4, you can use decodescript on it:

{
  "asm": "0 86918c31af903a4c7ed2dc8191156c0ef44a11b4",
  "desc": "addr(bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs)#h6rkqret",
  "address": "bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs",
  "type": "witness_v0_keyhash",
  "p2sh": "3Np3NnSFbrdMjRi9ECcnoWCfHBPVVHe5iw"
}

Additionally, there are scripts that decoderawtransaction won't decode, for example redeem scripts in witnesses of P2WSH inputs.

Take transaction b4bb98f79c50ef7fe74fb9fac705e3952650161477bd82b2687c953b71a44594 which spends one P2WSH input with a 2-of-3 multisignature script. The serialized script will be given by decoderawtransaction as the last item of txinwitness:

5221020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a210211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db856760082102e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf57253ae

Decoded by decodescript as:

{
  "asm": "2 020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a 0211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db85676008 02e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf572 3 OP_CHECKMULTISIG",
  "desc": "multi(2,020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a,0211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db85676008,02e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf572)#d63cpu6r",
  "type": "multisig",
  "p2sh": "37CYRhHBjdgoTC7KQpUuTH3zAcm9akWqAd",
  "segwit": {
    "asm": "0 ec21a1b4e8cdaa89737895168ead0c3210fc2d1c6bf9805e45a475f714c75140",
    "desc": "addr(bc1qass6rd8gek4gjumcj5tgatgvxgg0ctgud0ucqhj9536lw9x829qqpnu64e)#2wsw9unx",
    "hex": "0020ec21a1b4e8cdaa89737895168ead0c3210fc2d1c6bf9805e45a475f714c75140",
    "address": "bc1qass6rd8gek4gjumcj5tgatgvxgg0ctgud0ucqhj9536lw9x829qqpnu64e",
    "type": "witness_v0_scripthash",
    "p2sh-segwit": "3PCgTmZsCjK6KHJeEHJ7iCjVPqb3ygf5jF"
  }
}
4
  • I see! Could decodescript do everything which decoderawtransaction is able to? (I'd like to further understand when to use each of them appropriately). Would also like to know more examples for decodescript use cases please. Thank you very much! Jan 26, 2023 at 8:41
  • No, a transaction consists of a lot more than just scripts, so decodescript cannot do everything. I added an example of a P2WSH script. Jan 26, 2023 at 9:04
  • Sorry, how did you acquire 5221020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a210211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db856760082102e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf57253ae? Jan 31, 2023 at 3:24
  • Is decodescript typically used to decode ScriptPubKey & ScriptSig as well? Jan 31, 2023 at 3:49

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.