<p>In P2SH, the output script is created by applying HASH160 (SHA256+RIPEMD160) to the redeem script, which is then revealed at spend time. So if you created the previous output, you should know what the script was.</p>
<p>However, since the address <a href="https://mempool.space/address/3PA2J9aN9N82q4YZyhBYHBcHpVvpGewvX2" rel="nofollow noreferrer">3PA2J9aN9N82q4YZyhBYHBcHpVvpGewvX2</a> has already been spent from before, we can look at the blockchain and see the script is</p>
<pre><code>OP_0
OP_PUSHBYTES_20 01868bd1fb92e6547438e8bf07a6c08ee0157c8b
</code></pre>
<p>or <code>001401868bd1fb92e6547438e8bf07a6c08ee0157c8b</code> in hexadecimal form.</p>
<p>Your code should therefore be:</p>
<pre class="lang-js prettyprint-override"><code>psbt.addInput({
    hash: &quot;91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f&quot;,
    index: 1,
    witnessUtxo: {
        script: Buffer.from(&quot;a914eb762ef1946c2d462764d30112ce27c531fdd60387&quot;, 'hex'),
        value: 894242,
    },
    redeemScript: Buffer.from(&quot;001401868bd1fb92e6547438e8bf07a6c08ee0157c8b&quot;, 'hex'),
});
</code></pre>

CC BY-SA 4.0
2023-08-09T14:51:07.870Z
Is there a good tutorial where I can better understand this process? I'm having the same problem with another transaction and I'm not understanding how to resolve it.
Berry Blue
CC BY-SA 4.0
2023-07-19T09:28:17.377Z
2023-07-19T09:28:17.377Z
Vojtěch Strnad
<p>I'm trying to build a transaction using bitcoinjs-lib.</p>
<p>Here is my last transaction.
<a href="https://blockstream.info/api/tx/91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f" rel="nofollow noreferrer">https://blockstream.info/api/tx/91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f</a></p>
<pre class="lang-json prettyprint-override"><code>{
  &quot;weight&quot; : 705,
  &quot;txid&quot; : &quot;91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f&quot;,
  &quot;status&quot; : {
    &quot;block_height&quot; : 795582,
    &quot;block_hash&quot; : &quot;00000000000000000005386018b10cd1cde59d6923fd918757690c004b01c4b8&quot;,
    &quot;confirmed&quot; : true,
    &quot;block_time&quot; : 1687529469
  },
  &quot;vin&quot; : [
    {
      &quot;inner_redeemscript_asm&quot; : &quot;OP_0 OP_PUSHBYTES_20 01868bd1fb92e6547438e8bf07a6c08ee0157c8b&quot;,
      &quot;txid&quot; : &quot;e0c545a6dfaf1d5bb67f6349190d0653b597bda6585696cf217e55df07b1f190&quot;,
      &quot;prevout&quot; : {
        &quot;scriptpubkey_type&quot; : &quot;p2sh&quot;,
        &quot;scriptpubkey_address&quot; : &quot;3PA2J9aN9N82q4YZyhBYHBcHpVvpGewvX2&quot;,
        &quot;value&quot; : 956656,
        &quot;scriptpubkey_asm&quot; : &quot;OP_HASH160 OP_PUSHBYTES_20 eb762ef1946c2d462764d30112ce27c531fdd603 OP_EQUAL&quot;,
        &quot;scriptpubkey&quot; : &quot;a914eb762ef1946c2d462764d30112ce27c531fdd60387&quot;
      },
      &quot;scriptsig_asm&quot; : &quot;OP_PUSHBYTES_22 001401868bd1fb92e6547438e8bf07a6c08ee0157c8b&quot;,
      &quot;is_coinbase&quot; : false,
      &quot;vout&quot; : 1,
      &quot;sequence&quot; : 4294967295,
      &quot;witness&quot; : [
        &quot;304402206000ff05c68afd2b2aece9554ef25bae6d7402be96c6eedc866f4ca1dcafe0dd02204dfc8852bd4c4ae2f36cd0f4db82d22e37422b08e7d829f10e5aa9d8ae07d70901&quot;,
        &quot;02dba3c42f38c394dd7370884f23bd0dec5373e9d38439915124188f06b6a19395&quot;
      ],
      &quot;scriptsig&quot; : &quot;16001401868bd1fb92e6547438e8bf07a6c08ee0157c8b&quot;
    }
  ],
  &quot;vout&quot; : [
    {
      &quot;scriptpubkey_type&quot; : &quot;v1_p2tr&quot;,
      &quot;scriptpubkey_address&quot; : &quot;bc1p7cqr7rhvxy8c69k5ks3qp6h3nq85tskgxd4f0h0mz5g3lvtykgtq4ddhyc&quot;,
      &quot;value&quot; : 54980,
      &quot;scriptpubkey_asm&quot; : &quot;OP_PUSHNUM_1 OP_PUSHBYTES_32 f6003f0eec310f8d16d4b42200eaf1980f45c2c8336a97ddfb15111fb164b216&quot;,
      &quot;scriptpubkey&quot; : &quot;5120f6003f0eec310f8d16d4b42200eaf1980f45c2c8336a97ddfb15111fb164b216&quot;
    },
    {
      &quot;scriptpubkey_type&quot; : &quot;p2sh&quot;,
      &quot;scriptpubkey_address&quot; : &quot;3PA2J9aN9N82q4YZyhBYHBcHpVvpGewvX2&quot;,
      &quot;value&quot; : 894242,
      &quot;scriptpubkey_asm&quot; : &quot;OP_HASH160 OP_PUSHBYTES_20 eb762ef1946c2d462764d30112ce27c531fdd603 OP_EQUAL&quot;,
      &quot;scriptpubkey&quot; : &quot;a914eb762ef1946c2d462764d30112ce27c531fdd60387&quot;
    }
  ],
  &quot;size&quot; : 258,
  &quot;locktime&quot; : 0,
  &quot;version&quot; : 2,
  &quot;fee&quot; : 7434
}
</code></pre>
<p>Here is the code I'm working on. What do I use for the redeem script?</p>
<pre class="lang-js prettyprint-override"><code>const network = bitcoin.networks.bitcoin;
const psbt = new bitcoin.Psbt({ network });

