Episode 74: P2SH
Ep. 74

Episode 74: P2SH

•

Episode description

In this episode of Bitcoin, Explained, Aaron and Sjors explain pay-to-script-hash (P2SH), which allows bitcoin to be sent to and from the hash of a script. Besides (the current implementation of) P2SH itself, Aaron and Sjors also discuss some alternatives that were proposed around the time that P2SH was adopted in 2012.

For further reading on the history of P2SH, also see “The Battle for P2SH: The Untold History of Bitcoin’s First War“

Chapters

00:00:00 Introduction and listener boost
00:04:22 How pay-to-script-hash works
00:10:53 OP_EVAL: the first proposal
00:16:11 Why OP_EVAL raised recursion risks
00:23:43 P2SH script templates and soft forks
00:28:16 CheckHashVerify: the alternative proposal
00:33:58 Choosing P2SH: miner signaling and developer review
00:38:28 Deployment mishaps and upgrading Bitcoin

Episode Sponsor: https://voltage.cloud/

Sjors New Book: https://www.amazon.com/Bitcoin-Technical-innovations-Sjors-Provoost/dp/9090360425

Transcript

Download transcript (.vtt)
0:20

Aaron van Wirdum Live from Utrecht, this is Bitcoin

0:22

Sjors Provoost Explained.

0:24

Aaron van Wirdum Hey Sjors.

0:25

Sjors Provoost Hello.

0:26

Aaron van Wirdum You just came back from Advancing Bitcoin, is that right?

0:29

Sjors Provoost That's right.

0:30

Aaron van Wirdum Technical conference.

0:31

Sjors Provoost Yep.

0:31

Aaron van Wirdum How was that?

0:32

Sjors Provoost It was fun.

0:33

Aaron van Wirdum Did you learn anything new?

0:35

Sjors Provoost Many little things.

0:37

Aaron van Wirdum Anything we can discuss on the podcast?

0:39

Sjors Provoost I don't think so. Maybe later, but not.

0:41

Aaron van Wirdum Well, I knew that because we got a topic for today. Today. We're going to discuss P2SH, which somehow we never did before.

0:50

Sjors Provoost Well, we've mentioned it a few times, but we haven't done a full episode on it.

0:54

Aaron van Wirdum Yeah, we probably mentioned it many times, and it was actually a suggestion by someone. I don't remember who it was. Sorry if you're listening, dear listener, but it was someone on Twitter that suggested it, and then I went over the episode overview list, and I realized that we've never actually done an episode on P2SH. Pay-to-script-hash so let's do it today but first, Sjors, your favorite part of the show, you wanted to give us you what's it called again a booster… Explain what you're doing.

1:26

Sjors Provoost I'm just gonna read one boostagram. This is

1:29

Aaron van Wirdum Also, you sent me a couple of links on how people can stream the sats to me is that right?

1:34

Sjors Provoost That's right.

1:35

Aaron van Wirdum I'm not gonna do it Sjors, I decided… I decided, this is your special moment this is your special moment in the show. This is your love affair with the listeners.

1:47

Sjors Provoost No, you don't have to read any of the boost yourself. It's just that I now have to look at how many sats we got and then send you half and deal with invoices, which is annoying.

1:56

Aaron van Wirdum I don't want half. It's all yours.

1:58

Sjors Provoost All right.

1:59

Aaron van Wirdum Dear listener, if you want to make Sjors rich, if you want to make me regret this decision, send all your sats to Sjors. Do you want your own intro tune for this part of the show? Sjors?

2:11

Sjors Provoost No, that's all right.

2:12

Aaron van Wirdum I can make one for you.

2:13

Sjors Provoost All right, so we got a

2:15

Aaron van Wirdum Sjors stacks sats. Sjors stacks sats. Sjors stacks sats.

2:20

Sjors Provoost That is pretty good intro tune. Let's keep that one. it… for 20,000 sats from AtLawBitcoin saying appreciate you. so it's a pretty nice one. I think you're already regretting it.

2:32

Aaron van Wirdum 20,000 oh my god. Okay, Sjors, back to the show. Pay-to-script-hash so the way we're going to structure this episode is because I think probably a lot of our listeners know what pay-to-script-hash is although we might go into some details that not everyone knows about but then also what we're going to do is we're going to discuss a little bit about the history of pay-to-SH.

3:00

Sjors Provoost Exactly.

3:02

Aaron van Wirdum And that's actually largely based on an article that I wrote with Pete Rizzo Three years ago now something like that because it was kind of the first big Bitcoin controversy first big Bitcoin war you know it wasn't as big as a scaling war obviously but it was kind of like a prelude to that in a way it was sort of the precursor the prequel to to the big scaling war.

3:29

Sjors Provoost Yeah, there were at least like 20 angry posts on Bitcointalk.

3:34

Aaron van Wirdum Yeah, and there was some bickering on IRC and there was the [unclear]. Amir Taaki got involved with his own website. There was some debate about it back then.

