<p>First, yes you are correct in that each time you spend from the same p2sh address, you're specifying the script that hashes to that address. And yes, if you spend multiple times from that same address, you would be correct, this would be highly inefficient since you'd have to reveal the script each time and the script could be very long.</p>

<p>However, in bitcoin, it's not considered safe to re-use addresses. Each p2sh address should be used once, not many times like in your scenario above. In that case, it's not inefficient or suboptimal at all. That said, I suppose something like segwit could be used to store scripts off-band, but that would be a significant and complicated change for not much gain.</p>

CC BY-SA 3.0
2016-03-16T14:07:58.377Z
But if my unlocking script is always the same, which means its hash will always be the same, how is it possible to not re-use addresses in this case?
Henrique Barcelos
CC BY-SA 3.0
2016-03-16T14:09:24.087Z
Do you mean each of the partners in the deal should generate a different PubKey for each transaction?
Henrique Barcelos
CC BY-SA 3.0
2016-03-16T14:48:58.630Z
When you finally spend from the address, you should spend all utxos to that address at the same time and not use that address to receive funds again. A new p2sh address using different pubkeys should be made.
Jimmy Song
CC BY-SA 3.0
2016-03-16T19:27:28.533Z
That's what I thought. Thank you very much =]
Henrique Barcelos
CC BY-SA 3.0
2016-03-15T23:17:58.823Z
2016-03-15T23:17:58.823Z
Jimmy Song
<p>I'm following "Mastering Bitcoin" from Andreas Antonopoulos in order to understande the Bitcoin implementation.</p>

<p>In the <a href="http://chimera.labs.oreilly.com/books/1234000001802/ch05.html#p2sh" rel="nofollow">chapter about transactions</a>, he points some advantages of P2SH transactions:</p>

<blockquote>
  <ol>
  <li>Complex scripts are replaced by shorter fingerprints in the transaction output, making the transaction smaller.</li>
  <li>Scripts can be coded as an address, so the sender and the sender’s wallet don’t need complex engineering to implement P2SH.</li>
  <li>P2SH shifts the burden of constructing the script to the recipient, not the sender.</li>
  <li><strong>P2SH shifts the burden in data storage for the long script from the output (which is in the UTXO set) to the input (stored on the
  blockchain).</strong></li>
  <li>P2SH shifts the burden in data storage for the long
  script from the present time (payment) to a future time (when it is
  spent).</li>
  <li>P2SH shifts the transaction fee cost of a long script from the sender to the recipient, who has to include the long redeem script to
  spend it.</li>
  </ol>
</blockquote>

<p>About item 4, it still looks suboptimal to me.</p>

<p>Imagine that I have a firm where in order to spend the payment from customers I also have to get the signatures from at least 1 of 2 other partners. My script would look like:</p>

<pre><code>2 &lt;My PubKey&gt; &lt;Partner 1's PubKey&gt; &lt;Partner 2's PubKey&gt; 3 OP_CHECKMULTISIG
</code></pre>

<p>So, for every incoming payments that I'd like to spend, my <code>scriptSig</code> would have to be bloated with the script above plus 2 signatures <code>&lt;sig1&gt; &lt;sig2&gt;</code>.</p>

<p>Imagine that I'll have thousands of these transactions, this would be bloating the blockchain, probably decreasing the processing capabilities of the network.</p>

<p>I was wondering: is there any way of mantaining these scripts as "accounts" in "script wallets" and not storing it inside the blockchain?</p>

<p>When there is need for validation, I could simply retrieve this script form the "wallet" and check the transaction.</p>

<p>How would it be possible?</p>

<p2sh><smartcontracts>
Storing scripts (smart contracts) outside the blockchain
Henrique Barcelos
# Storing scripts (smart contracts) outside the blockchain

(Asked by: username_0 on 2016-03-14)

I'm following "Mastering Bitcoin" from Andreas Antonopoulos in order to understande the Bitcoin implementation.

In the [chapter about transactions](http://chimera.labs.oreilly.com/books/1234000001802/ch05.html#p2sh), he points some advantages of P2SH transactions:

> 1. Complex scripts are replaced by shorter fingerprints in the transaction output, making the transaction smaller.
> 2. Scripts can be coded as an address, so the sender and the sender’s wallet don’t need complex engineering to implement P2SH.
> 3. P2SH shifts the burden of constructing the script to the recipient, not the sender.
> 4. **P2SH shifts the burden in data storage for the long script from the output (which is in the UTXO set) to the input (stored on the
>    blockchain).**
> 5. P2SH shifts the burden in data storage for the long
>    script from the present time (payment) to a future time (when it is
>    spent).
> 6. P2SH shifts the transaction fee cost of a long script from the sender to the recipient, who has to include the long redeem script to
>    spend it.

About item 4, it still looks suboptimal to me.

Imagine that I have a firm where in order to spend the payment from customers I also have to get the signatures from at least 1 of 2 other partners. My script would look like:

```
2 <My PubKey> <Partner 1's PubKey> <Partner 2's PubKey> 3 OP_CHECKMULTISIG

```

So, for every incoming payments that I'd like to spend, my `scriptSig` would have to be bloated with the script above plus 2 signatures `<sig1> <sig2>`.

Imagine that I'll have thousands of these transactions, this would be bloating the blockchain, probably decreasing the processing capabilities of the network.

I was wondering: is there any way of mantaining these scripts as "accounts" in "script wallets" and not storing it inside the blockchain?

When there is need for validation, I could simply retrieve this script form the "wallet" and check the transaction.

How would it be possible?

**Tags:** p2sh, smartcontracts


---

## 1 answers

### Answer by username_1 on 2016-03-15. Score: 1 | Accepted answer

First, yes you are correct in that each time you spend from the same p2sh address, you're specifying the script that hashes to that address. And yes, if you spend multiple times from that same address, you would be correct, this would be highly inefficient since you'd have to reveal the script each time and the script could be very long.

However, in bitcoin, it's not considered safe to re-use addresses. Each p2sh address should be used once, not many times like in your scenario above. In that case, it's not inefficient or suboptimal at all. That said, I suppose something like segwit could be used to store scripts off-band, but that would be a significant and complicated change for not much gain.

> **Comments:**
> **username_0**: But if my unlocking script is always the same, which means its hash will always be the same, how is it possible to not re-use addresses in this case?  
> **username_0**: Do you mean each of the partners in the deal should generate a different PubKey for each transaction?  
> **username_1**: When you finally spend from the address, you should spend all utxos to that address at the same time and not use that address to receive funds again. A new p2sh address using different pubkeys should be made.  
> **username_0**: That's what I thought. Thank you very much =]  

---

