<p>To convert the response from bitcoin-cli <code>decoderawtransaction</code> to the script format you mention(<code>scriptSig</code> which unlocks <code>scriptPubKey</code>),</p>
<p>For segwit, you can get <code>scriptSig</code> part from <code>txinwitness</code> nested inside <code>vin</code>, which consists of signature <code>30440220711e2049f666f750f4b1816ca32c0db56de99e5f461fd454f209dae8e274e76f02206e5870f1f52ad70674a6a810218de0c15c45b153cb8b2a0ec2dc33a3f23dc14f01</code> and public key <code>030713ff1cf6035cbaf5eb4a2f27d99214b72313f47276047cee2a7da5f35693b1</code> here. If it's not segwit it would be located in <code>scriptSig</code> part.</p>
<p>To get <code>scriptPubKey</code> which is unlocked by <code>vin</code>, you should command <code>decoderawtransaction</code> again with other raw transaction hex, of which <code>vout</code> utxo matches <code>vin</code>, because <code>scriptPubKey</code> info is included inside <code>vout</code> only. You can get raw transaction hex by <code>getrawtransaction</code> with <code>txid</code>, which is <code>d26bb78f9fa8b1e3f0b711af2861a3e02d579be61bc6efc3a84ed7bd82589466</code> here(you can find in <code>vin</code>).</p>
<p>For segwit V0, script pub key itself would be <code>OP_0 OP_PUSHBYTES_20 &lt;key-hash&gt;</code> or <code>OP_0 OP_PUSHBYTES_32 &lt;script-hash&gt;</code>. If key-hash(p2wpkh), script format you want would be <code>OP_DUP OP_HASH160 &lt;key-hash&gt; OP_EQUALVERIFY OP_CHECKSIG</code>, else if script-hash(p2wsh), <code>OP_SHA256 &lt;script-hash&gt;</code>.</p>

CC BY-SA 4.0
2024-06-21T03:23:02.703Z
2024-06-21T05:12:10.887Z
2024-06-21T05:12:10.887Z
Hyunhum Cho
<p>My main problem is understanding how to convert the response from <code>bitcoin-cli &quot;decoderawtransaction&quot;</code> to the script format i am supposed to put in the debugginng tool. i.e my problem is understanding how to build the bitcoin script from the decoded transaction especially when trying to spend a segwit innput</p>
<p>For example how do i convert something like this</p>
<pre><code>{
&quot;txid&quot;: &quot;159cdd5017ff7d2c4e1d5cd37793a125b8545b9bc99eb9cbec427978d11956c7&quot;,
&quot;hash&quot;: &quot;311d68d33ae87866a9d1e5482b8311bd2e9f9199f652747734b2515dc43bd835&quot;,
&quot;version&quot;: 1,
&quot;size&quot;: 222,
&quot;vsize&quot;: 141,
&quot;weight&quot;: 561,
&quot;locktime&quot;: 0,
&quot;vin&quot;: [
    {
        &quot;txid&quot;: &quot;d26bb78f9fa8b1e3f0b711af2861a3e02d579be61bc6efc3a84ed7bd82589466&quot;,
        &quot;vout&quot;: 0,
        &quot;scriptSig&quot;: {
            &quot;asm&quot;: &quot;&quot;,
            &quot;hex&quot;: &quot;&quot;
        },
        &quot;txinwitness&quot;: [
            &quot;30440220711e2049f666f750f4b1816ca32c0db56de99e5f461fd454f209dae8e274e76f02206e5870f1f52ad70674a6a810218de0c15c45b153cb8b2a0ec2dc33a3f23dc14f01&quot;,
            &quot;030713ff1cf6035cbaf5eb4a2f27d99214b72313f47276047cee2a7da5f35693b1&quot;
        ],
        &quot;sequence&quot;: 4294967295
    }
],
&quot;vout&quot;: [
    {
        &quot;value&quot;: 0.0005,
        &quot;n&quot;: 0,
        &quot;scriptPubKey&quot;: {
            &quot;asm&quot;: &quot;0 27b107c892a49b83f76c1e6c5d5860ba36d5da7f&quot;,
            &quot;hex&quot;: &quot;001427b107c892a49b83f76c1e6c5d5860ba36d5da7f&quot;,
            &quot;reqSigs&quot;: 1,
            &quot;type&quot;: &quot;witness_v0_keyhash&quot;,
            &quot;addresses&quot;: [
                &quot;bc1qy7cs0jyj5jdc8amvrek96krqhgmdtknljra09w&quot;
            ]
        }
    },
    {
        &quot;value&quot;: 0.00019448,
        &quot;n&quot;: 1,
        &quot;scriptPubKey&quot;: {
            &quot;asm&quot;: &quot;0 2d5704072aa356ee73ce7dbcdcd77a56da29661f&quot;,
            &quot;hex&quot;: &quot;00142d5704072aa356ee73ce7dbcdcd77a56da29661f&quot;,
            &quot;reqSigs&quot;: 1,
            &quot;type&quot;: &quot;witness_v0_keyhash&quot;,
            &quot;addresses&quot;: [
                &quot;bc1q94tsgpe25dtwuu7w0k7de4m62mdzjesle4zjex&quot;
            ]
        }
    }
]
</code></pre>
<p>}</p>
<p>into something like this</p>
<pre><code>    30450221009f61f453f44e807fdc538ca21710393d34005dd5709dabd8b6b9ccf09ea0b36a0220420d91a33f43d7b979471220e12cd1025975bd2e6c0bf2a14eb65ad89d578104 
02de8f92034b9b3c956c1896d23a628537561e29faa772438aac2265c91ede6519
OP_DUP
OP_HASH160
393164d26cc8a065cb4915f46f6346d63ae26075
OP_EQUALVERIFY OP_CHECKSIG  
</code></pre>
<p>Can someone please explain this or point me to a reading that explains this?</p>