3:46

Sjors Provoost Speaking of IRC, yesterday I tried to find the IRC logs from 2011 and all the websites that link to old IRC logs pointing to a website, Bitcoin something, which doesn't exist anymore. So it's a bit annoying.

4:02

Aaron van Wirdum Wait, the logs from 2011 are gone? is that what you're saying?

4:05

Sjors Provoost I don't know if they're gone they're not on the sites where the other sites link to so if somebody still has IRC logs I'd like to know where they are.

4:12

Aaron van Wirdum Oh, I mean I can look that up for you I'm sure I would have that somewhere unless it got deleted. we'll find out. anyways, okay, so let's just start at the beginning and I think we'll start with a more sort of high-level description of pay-to-script-hash, P2SH. What is P2SH?

4:36

Sjors Provoost Because what we'll get to is that there were actually three proposals, but they're all really doing P2SH. It's just that one of them came to be known that. with by that name but P2SH just means pay-to-script-hash and there are multiple ways to pay to a script hash so what it does is…

4:51

Aaron van Wirdum Well, I guess my question is about what we call pay-to-script-hash today but I think your point is that what you're going to describe kind of applies to all of them.

4:59

Sjors Provoost Yeah, exactly, so the idea is that so when you want to receive Bitcoin you need to tell the sender, where to send it to, and what you're really telling them is what script to send it to. And usually that script is just like, okay, whoever has the private key belonging to the public key can spend it, but the script can be more complicated. And this is a problem because the more complicated your script is, the longer it is. And so that means that the sender has to pay you fees and that the addresses can look very small or the addresses can look very long if you have a long script. So in order to avoid that, the idea was to send your coins to the hash of a script because a hash is always the same length. And so therefore, the idea of paying the script hash is that the recipient gives you not the script itself, but the hash of the script. And then you... turn that into a special your wallet will then do its magic and make sure that the coins are sent to the right place that can only and then when recipient wants to spend it they have to reveal the original script and the precise details vary per proposal but that's a general idea right? you reveal the script the blockchain checks that it matches the hash of the script that you previously said it would be, and then you have to satisfy whatever that script says right?

6:11

Aaron van Wirdum So back in the early days of Bitcoin if you wanted to receive money if you wanted to lock how do I say… what's the best way to put this?

6:21

Sjors Provoost Encumber money.

6:22

Aaron van Wirdum Let's say you you want to say I want to receive money from you sure but I want to receive it in such a way that the money is locked into a multisig let's say just a multisig.

6:32

Sjors Provoost Yeah,

6:32

Aaron van Wirdum then what I would do in the early days well actually it wouldn't even work in the early days right because of… it would… No, policy rules… what's it called?

6:41

Sjors Provoost Well, it would be difficult to get the transaction mined. Exactly. You'd have to work with a miner because of the standardness rules.

6:47

Aaron van Wirdum Standardness, that's what I was looking for. but let's ignore that for a second so if I wanted to receive coins in such a way that it's in a multisig I got several keys dispersed in several locations that's how I want to receive the coins then I would actually send the script to you and you would send it to that script right?

7:06

Sjors Provoost Yeah, you would… Yeah, exactly. You would have to tell me, okay, this is my multisig script. Please send it here. And that was annoying because there was also no address format for it. And so it's a very difficult UI or user experience.

7:19

Aaron van Wirdum Right. So then you're going to have to pay the fees for the fact that the transaction is not bigger because it contains a whole script. And also it's immediately visible on the blockchain that it is sent to a multisig, which is maybe not great for privacy.

7:38

Sjors Provoost Yeah, so the privacy, the nice thing about paying to script hash is that the privacy is slightly better. At least you have privacy for longer because eventually if you spend it, the script will show up. But until you spend it, the script will not show up.

7:53

Aaron van Wirdum Yeah, and and you're the one paying the fee rather like I'm the one who wants a complicated multisig address but then you have to pay the fee for including it in that address right?

8:04

Sjors Provoost Yeah, in so for P2SH yes I the sender— would have to pay a higher fee in order to send to somebody with a complicated setup which kind of doesn't make sense and now they'll… it's more fair in the sense that if you have a complicated setup anybody can send to you it doesn't cost them anything extra but once you want to spend from your complicated setup then you have to pay the extra fees

8:24

Aaron van Wirdum Right? Yeah, no, I was just summing up how it would work before pay to a script hash was there so I sent you the whole script so that's big so you have to pay a big fee even though I'm the one who wants to use a complicated script and then also it's slightly worse for privacy so that's why developers early on in like 2011 came up with this idea let's instead send it to a hash and I give you just the hash I don't give you the script I give you the hash and when I want to spend it I reveal the whole scripts and then anyone can check that the script matches the hash and that's what I spend it from… that's how I it's only then that I… include all the multiple signatures in the multisig case, right?

9:10

Sjors Provoost Yeah.

9:10

Aaron van Wirdum So that's the basics of P2SH.

9:13

