Sunday, December 22, 2024

bitcoinjs – Transaction sending failed (No script discovered for enter #0)

I attempted to make a easy btc switch however i obtained this error: No script discovered for enter #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 operate (req, res) {
    const regtestUtils = new RegtestUtils({
        APIURL: "https://regtest.bitbank.cc/1",
        APIPASS: "satoshi",
    });

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

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

    const p2pkh = bitcoin.funds.p2pkh({
        pubkey: keyPair.publicKey,
        community,
    });

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

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

    const tx = txb.construct();

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

    return;

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

    // This verifies that the vout output of txId transaction is definitely for worth
    // in satoshis and is locked for the handle given.
    // The utxo could be unconfirmed. We're simply verifying it was not less than positioned in
    // the mempool.
    await regtestUtils.confirm({
        txId: tx.getId(),
        handle: regtestUtils.RANDOM_ADDRESS,
        vout: 0,
        worth: 1e4,
    });

    res.standing(400).json({
        error: false,
        message: handle,
    });
};

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles