3

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();

1 Answer 1

4
+100

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'),
});
1
  • 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
    Commented Aug 9, 2023 at 14:51

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.