Sjors Provoost Yes, in general. And so the hash of a script happens to be the same size of the hash of a public key, which means that it will just look like an address. You just start with a three instead of a one. So that's nice too.

9:26

Aaron van Wirdum Right. Why is it three, not a two?

9:29

Sjors Provoost Because it's a version number. So the first number in the address, in the legacy addresses, we've done an episode about addresses. The first letter represents a version number of the address scheme. So if it starts with a 1, which is not actually a 1 because it's base 58, but we're not going to bother you with that, that means that it has to be interpreted in a certain way, and if the address starts with a 3, it has to be interpreted in another way.

9:55

Aaron van Wirdum But why is it not a 2?

9:57

Sjors Provoost Because the numbers 1 and 3 do not actually represent the numbers 1 and 3 because it's base 58. We did a whole episode about that, Aaron. You should just listen back to the original episode about addresses.

10:10

Aaron van Wirdum I definitely forgot about it. I thought the reason was because 2 was already used by testnet.

10:15

Sjors Provoost Yes.

10:16

Aaron van Wirdum So 1 mainnet, 2 testnet, 3, you know, next 1.

10:21

Sjors Provoost That could also be true.

10:23

Aaron van Wirdum Okay.

10:24

Sjors Provoost It doesn't really matter. It's just that once you're sending to it, the wallet that's sending has to put something on the blockchain. And the only thing that's in the address is the hash of something, either the hash of a public key or the hash of a script. And that's not enough to put on the blockchain. You actually have to put more information on the blockchain as a sender. And the version number tells you your wallet exactly what to put on the blockchain. So the standard script templates that go around the hash.

10:53

Aaron van Wirdum Moving on. So this was the basics of pay-to-script-hash and why it was implemented. But now we'll sort of take a trip down memory lane and go back to some historical context. So first of the first proposal that would do something like this. So this pay-to-script-hash idea was around. And then the first idea that was actually proposed and implemented and almost deployed was called OP_EVAL. So OP_EVAL, I think the right way to looking at it was a way of doing pay-to-script-hash, right?

11:43

Sjors Provoost Yes. And arguably the most obvious way to do it Though, of course, that depends on what you were thinking of at the time when you were designing it. But basically, the idea of OP_EVAL is that, again, as a sender, you just get a hash and you put that on the blockchain. Now, the recipient wants to spend it. They reveal the original script. And then the way that that original script is run is by this opcode called OP_EVAL. So that means the sender has to put the word OP_EVAL somewhere in the thing on the blockchain. And then the recipient will actually put the script and then the script gets executed. Yeah.

12:22

Aaron van Wirdum So to be perfectly clear, in case anyone is confused at this point, OP_EVAL is not actually in Bitcoin. This was an early proposal that was rejected.

12:30

Sjors Provoost Yes.

12:31

Aaron van Wirdum Okay, so who's putting OP_EVAL? Maybe you just said this, but who's putting OP_EVAL where exactly?

12:39

Sjors Provoost The sender has to do it. So this is one of the things you implicitly communicate when you have a version number in your address. Now, in this case, this standard was never materialized, but maybe the address would have started with a 7 or something. And then as a sender, you get a hash of a script and you put on the blockchain something like OP_DUP, the hash of the script OP_EVAL. A little bit longer, but something like that.

13:01

Aaron van Wirdum Wait, so I'm the one who wants to receive money from you in a multisig address. Let's just stick to that example. So in the case of OP_EVAL, what exactly do I send you? Do I send you…

13:11

Sjors Provoost The hash of the script. So the hash of your multisig script.

13:14

Aaron van Wirdum Okay, and does this script include OP_EVAL?

13:16

Sjors Provoost No.

13:17

Aaron van Wirdum Okay, so I just sent you the script of the multisig. I sent it to you. What do you do?

13:21

Sjors Provoost You sent me the hash of the script of the multisig.

13:23

Aaron van Wirdum I guess I can do either, right? But sure, I'll send you the hash.

13:27

Sjors Provoost Yeah, that's the whole point basically to make it simple. So you're sending me the hash. Then what I do is I create a script, a scriptPubKey on the blockchain. That's where the coins are going. And I think it says the first instruction is like duplicate whatever's on the stack and then put the hash there. And then it says compare the thing that's on the stack with the hash. And then if that's true, then you call OP_EVAL. And this is a very similar pattern to how you spend from a public key. Because of the public key, you're also basically, the sender is putting the hash of the public key on the blockchain. And then the receiver, when they want to spend it, they have to reveal the actual public key.

14:08

Aaron van Wirdum Oh, sorry. Do you put OP_EVAL in the input or in the output?

14:12

Sjors Provoost In the output.

14:13

Aaron van Wirdum Okay. So I sent you the hash and then you put that hash in the output and you put OP_EVAL next to it.

14:19

Sjors Provoost Yep.

14:20

Aaron van Wirdum Okay, and then when I want to spend the coins then I include the script.

14:26

