<p>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.</p>
<p>Consider the transaction <a href="https://mempool.space/tx/60267bfd9494e1a6a5218184c36bf4c97adccbbde40487a2f92fcfffc10d0500" rel="nofollow noreferrer">60267bfd9494e1a6a5218184c36bf4c97adccbbde40487a2f92fcfffc10d0500</a>. Here is its serialization in hexadecimal:</p>
<p><code>010000000001018154f1677366e5799f61646e5cea1ea13ef23c0bd3ba4cc845dcdc3f061c47570100000000ffffffff01035a28000000000016001486918c31af903a4c7ed2dc8191156c0ef44a11b40141f33812c31e4c1ae9a46c55e1463121d42c1c5cd64afc86fe94d2a126b2e515823954b079e11bf1251aa9d585cf4a83a0473634f1c22891249dacf9f93c976ff40100000000</code></p>
<p>When you feed this into <code>decoderawtransaction</code>, you get this:</p>
<pre><code>{
  &quot;txid&quot;: &quot;60267bfd9494e1a6a5218184c36bf4c97adccbbde40487a2f92fcfffc10d0500&quot;,
  &quot;hash&quot;: &quot;9aab2076ba5b8f2656d1a2b9f9928c6cb6a4d6f607ef88589ea6b6e842f3f320&quot;,
  &quot;version&quot;: 1,
  &quot;size&quot;: 151,
  &quot;vsize&quot;: 100,
  &quot;weight&quot;: 397,
  &quot;locktime&quot;: 0,
  &quot;vin&quot;: [
    {
      &quot;txid&quot;: &quot;57471c063fdcdc45c84cbad30b3cf23ea11eea5c6e64619f79e5667367f15481&quot;,
      &quot;vout&quot;: 1,
      &quot;scriptSig&quot;: {
        &quot;asm&quot;: &quot;&quot;,
        &quot;hex&quot;: &quot;&quot;
      },
      &quot;txinwitness&quot;: [
        &quot;f33812c31e4c1ae9a46c55e1463121d42c1c5cd64afc86fe94d2a126b2e515823954b079e11bf1251aa9d585cf4a83a0473634f1c22891249dacf9f93c976ff401&quot;
      ],
      &quot;sequence&quot;: 4294967295
    }
  ],
  &quot;vout&quot;: [
    {
      &quot;value&quot;: 0.02644483,
      &quot;n&quot;: 0,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;0 86918c31af903a4c7ed2dc8191156c0ef44a11b4&quot;,
        &quot;desc&quot;: &quot;addr(bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs)#h6rkqret&quot;,
        &quot;hex&quot;: &quot;001486918c31af903a4c7ed2dc8191156c0ef44a11b4&quot;,
        &quot;address&quot;: &quot;bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs&quot;,
        &quot;type&quot;: &quot;witness_v0_keyhash&quot;
      }
    }
  ]
}
</code></pre>
<p>Among many other things, this also decodes the transaction's output scripts (under <code>scriptPubKey</code>). However, if you have a serialized script outside the context of a transaction, which in this case would be <code>001486918c31af903a4c7ed2dc8191156c0ef44a11b4</code>, you can use <code>decodescript</code> on it:</p>
<pre><code>{
  &quot;asm&quot;: &quot;0 86918c31af903a4c7ed2dc8191156c0ef44a11b4&quot;,
  &quot;desc&quot;: &quot;addr(bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs)#h6rkqret&quot;,
  &quot;address&quot;: &quot;bc1qs6gccvd0jqayclkjmjqez9tvpm6y5yd5azp9qs&quot;,
  &quot;type&quot;: &quot;witness_v0_keyhash&quot;,
  &quot;p2sh&quot;: &quot;3Np3NnSFbrdMjRi9ECcnoWCfHBPVVHe5iw&quot;
}
</code></pre>
<p>Additionally, there are scripts that <code>decoderawtransaction</code> won't decode, for example redeem scripts in witnesses of P2WSH inputs.</p>
<p>Take transaction <a href="https://mempool.space/tx/b4bb98f79c50ef7fe74fb9fac705e3952650161477bd82b2687c953b71a44594" rel="nofollow noreferrer">b4bb98f79c50ef7fe74fb9fac705e3952650161477bd82b2687c953b71a44594</a> which spends one P2WSH input with a 2-of-3 multisignature script. The serialized script will be given by <code>decoderawtransaction</code> as the last item of <code>txinwitness</code>:</p>
<p><code>5221020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a210211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db856760082102e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf57253ae</code></p>
<p>Decoded by <code>decodescript</code> as:</p>
<pre><code>{
  &quot;asm&quot;: &quot;2 020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a 0211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db85676008 02e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf572 3 OP_CHECKMULTISIG&quot;,
  &quot;desc&quot;: &quot;multi(2,020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a,0211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db85676008,02e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf572)#d63cpu6r&quot;,
  &quot;type&quot;: &quot;multisig&quot;,
  &quot;p2sh&quot;: &quot;37CYRhHBjdgoTC7KQpUuTH3zAcm9akWqAd&quot;,
  &quot;segwit&quot;: {
    &quot;asm&quot;: &quot;0 ec21a1b4e8cdaa89737895168ead0c3210fc2d1c6bf9805e45a475f714c75140&quot;,
    &quot;desc&quot;: &quot;addr(bc1qass6rd8gek4gjumcj5tgatgvxgg0ctgud0ucqhj9536lw9x829qqpnu64e)#2wsw9unx&quot;,
    &quot;hex&quot;: &quot;0020ec21a1b4e8cdaa89737895168ead0c3210fc2d1c6bf9805e45a475f714c75140&quot;,
    &quot;address&quot;: &quot;bc1qass6rd8gek4gjumcj5tgatgvxgg0ctgud0ucqhj9536lw9x829qqpnu64e&quot;,
    &quot;type&quot;: &quot;witness_v0_scripthash&quot;,
    &quot;p2sh-segwit&quot;: &quot;3PCgTmZsCjK6KHJeEHJ7iCjVPqb3ygf5jF&quot;
  }
}
</code></pre>

