<blockquote>
<p>Why did the transaction was accepted by the network despite not working as intended</p>
</blockquote>
<p>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.</p>
<blockquote>
<p>Why did my input did not go to the desired p2sh address &quot;2N7e6ZAGXoepdVYu2Y8ho7fTf6oxE3j9UwS&quot;</p>
</blockquote>
<p>You set the output script to <code>dat_redeem_script_op</code> rather than <code>target_script</code>. So the output script is actually your multisig script rather than the P2SH script you intended.</p>
<blockquote>
<p>It is on the testnet but if it was not can I retrieve my satoshis on this tx ?</p>
</blockquote>
<p>No. Even if you had done this correctly with P2SH, it would still be unspendable. This is because your &quot;public keys&quot; are actually public key hashes. However you do not use any opcodes to for public key hashing. <code>OP_CHECKMULTISIG</code> takes public keys so it will try to interpret your public key hashes as pubkeys and fail to do so.</p>
<p>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.</p>

CC BY-SA 4.0
2021-02-13T23:11:45.120Z
I could have delete the target_script line sorry, I mean if I use it instead of dat_redeem_script_op it will no longer be a p2sh transaction
Saxtheowl
CC BY-SA 4.0
2021-02-13T22:29:59.613Z
2021-02-13T22:29:59.613Z
Ava Chow
<p>I have try to build a 2 of 2 p2sh transaction from scratch using the book &quot;programming bitcoin&quot; by Jimmy Song, we are not directly guided to make one of those so I had to modify some of the function we had to build in the book but I guess it must be working.</p>
<p>Here is my tx on testnet: <a href="https://www.blockchain.com/btc-testnet/tx/7eba40f4c00e3da529309011c6fd8066c97fd24adca60b0d024976c360bcdd8b" rel="nofollow noreferrer">https://www.blockchain.com/btc-testnet/tx/7eba40f4c00e3da529309011c6fd8066c97fd24adca60b0d024976c360bcdd8b</a></p>
<p>And here is my code: <a href="https://pastebin.com/1mCv73i2" rel="nofollow noreferrer">https://pastebin.com/1mCv73i2</a></p>
<p>using the functions of the book: <a href="https://github.com/jimmysong/programmingbitcoin" rel="nofollow noreferrer">https://github.com/jimmysong/programmingbitcoin</a></p>
<p>My question are:</p>
<ul>
<li>Why did the transaction was accepted by the network despite not working as intended</li>
<li>Why did my input did not go to the desired p2sh address &quot;2N7e6ZAGXoepdVYu2Y8ho7fTf6oxE3j9UwS&quot;</li>
<li>It is on the testnet but if it was not can I retrieve my satoshis on this tx ?</li>
</ul>
<p>edit: I found my error about my redeemscript and updated my code, but now I have a &quot;Code: -25, Error: bad-txns-inputs-missingorspent&quot; error when I want to broadcast it</p>
<p>here is my raw transaction, and I have updated the pastebin for the code:
<a href="https://pastebin.com/8sQBpnVY" rel="nofollow noreferrer">https://pastebin.com/8sQBpnVY</a></p>

CC BY-SA 4.0
2021-02-13T23:40:54.380Z
What do you believe to be invalid about the transaction?
Pieter Wuille
CC BY-SA 4.0
2021-02-13T23:47:41.493Z
Well, I just realize that I dont use the address I want to send the coin to anywhere, so I must have missed something, in fact im not sure as to where to put the address that will have a p2sh tx
Saxtheowl
CC BY-SA 4.0
2021-02-14T00:00:28.593Z
What do you mean with "P2SH tx"? Outputs can be P2SH or not, and a transaction can have a mix of P2SH and non-P2SH outputs. Inputs can be spending P2SH outputs or other outputs. Transactions can have a mix P2SH-output spending inputs and non-P2SH-output-spending inputs. It's not a property of the transaction, but of the individual inputs and outputs.
Pieter Wuille
CC BY-SA 4.0
2021-02-14T13:50:43.777Z
My error came from the fact that I use " 0x52, public_key1, public_key2, 0x52, 0xae " as my ScriptPubKey
Saxtheowl
CC BY-SA 4.0
2021-02-14T18:22:35.297Z
That's a valid multisig script. It doesn't have a corresponding address, but that doesn't make it invalid.
Pieter Wuille
CC BY-SA 4.0
2021-02-14T22:01:38.893Z
well, I fixed my problem but now I have a "Error validating transaction: witness script detected in tx without witness data." when broadcasting it on the testnet, I am gonna finish the chapter about segwit before going back at it again, despite the fact that I dont know how can there be witness in my tx, and we havent even covered this part yet in the book
Saxtheowl
<transactions><script><multi-signature><p2sh><programming-bitcoin-book>
Invalid p2sh transaction accepted by the network
Saxtheowl
# Invalid p2sh transaction accepted by the network

(Asked by: username_0 on 2021-02-13)

I have try to build a 2 of 2 p2sh transaction from scratch using the book "programming bitcoin" by Jimmy Song, we are not directly guided to make one of those so I had to modify some of the function we had to build in the book but I guess it must be working.

Here is my tx on testnet: <https://www.blockchain.com/btc-testnet/tx/7eba40f4c00e3da529309011c6fd8066c97fd24adca60b0d024976c360bcdd8b>

And here is my code: <https://pastebin.com/1mCv73i2>

using the functions of the book: <https://github.com/jimmysong/programmingbitcoin>

My question are:

* Why did the transaction was accepted by the network despite not working as intended
* Why did my input did not go to the desired p2sh address "2N7e6ZAGXoepdVYu2Y8ho7fTf6oxE3j9UwS"
* It is on the testnet but if it was not can I retrieve my satoshis on this tx ?

edit: I found my error about my redeemscript and updated my code, but now I have a "Code: -25, Error: bad-txns-inputs-missingorspent" error when I want to broadcast it

here is my raw transaction, and I have updated the pastebin for the code:
<https://pastebin.com/8sQBpnVY>

**Tags:** transactions, script, multi-signature, p2sh, programming-bitcoin-book

> **Comments:**
> **username_1**: What do you believe to be invalid about the transaction?  
> **username_0**: Well, I just realize that I dont use the address I want to send the coin to anywhere, so I must have missed something, in fact im not sure as to where to put the address that will have a p2sh tx  
> **username_1**: What do you mean with "P2SH tx"? Outputs can be P2SH or not, and a transaction can have a mix of P2SH and non-P2SH outputs. Inputs can be spending P2SH outputs or other outputs. Transactions can have a mix P2SH-output spending inputs and non-P2SH-output-spending inputs. It's not a property of the transaction, but of the individual inputs and outputs.  
> **username_0**: My error came from the fact that I use " 0x52, public_key1, public_key2, 0x52, 0xae " as my ScriptPubKey  
> **username_1**: That's a valid multisig script. It doesn't have a corresponding address, but that doesn't make it invalid.  
> **username_0**: well, I fixed my problem but now I have a "Error validating transaction: witness script detected in tx without witness data." when broadcasting it on the testnet, I am gonna finish the chapter about segwit before going back at it again, despite the fact that I dont know how can there be witness in my tx, and we havent even covered this part yet in the book  

---

## 1 answers

### Answer by username_2 on 2021-02-13. Score: 4 | Accepted answer

> 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.

> **Comments:**
> **username_0**: I could have delete the target_script line sorry, I mean if I use it instead of dat_redeem_script_op it will no longer be a p2sh transaction  

---