Sjors Provoost Yeah, the only basically what you will do when you're spending it you include the script and you include whatever is necessary to satisfy the script right?

14:34

Aaron van Wirdum And do I put OP_EVAL anywhere?

14:36

Sjors Provoost No, no.

14:37

Aaron van Wirdum Okay, so then other nodes on the network… They're gonna look at...

14:43

Sjors Provoost They start basically because you always start with the signature side. So what they'll do is they'll say, okay, I'm putting some signatures on the stack. I'm putting the actual script on the stack. And then I'm gonna run the scriptPubKey, which will say, okay, you need to duplicate. So you need to duplicate the hash of the script. No, I don't know what you need. Anyway, it basically says, okay, now you should check that the hash of the script actually matches the script that was put in the stack. And that's because then you call, then you encounter this eval instruction and that's when you execute the whole script and when you need the signatures.

15:17

Aaron van Wirdum Just to make sure that I'm understanding this. So when other, when nodes verifying new transactions, they, what does OP_EVAL mean to them exactly? It means that they need to compare…

15:29

Sjors Provoost It means execute the script that's on the stack.

15:34

Aaron van Wirdum Shouldn't they always do that?

15:37

Sjors Provoost No, normally you are executing a script and the script can put things on the stack, like numbers, et cetera. The stacks are like plates basically. So you can put numbers on the plate and you can say, okay, is this number on the first plate? Is that the same as the number on the next plate? But you do not execute code that is on the stack. So things that are on the stack are numbers or text or keys or whatever it is or dates, but they are not code.

16:02

Aaron van Wirdum Okay.

16:03

Sjors Provoost And what this eval thing does is, no, no, no, pretend that what is on the stack is not a number or a text, pretend that it's code, and then just run it like that.

16:11

Aaron van Wirdum Okay. Well, you say pretend, but it's actually code then, right?

16:14

Sjors Provoost Well, so this is where the downside of this proposal comes in.

16:18

Aaron van Wirdum Okay.

16:19

Sjors Provoost But maybe some people have worked with JavaScript in the past. And in JavaScript, you can, you know, there's also a command called eval. And it's extremely dangerous. So generally when you're building a JavaScript web application, you really should not use eval. And the reason for that tends to be that you cannot predict what that thing is going to do if you're just trying to analyze your JavaScript code. So your JavaScript code might see something like eval and then a piece of text. So if you're just analyzing the code, you might think, oh, it's just text. What could go wrong? But now it turns out this text is actually treated as code. You can no longer just treat text as if it texts. And the same thing goes with Bitcoin. That's sort of where the downside of this eval proposal comes in. Normally, when you see a piece of Bitcoin script, you can look at what it's going to do because you can see all the opcodes in it. And you can see, okay, it's pushing some number, whatever. It's pushing some text, doesn't matter. And you can reason about it. And then after you've done some sanity checks, that's when you actually run the script. But with eval, you cannot do that anymore because… anywhere in this script could be a piece of text that initially you think is just text, so you've ignored it, but now it's suddenly code, so you should not have ignored it. But conversely, if you're just looking at the text and you're treating it as code, well, maybe it really is just text and you should have ignored it.

17:37

Aaron van Wirdum Okay.

17:38

Sjors Provoost So it just creates...

17:38

Aaron van Wirdum So it makes it hard to do a sanity check.

17:42

Sjors Provoost Yeah, a sanity check without running the code.

17:45

Aaron van Wirdum What is insane in this context? What is it you're afraid of?

17:50

Sjors Provoost Well, there's many things you'd be afraid of because my understanding is that this would actually make Bitcoin Turing complete. And Turing completeness is bad, basically, because it means you can execute any arbitrary program, which could include loops, infinite loops, for example. So one of the things you might be afraid of is an infinite loop.

18:07

Aaron van Wirdum That's the core risk, right?

18:09

Sjors Provoost That's one of the risks when you have, basically, when you can run any program you want. Well, one of those programs could be an infinite loop where you're trying to validate the block and you will never finish validating it because you keep going in circles because you have a loop.

18:22

Aaron van Wirdum Can you give an example of an infinite loop just for people like me that this is not obvious to me what that means?

18:32

Sjors Provoost Well, how you would do it, like normally in a programming language,

18:35

Aaron van Wirdum You can just say do while.

18:38

Sjors Provoost Which basically, you just basically say, okay, go in a loop until some condition is met. Usually that's how a programming language works. And then inside that loop, you might print something on the screen. And if the condition that to leave the loop never happens, then you never leave the loop. So you keep printing something on the screen line by line by line and you never stop.

18:59

Aaron van Wirdum What is a loop?

19:02

Sjors Provoost Basically, I guess a circle of code. So you run an instruction and then you go to the top of the loop and you run the instruction again. So it's a piece of code that you keep running again and again until you, for some reason, stop running it.

19:15

Aaron van Wirdum Okay, so I'm going to just try an example.

19:18

