<p>It may be that you get <code>No script found for input #0.</code> because your</p>
<pre><code>    .addInput({
        hash: unspent.txId,
        index: unspent.vout,
    })
</code></pre>
<p>omits the details of the previous transaction from which the library can construct the unlocking script?</p>
<p>Very very sadly, the only documentation for bitcoinjs appears to be the <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts" rel="nofollow noreferrer">examples</a>. What I believe may be the relevant example has a <code>nonWitnessUtxo:</code> data value specified.</p>
<pre><code>psbt.addInput({
  // if hash is string, txid, if hash is Buffer, is reversed compared to txid
  hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
  index: 0,
  sequence: 0xffffffff, // These are defaults. This line is not needed.

  // non-segwit inputs now require passing the whole previous tx as Buffer
  nonWitnessUtxo: Buffer.from(
    '0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
      '452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
      'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
      '9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
      '631e5e1e66009ce3710ceea5b1ad13ffffffff01' +
      // value in satoshis (Int64LE) = 0x015f90 = 90000
      '905f010000000000' +
      // scriptPubkey length
      '19' +
      // scriptPubkey
      '76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac' +
      // locktime
      '00000000',
    'hex',
  ),
</code></pre>
<p>That second comment looks important.</p>
<p>Obviously you'd fetch that prior transaction data rather than including it as literal constants.</p>
<p>If I were using bitcoinjs-lib, I would try to inspect exactly what is in tx.toHex() value just before it is broadcast.</p>

CC BY-SA 4.0
2024-01-10T21:40:49.520Z
2024-01-10T21:45:51.313Z
2024-01-10T21:45:51.313Z
RedGrittyBrick
<p>I tried to make a simple btc transfer but i got this error: No script found for input #0. What did i miss?</p>
<pre><code>const bitcoin = require(&quot;bitcoinjs-lib&quot;);
const ECPairFactory = require(&quot;ecpair&quot;);
const ecc = require(&quot;tiny-secp256k1&quot;);
const { RegtestUtils } = require(&quot;regtest-client&quot;);

module.exports = async function (req, res) {
    const regtestUtils = new RegtestUtils({
        APIURL: &quot;https://regtest.bitbank.cc/1&quot;,
        APIPASS: &quot;satoshi&quot;,
    });

    const network = regtestUtils.network; // regtest network params

    const ECPair = ECPairFactory.ECPairFactory(ecc);
    const keyPair = ECPair.fromWIF(
        &quot;xxxxxxxx&quot;
    );

    const p2pkh = bitcoin.payments.p2pkh({
        pubkey: keyPair.publicKey,
        network,
    });

    const unspent = await regtestUtils.faucet(p2pkh.address, 2e4);

    const txb = new bitcoin.Psbt({ network });
    txb.setVersion(2)
        .setLocktime(0)
        .addInput({
            hash: unspent.txId,
            index: unspent.vout,
        })
        .addOutput({
            address: regtestUtils.RANDOM_ADDRESS,
            value: 1e4,
        })
        .signAllInputs(keyPair);

    const tx = txb.build();

    console.log(&quot;hash&quot;, tx.toHex());

    return;

    // build and broadcast to the Bitcoin Local RegTest server
    await regtestUtils.broadcast(tx.toHex());

    // This verifies that the vout output of txId transaction is actually for value
    // in satoshis and is locked for the address given.
    // The utxo can be unconfirmed. We are just verifying it was at least placed in
    // the mempool.
    await regtestUtils.verify({
        txId: tx.getId(),
        address: regtestUtils.RANDOM_ADDRESS,
        vout: 0,
        value: 1e4,
    });

    res.status(400).json({
        error: false,
        message: address,
    });
};
</code></pre>

<bitcoinjs>
Transaction sending failed (No script found for input #0)
István Tóth
# Transaction sending failed (No script found for input #0)

(Asked by: username_0 on 2024-01-10)

I tried to make a simple btc transfer but i got this error: No script found for input #0. What did i miss?
const bitcoin = require("bitcoinjs-lib");
const ECPairFactory = require("ecpair");
const ecc = require("tiny-secp256k1");
const { RegtestUtils } = require("regtest-client");

module.exports = async function (req, res) {
    const regtestUtils = new RegtestUtils({
        APIURL: "https://regtest.bitbank.cc/1",
        APIPASS: "satoshi",
    });

    const network = regtestUtils.network; // regtest network params

    const ECPair = ECPairFactory.ECPairFactory(ecc);
    const keyPair = ECPair.fromWIF(
        "xxxxxxxx"
    );

    const p2pkh = bitcoin.payments.p2pkh({
        pubkey: keyPair.publicKey,
        network,
    });

    const unspent = await regtestUtils.faucet(p2pkh.address, 2e4);

    const txb = new bitcoin.Psbt({ network });
    txb.setVersion(2)
        .setLocktime(0)
        .addInput({
            hash: unspent.txId,
            index: unspent.vout,
        })
        .addOutput({
            address: regtestUtils.RANDOM_ADDRESS,
            value: 1e4,
        })
        .signAllInputs(keyPair);

    const tx = txb.build();

    console.log("hash", tx.toHex());

    return;

    // build and broadcast to the Bitcoin Local RegTest server
    await regtestUtils.broadcast(tx.toHex());

    // This verifies that the vout output of txId transaction is actually for value
    // in satoshis and is locked for the address given.
    // The utxo can be unconfirmed. We are just verifying it was at least placed in
    // the mempool.
    await regtestUtils.verify({
        txId: tx.getId(),
        address: regtestUtils.RANDOM_ADDRESS,
        vout: 0,
        value: 1e4,
    });

    res.status(400).json({
        error: false,
        message: address,
    });
};

**Tags:** bitcoinjs


---

## 1 answers

### Answer by username_1 on 2024-01-10. Score: 1

It may be that you get No script found for input #0. because your
    .addInput({
        hash: unspent.txId,
        index: unspent.vout,
    })

omits the details of the previous transaction from which the library can construct the unlocking script?
Very very sadly, the only documentation for bitcoinjs appears to be the examples. What I believe may be the relevant example has a nonWitnessUtxo: data value specified.
psbt.addInput({
  // if hash is string, txid, if hash is Buffer, is reversed compared to txid
  hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
  index: 0,
  sequence: 0xffffffff, // These are defaults. This line is not needed.

  // non-segwit inputs now require passing the whole previous tx as Buffer
  nonWitnessUtxo: Buffer.from(
    '0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
      '452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
      'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
      '9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
      '631e5e1e66009ce3710ceea5b1ad13ffffffff01' +
      // value in satoshis (Int64LE) = 0x015f90 = 90000
      '905f010000000000' +
      // scriptPubkey length
      '19' +
      // scriptPubkey
      '76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac' +
      // locktime
      '00000000',
    'hex',
  ),

That second comment looks important.
Obviously you'd fetch that prior transaction data rather than including it as literal constants.
If I were using bitcoinjs-lib, I would try to inspect exactly what is in tx.toHex() value just before it is broadcast.


---

