 All right. Well, hello everyone and welcome to Sailor Academy's Bitcoin for developers unit number unit number three You got to hear a little bit of the behind-the-scenes because while I unmuted you I did not so hello everyone and Thank you so much for joining us. I hope if you have not seen the previous two videos in this series You could check them out on in the playlist below and also just to mention this is a supplement to Sailor Academy's CS102 so you should definitely Find the link below to the course and start taking the course and catch up with us Of course if you're watching a long live feel free to leave questions in the chat and we'll get to those You know as soon as we can but with that I'll just hand it over here to hand and we can get started All right. Welcome everyone. This is unit three and as Mike said Please do ask lots of questions in the chat and as we go through this I'll pause periodically to answer any questions that you all have put in the chat So, please do ask lots of questions That you know helps us all learn stuff and just makes it more fun and more interesting if we are you know Having a conversation rather than just me sitting in an office by myself talking to my computer screen It makes it much more fun. So let's dive into it unit three Covers what is a digital signature covers? Introduction Bitcoin transactions and how Bitcoin transactions use signatures but what we'll focus on today is we'll really get a bit in depth on pay to public key hash transactions and I think if we really Dive into that and focus on walking through that here today that will kind of cover No, help us get a understanding of you know, all of these topics here that units three covers And again, this is supplementary to unit three of the course So we're not going to cover everything that's in unit three But here today we'll focus on pay to public key hash transactions and that's really still the I think the most common type of Bitcoin transaction and It's not quite the original but like it does just call it like the original form of Bitcoin transactions So if we really grasp what's happening in P2PKH Transactions, then we can they gives us a really solid foundation for understanding You know, what's going on and all kinds of other transactions. They're now on the network So let's go ahead and get into that again digital signatures Hope y'all can't hear my neighbor's dog going nuts This is a lovely graph we have here showing, you know, some elliptic curve Mathematics some graph there of that which is quite cool So we did in the last unit unit to talk a bit about Cryptography and public and private key pairs in the different ways the different cryptographic functions that we use In Bitcoin and we'll come back to that a bit more and we'll focus in a bit on signatures And at the end of this session today, we'll do a quick example where we'll go through Creating a signature and validating it in Python But we're not going to dive too heavily into the math again. There's you know description of Elliptic curves math mathematics and how it's used to generate public and private keys and all that good stuff that we cover In the course from the book Mastering Bitcoin But then again, we're not we're just going to need to be aware We just need to be aware of what this stuff is and that'll help us work with it I'm not gonna dive too much into the math Okay So let's start first with a bit of a history lesson pay to public key hash is The most common type of Bitcoin transaction not quite the original because at first there was pay to public key But for some security and privacy reasons that with pay to public key hash also helps Compress the data that goes in the blockchain. So for a couple of those reasons pay to public key was changed to public key hash But it's I think it's helpful just to think about you know pay to public key because this Is a very Sort of a more simplified version and if we can understand kind of what was the thought process was is You know Bitcoin is the system that's built on this, you know public key Infrastructure right this idea that you have this Public and private key pairs right and everything's kind of built up Upon that so the idea is originally as we walk through these transactions You can kind of think of maybe the way satoshi was thinking about this is like Okay, I have a public and private key pair. I'm keeping that private key private that public key is public And if someone wants to send me bitcoin, they essentially send that bitcoin to my pub key Right and kind of lock it to my pub key And then once that bitcoin is locked to my pub key The only person that can spend it is me because I have the private key associated with that pub key and that key pair And the only way to spend it is to have the network agree that this is a valid transaction is if I produce a signature On that transaction that we can all validate came from the private key Associated with that pub key that the bitcoin is locked to right so that's that's basic idea of you know, how this Simplified form or the most simple form of bitcoin transactions work is that we just all have these pub keys Right and people just send bitcoin to your pub key And then you are the only person that can spend it from there that can send it to someone else's pub key Because you're the only person assuming you're the only person that has that private key Then you're the only person that can produce a signature That's necessary to create a valid transaction to send those funds out to someone else's pub key, right So I think it's helpful To know that history to understand that that's the basic fundamental ideas. We can lock Bitcoin to pub keys and then only the person with the corresponding private key can create a valid signature to send it to someone else's Pub key right to lock it to someone else's pub key. That's the basic Form of transactions and then it's just gotten, you know, all kinds of different complexity and different functionality have been added to it from there Including right at the beginning when paid a public key became paid a public key hash again. This was Instead of sending funds to just someone's pub key you would send it to you know an address Which is basically a hashed and formatted version of a pub key There's a number of different reasons for that to make it, you know Even more difficult to sort of attempt to reverse engineer that right because it's not directly the pub key It's the hash of the pub key Also, the different types of hashes used to help compress the data. So you have less, you know, you're taking up less space on the blockchain Here we go. This is uh, just what I was talking about you have this pub key First you hash it with Shah 256, which you know is a very very secure hashing algorithm Right, then you hash it again with this ripe mb 160, which makes it a shorter hash, right? Then that goes through some formatting This base 58 check encoding Excuse me, which we'll talk about later in the course Or is discussed later in the course and then that creates this address Right So it started with this very simple idea that you're just going to send funds to someone's pub key They can only unlock it with their corresponding private key But we you know for security reasons and you know data blockchain chain space reasons, right? Added this complexity to it. But that's the basic idea All right. Now here we are I just want to show you this to So that if you come across You know as you're working with this tech you're going to come across with different types of bitcoin addresses So you should have some familiarity with what all these things mean I'm here We're just going to be talking about this first one pay to public key hash That address as it's formatted here as it goes through this whole process Comes out at the other end looking like this starting with the one The next sort of upgrade in bitcoin transactions was a pay to script hash Which is how you get multi sig which is all sorts of really fascinating stuff But we're not going to talk about that today But that starts that address starts with the three After that we had You know segwit, right? And then you get this p2 pay to witness public key hash, right and those addresses look like this bc1 Q right and then the latest sort of change to bitcoin transactions was the pay to tap route transaction, which looks like this bc1p Again, that's outside of the scope of what we'll talk about For today and a lot especially this one is pretty new But it's just helpful for you to keep this in mind that you know bitcoin transactions You know you have this formatted address and they will look different Depending on what type of address what type of transaction that is so you might see addresses on the bitcoin blockchain That look like this these different types of formats Okay So before we start walking through a pay to public key hash transactions I'll pause in case we have any questions on that bit of history there Mike, do we have any questions? Um, we don't have any questions right now some people saying hello in the chat Hello there everybody and again if you have any questions feel free to go ahead and leave them down on the chat and I will I'll let you know If any come through Okay, awesome. All right. Let's dive into pay to public key hash transactions and here One of the most difficult things about trying to teach You know bitcoin and bitcoin technology and how this all works is trying to decide Where to the start right because there's so many different Interconnected concepts and technologies and techniques etc that are all working together And to understand that you kind of have to understand the whole constellation of things and that are you know being used But when you're teaching it when you're trying to explain it You kind of just have to pick a starting point and it's difficult because everything's kind of dependent on everything else So here what I've done is I've just picked a method for walking through this What we're kind of going to come back and forth on some of these things and touch on things briefly And then come back around to them because they all are interconnected and there's no clear direction to start with this concept And then lead up to that one So we'll bounce around a bit and I think often also because these are you know, this is New technology and there's lots of different components at play and you kind of have to understand all of them to get the full picture And that sometimes it takes a number of passes at it Um to to fully grasp it. So we've already gone through unit three or if you've read some of you know Mastering bitcoin second edition that we reference in the course and watch some of the videos you might already have an idea but Well, a lot of these things and every time I'm learning something new To bitcoin you know with taproot stuff like I'll read the same things over and over again until it like properly settled into my head So we'll come back and forth a few times All right. So again, um a key pair is created, right? So we're kind of going to walk through the process of a pay to public key hash transaction, right? So a key pair is created That is a pub key and associated private key right All right, the public key is hashed and formatted to create an address Okay, we got that funds are locked to that address, right? So if I've gone and I've instructed my bitcoin software to create a key pair And then take that public key and format it into an address Then let's say Mike sends me some bitcoin. He would just broadcast to the bitcoin network. Hey, I'm taking these, you know Tiny transaction I'm taking his 100,000 satoshis and I'm going to send it to this address, right? So what happens is he locks those funds Creates a locking script um on broadcast that to the bitcoin Network it gets, you know mind in a block it becomes part of the blockchain There are funds that are locked to my address, right that is associated with my public key or produced from my public key All right a transaction that spends funds from that address needs to include a signature that was produced From the private key corresponding to the pub key that the address was created from All right, so I go through this process. I create an address. I Tell Mike about my address. He sends funds to that address locks them on the bitcoin blockchain to My to that address that I've produced But let's say I then want to send some of those funds to my friend, uh, daniella Right in order to send those funds that are locked to my address and then Relock them to her address. I have to first unlock them and in this, um, you know Pay to public key hash type transaction What I would do is just produce a transaction that says, hey, I'm taking this bitcoin here And I'm sending it to this address over there and then I would create a signature And that signature in order for that transaction to be valid can only have been created by the private key Corresponding to the pub key that was used to create that address, right? All right, um the signature That signature from my private key unlocks those funds so I can send them to daniella and relock them to daniella's address All right to understand Real quick we had we had some questions back there I wanted to see if you get through to see if maybe they you've already answered them for people But there we have two questions here about Basically about it seems about the con the concept of older addresses interacting or targeting newer addresses Hmm So like the idea of being if I have funds locked to a pay to public key hash address Can I take those funds and send them to a pay to tap rich address? Uh when train Getting more stuff in but uh, yeah, the so the original question was Uh, is there any specific? Is there any special consideration? Uh When transacting with an older address? type when targeting a newer type Okay, so yeah, I think that's that's kind of this question Let's say I have funds locked at a pay to public key hash transaction But daniella is now want sends me a pay to tap rich address right and how would I go through that process? So we'll look a bit in a minute. We'll dive a bit more in depth with the Locking and unlocking but essentially yeah, that wouldn't be a problem because in that bitcoin transaction What I would do is I would have to provide a valid unlocking script Right for the funds that were locked to my address So I would have to provide that signature that satisfies that locking script But then in the output of that transaction where I am sending funds to daniella's Pay to tap route address I would create a locking script that is appropriate For that address right and talking about locking scripts for pay to tap route is Addresses is like outside the scope of the course. Um, what we're doing today But essentially that's not a problem. I just have to provide an unlocking script Which we'll see in a second here as we go to here This input right here's the unlocking scripts. So I would have to provide an unlocking script that is appropriate for that pay to Public key hash address and then in the output this new locking script Then I can create a locking script that's appropriate For the address that where daniella is receiving funds I hope that helps there any other questions Mike Well, you're either on mute or there are no questions. So I will move on All right, I'm sorry about that Uh, they can hear me. Um, yeah We I think I think we should probably move on just because we there are some questions But I think having seen the slides they might be answered going on So if your question isn't answered in the next couple slides, please please restate it and we'll get back to it Gotcha. Okay. Let's do that. Okay. So we've gone through this the whole, you know funds are locked my address I have to unlock them to send them on to daniella's address, right? All right, so to understand the locking and unlocking locking Let's go through inputs and outputs and the utx o the all important utx o So let's look at this picture, which is one way to envision utxos and again This is like this circular sort of thing where we're going to talk now about utxos But we might come back to it in a bit because these all these concepts go together All right. So first of all a utx o is an unspent transaction output So let's go back to that example I have created this key pair from that pub. He have produced an address might have sent funds to That address and locked them to that address that I produced So that is an unspent transaction output. So again, if we come back here and look at the structure of transaction I'll dive in depth in this in a minute But here is this output right some sort of input and there is this output and here this is You know funds being locked to You know locked with this particular script, right? So they are locked, right? It's an output That is locked with a particular locking script But is out on the blockchain and in theory it is spendable bitcoin, right? If someone can produce the appropriate Unlocking script it is spendable, right? So this is important because so much in bitcoin we think about utx os, right? These are basically chunks of bitcoin on the blockchain that could be spent So let's look at here. Let's walk through this example from the mastering bitcoin book We have this is like a whole block here. This is numbered, right? And this is another block and this is another block, etc So in this block, we had a whole bunch of transactions, right? So Joe here had a utx o But he took that output that he had and he used it to create a transaction to alice which creates another output, right? Then alice took, you know, and that happened in this block and then alice used that Output that she had to create another transaction where she sent some funds to bob, but some back to her right bob and and so here she has this Funds that she potentially could unlock in use. This is a unspent transaction Output a utx o bob then goes and takes his utx o and uses it as an input so we spend it But he produces two outputs one back to him and one to go past here, right? So as we look at this we want to track, you know, once you have an output Once that is used as an input on a transaction. It is then a spent transaction output, right? And so in the bitcoin worlds, we're thinking about a lot of this stuff We're generally much less concerned with the spent transaction outputs than we are with the unspent transaction outputs Like, you know, your bitcoin node Will track all the unspent transaction outputs it can find right it's going to find all of them on the blockchain It's going to track it and like keep like, you know Keep a track of those and keep an eye out for them to see if anyone wants to spend those So utx o is just bits of bitcoin that could be unlocked and transferred elsewhere, right? And you can use this picture to imagine it if you like all of these ones in red are Um outputs that were then used as inputs They're spent bitcoin that bitcoin is moved on from that address and then all these ones in green Are these outputs like funds that'll lock somewhere that could potentially be spent right So well with that understanding of utx o's we'll kind of dive back into our whole process walking through this transaction process All right We're not going to go too in depth with this at the moment because we just want to get the concept of inputs and outputs And then in a couple of slides we'll come back to this picture here and really walk through it in a bit more depth But again, what's in what's important to pick up from this slide is just the idea that all bitcoin transactions Have some inputs that are used for them, right? This was a utx o here Right when mic sends me that bitcoin and it's locked to my address That's utx o and then if I want to send daniela some funds I can use that utx o as an input And then in this bitcoin transaction create a new output. So this is now no longer a utx o It's a spent transaction output. We don't care about that anymore As long as it's valid, right? And then what we now care about are these other these two outputs which I created here Perhaps one going back to myself and then one going on to daniela, right? And these are our new utx o's So we consumed one utx o here as an inputs and produced these two New utx o's as an output. This is no longer a utx o at spent Now we care about these two utx o's these two outputs unspent transaction outputs Okay, so every bitcoin transaction is kind of going to go through that process inputs Outputs consume a utx o create new utx o's right for that process. All right So a couple of things about Um utx o's and then we'll pause for questions and then we'll dive a bit more into depth on This um image here this process All right transaction outputs consist of two parts an amount of bitcoin denominations Toshis the smallest unit of bitcoin Um an cryptographic puzzle that determines the conditions required to spend that out of course Okay, so if we come back and look at this we can kind of see that's what's going on here We have a value and we have this locking script Which we'll talk about more in a minute. All right So it's also known as a locking script a witness script for a script pub key Actually in here. Let's talk about it. This says script pub key um and script sig right script signature and you can see that this is what it looks like in the bitcoin You know bitcoin core code And it got this name because of this original type of transaction, which is the pay to You know pub key pay to pub key hash right So with that in mind, I guess when the bitcoin was written I guess the toshi was only envisioning this type of transaction right so script pub um key It's like saying i'm locking the the bitcoin to this public key right and in order to spend You know that bitcoin you have to unlock it with the signature right but of course now because we have much more complex versions of bitcoin transactions that terminology isn't really entirely accurate So we often I tend to use just the term locking and unlocking script right but witness script or script or script pub key etc Like there's a different terminology used for this. I prefer locking and unlocking script. So that's what I use All right, each input contains an unlocking script and refers to a previously existing utxl All right, the validation software will copy the unlocking script retrieve the utxl reference by the inputs and copy the Locking script from that utxl the unlocking and locking script are then executed in sequence. Okay, so that was a lot But we'll walk through it in a bit more depth In the coming slides in just a second. All right When we say that a user's wallet has received bitcoin What we mean is that the wallet has detected on the blockchain a utxl that can be spent with one of the Keys controlled by that wallet right your wallet is like scans the bitcoin blockchain is like a ha That utxl I think I can spend it if I want to so that then your wallet says that's part of my balance right All right Thus the user's bitcoin balance is the sum of all utx those of the user's wallet can spend in which may be Scattered among hundreds of transactions and hundreds of blocks right So the idea of telling you this here is that you know, if you get a bitcoin wallet and install it on your phone and open it It's kind of going to say here's your balance right and this is your wallets making things really easy for humans That's actually not what's happening on the bitcoin blockchain. Things are scattered these utxos. I'll be back up to this picture These utxos might be scattered over a number of different blocks. We can see alice has three Different utxos right which are scattered over, you know, all these different blocks Here and then but this might be the current utxo sets down here So her wallet might be aware of this utxo this utxo and this utxo and it looks sort of Different on the blockchain than it is presented for alice in her wallet Okay, so Now that we've talked through that we'll dive more in depth into what's happening in this particular bitcoin transaction, but first If there are questions that uh, we've not yet properly answered We might answer them here, but mike if we've got any interesting ones, please let me know Um, well, we that we had a question about utxo. So I hope that uh that that was that uh that helped to answer that one. Um, uh You know what while we just go on i'll i'll i'll collate some and uh, if you guys have questions as we go along After this next slide i'll i'll be back with some Okay, awesome. Thank you sounds like a good plan All right, so here, um, we're going to Um, this as you look at this it's kind of confusing right first time you look at this You're like wait, where's the bitcoin and what's happening and what's this be out in sequence and I don't get what's going on here, right? um, and what we need to keep in mind is the bitcoin software is always trying to preserve Um space in the bitcoin blockchain, right? So we want to broadcast and record absolutely as little information as we need to so we don't bloat the bitcoin blockchain, right? so A lot of what's happening here is just reference to other things For example, let's look at this input right and this v in these are the inputs being used For this transaction, right? So again, if this is that um mike has sent me some bitcoin and I want to send some of that bitcoin on to daniella I what the This might be the transaction that my wallet puts together for me, right? And it's going to say Hey, this is the inputs i'm using this is a utx o which i'm going to consume and thus destroy to create new utx os, right? So this is the utx o that i'm going to use I'm not going to tell you all the information about it because that would be a duplicate all the information about that utx So already exists in the blockchain. So all i'm going to do is just point you to it This I forgot that this is just an image and I can't highlight it. Maybe I will go Here There we go. Haha. Okay. So and then maybe I can zoom in a little bit Control plus Thank you mike. That was it. All right So hopefully you can see this a little bit better But the idea is is i'm just going to tell all the bitcoin nodes that I broadcast this transaction to them like Hey, I'm not going to put all the details of this utx o here because i'm just going to tell you where to find it You go back and look at the blockchain and you look for this transaction And you look at this specific outputs of that transaction because we can see here Transactions can have a number of different outputs, right? So if you look at the very first Outputs of this transaction, you're going to find the utx o that i'm talking about and that I want to use And so, you know someone else's bitcoin node Right, it's going to go out and say oh, okay. All right. I have to go find that utx So here it's going to find another transaction that looks very similar to this It's going to say right here's the first output of that transaction here is the locking script So i'm going to copy that right Then i'm also going to take this unlocking script You can see it's kind of big here. There's a lot going on It's going to copy this whole thing boom and then it's going to run those two things together We won't talk too much in depth here about bitcoin scripting because we cover that in depth in unit seven But the idea is it takes the unlocking script It puts it together or it takes the locking script It puts it together with the unlocking script and it runs those two together and see if one can satisfy the other Right, if so then my transaction to daniella is valid, right? So let's look a bit at what's happening here You can we'll stick to this one here. So this is complex, but hey, basically it's saying i'm locking this to this public key right Again, we'll get into this in unit seven kind of simplified But then it's saying hey I have to satisfy this this big long thing here is going to be a signature Right and we'll talk a bit more depth and signatures in a minute And here is the pub key, right? So these look a bit different. They're all formatted a bit different again unit seven We'll talk about that, but that's basically what's happening. So if we come back here And we look at this one all right here is um More in-depth view is that locking script and that unlocking script And we'll talk about how these are run together also in unit seven But basically it's saying hey do all these things to format this pub key right this hash of a pub key, right? And then I have to unlock it and spend it. I have to produce a signature That was that can be validated Um using this pub key right so signature that was produced with the private key corresponding with this pub key Right and these have to match up. So I have to prove that I can produce a signature That um is from the private key corresponding with this pub key, right? That's what I need to do to unlock that utx o and send some funds onto daniella right So that's roughly what's happening here Let's go through some of this because you might be still looking around going why is there all kinds of different things This is script version again. That's for later lock time. That's time time locks and all this stuff very Fascinating stuff. That's for later This v in these are all the different inputs that we're using It might be that I want to send daniella more money than mike sent me Right, I want to send her more bitcoin than I received from mike So I have to use another utx o that I got from charlie earlier, right? And I have to put those together to then be able to have enough funds to send on to daniella, right? So I could have a whole bunch of inputs. I could have one or many many many outputs, right? But here that's what's happening. It's we're for every Input we're referencing a previous utx o via this and we're producing An unlocking script which in this case is a signature But it could be a number of different things and we're producing an unlocking script in this sequence again That's used for time lock sort of stuff. Um, which is outside scope for today But that's just for more complicated transactions Okay, then let's come down and look at the out the out is a bit more human readable, right? We can see exactly how much we're sending, right? Maybe this is coming back to myself and this is the funds i'm sending on to daniella And we would know here Even though it's not expressly stated if this is a valid transaction Then this utx o referenced here would have to have at least this much Bitcoin in it, right? So zero point zero Nine nine, you know, whatever it is, right? You add those two up any difference between that input and those outputs is assumed to be a Transaction fee which goes to the miner Right and again, we're putting as little data as we need to in the blockchain So when you see this transaction if you're trying to read this yourself as a human or your bitcoin node it's going to have to Sort of create a lot of the data like go reference other things and piece together The complete picture of what's happening here because again, we only give absolutely as much data We have to not drop more, right? So we preserve that space in the blockchain Okay And then looking at this v out, right? This would be output zero This would be output one Right and we have a value which is nominated in satoshis And then we have this Script pub key, which is the locking script, right? So this in this pay to public key or pay the public key hash format It is essentially the pub key That you're locking funds too But because we now have much wider variety of bitcoin transactions, it's the locking script Right and we'll stick with that for the moment Okay, so that was a lot That is, you know, let's just hold it here on this slide. That was a lot that's a lot to Sort of digest again, like I said, you might want to come back to this or you know Check out like rewatch one of the videos from unit three. There's a good one there where andrea talks through this This is also, you know Chapter six of mastering bitcoin goes through this, you know, so if it does it all quite gel for you at the moment Just, you know Come back tomorrow and come at it from a slightly different angle and hopefully then that makes it all Gel properly in your head. But also what helps is asking questions. Mike. Do we have any questions at this point? Mike is perhaps on mute All right, I like that one cool questions. Okay So signature verification, who's doing the signature verification? And again, this goes back to unit one where we talked about everyone needs to know about everything on the bitcoin network So who's verifying the signatures? Everyone everyone is So what happens with the node is if i'm if i'm a node, right? And i'm sitting there and i'm watching this transaction, you know go from, you know Hannah to daniella and i'm like, is this valid, right? Every time I see a new transaction come in I'm going to look at it. I'm going to go read the data. I'm going to go gather all the other information that I need I'm going to, you know, create a complete picture of that transaction And then i'm going to validate it. So every node On the bitcoin network is validating the signatures with every transaction, right? It gets a bit more complicated, but um Every single transaction is validated by every single node on the network So i'm going to go and i'm going to run and we'll do this in a minute in python So we can kind of get a feel for of course this little difference In bitcoin a bit more complex. We'll get a feel for doing it in python um And so i'm going to validate it right and i'm going to run it through my software and it comes out with Hey, that's that signature matches, you know that pubkey and that message excellent. Boom. That's valid I'll then broadcast it on to other nodes, right? So if something is invalid It's gonna, you know, the first node to see it is going to see it as invalid And then we'll refuse to broadcast it on to the rest of the network So an invalid transaction with the bad signature isn't going to get very far on the bitcoin network So I hope that answers that question then to the question of can you disagree? um And the simple answer is no right because if we had a case where there was a transaction where some nodes thought that was valid Right, maybe that we're using See these little things dupe or off dupe off hash 160, right? These are our op codes, right? So they're um like functions that the bitcoin software runs It's to check on this stuff and if someone was perhaps using an op code that one node Thought existed and another node thought was invalid and didn't exist If that had ever happened, right you would then wind up with a split in the network, right? That would fork bitcoin into like two different versions So essentially No, we can never disagree because if we disagree Then we we kind of fork the whole network So this is part of why updating bitcoin and changing it is so contentious and slow and difficult because it's pretty catastrophic if we wind up with a situation where nodes Disagree about what is there is not? A valid transaction and we'll talk more about that In unit eight the short answer is no we never disagree. We hope So Mike any more questions No, we're all right. All right. Thanks Mike. Okay, so hopefully now we all have a pretty good idea of what's going on with UTXOs and locking scripts and unlocking scripts and pay to pub key hash transactions and inputs and outputs Right, that's the idea is that the goal for this session is have you all like form that picture that map in your head of what's the flow of what's happening here and what are the necessary components and How a node puts this data together, etc So hopefully we've achieved that if not, please do come back and watch this stuff again sometimes I mean for me it usually takes like three passes or something All right. I think we're going to skip this for a moment This is just a link to go look at I think would I try to find this transaction in the bitcoin blockchain? but I think we'll move on to trying our exercise to Sign and validate some data because that's really fun. And then maybe we'll come back to poking around a bitcoin walk explorer if we get the time so Let's move on to our exercise and what we're going to do is sign and validate some data, right So if you're following along with this again, you want we're going to do this in python Um, you can install python on your machine. I'm on a ubuntu machine here So that's the examples that I'm showing. I have a python installed on my ubuntu machine You can also, you know run this And you know all kinds of different You know websites will let you run python in the browser, etc So there's lots of different ways you can follow along with this But Importantly what I've already done and what you'd need to do to follow along with this is make sure that python knows to go and download this specific Library so ecdsa elliptic curve something signature algorithm I can't remember exactly what it is, but this is the mathematical library Right that we need to do this this math this complicated math And i'm downloading this one because there's other ways you could do it in python Or we could do signatures and we don't need this specific library, but i'm using this one because this is The um, it creates the type of signatures That bitcoin uses right so this s e c p two five six k one Right this long complicated thing. This is the type of signature that bitcoin uses so Here i'm just telling you how to get the library. Um, you would need if you want to work with That bitcoin variety of signatures now there are the ones, but this is the one we're talking about today It's sort of the original Okay, so I have um this Code hold up here. I'll show you in a second All right, but we're going to do first is we need to generate a key Specifying which curve to use and this is the the curve right the type of math that we want to use to create the signature I said it um in uh last week and i'll say it again today Don't roll your own crypto right don't go try and creating your own curve Don't try and create your own math here unless you really want to be a cryptographer right then definitely do that But if you're not going to focus on being a cryptographer and working on these algorithms Just use a well tested, you know well used Common library because if you don't you wind up causing yourself a lot more trouble Then you're able to fix Or it could be you know more a lot more trouble than it's worth. Okay So here we go. I have this file. I'm just going to uncomment as we go First we can see that we are importing this signing key like this particular The library to you know do the math that is necessary for this particular variety of Signature right So from this library here. Okay, then we're going to um generate a key Is signing key so Here we've been talking about public keys and private keys, but when you're talking about signatures They're often called a signing key and the verifying key We can just remember that the signing key is The private key and the verifying key is our public key. All right So first thing we do is Generate the signing key right generate the pub key. I'm sorry Private key so always when we're generating these key pairs. You start with the public key Bank I keep getting it backwards. Let me try that one more time when you're generating these key pairs You always start with the private key and then that you know a public key is derived from the private key So the first thing we do here is generate a private key or a signing key You're using this specific math here in that library Then we'll go and use that um to create this verifying key So we take this signing key Run this function on it and produce this verifying key or a pub key All right now um to Oh, not that one. Um, all right, so There are sort of Three components we need to think about here at least when verifying is we need to have The message the signature and the pub key right so we need to create this message right now This is where bitcoin transaction this lovely phrase here. Not your keys. Not your coins We could replace that with an actual Bitcoin transaction with inputs and outputs, etc um But here just for demonstration. We're just creating a string my simple string which we'll sign So here it's not your keys not your coins All right, so then Once we've created this message, right? This is the creation of our message But then we'll use the signing key Which remember is our private key and we'll run this function on it to sign it And that's going to produce a signature, right? And let's come back here for a moment And look at this. This is one of my favorite Graph graphics to use when talking about this stuff But before and we were talking about last week We're talking about a difference between encrypting data signing data and hashing data. Remember when we are signing We're taking the message We're not altering the message. We're not really doing anything to that. We're just producing An additional bit of information, which is That signature, right? So when we go to validate, we're still going to take the original message We're just going to add to it that signature and then we're going to validate it against the pub key Okay So let's come back here All right, so now we've done that. We've imported our library We've generated the private key or the signing key. We've generated the pub key or the verifying key We've created a message and we have produced a signature Now let's print that out so we can have a look at it So i'm going to save this I'm going to come over here to terminal Hopefully y'all can see that pretty well. I zoomed in a bit today and then There we go. I'm going to run this file and hopefully it's spit out a signature Ta-da, it doesn't look very nice, right? Because that's readable for computers Not for humans, right? But that is our signature. Okay, so That's what we spit out on the screen, but we have all this different data We have keys. We have the message. We have the signature All right, so now let's go through and try to Validate that this is um A valid signature, right? We're going to go through and check this And look at this, you know, because it's python and we're trying to sort of do all these different things So it's kind of difficult a little bit difficult to read it Let's go look at the different components that we're putting in Like the different components different bits of data that we need to validate the signature All right, the first thing is our message not your keys, not your coins Exclamation mark, right can't change anything about that that message or it will no longer be valid All right, then we have this bit of data which is the signature that we just created and spit out on the screen And then we have our verifying key or our pub key, right? So we have those three bits of data And then we can put these all together and fancy math happens and if they all match up Right, we can be pretty darn sure that this message was um signed By the person that holds the private key associated with this pub key this verifying key, all right So we can do this and then here we are So just the functions that we've used here in python. This isn't really going to print anything out on the screen It's just going to throw an error if it doesn't work, right? So I have this print statement there And if we managed to see this as an output in terminal, then we know that that assert function has ran without throwing an error So essentially it validated the signature So let's say this We'll come back here And we'll run that and let's see what happens Ta-da Congrats you successfully validated the signature Now let's look at why this stuff is so important to bitcoin, right? It's so important because this is how I can be assured that you know, mike sent me those funds and he locked it You know with that locking script to my address The idea being that only me the person that's hopefully the only person that's holding the private key associated with that pub key that Mike locked those funds to only I should be able to produce a valid signature To send those funds off to daniella or charlie or whomever, right? Now if this this needs to be the case that um only I can do that and this message Used to create that signature can't be tampered with otherwise Mike could see that transaction and just alter the message They're like actually one of those outputs is going to come back to me, right? And anyone could do this right and just alter that transaction so that they're receiving That bitcoin or they're getting that utx o which they can unlock right instead of daniella getting it So the idea being is it if we alter this message in any way This signature um cannot be validated, right? It'll throw an error, right? So let's change that and i'm just going to change this exclamation Point to a period right very very tiny change. I'm going to save that and then we're going to go Back to the terminal here and we're going to run this again and see what happens Boom here. We go. We got an error bad signature error signature verification failed So this is mission accomplished for this cryptography, right? It says if I try and alter this message because this message was the input used when we created this signature, right? If I alter this message In any fashion from what was used to create the signature then i'm not going to be able to validate it So this is how For this type of transaction. This is how The bitcoin system prevents someone from just going in and being like actually that bitcoin is mine Send it over here, right? You can't do that because the math the public keys the verification, etc Won't Line up and won't work all right so that hopefully Was like I always enjoy messing with this stuff in python Hopefully you all can follow along with this And have a bit of fun with that as well But at this point we only got a few minutes left might do we have any questions? Yes, I believe that's accurate, but I remember about it is that Yeah, yeah, and I can't remember exactly what the b is but that sounds right because I remember when I was putting this script together It's like if that's not there it's not going to work Right, that's how python knows that this is this is a string that i'm passing it. Yeah awesome All right Any other questions? Right now. No, we don't But if anyone has any questions, please feel free to put them down in the chat below and But while we do that I'll just vamp for a minute. Thank you everyone for joining us and of course Thank you hannah for taking us through this So far and of course if you just getting here This is again a supplement to sailor academy's course So please find it in the link below and uh, so taking the course you can find the other videos also if you need to catch up and um Yeah, I'm not seeing any questions coming in that's about um, oh never mind every time I say it We'll get another one. All right. What do we got? Is there any reason to transition uh, utxos from one style of address to a newer one? Okay, so if you're holding um bitcoin in let's say pay to key hash Addresses would you want maybe to then hold that bitcoin in pay to taproot addresses? And the answer is yeah, totally um, or how about you know, maybe you want to um, move that from pay to public key hash addresses to like segwit addresses maybe because you're then going to go and Use that money um on the bitcoin lightning network, etc. And there's all sorts of specific use cases we could get into maybe there's some Um functionality you want to use that requires um taproot So you would want to move your funds to taproot addresses So um, yes, there's definitely reasons why you would want to use one type of address over the other But I don't think you really have to worry about like if I'm storing bitcoin in Pay to public key hash. Do I ever need to like move it or whatever? It's like no not really because if you're storing it here Um in this, you know, maybe older type of address But you're going to want to use this service over here that requires a newer type of address Then you know, whenever it is you get around to doing that just you would create a transaction that as you went You know, like if you wanted to open a lightning channel or something When you moved those funds you would take these utxos as an input and those outputs would just be of the new variety would be, you know segwit address that you would use as the output to open that lightning channel So functionally, I'd say you don't really need to concern yourself with um What type of address formats your bitcoin is currently stored in It's just whenever you want to do a new feature you would have to have a wallet That was uh, you know speaks that language Can can is enabled with that feature and then as you move funds over to go use that feature That transaction would just take an input from an old address type and spit it out lock it to a new address type So kind of but don't worry about it. I think it's the address is the answer there Excellent. We got a thank you there in in the chat and I guess the final question will go out on here real quick is um um Someone wondering, um, they won't have to be writing code to create signatures. Will they? Or they will be happy. Um Yes and no, so this like hold on. Let's go back here This is like the level of code that you might have to write right unless you really want to get in depth You really want to be like a photographer or a protocol? Um engineer then then you would be but if you're just like a lot of the work that I've done is just sort of integrating Bitcoin functionality into other systems like working with bitcoin not really being a bitcoin protocol engineer and then in this case like Generally, it's going to be finding the appropriate library making sure it's a really good solid library and asking google We're referencing the documentation to find the appropriate functions to use in that library Like what we see here is kind of in general Unless you're going to be a cryptographer or a protocol engineer. This is sort of the level on which you'll have to actually work with that that math Okay, excellent. Thanks a lot. Um, and I think with that we'll uh, we'll call this one and again I just big thank you to hannah for taking us through this and a big thank you to everyone for joining us We will see you for unit form say unit four same time uh next week And if you are watching this later and you have any questions leave them in the comments. Um, thank you again everybody See you next week