Sjors Provoost So typically you might loop from 1 to 10, and you print the number. So you can say for i is 0, and then you say print i, and then at the end of the loop you do i++, which increases i, and then you check if i is more than 10, you exit the loop. That's one example.

19:40

Aaron van Wirdum If I tell my code, I write code that says hash this piece of data with the nonce until it spells Sjors is the king. And then I will keep hashing that until it says that. Is that like an infinite loop?

19:54

Sjors Provoost Well, that's probably an infinite loop, yeah, because that's never going to happen.

19:57

Aaron van Wirdum Yeah, I mean, it might happen at some point, right?

19:59

Sjors Provoost But it's practically an infinite loop. Yeah, exactly. It's just as bad because it will grind your computer to a halt.

20:06

Aaron van Wirdum Okay, so and then the problem is... To verify if a transaction is valid, you first have to basically keep checking all kinds of different things.

20:15

Sjors Provoost Yeah, and so an infinite loop in Bitcoin would probably be...

20:18

Aaron van Wirdum New blocks come in and just grinds your... Your node basically can't keep up with the blockchain because it's still verifying an infinite loop from a transaction from four years ago.

20:28

Sjors Provoost Yeah, and I guess an example of this infinite loop could be that the thing you're calling eval on says OP_DUP, which means duplicate. And then it says OP_EVAL again. And so what you would do is you see eval, you duplicate the actual thing that you want to evaluate, and then you call eval on it again. And then... you know you're calling eval on something that's that same string again which says okay duplicate myself and so you keep duplicating the thing that you're trying to evaluate and so that will be an infinite loop. Now there were in this proposal some protections against that.

21:04

Aaron van Wirdum Well, just to be clear was this the example that you just gave was that sort of the specific concern with OP_EVAL?

21:12

Sjors Provoost Well, that was one example of how it could be to an infinite loop.

21:15

Aaron van Wirdum Okay.

21:15

Sjors Provoost Yeah, but there was a protection against that in the implementation of it it would basically only allow two loops or like to recurse that's the term basically recursion. so how often can you go back inside yourself inside the loop? and there was a limit of two, but I think then there was a mistake in the implementation that that protection didn't work and so people got a little scared.

21:40

Aaron van Wirdum Okay, well this got real technical real fast but let's so… Do you think we've now explained the problem with OP_EVAL?

21:51

Sjors Provoost Yeah, so basically in theory you could have fixed all these specific issues, but the general risk remains when you have something like eval in any programming language is just kind of dangerous. So you don't want to, you preferably don't want to do it. It makes it more difficult to reason about the code, very easy to make a mistake, and one mistake and you have an infinite loop.

22:11

Aaron van Wirdum Okay, so is it fair if I summarize the problem with OP_EVAL? In that it opens the door to potentially infinite loops, which would kind of break Bitcoin.

22:22

Sjors Provoost Yeah, I guess so.

22:23

Aaron van Wirdum Okay.

22:24

Sjors Provoost And keep in mind that Ethereum does allow something like Eval. And the reason they get away with it is because they have gas. And so you use up your gas and the loop won't be infinite because you run out of gas at some point.

22:36

Aaron van Wirdum I see. Okay. So that was the problem with OP_EVAL. And then... This was discovered very last minute by Russell O'Connor back in 2011. It was ready to be deployed. And then Russell O'Connor said, guys, what are you doing? You got to stop this right now.

22:57

Sjors Provoost Yeah, he literally said, you guys need to stop what you're doing and really understand Bitcoin.

23:02

Aaron van Wirdum Right, yeah. And then... The core developers at the time, which were led by Gavin Andresen, but also a lot of the kind of currently known developers were already involved, right? Greg Maxwell Pieter Wuille, Luke Dashjr, Amir Taaki also.

23:18

Sjors Provoost Yep.

23:19

Aaron van Wirdum Anyway, so they figured out we've got to figure out something else.

23:24

Sjors Provoost Well, they didn't even agree on that, I think, initially, because there were two possibilities. Either you do something completely different or you just... play whack-a-mole basically so you you stick with the OP_EVAL you fix the specific bug you find but you know there might be another bug but you just hope that there isn't

23:42

Aaron van Wirdum Right, and but then Gavin Gavin Andresen he proposed pay to what we would call pay-to-script-hash today so really OP_EVAL is kind of a form of pay-to-script-hash so how we're just gonna name this one pay-to-script-hash because that's what we're calling it today, I guess.

24:03

Sjors Provoost Exactly. So he could have basically stuck to the OP_EVAL proposal. I think he tried initially. But then I guess somehow we got the idea of doing it in a different way that was safer.

24:13

Aaron van Wirdum So what is this different way and why is it safer?

24:16

Sjors Provoost So this different way is that we have a type of script. So when on the blockchain you see a script that has a very specific form. So I don't know, it starts with OP_EQUAL, then it has a hash, and then it starts with something like that. If the script interpreter sees that particular form, it will understand that what it tries to do is be a P2SH transaction. Because from the old node point of view, it would just see the hash of a script. And all it would do is check whether it matches the script that you're putting on the stack. But it wouldn't do anything. So from an old node point of view, such a template just looks like doing nothing, giving the money away. But from the new node's point of view, they'll recognize this. They will take the script, the actual script, and run it.

