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)"}
// 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});
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:
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}
);