psbt.addInput({
    hash: &quot;91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f&quot;,
    index: 1,
    witnessUtxo: {
        script: Buffer.from(&quot;a914eb762ef1946c2d462764d30112ce27c531fdd60387&quot;, 'hex'),
        value: 894242,
    },
    redeemScript: Buffer.from(&quot;???&quot;, 'hex'),
});

psbt.addOutput({
    address: to_address,
    value: Math.floor(amount * 1e8),
});

psbt.signInput(0, keyPair);
psbt.finalizeInput(0);
const tx = psbt.extractTransaction();
return tx.toHex();
</code></pre>

<transactions><p2sh><javascript><bitcoinjs><p2sh-p2wpkh>
How to build a transaction using bitcoinjs-lib
Berry Blue
# How to build a transaction using bitcoinjs-lib

(Asked by: username_0 on 2023-07-17)

I'm trying to build a transaction using bitcoinjs-lib.
Here is my last transaction.
https://blockstream.info/api/tx/91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f
{
  "weight" : 705,
  "txid" : "91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f",
  "status" : {
    "block_height" : 795582,
    "block_hash" : "00000000000000000005386018b10cd1cde59d6923fd918757690c004b01c4b8",
    "confirmed" : true,
    "block_time" : 1687529469
  },
  "vin" : [
    {
      "inner_redeemscript_asm" : "OP_0 OP_PUSHBYTES_20 01868bd1fb92e6547438e8bf07a6c08ee0157c8b",
      "txid" : "e0c545a6dfaf1d5bb67f6349190d0653b597bda6585696cf217e55df07b1f190",
      "prevout" : {
        "scriptpubkey_type" : "p2sh",
        "scriptpubkey_address" : "3PA2J9aN9N82q4YZyhBYHBcHpVvpGewvX2",
        "value" : 956656,
        "scriptpubkey_asm" : "OP_HASH160 OP_PUSHBYTES_20 eb762ef1946c2d462764d30112ce27c531fdd603 OP_EQUAL",
        "scriptpubkey" : "a914eb762ef1946c2d462764d30112ce27c531fdd60387"
      },
      "scriptsig_asm" : "OP_PUSHBYTES_22 001401868bd1fb92e6547438e8bf07a6c08ee0157c8b",
      "is_coinbase" : false,
      "vout" : 1,
      "sequence" : 4294967295,
      "witness" : [
        "304402206000ff05c68afd2b2aece9554ef25bae6d7402be96c6eedc866f4ca1dcafe0dd02204dfc8852bd4c4ae2f36cd0f4db82d22e37422b08e7d829f10e5aa9d8ae07d70901",
        "02dba3c42f38c394dd7370884f23bd0dec5373e9d38439915124188f06b6a19395"
      ],
      "scriptsig" : "16001401868bd1fb92e6547438e8bf07a6c08ee0157c8b"
    }
  ],
  "vout" : [
    {
      "scriptpubkey_type" : "v1_p2tr",
      "scriptpubkey_address" : "bc1p7cqr7rhvxy8c69k5ks3qp6h3nq85tskgxd4f0h0mz5g3lvtykgtq4ddhyc",
      "value" : 54980,
      "scriptpubkey_asm" : "OP_PUSHNUM_1 OP_PUSHBYTES_32 f6003f0eec310f8d16d4b42200eaf1980f45c2c8336a97ddfb15111fb164b216",
      "scriptpubkey" : "5120f6003f0eec310f8d16d4b42200eaf1980f45c2c8336a97ddfb15111fb164b216"
    },
    {
      "scriptpubkey_type" : "p2sh",
      "scriptpubkey_address" : "3PA2J9aN9N82q4YZyhBYHBcHpVvpGewvX2",
      "value" : 894242,
      "scriptpubkey_asm" : "OP_HASH160 OP_PUSHBYTES_20 eb762ef1946c2d462764d30112ce27c531fdd603 OP_EQUAL",
      "scriptpubkey" : "a914eb762ef1946c2d462764d30112ce27c531fdd60387"
    }
  ],
  "size" : 258,
  "locktime" : 0,
  "version" : 2,
  "fee" : 7434
}