25:04

Aaron van Wirdum Yeah, so you're getting into a topic that we haven't mentioned so far, which is that pay-to-script-hash as well as OP_EVAL can be implemented as a soft fork. This was kind of a discovery by the Bitcoin developers at the time that this was even possible.

25:20

Sjors Provoost Yeah, that's fun to read on the forums and probably also in IRC. You can find a log that... Yeah, they discovered that there are ways to do soft forks this way by either in the OP_EVAL proposal by saying we take an existing opcode that does nothing.

25:36

Aaron van Wirdum Yeah, the OP_NOPs.

25:37

Sjors Provoost The OP_NOP. And then because it does nothing, old nodes will think, okay, this transaction is valid because nothing changes. And new nodes will actually do what the new opcode says. And that mechanism was new for the developers, but very likely Satoshi knew that this would be possible because otherwise why did he put the OP_NOPs in there?

25:57

Aaron van Wirdum Right. Yeah. So let's get into that.

26:00

Sjors Provoost It was rediscovered, essentially.

26:02

Aaron van Wirdum Right. So let's get into that a little bit. So the OP_NOPs, these are op codes put in there by Satoshi, which... old nodes or non-upgraded nodes or whatever you want to call it, if they see OP_NOP, they will just consider the transaction valid?

26:16

Sjors Provoost Well, they don't consider the transaction valid necessarily, but it doesn't change anything about the validity. So if whatever happened before OP_NOP... was valid then after OP_NOP it's still valid.

26:27

Aaron van Wirdum Right but then these OP_NOP codes can be reinterpreted by newer nodes after a soft fork and then they start to mean something else. So in this specific example they would have started to mean OP_EVAL after that.

26:43

Sjors Provoost Yes, and although the OP_EVAL concept was abandoned the idea of doing a soft fork this way by having a script that means nothing to all nodes or like it just means take the money for all nodes it it has a more strict interpretation for the new nodes right?

26:58

Aaron van Wirdum The thing though is that with OP_EVAL there was an actual explicit reinterpretation of an OP_NOP. And then with pay-to-script-hash, that was not the case. So it's used the same logic for a soft fork. It just didn't reinterpret an actual OP_NOP.

27:17

Sjors Provoost Exactly. What it interpreted was a template. So if a transaction looks like a duck… For example, you know, your old rules would be, oh, we just take a photo and we say what animal it is. We see a photo and we say it's a cat or it's a dog. And the new rule would be, well, but if it's a cat, we're actually going to look at the skin color of the cat and do some other game. And this costs some friction because, you know, it depends on your taste, I guess, whether or not you think this particular way of doing it is good or bad.

27:48

Aaron van Wirdum Yeah, it was described at the time, even by people that supported it, like Amir Taaki. He said, I don't know the exact quote, I don't know if we have it here, but he definitely called it a hack.

27:58

Sjors Provoost Well, and you could ask, was this the will of Satoshi, you know, that kind of interpretation, because the OP_NOP system was, well, at least you could think that the OP_NOP system was intended to be used in the way that eval was using it, whereas this idea of using a template was not basically in there. So mostly I think Luke Dashjr was very not happy with this way of doing it. And so for, I think for, you know, you could say aesthetic reasons, but you know, he might look at that differently. So he came up with a different proposal that was somewhere in between. It didn't use an OP_NOP, I think. In fact, I think it repurposed OP_CODESEPARATOR or something like that. In any case, he basically came up with an alternative design that didn't use this templating mechanism. And then there was a lot of drama. And the downside of the drama was that not much attention was really paid to the real proposal that Luke was doing. And there was much more bickering back and forth, at least on the forums. And so, yeah, I haven't studied that proposal in a lot of detail either. I mean, I wasn't around it. I can say some things about it, but it might not be the most fair description.

29:08

Aaron van Wirdum Yeah, so let's get to that in a second, just to clarify. So first, there was OP_EVAL that turned out to have these problems with recursion then the alternative that was proposed by Gavin Andresen was we'll just call it pay-to-script-hash even though all of them are really pay-to-script-hash but this one is like explicitly called pay-to-script-hash and then like Luke Dashjr came around he said I don't like this at all let's do and this is what we call in our article at least CheckHashVerify So CHV is what we call it. I don't know if he called it that at a time, probably, though.

29:47

Sjors Provoost Right, but you see it's really the same thing, right? You're checking a hash, namely the hash of a script, and you're verifying that it's actually a valid script.

29:56

Aaron van Wirdum You just blew my mind, Sjors. You're right. You're right. It's the same thing.

30:01

Sjors Provoost Yeah. So it's a way of you can pay to a script hash, or there's probably other things you could have done.

