How to spend from a custom P2SH script using bitcore-lib

I could not find anywhere on the web about it. I was hopeless until I noticed that I could, without modifying the library.

First choose a similar class in Bitcore-lib.

  • multisigscripthash (<== suggested) or publickeyhash for P2SH outputs
  • multisig or publickey for bare script outputs (don’t use these, these are nonstandard)

Then copy it to a new file in your project such as this one**. Do not use typescript classes even if your project is of kind Typescript.

Then change require paths. Also require inherit from NPM. As in the boilerplates above, we need to inherit from Transaction.Input

A function such as “buildP2SHPushIn” in the example should produce the redeemscript. Remember, the last element of P2SH redeem scripts is a push of the P2SH script.

And finally use addInput instead of the higher-level function from to add inputs as shown here**


**The examples are for Bitcoin Cash, but they can be used the same way in Bitcoin. Do not copy from them, however!

multi signature – P2SH address as a product of public keys

Assuming the multisig 2 of 3, if I do have extended public keys (xpub) for all 3 cosigners (with proper derivations), can I somewhat get the result address (P2SH)? How to calculate the “script” out of these public keys?

Would be cool to have scheme for doing so or a code/pseudocode in python if possible.

p2sh – Transaction Puzzles, Awareness and Questions

I was just reading about the two following scenarios:

enter image description here

enter image description here

My understanding is that we have two transactions here that don’t follow the usual scriptSig/scriptPubKey situation, and I think transactions like these are sometimes called “contracts”. In both cases, some funds were sent to a bitcoin address, so they are valid UTXO’s floating around in the UTXO set, but instead of the usual situation where someone just needs to provide 0-knowledge-proof that they have private keys associated with that address, something else needs to be done to “unlock the funds”. (find the data, or find a collision).

I have some questions:

  1. How would people (likely power users) even be aware that these funds were out there, and available to be redeemed if they solved the puzzles. Did the creators of the transactions likely announce somewhere (via forums or internet), or could someone with full node / full blockchain build a tool that watches for these types of special PKscripts?
  2. What does it mean in the second photo when it says “Note that while transactions like this are fun, they are not secure, because they do not contain any signatures and thus any transaction attempting to spend them can be replaced with a different transaction sending the funds somewhere else.”. It seems to me if someone solved the puzzle they could immediately send the funds to themselves with the standard PKscript. What does it mean the transaction could be “replaced”?
  3. What kind of wallet or software would a power user claiming these funds be using? Certainly one couldn’t just hop on a ledger s and try to claim these funds with the solution to the puzzle. It seems you would need more freedom to provide the puzzle solution.

script – Invalid p2sh transaction accepted by the network

Why did the transaction was accepted by the network despite not working as intended

The network does not check the validity of output scripts. An output script can contain pretty much any data and the transaction will still be consensus valid. But your transaction is not actually invalid. It’s just like a bare multisig now, but since the pubkeys are hashes, it isn’t.

Why did my input did not go to the desired p2sh address “2N7e6ZAGXoepdVYu2Y8ho7fTf6oxE3j9UwS”

You set the output script to dat_redeem_script_op rather than target_script. So the output script is actually your multisig script rather than the P2SH script you intended.

It is on the testnet but if it was not can I retrieve my satoshis on this tx ?

No. Even if you had done this correctly with P2SH, it would still be unspendable. This is because your “public keys” are actually public key hashes. However you do not use any opcodes to for public key hashing. OP_CHECKMULTISIG takes public keys so it will try to interpret your public key hashes as pubkeys and fail to do so.

If you had constructed the multisig script correctly with public keys, this would be spendable as a bare multisig. It would be spendable in the same way that a P2SH multisig is spendable except you do not provide the multisig script in the input’s scriptSig.

Why were custom scripts made non-standard except if they’re P2SH?

Custom script = nonstandard

P2SH custom script = standard

Why?

Difficulties trying to redeem a OP_CHECKTIMELOCKVERIFY P2SH script

I have created the following script which should always be spendable by Alice providing a signature under pk_a and pk_x or after a certain time_lock by Bob providing a signature under pk_b.

OP_IF
<lock_time>
OP_CHECKTIMELOCKVERIFY
OP_DROP
<pk_b>
OP_CHECKSIGVERIFY
OP_ELSE
2
<pk_a>
<pk_x>
2
OP_CHECKMULTISIGVERIFY
OP_ENDIF

Naturally, this script is wrapped in a P2SH. I have sent some funds to this pubScript on the testnet and am now trying to spend it (via the Alice case) Here is the transaction:

0100000001928b2cfd5f9c153714ee7968f5f7e252d5361723939ecdf44b729e059f86f70300000000fd0b014c940048304502210097eac0c34d505409ad65759651d7785540e612e865549f620258369b54adb182022063d0ce198eb50eddc86c6e00a0f14f5a998ddc485936b5fe4448dedbe2ae5e1b01483045022100d01a983e266c64fef31f1acaceeff64452a02b3d41d828118867fd67e6ac8276022057ef2a039e14b4a02c093528b4138050b352e71db704927f9e11ad047b4e791601004c73630312181db17521031d73e6f2e1022bfa98d2c8ba5ce9b100f27202bd4f49c6208316b1b488c16601ad6752210258843c644fc10bedc8640a12baf78106b62f01d8bd45294842fb20451377fc6b2102b56faea8db4cc9332d9d80204b81ea2d23a713dc307e350152c1eb4189d156d252af68ffffffff01b8820100000000001976a914773033d94eeaf80f3fd02f177be5b760c488aeab88ac00000000

