Categories: Uncategorized

“What happens when I click SEND?” The beginning of a BTC transaction.

Let’s talk about the beginning of the lifecycle of a transaction.

A transaction in the Bitcoin world has the same goal of a transaction in our classic monetary world, which is to move value from one entity to another.

Let’s say we want to give a bitcoin to Bob. Because Bob is so cool! I start by opening my Bitcoin wallet software.

To initiate a Bitcoin transaction you need first of all a private key (derived with a particular algorithm of key generation, that is as random as possible so nobody can generate the same key as you). No worries, the wallet software takes care of this private key generation (and storage) for you.

Once you have generated this private key, you can use some math magic to create a public key associated to it. What is peculiar here is that you can keep the public key, well, public, while the private stays strictly private.

What does this public key represent exactly? It represents the address of your wallet, which basically identifies a unique “point” in the bitcoin network where Bitcoins can be received or spent, similarly to the address of your house, or the IP address to on the internet network. (Technically there is an additional step between public key and wallet address, but it’s irrelevant for this article’s sake)

If you have good intuition and are following along well, you should be asking yourself: “a-ha! If you publish a public key or a wallet address, can I just copy it, derive the original secret private key and steal all your money?” Sorry sir but no, because some math magic comes again into play and does not allow you to reverse the public key / address generation. Well actually you can.. But in order to achieve it, you will have to try all possible combinations of private keys and derive public keys from them until you match it with the public key you want to reverse. This is something called brute-forcing, and it will take you a few hundreds of years probably. So forget about it, figure out another way to get rich.

Remember that cryptography is not about making it impossible to decipher a secret, but making the process so expensive and lengthy that is not even worth trying.

[A little side note for people that do not speak cryptography] What I mean exactly by “private key” is a long string of numbers and letters, like 5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF . This “key” is just a crazy random combination of alphanumeric characters. Keep in mind there are more possible private keys combinations than atoms of the universe, a lot more.

So back to the example transaction: I say that I want to transfer 1 bitcoin, and that I want to transfer it to Bob. Wait.. What is Bob? If you paid attention you know already that “Bob” is just a public key! Therefore a public-key-derived wallet address! So I insert that wallet address’s alphanumeric string into my wallet software’s “new transaction” section/page in the receiver field.

Next my wallet software will have another field where you indicate the amount and obviously in this case I put 1 bitcoin in it.

There are a few other extra options when sending a transactions, such as the priority value, which ultimately determines the fee of the transaction. The higher the fee, the faster the transaction gets mined (processed) and finalized. Why? Because miners want money! So if we pay them with higher fees they will prioritize our transaction over the others!

So we put all these information (amount to be transferred, recipient’s wallet address, fees) and click SEND. What happens now?

Our wallet software takes all this data, creates a signature of this data by feeding it with the private key (only the owner that wants to initiate the transaction has it, remember?) into a cryptographic algorithm. The signature is obviously appended to the data obviously. Finally it parses everything into a machine-to-machine format. Then it will put this nicely packaged data into the wire and send it over to one or many Bitcoin nodes.

Something I think is really cool about this is that theoretically we don’t really need a wallet software to make a transaction! If we are a super tech ninja we can create a transaction ourselves using the command line and some specific cryptographic tools (to make the digital signature), encode it into the standardized format, and then send it over manually to a Bitcoin Node! How sick is that?!

So each bitcoin Node that receives the transaction goes ahead and validates it. It does so because every node keeps a whole record of the blockchain, so knows exactly if you actually can spend these funds, if the digital signature has a valid mathematical relationship with the originating wallet public address and other sanity checks.

If all the checks are passed, the transaction is valid! And the node propagates that transaction to other nodes, which will do the same thing continuously.

So by default nodes don’t trust anything, that’s why if Bob creates a fake transaction saying “The Blockchain Lion is sending me 100 bitcoins, not one!” the nodes will immediately figure out that it’s fake, Why? Well Because first of all Bob does not know my secret private key, and therefore cannot create a digital signature of this transaction that mathematically aligns with my public key / wallet address. Secondary I don’t have 100 bitcoins, so the nodes will also see that I cannot spend that balance.. C’mon guys if I had 100 bitcoins I would be on a beach in the Bahamas. So this fake transaction will get rejected by the node, and it will not get propagated, Bob’s scam starts and ends immediately, he just wasted time.

What is specifically happening inside a Node once a few transactions (ours plus a bunch of others) are validated?

The node adds each valid transaction to a mempool (memory pool) which can be seen as a big bucket that holds a list of these validated transactions. Please note that since the bitcoin network is made up of many nodes, there will be many mempools! And because of the difference in timing and propagation the mempools will not all contain the same transactions. But they will probably have the majority of them overlapping.

So what happens now? The nodes that are also miners (= that are also running mining software) will take the transactions in the mempool and mine them into a new block, which will be added to the blockchain. Once a transaction is mined into a block then the transaction can be considered finalized. Also keep in mind that the more blocks are mined after that block, the more the transactions inside it are computationally irreversible.

(I am skipping how mining works because that needs a separate article, and I think I did write about that already in my previous posts.)

When my transaction to Bob is completed, Bob’s wallet software will read a balance of +1 bitcoin compared to his previous balance. Even if I say “balance” keep in mind that bitcoin wallets don’t really have a balance, but they read the whole blockchain history and count all the transactions that were received to the wallet address and not spent.

Do NOT ever think of at a bitcoin wallet in the same way as you think of your online banking account or PayPal account, because banks and traditional payment systems keep a balance by using a central database and software that adds or removes funds for each user. Understanding cryptocurrency requires you to break existing traditional models and understand distributed computing and networks.

I hope you enjoyed this focused view of the beginning/initiation of a transaction. It’s really important to know these basics in my opinion.

If something was not clear or you want to discuss anything related to transactions with me I warmly invite you to comment below.

Fede

Full Stack Software Engineer at Keenn.com

Share
Published by
Fede

Recent Posts

The Blockchain Notary: 5-minute file certification without corruptible intermediaries

Hello everyone, this article is going to be cool, because I finally got to write…

5 years ago

Are You an Ethereum Developer for Real? Answer These 5 Questions!

Hello friends! I received a reddit notification a few days ago from the r/ethdev subreddit.…

5 years ago

How I deployed a dapp on Ropsten from an EC2 instance (without using Infura)

This is a fairly technical article that I want to publish to help fellow developers…

5 years ago

[VIDEO] Creating and signing a Raw Ethereum Transaction

Today we're looking into a more technical way of creating and signing a transaction. We're…

5 years ago

[VIDEO] Anatomy of an Ethereum Transaction

Here's a video I made about the components of a transaction in Ethereum: https://www.youtube.com/watch?v=u9dR66TdVGA Here…

5 years ago

[Q&A] Where did Bitcoin start?

This is a very simple question that can be answered by looking at the history…

5 years ago