30:07

Aaron van Wirdum CheckHashVerify so okay, so and you wanted to explain how that works kind of you have somebody so again this one wasn't implemented so if you are the listener don't really understand how this works it doesn't even matter because it's not in Bitcoin but if you're interested and curious anyways Sjors is going to explain it to you now

30:25

Sjors Provoost Well, not entirely. so it's I think it's BIP 12, right? That was the number given to it?

30:29

Aaron van Wirdum [unclear], right, oh, the debate was between BIP 16, which was pay-to-script-hash, and 17.

30:35

Sjors Provoost So I don't completely understand how it works. All I know is that it did not use the templating technique.

30:40

Aaron van Wirdum I think OP_EVAL may have been 12.

30:42

Sjors Provoost And it didn't exactly, it didn't use eval, but it did something similar to eval. I believe it was still treating text as code, but I might be wrong about that part. In any case, one of the things that it contained was the legendary OP_CODESEPARATOR. And maybe one day we'll do an episode about OP_CO… OP_CODESEPARATOR. I don't understand it. I think it generally gives people two kinds of responses either they don't know it and so they have no response or they know it and they fear it.

31:13

Aaron van Wirdum This is something that is in Bitcoin or is not?

31:15

Sjors Provoost Yes, it is in Bitcoin it's been there from the beginning it led to the arguably biggest bug ever which is that in the beginning you could take everybody's Bitcoin right, and that had to do with the OP_CODESEPARATOR

31:26

Aaron van Wirdum So it's non-standard?

31:27

Sjors Provoost I'm not sure I think it has become non-standard or at least it's become non-standard in certain circumstances. What you do see is a trend that newer soft forks like SegWit and Taproot will make more and more restrictions on that particular opcode. And there was a proposal by Matt Corallo, the Great Consensus Cleanup in 2015, that would also make it invalid outside of SegWit or P2SH. Though that was controversial because there might be some really obscure transaction out there that uses it. So anyway, that is really almost FUD. I guess I'm saying, oh my God, this proposal had this evil opcode inside of it, but it did. But at the time, of course, it wasn't probably considered that evil. So really the question was, you have three proposals. Which one are you going to pick? If you ignore all the drama, it's like you have three options. What do you want to do? You also kind of want to do it quickly because that was sort of Gavin Andresen's point. People are losing Bitcoin left and right. We should have more advanced scripts functionality. I don't know how much of that actually got used in the end.

32:32

Aaron van Wirdum Well, let me... Well, I mean, pay-to-script-hash is used all the time, right?

32:37

Sjors Provoost Well, if you ignore wrapped SegWit, it's only used for very standard multisig transactions. Probably two or three that you could have done with raw script.

32:47

Aaron van Wirdum That still counts.

32:48

Sjors Provoost Yeah, but you could already do those.

32:49

Aaron van Wirdum That was the whole point.

32:50

Sjors Provoost You could already do those with bare multisig. If you just make them standard and invent an address format. Was it worth the risk? I mean, that's the question, right? Is it worth making a change to Bitcoin that could break it and almost would have broken it in the case of OP_EVAL?

33:04

Aaron van Wirdum Well, I want to first ask, so I want to take one step back because I feel we moved on before I was ready to move on.

33:15

Sjors Provoost All right.

33:16

Aaron van Wirdum Okay, so how does CheckHashVerify, Luke Dashjr's alternative proposal, if you know this, how did it... prevent the loop-de-loops?

33:26

Sjors Provoost I'm not sure if it did. I think it did.

33:28

Aaron van Wirdum I mean, it must have, right? Because that was the whole point.

33:31

Sjors Provoost Yeah, I think you somehow split the script and the hash of the script in a way that it's interpreted, but you're not using eval, so you can't duplicate it. So eval would allow you to sort of recursively duplicate stuff. But I think here you would just have the hash and then you would have the script itself. And so I don't think you'd have any recursion or loops.

33:53

Aaron van Wirdum So, yeah.

33:55

Sjors Provoost As you can see, I did not study that proposal in enough detail.

33:58

Aaron van Wirdum That's fair. Yeah. So I think the real situation, I think OP_EVAL was kind of done, right? So then the debate was really about pay-to-script-hash and CheckHashVerify.

34:09

Sjors Provoost Yeah. And then, of course, the pay-to-script-hash was implemented by Gavin Andresen, who also implemented OP_EVAL and who was the lead maintainer. And so that probably got the most attention, the most review. So that also tends to... give it a little bit of favor almost by default if the other proposal was equal.

34:26

Aaron van Wirdum Yeah, and also very important to… there was ultimately kind of really a vote between the two and that was a hash power vote. Like the miners could pretty explicitly just vote for one or the other.

34:42

Sjors Provoost Yeah, I think both of them kind of went on a campaign. Well, I think Luke Dashjr used his own pool to campaign for it, and they may have also convinced some other ones. So there was something similar to BIP 9, though much more primitive. The idea that miners would put something in the block to say that they liked the proposal or not. So that happened.