and the scriptSig

4c940048304502210097eac0c34d505409ad65759651d7785540e612e865549f620258369b54adb182022063d0ce198eb50eddc86c6e00a0f14f5a998ddc485936b5fe4448dedbe2ae5e1b01483045022100d01a983e266c64fef31f1acaceeff64452a02b3d41d828118867fd67e6ac8276022057ef2a039e14b4a02c093528b4138050b352e71db704927f9e11ad047b4e791601004c73630312181db17521031d73e6f2e1022bfa98d2c8ba5ce9b100f27202bd4f49c6208316b1b488c16601ad6752210258843c644fc10bedc8640a12baf78106b62f01d8bd45294842fb20451377fc6b2102b56faea8db4cc9332d9d80204b81ea2d23a713dc307e350152c1eb4189d156d252af68

Here I have decoded the scriptSig

4c94 LENGTH
00 ZERO_BYTES (Because of the OP_CHECKMULTISIGVERIFY, I read this is recommended)
48 LENGTH
304502210097eac0c34d505409ad65759651d7785540e612e865549f620258369b54adb182022063d0ce198eb50eddc86c6e00a0f14f5a998ddc485936b5fe4448dedbe2ae5e1b01 SIG_A
48 LENGTH
3045022100d01a983e266c64fef31f1acaceeff64452a02b3d41d828118867fd67e6ac8276022057ef2a039e14b4a02c093528b4138050b352e71db704927f9e11ad047b4e791601 SIG_X
00 ZERO_BYTES (To make the OP_IF evaluate to false)
4c73 LENGTH
63 OP_IF
0312181d LOCK_TIME
b1 OP_CHECKTIMELOCKVERIFY
75 OP_DROP
21031d73e6f2e1022bfa98d2c8ba5ce9b100f27202bd4f49c6208316b1b488c16601 PK_B
ad OP_CHECKSIGVERIFY
67 OP_ELSE
52 2
210258843c644fc10bedc8640a12baf78106b62f01d8bd45294842fb20451377fc6b PK_A
2102b56faea8db4cc9332d9d80204b81ea2d23a713dc307e350152c1eb4189d156d2 PK_X
52 2
af OP_CHECKMULTISIGVERIFY
68 OP_ENDIF

Now when I try to send this transaction to the testnet it fails with mandatory-script-verify-flag-failed (Locktime requirement not satisfied) which confuses me, because after it hashed and compared the script I would assume that there is the 0x00 on the stack and therefore the OP_IF should be false and we go into the OP_ELSE case. However, this seems not to be the case. Could it be that the 0x00 is not at the right place?

Thanks in advance for any help!

How are p2sh address spent

So, I was looking into how P2SH transactions are working and by looking at different blog post and in particular to this answer, I have the following question.

So, let’s take the same transaction:

enter image description here

What I am not getting is how the operation are done.

  • Is the scriptSig executed first?
  • What is checked to be equal to 0b49fe...df1? How is it calculated ?

multi signature – Electrum: display redeem script of a P2SH utxo

I use Electrum with a multisig wallet. I would like to figure out how to prove ownership of a “coin”.

I understand that I need to share the whole redeem script so that it’s hash can be verified against the UTXO I want to prove ownership off. And then I need to sign a message using at least N of M (as defined when creating the multisig wallets) private keys corresponding to the public keys in the redeem script.

My question is how can I display the redeem script in Electrum, in a human readable way that allows to clearly identify N, M and all of the public keys?

How can I display the data structure encoding the redeem script on the blockchain, whose hash constitutes the ps2h address (without spending the UTXO)?

script – How to create a p2sh transaction with a scriptsig of OP_true?

Here you have an example of a transaction spending from a P2PKH output and creating a P2SH output with an OP_1.

And here you have the counterpart, a transaction spending from the first one (with an input script ‘0151’) and generating a P2PKH output.

I’ve created both using a Python library i’m coauthor of. You can check it out here.

bitcoin core – Are P2SH transactions with disabled opcodes relayed/mined?

Many OPCODES like OP_AND, OP_CAT, OP_SUBSTR are disabled as described in https://en.bitcoin.it/wiki/Script. Although, with Bitcoin Core 0.10.0 Gavin Andresen proposed “any Script” in P2SH transactions to be relayed/mined:
enter image description here
I’m still unsure if this applies disabled opcodes. I’ve come across a Mainnet coinbase transaction with disabled “OP_AND” opcode here, and a Testnet transaction here. I’m very curios if P2SH transactions with disabled opcodes in the redeem script relayed and mined on the BTC Mainnet.