<blockquote>
  <p>Of course, I'm not talking about transactions with OP_CHECKSIG</p>
</blockquote>

<p>I think this is the core of your misunderstanding. P2SH is not <em>intended</em> to be secure if you use an insecure script. It is simply a way to conveniently and cheaply producing an output with a policy and security that is equivalent to a script.</p>

<p>In practice, no script without a type of CHECKSIG operation will be secure, for the reasons you give yourself.</p>

CC BY-SA 4.0
2018-12-18T02:26:52.817Z
2018-12-18T02:26:52.817Z
Pieter Wuille
<p>You're correct that anybody could just replace the outputs and take the funds for themselves.  This script has no authorization needed on the spender's part, so once the redeemscript is known, it's practically become an anyone-can-spend script.</p>

<p>Assigning an RBF enabled sequence (or not) on the transaction makes no difference (perhaps only for relay policy), since any sequence set can also be replaced by anybody.</p>

CC BY-SA 4.0
2018-12-17T15:22:07.723Z
2018-12-17T15:22:07.723Z
arubi
<p>Maybe my question is stupid, but</p>

<p>imagine that I'm trying to spend inputs from p2sh. </p>

<p>For example, p2sh scriptPubKey looks like:</p>

<pre><code>OP_SHA256 4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a OP_EQUAL
</code></pre>

<p>I know, that the SHA256(0x01) = 4bf5122f...85459a (hacker do not know that, until I broadcast tx)</p>

<p>So I broadcast transaction with the folowing scriptsig:</p>

<pre><code>OP_1 (0x51)
</code></pre>

<p>Why hacker can't take this unconfirmed transaction, extract scriptsig, and replace outputs to his own outputs ? </p>

<p>Also, what to do, if RBF (replace-by-fee) flag is set?</p>

<p>Of course, I'm not talking about transactions with OP_CHECKSIG</p>

CC BY-SA 4.0
2018-12-17T15:30:33.307Z
Just to be technically correct, a `P2SH` scriptPubKey` is actually `OP_HASH160 <scriptHash> OP_EQUAL`, see https://en.bitcoin.it/wiki/Transaction#Pay-to-Script-Hash
JBaczuk
CC BY-SA 4.0
2018-12-17T15:37:28.630Z
Yes, thank you. Is there any diff with HASH160, or problem the same ?
Andrew
CC BY-SA 4.0
2018-12-17T15:49:31.410Z
It's not really relevant to the question, just for correctness.
JBaczuk
<bitcoind><transaction-verification>
P2SH secure spending. How?
Andrew
# P2SH secure spending. How?

(Asked by: username_0 on 2018-12-17)

Maybe my question is stupid, but
imagine that I'm trying to spend inputs from p2sh. 
For example, p2sh scriptPubKey looks like:
OP_SHA256 4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a OP_EQUAL

I know, that the SHA256(0x01) = 4bf5122f...85459a (hacker do not know that, until I broadcast tx)
So I broadcast transaction with the folowing scriptsig:
OP_1 (0x51)

Why hacker can't take this unconfirmed transaction, extract scriptsig, and replace outputs to his own outputs ? 
Also, what to do, if RBF (replace-by-fee) flag is set?
Of course, I'm not talking about transactions with OP_CHECKSIG

**Tags:** bitcoind, transaction-verification

> **Comments:**
> **username_1**: Just to be technically correct, a `P2SH` scriptPubKey` is actually `OP_HASH160 <scriptHash> OP_EQUAL`, see https://en.bitcoin.it/wiki/Transaction#Pay-to-Script-Hash  
> **username_0**: Yes, thank you. Is there any diff with HASH160, or problem the same ?  
> **username_1**: It's not really relevant to the question, just for correctness.  

---

## 2 answers

### Answer by username_3 on 2018-12-17. Score: 4 | Accepted answer

You're correct that anybody could just replace the outputs and take the funds for themselves. This script has no authorization needed on the spender's part, so once the redeemscript is known, it's practically become an anyone-can-spend script.

Assigning an RBF enabled sequence (or not) on the transaction makes no difference (perhaps only for relay policy), since any sequence set can also be replaced by anybody.


---

### Answer by username_2 on 2018-12-18. Score: 4

> Of course, I'm not talking about transactions with OP\_CHECKSIG

I think this is the core of your misunderstanding. P2SH is not *intended* to be secure if you use an insecure script. It is simply a way to conveniently and cheaply producing an output with a policy and security that is equivalent to a script.

In practice, no script without a type of CHECKSIG operation will be secure, for the reasons you give yourself.


---