34:58

Aaron van Wirdum Yeah, well, it was kind of a precursor to BIP 9, I would say, where BIP 9 was more about readiness signaling. Like that idea kind of evolved over the years. Well, for the very first time that hash power signaling was used, it was arguably actually like a vote. Like that's really what happened the first time, I would say. So that's how pay-to-script-hash got, at least is what I remember.

35:23

Sjors Provoost That was one element in it. But I mean, there was debate about whether that was a fair way to do it at all, because the lead maintainer, of course, had access to many more miners. So from a social point of view, he would be able to get more votes that way because miners would not look at the actual code. But there was another thing that happened, which I think was more fair, at least relatively. I think after some of the initial heated debate died down, some of the core devs at the time, so Greg Maxwell and Pieter Wuille and a few others, were asked to like take a few days, look at all three proposals and... Then they made a little chart or a little diagram.

35:57

Aaron van Wirdum Oh, that's right.

35:58

Sjors Provoost Yeah, saying like, okay, this one I'm absolutely against or this one I have a preference for or this one is fine. And if you look at that little table, the P2SH also won in the sense that nobody was completely opposed to it, not even Luke, although he really didn't prefer it. And it had the preference of some of the experienced developers.

36:20

Aaron van Wirdum Yeah, I'll include the article Rizzo and I wrote in the show notes where this whole debate is sort of outlined.

36:27

Sjors Provoost Yeah, so I think it was a little bit more to it than just a brute vote, basically. There was more review on it. But nowadays, I think if you had a situation like this, it would not be resolved in a matter of days and weeks. And back then it was resolved in a matter of days or weeks, which is way too quick for something like this.

36:46

Aaron van Wirdum Right, yeah. And I think, well, actually, I'm sure. Fairly sure that, for example, Greg Maxwell, who at the time… preferred BIP 16 has come around and said that BIP 17 would have actually been better.

37:00

Sjors Provoost Okay, I didn't know that so that that could be the case.

37:02

Aaron van Wirdum Yeah, in in Greg Maxwell style, he just you know mentioned this off the cuff in some Reddit comment somewhere, so it's hard to find but I'm pretty sure that I'm like sure that he did it I just don't really remember the reason or the motivation.

37:17

Sjors Provoost Right, I I read a post by him during these days where he was articulating very well why he supported the P2SH variant that we have now but obviously you know as time goes by he could have changed his mind on it.

37:30

Aaron van Wirdum Yeah, okay, so we've discussed what pay-to-script-hash is both in a sort of general sense as well as in a more specific sense of the proposal that we actually call pay-to-script-hash and that we use today.

37:43

Sjors Provoost And to make a very quick jump to the future we still use this pattern Basically in the way we, in SegWit and in Taproot, the idea of there is a hash and the blockchain magically knows that this hash should be revealed as a script and then the script should be executed. So this whole idea of pattern matching is still there. And so apparently it's good enough to keep repeating it. Though there is also the argument of not writing new code if you don't have to.

38:11

Aaron van Wirdum Yeah, well, I was going to say, so we've discussed P2SH more generally, more specifically, and then also some of the proposals that were rejected OP_EVAL and CHV. Gave an idea about the historical context. Is that the episode, Sjors?

38:28

Sjors Provoost I guess there's two little things that might be fun to mention briefly. One is that there was some screw up with the deployment. At least that's what I read in the overview of soft forks in general. Apparently, because of this mechanism, which was kind of a vote, but also kind of an automatic activation, depending on how you run your software, a subgroup of miners... activated the software too early, and so they started rejecting blocks basically they they I think they I don't know if they activated OP_EVAL accidentally, or they activated P2SH but anyway they did it too early and they weren't in in the majority, so they couldn't enforce the software, so it's like an accidental UASF except they weren't like super passionate about it so they just went on I mean at the time Bitcoin was worth nothing so they didn't worth… they didn't lose like a giant fortune. And the other thing that is probably fun to mention is I think the software was backported to even older versions of Bitcoin Core, like version 0.3 and 0.5. And apparently something went wrong doing that. So those versions of Bitcoin Core would actually also fork themselves off the network. So they had to be sort of undone again. So I guess the question to Jameson Lopp is, was that a hard fork? But I don't know.

39:44

Aaron van Wirdum Yeah, there's so this was the pay-to-script-hash was the first soft fork after Satoshi left so it was also the first protocol upgrade after Satoshi left so it was the first time that developers started to figure out how to actually upgrade Bitcoin and who should do what what… so that's a you know that's kind of the start of that whole debate and it's very interesting in that sense but I would also say that's like a whole other episode or…

40:15

Sjors Provoost Yes.

40:15

Aaron van Wirdum So as far as pay-to-script-hash itself comes, I think we've kind of covered it at this point right?

40:22

Sjors Provoost I think so. So okay, in that case, thank you for listening to Bitcoin,

40:26

Aaron van Wirdum Explained.