ChrisCho-H / bitcoin-sdk-js

Bitcoin TypeScript/JavaScript Library for NodeJS, Browser and Mobile. Segwit & Taproot support.✨
MIT License
32 stars 4 forks source link

Transaction for Single(or Multi) Signer with TimeLock (or || and) HashLock(P2SH, P2WSH) | Script failed an OP_EQUALVERIFY operation #6

Closed omariosman closed 1 month ago

omariosman commented 1 month ago

The issue

I am trying to create Transaction for Single Signer with TimeLock and HashLock(P2SH, P2WSH) However when I try to broadcast the tx to https://blockstream.info/testnet/tx/push it gives me this error: sendrawtransaction RPC error: {"code":-26,"message":"mandatory-script-verify-flag-failed (Script failed an OP_EQUALVERIFY operation)"}

Can you inspect what is wrong with my script?

Tx raw:

010000000168f1479d7e6b2b67fde3c227324f18d93a5b0620a11fb872863891fe4e35c0ab00000000c1483045022100cb7388b758f102e83f8abffa05db679208dfb7a0594a5603e1db6359cf47a98d02203d689d39531dc45fef5ff3cb2fcaf0e9aff075e76bd014eaf1b4fd6d20c731c60103abcdef004c716303a4bd2db175210390bc63a657102d0a68f2daa6934475f4f9093996c2293f77800d36fdb5ed616067aa204533a01d26697df306b3380e08f4fae30f488d2985e6449e9bd9bd86849ddbc6882103e5a00cdb4a5a432bde9a39d0c71bac483f9c587974474e0ce5e7c5cb0f0bdaf668acfdffffff01d40200000000000017a914f0fd1141ff6ae934c08c7e41adf36da2867e10b48700000000

Tx decoded:

    "txid": "791cd85233e6827c321c76e67806aabd5b597e90df8de559fb95865e5601997e",
    "hash": "791cd85233e6827c321c76e67806aabd5b597e90df8de559fb95865e5601997e",
    "version": 1,
    "size": 276,
    "vsize": 276,
    "weight": 1104,
    "locktime": 0,
    "vin": [
        {
            "txid": "abc0354efe91388672b81fa120065b3ad9184f3227c2e3fd672b6b7e9d47f168",
            "vout": 0,
            "scriptSig": {
                "asm": "3045022100cb7388b758f102e83f8abffa05db679208dfb7a0594a5603e1db6359cf47a98d02203d689d39531dc45fef5ff3cb2fcaf0e9aff075e76bd014eaf1b4fd6d20c731c6[ALL] -7327147 0 6303a4bd2db175210390bc63a657102d0a68f2daa6934475f4f9093996c2293f77800d36fdb5ed616067aa204533a01d26697df306b3380e08f4fae30f488d2985e6449e9bd9bd86849ddbc6882103e5a00cdb4a5a432bde9a39d0c71bac483f9c587974474e0ce5e7c5cb0f0bdaf668ac",
                "hex": "483045022100cb7388b758f102e83f8abffa05db679208dfb7a0594a5603e1db6359cf47a98d02203d689d39531dc45fef5ff3cb2fcaf0e9aff075e76bd014eaf1b4fd6d20c731c60103abcdef004c716303a4bd2db175210390bc63a657102d0a68f2daa6934475f4f9093996c2293f77800d36fdb5ed616067aa204533a01d26697df306b3380e08f4fae30f488d2985e6449e9bd9bd86849ddbc6882103e5a00cdb4a5a432bde9a39d0c71bac483f9c587974474e0ce5e7c5cb0f0bdaf668ac"
            },
            "sequence": 4294967293
        }
    ],
    "vout": [
        {
            "value": 0.00000724,
            "n": 0,
            "scriptPubKey": {
                "asm": "OP_HASH160 f0fd1141ff6ae934c08c7e41adf36da2867e10b4 OP_EQUAL",
                "desc": "addr(3PfFG1qM8L371kdKsBDCQDhnrawGhm8CUF)#wn2e2eje",
                "hex": "a914f0fd1141ff6ae934c08c7e41adf36da2867e10b487",
                "address": "3PfFG1qM8L371kdKsBDCQDhnrawGhm8CUF",
                "type": "scripthash"
            }
        }
    ]
}```

### Script:

import * as bitcoin from 'bitcoin-sdk-js';

const receiverPubkey = "03e5a00cdb4a5a432bde9a39d0c71bac483f9c587974474e0ce5e7c5cb0f0bdaf6"; const privKey = "cef60d39c08e79bf442e708491011384fd88ca8b5a56f2f64097578e3e677bb8";

// initialize Bitcoin Transaction object const tx = new bitcoin.Transaction();

const txId = "abc0354efe91388672b81fa120065b3ad9184f3227c2e3fd672b6b7e9d47f168"; const value = 1000; const BLOCK_HEIGHT = 2997668; const fee = 276;

// add UTXO to spend as an input await tx.addInput({ txHash: txId, // transaction id of utxo index: 0, // index of utxo in transaction value: value, // value of utxo(unit is satoshi) } as bitcoin.UTXO);

// sends bitcoin to single(or multi) sig script with timelock + hashlock await tx.addOutput({ address: await bitcoin.address.generateScriptAddress( / WATCH OUT! Order matters. You must place script in the order of timelock, hashlock and single(or multi) sig to spend this output with bitcoin-sdk-js / (await bitcoin.script.generateTimeLockScript(BLOCK_HEIGHT)) + (await bitcoin.script.generateHashLockScript("abcdef")) + // you can use generateMultiSigScript instead of single sig here (await bitcoin.script.generateSingleSigScript(receiverPubkey, "legacy")), "legacy", "testnet" ), value: value - fee, // value of utxo - fee } as bitcoin.Target);

// if transaction use timelock input, must set tx locktime bigger than input timelock await tx.setLocktime(BLOCK_HEIGHT);

// // if input utxo requires single sig with smart contract(p2wsh or p2sh) await tx.signInput( privKey, 0, // input index to sign 'legacy', // default is segwit, you might use legacy if necessary await bitcoin.script.generateTimeLockScript(BLOCK_HEIGHT), // is timelock input? provide script 'abcdef', // is hashlock input? provide secretHex to unlock );

// You can broadcast signed tx here: https://blockstream.info/testnet/tx/push const txToBroadcast: string = await tx.getSignedHex(); console.log(${txToBroadcast});