CC BY-SA 4.0
2023-01-26T08:41:46.587Z
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!
greencappybara
CC BY-SA 4.0
2023-01-26T09:04:12.677Z
No, a transaction consists of a lot more than just scripts, so `decodescript` cannot do everything. I added an example of a P2WSH script.
Vojtěch Strnad
CC BY-SA 4.0
2023-01-31T03:24:51.993Z
Sorry, how did you acquire `5221020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a210211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db856760082102e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf57253ae`?
greencappybara
CC BY-SA 4.0
2023-01-31T03:49:23.297Z
Is `decodescript` typically used to decode ScriptPubKey & ScriptSig as well?
greencappybara
CC BY-SA 4.0
2023-01-26T07:27:14.340Z
2023-01-26T09:03:42.123Z
2023-01-26T09:03:42.123Z
Vojtěch Strnad
<p>I'm looking for an explanation on the difference between the bitcoin CLI for <code>decoderawtransaction</code> and <code>decodescript</code> please.</p>
<p>From what I know, <code>decodetransaction</code> 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 &amp; the original address. How is this different from <code>decodescript</code> &amp; when should I be using one vs the other?</p>
<p>Thanks.</p>

<transactions><script><bitcoin-cli>
What's the difference between decoderawtransaction and decodescript?
greencappybara
# What's the difference between decoderawtransaction and decodescript?

(Asked by: username_0 on 2023-01-26)

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.

**Tags:** transactions, script, bitcoin-cli


---

## 1 answers

### Answer by username_1 on 2023-01-26. Score: 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"
  }
}

> **Comments:**
> **username_0**: 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!  
> **username_1**: No, a transaction consists of a lot more than just scripts, so `decodescript` cannot do everything. I added an example of a P2WSH script.  
> **username_0**: Sorry, how did you acquire `5221020fc49e0380f0ba7d849609e746e36557d1ad457cb88888a73fdbd9a7d7ab3b6a210211bcd0fc487c5ed1c60adb5379c9b078874b4e525a4df2c27540c9db856760082102e8033bc9956afcd3abba9cfe0817ab16c5ec84e2f8edc1da4373bac54dacf57253ae`?  
> **username_0**: Is `decodescript` typically used to decode ScriptPubKey & ScriptSig as well?  

---