CC BY-SA 4.0
2021-02-15T14:01:22.227Z
Does this answer your question? [Decoding ScriptSig](https://bitcoin.stackexchange.com/questions/71867/decoding-scriptsig)
Antoine Poinsot
<script><raw-transaction><transaction-verification>
How do i debug a bitcoin transaction
akinmail
# How do i debug a bitcoin transaction

(Asked by: username_0 on 2021-02-10)

My main problem is understanding how to convert the response from bitcoin-cli "decoderawtransaction" to the script format i am supposed to put in the debugginng tool. i.e my problem is understanding how to build the bitcoin script from the decoded transaction especially when trying to spend a segwit innput
For example how do i convert something like this
{
"txid": "159cdd5017ff7d2c4e1d5cd37793a125b8545b9bc99eb9cbec427978d11956c7",
"hash": "311d68d33ae87866a9d1e5482b8311bd2e9f9199f652747734b2515dc43bd835",
"version": 1,
"size": 222,
"vsize": 141,
"weight": 561,
"locktime": 0,
"vin": [
    {
        "txid": "d26bb78f9fa8b1e3f0b711af2861a3e02d579be61bc6efc3a84ed7bd82589466",
        "vout": 0,
        "scriptSig": {
            "asm": "",
            "hex": ""
        },
        "txinwitness": [
            "30440220711e2049f666f750f4b1816ca32c0db56de99e5f461fd454f209dae8e274e76f02206e5870f1f52ad70674a6a810218de0c15c45b153cb8b2a0ec2dc33a3f23dc14f01",
            "030713ff1cf6035cbaf5eb4a2f27d99214b72313f47276047cee2a7da5f35693b1"
        ],
        "sequence": 4294967295
    }
],
"vout": [
    {
        "value": 0.0005,
        "n": 0,
        "scriptPubKey": {
            "asm": "0 27b107c892a49b83f76c1e6c5d5860ba36d5da7f",
            "hex": "001427b107c892a49b83f76c1e6c5d5860ba36d5da7f",
            "reqSigs": 1,
            "type": "witness_v0_keyhash",
            "addresses": [
                "bc1qy7cs0jyj5jdc8amvrek96krqhgmdtknljra09w"
            ]
        }
    },
    {
        "value": 0.00019448,
        "n": 1,
        "scriptPubKey": {
            "asm": "0 2d5704072aa356ee73ce7dbcdcd77a56da29661f",
            "hex": "00142d5704072aa356ee73ce7dbcdcd77a56da29661f",
            "reqSigs": 1,
            "type": "witness_v0_keyhash",
            "addresses": [
                "bc1q94tsgpe25dtwuu7w0k7de4m62mdzjesle4zjex"
            ]
        }
    }
]

}
into something like this
    30450221009f61f453f44e807fdc538ca21710393d34005dd5709dabd8b6b9ccf09ea0b36a0220420d91a33f43d7b979471220e12cd1025975bd2e6c0bf2a14eb65ad89d578104 
02de8f92034b9b3c956c1896d23a628537561e29faa772438aac2265c91ede6519
OP_DUP
OP_HASH160
393164d26cc8a065cb4915f46f6346d63ae26075
OP_EQUALVERIFY OP_CHECKSIG  

Can someone please explain this or point me to a reading that explains this?

**Tags:** script, raw-transaction, transaction-verification

> **Comments:**
> **username_1**: Does this answer your question? [Decoding ScriptSig](https://bitcoin.stackexchange.com/questions/71867/decoding-scriptsig)  

---

## 1 answers

### Answer by username_2 on 2024-06-21. Score: 1

To convert the response from bitcoin-cli `decoderawtransaction` to the script format you mention(`scriptSig` which unlocks `scriptPubKey`),

For segwit, you can get `scriptSig` part from `txinwitness` nested inside `vin`, which consists of signature `30440220711e2049f666f750f4b1816ca32c0db56de99e5f461fd454f209dae8e274e76f02206e5870f1f52ad70674a6a810218de0c15c45b153cb8b2a0ec2dc33a3f23dc14f01` and public key `030713ff1cf6035cbaf5eb4a2f27d99214b72313f47276047cee2a7da5f35693b1` here. If it's not segwit it would be located in `scriptSig` part.

To get `scriptPubKey` which is unlocked by `vin`, you should command `decoderawtransaction` again with other raw transaction hex, of which `vout` utxo matches `vin`, because `scriptPubKey` info is included inside `vout` only. You can get raw transaction hex by `getrawtransaction` with `txid`, which is `d26bb78f9fa8b1e3f0b711af2861a3e02d579be61bc6efc3a84ed7bd82589466` here(you can find in `vin`).

For segwit V0, script pub key itself would be `OP_0 OP_PUSHBYTES_20`  or `OP_0 OP_PUSHBYTES_32` . If key-hash(p2wpkh), script format you want would be `OP_DUP OP_HASH160  OP_EQUALVERIFY OP_CHECKSIG`, else if script-hash(p2wsh), `OP_SHA256` .


---