Here is the code I'm working on. What do I use for the redeem script?
const network = bitcoin.networks.bitcoin;
const psbt = new bitcoin.Psbt({ network });

psbt.addInput({
    hash: "91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f",
    index: 1,
    witnessUtxo: {
        script: Buffer.from("a914eb762ef1946c2d462764d30112ce27c531fdd60387", 'hex'),
        value: 894242,
    },
    redeemScript: Buffer.from("???", 'hex'),
});

psbt.addOutput({
    address: to_address,
    value: Math.floor(amount * 1e8),
});

psbt.signInput(0, keyPair);
psbt.finalizeInput(0);
const tx = psbt.extractTransaction();
return tx.toHex();

**Tags:** transactions, p2sh, javascript, bitcoinjs, p2sh-p2wpkh


---

## 1 answers

### Answer by username_1 on 2023-07-19. Score: 4 | Accepted answer

In P2SH, the output script is created by applying HASH160 (SHA256+RIPEMD160) to the redeem script, which is then revealed at spend time. So if you created the previous output, you should know what the script was.
However, since the address 3PA2J9aN9N82q4YZyhBYHBcHpVvpGewvX2 has already been spent from before, we can look at the blockchain and see the script is
OP_0
OP_PUSHBYTES_20 01868bd1fb92e6547438e8bf07a6c08ee0157c8b

or 001401868bd1fb92e6547438e8bf07a6c08ee0157c8b in hexadecimal form.
Your code should therefore be:
psbt.addInput({
    hash: "91feea5f5c7b67b3e56c0f5d23d6da3065855c23b4cc7dded8efd8ff8065500f",
    index: 1,
    witnessUtxo: {
        script: Buffer.from("a914eb762ef1946c2d462764d30112ce27c531fdd60387", 'hex'),
        value: 894242,
    },
    redeemScript: Buffer.from("001401868bd1fb92e6547438e8bf07a6c08ee0157c8b", 'hex'),
});

> **Comments:**
> **username_0**: Is there a good tutorial where I can better understand this process? I'm having the same problem with another transaction and I'm not understanding how to resolve it.  

---

